From a4873a6a83c7082438b4f6d7cb39528cb7b21ce1 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Thu, 6 Jun 2024 13:57:48 -0700 Subject: [PATCH] Simplified some code for logging diagnostics in checker and binder. --- packages/pyright-internal/src/analyzer/binder.ts | 11 +++-------- packages/pyright-internal/src/analyzer/checker.ts | 10 ---------- .../pyright-internal/src/analyzer/typeEvaluator.ts | 6 +++--- .../src/analyzer/typeEvaluatorTypes.ts | 4 +--- packages/pyright-internal/src/tests/testState.test.ts | 2 +- 5 files changed, 8 insertions(+), 25 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/binder.ts b/packages/pyright-internal/src/analyzer/binder.ts index 94f61f4c1..2c13b65cf 100644 --- a/packages/pyright-internal/src/analyzer/binder.ts +++ b/packages/pyright-internal/src/analyzer/binder.ts @@ -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': diff --git a/packages/pyright-internal/src/analyzer/checker.ts b/packages/pyright-internal/src/analyzer/checker.ts index 2aab34c95..7bb27f38a 100644 --- a/packages/pyright-internal/src/analyzer/checker.ts +++ b/packages/pyright-internal/src/analyzer/checker.ts @@ -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, diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index 383d2e405..6f7739a3e 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -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; } diff --git a/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts b/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts index 90bfe9553..0d127239b 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts @@ -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; diff --git a/packages/pyright-internal/src/tests/testState.test.ts b/packages/pyright-internal/src/tests/testState.test.ts index 39feb2c34..4b9112c11 100644 --- a/packages/pyright-internal/src/tests/testState.test.ts +++ b/packages/pyright-internal/src/tests/testState.test.ts @@ -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 = `