mirror of
https://github.com/microsoft/pyright.git
synced 2024-08-16 11:20:22 +03:00
Simplified some code for logging diagnostics in checker and binder.
This commit is contained in:
parent
32076952c7
commit
a4873a6a83
@ -367,7 +367,6 @@ export class Binder extends ParseTreeWalker {
|
||||
|
||||
if (!importResult.isImportFound) {
|
||||
this._addDiagnostic(
|
||||
this._fileInfo.diagnosticRuleSet.reportMissingImports,
|
||||
DiagnosticRule.reportMissingImports,
|
||||
LocMessage.importResolveFailure().format({
|
||||
importName: importResult.importName,
|
||||
@ -385,7 +384,6 @@ export class Binder extends ParseTreeWalker {
|
||||
!importResult.pyTypedInfo
|
||||
) {
|
||||
const diagnostic = this._addDiagnostic(
|
||||
this._fileInfo.diagnosticRuleSet.reportMissingTypeStubs,
|
||||
DiagnosticRule.reportMissingTypeStubs,
|
||||
LocMessage.stubFileMissing().format({ importName: importResult.importName }),
|
||||
node
|
||||
@ -752,7 +750,6 @@ export class Binder extends ParseTreeWalker {
|
||||
this._usesUnsupportedDunderAllForm = true;
|
||||
|
||||
this._addDiagnostic(
|
||||
this._fileInfo.diagnosticRuleSet.reportUnsupportedDunderAll,
|
||||
DiagnosticRule.reportUnsupportedDunderAll,
|
||||
LocMessage.unsupportedDunderAllOperation(),
|
||||
node
|
||||
@ -863,7 +860,6 @@ export class Binder extends ParseTreeWalker {
|
||||
|
||||
if (node.chainedTypeAnnotationComment) {
|
||||
this._addDiagnostic(
|
||||
this._fileInfo.diagnosticRuleSet.reportInvalidTypeForm,
|
||||
DiagnosticRule.reportInvalidTypeForm,
|
||||
LocMessage.annotationNotSupported(),
|
||||
node.chainedTypeAnnotationComment
|
||||
@ -956,7 +952,6 @@ export class Binder extends ParseTreeWalker {
|
||||
this._usesUnsupportedDunderAllForm = true;
|
||||
|
||||
this._addDiagnostic(
|
||||
this._fileInfo.diagnosticRuleSet.reportUnsupportedDunderAll,
|
||||
DiagnosticRule.reportUnsupportedDunderAll,
|
||||
LocMessage.unsupportedDunderAllOperation(),
|
||||
node
|
||||
@ -1111,7 +1106,6 @@ export class Binder extends ParseTreeWalker {
|
||||
this._usesUnsupportedDunderAllForm = true;
|
||||
|
||||
this._addDiagnostic(
|
||||
this._fileInfo.diagnosticRuleSet.reportUnsupportedDunderAll,
|
||||
DiagnosticRule.reportUnsupportedDunderAll,
|
||||
LocMessage.unsupportedDunderAllOperation(),
|
||||
node
|
||||
@ -3842,7 +3836,6 @@ export class Binder extends ParseTreeWalker {
|
||||
|
||||
if (!declarationHandled) {
|
||||
this._addDiagnostic(
|
||||
this._fileInfo.diagnosticRuleSet.reportInvalidTypeForm,
|
||||
DiagnosticRule.reportInvalidTypeForm,
|
||||
LocMessage.annotationNotSupported(),
|
||||
typeAnnotation
|
||||
@ -4224,7 +4217,9 @@ export class Binder extends ParseTreeWalker {
|
||||
return getUniqueFlowNodeId();
|
||||
}
|
||||
|
||||
private _addDiagnostic(diagLevel: DiagnosticLevel, rule: DiagnosticRule, message: string, textRange: TextRange) {
|
||||
private _addDiagnostic(rule: DiagnosticRule, message: string, textRange: TextRange) {
|
||||
const diagLevel = this._fileInfo.diagnosticRuleSet[rule] as DiagnosticLevel;
|
||||
|
||||
let diagnostic: Diagnostic | undefined;
|
||||
switch (diagLevel) {
|
||||
case 'error':
|
||||
|
@ -251,7 +251,6 @@ export class Checker extends ParseTreeWalker {
|
||||
if (codeComplexity > maxCodeComplexity) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportGeneralTypeIssues,
|
||||
DiagnosticRule.reportGeneralTypeIssues,
|
||||
LocMessage.codeTooComplexToAnalyze(),
|
||||
{ start: 0, length: 0 }
|
||||
@ -844,7 +843,6 @@ export class Checker extends ParseTreeWalker {
|
||||
if (node.typeComment) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportInvalidTypeForm,
|
||||
DiagnosticRule.reportInvalidTypeForm,
|
||||
LocMessage.annotationNotSupported(),
|
||||
node.typeComment
|
||||
@ -899,7 +897,6 @@ export class Checker extends ParseTreeWalker {
|
||||
if (node.typeComment) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportInvalidTypeForm,
|
||||
DiagnosticRule.reportInvalidTypeForm,
|
||||
LocMessage.annotationNotSupported(),
|
||||
node.typeComment
|
||||
@ -1169,7 +1166,6 @@ export class Checker extends ParseTreeWalker {
|
||||
if (!isUnboundedTupleClass(type)) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportAssertAlwaysTrue,
|
||||
DiagnosticRule.reportAssertAlwaysTrue,
|
||||
LocMessage.assertAlwaysTrue(),
|
||||
node.testExpression
|
||||
@ -1386,7 +1382,6 @@ export class Checker extends ParseTreeWalker {
|
||||
if (error.errorType === UnescapeErrorType.InvalidEscapeSequence) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportInvalidStringEscapeSequence,
|
||||
DiagnosticRule.reportInvalidStringEscapeSequence,
|
||||
LocMessage.stringUnsupportedEscape(),
|
||||
{ start: start + error.offset, length: error.length }
|
||||
@ -1400,7 +1395,6 @@ export class Checker extends ParseTreeWalker {
|
||||
if (escapeOffset >= 0) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportGeneralTypeIssues,
|
||||
DiagnosticRule.reportGeneralTypeIssues,
|
||||
LocMessage.formatStringEscape(),
|
||||
{ start, length: 1 }
|
||||
@ -1436,7 +1430,6 @@ export class Checker extends ParseTreeWalker {
|
||||
if (node.strings.length > 1 && !node.isParenthesized) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportImplicitStringConcatenation,
|
||||
DiagnosticRule.reportImplicitStringConcatenation,
|
||||
LocMessage.implicitStringConcat(),
|
||||
node
|
||||
@ -1571,7 +1564,6 @@ export class Checker extends ParseTreeWalker {
|
||||
) {
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportWildcardImportFromLibrary,
|
||||
DiagnosticRule.reportWildcardImportFromLibrary,
|
||||
LocMessage.wildcardLibraryImport(),
|
||||
node.wildcardToken || node
|
||||
@ -3610,7 +3602,6 @@ export class Checker extends ParseTreeWalker {
|
||||
|
||||
this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportUnusedImport,
|
||||
DiagnosticRule.reportUnusedImport,
|
||||
LocMessage.unaccessedImport().format({ name: multipartName }),
|
||||
textRange
|
||||
@ -4437,7 +4428,6 @@ export class Checker extends ParseTreeWalker {
|
||||
// This means the user has a module that is overwriting the stdlib module.
|
||||
const diag = this._evaluator.addDiagnosticForTextRange(
|
||||
this._fileInfo,
|
||||
this._fileInfo.diagnosticRuleSet.reportShadowedImports,
|
||||
DiagnosticRule.reportShadowedImports,
|
||||
LocMessage.stdlibModuleOverridden().format({
|
||||
name: moduleName,
|
||||
|
@ -3038,7 +3038,6 @@ export function createTypeEvaluator(
|
||||
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
||||
addDiagnosticForTextRange(
|
||||
fileInfo,
|
||||
fileInfo.diagnosticRuleSet.reportGeneralTypeIssues,
|
||||
DiagnosticRule.reportGeneralTypeIssues,
|
||||
LocMessage.codeTooComplexToAnalyze(),
|
||||
errorRange
|
||||
@ -3235,11 +3234,12 @@ export function createTypeEvaluator(
|
||||
|
||||
function addDiagnosticForTextRange(
|
||||
fileInfo: AnalyzerFileInfo,
|
||||
diagLevel: DiagnosticLevel,
|
||||
rule: DiagnosticRule | '',
|
||||
rule: DiagnosticRule,
|
||||
message: string,
|
||||
range: TextRange
|
||||
) {
|
||||
const diagLevel = fileInfo.diagnosticRuleSet[rule] as DiagnosticLevel;
|
||||
|
||||
if (diagLevel === 'none') {
|
||||
return undefined;
|
||||
}
|
||||
|
@ -9,7 +9,6 @@
|
||||
|
||||
import { CancellationToken } from 'vscode-languageserver-protocol';
|
||||
|
||||
import { DiagnosticLevel } from '../common/configOptions';
|
||||
import { ConsoleInterface } from '../common/console';
|
||||
import { Diagnostic, DiagnosticAddendum } from '../common/diagnostic';
|
||||
import { DiagnosticRule } from '../common/diagnosticRules';
|
||||
@ -676,8 +675,7 @@ export interface TypeEvaluator {
|
||||
) => Diagnostic | undefined;
|
||||
addDiagnosticForTextRange: (
|
||||
fileInfo: AnalyzerFileInfo,
|
||||
diagLevel: DiagnosticLevel,
|
||||
rule: DiagnosticRule | '',
|
||||
rule: DiagnosticRule,
|
||||
message: string,
|
||||
range: TextRange
|
||||
) => Diagnostic | undefined;
|
||||
|
@ -10,11 +10,11 @@ import assert from 'assert';
|
||||
|
||||
import { combinePaths, getFileName, normalizeSlashes } from '../common/pathUtils';
|
||||
import { compareStringsCaseSensitive } from '../common/stringUtils';
|
||||
import { Uri } from '../common/uri/uri';
|
||||
import { Range } from './harness/fourslash/fourSlashTypes';
|
||||
import { runFourSlashTestContent } from './harness/fourslash/runner';
|
||||
import { parseAndGetTestState } from './harness/fourslash/testState';
|
||||
import * as factory from './harness/vfs/factory';
|
||||
import { Uri } from '../common/uri/uri';
|
||||
|
||||
test('Create', () => {
|
||||
const code = `
|
||||
|
Loading…
Reference in New Issue
Block a user