Skip to content

Commit

Permalink
attempt to improve baseline matching
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Sep 28, 2024
1 parent 54ee1a1 commit 754a147
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions packages/pyright-internal/src/baseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { extraOptionDiagnosticRules } from './common/configOptions';
import { fileExists } from './common/uri/uriUtils';
import { FileSystem, ReadOnlyFileSystem } from './common/fileSystem';
import { pluralize } from './common/stringUtils';
import { Range } from './common/textRange';

export interface BaselinedDiagnostic {
code: DiagnosticRule | undefined;
range: { startColumn: number; endColumn: number };
range: Range;
}

interface BaselineFile {
Expand Down Expand Up @@ -43,7 +44,7 @@ const diagnosticsToBaseline = (rootDir: Uri, filesWithDiagnostics: readonly File
baselineData.files[filePath].push(
...errorDiagnostics.map((diagnostic) => ({
code: diagnostic.getRule() as DiagnosticRule | undefined,
range: { startColumn: diagnostic.range.start.character, endColumn: diagnostic.range.end.character },
range: diagnostic.range,
}))
);
}
Expand Down Expand Up @@ -136,18 +137,31 @@ export const filterOutBaselinedDiagnostics = (
file: Uri,
diagnostics: Diagnostic[]
) => {
const baselinedErrorsForFile = getBaselinedErrorsForFile(fs, rootDir, file);
let baselinedErrorsForFile = getBaselinedErrorsForFile(fs, rootDir, file);
for (const index in diagnostics) {
const diagnostic = diagnostics[index];
const diagnosticRule = diagnostic.getRule() as DiagnosticRule | undefined;
const matchedIndex = baselinedErrorsForFile.findIndex(
(baselinedError) =>
baselinedError.code === diagnosticRule &&
baselinedError.range.startColumn === diagnostic.range.start.character &&
baselinedError.range.endColumn === diagnostic.range.end.character
);
if (matchedIndex >= 0) {
baselinedErrorsForFile.splice(matchedIndex, 1);
const matchedError = baselinedErrorsForFile
// first find all matching diagnostics with the same diagnostic code, column numbers
// and number of lines
.filter(
(baselinedError) =>
baselinedError.code === diagnosticRule &&
baselinedError.range.start.character === diagnostic.range.start.character &&
baselinedError.range.end.character === diagnostic.range.end.character &&
baselinedError.range.end.line - baselinedError.range.start.line ===
diagnostic.range.end.line - diagnostic.range.start.line
)
// then find the one with the closest line number
.sort((prev, next) =>
Math.abs(
diagnostic.range.start.line -
prev.range.start.line -
(diagnostic.range.start.line - next.range.start.line)
)
)[0];
if (matchedError) {
baselinedErrorsForFile = baselinedErrorsForFile.filter((baselinedError) => baselinedError !== matchedError);
// if the baselined error can be reported as a hint (eg. unreachable/deprecated), keep it and change its diagnostic level to that instead
// TODO: should we only baseline errors and not warnings/notes?
if (diagnosticRule) {
Expand Down

0 comments on commit 754a147

Please sign in to comment.