diff --git a/packages/frontend/editor-ui/src/plugins/codemirror/typescript/worker/linter.test.ts b/packages/frontend/editor-ui/src/plugins/codemirror/typescript/worker/linter.test.ts new file mode 100644 index 0000000000..51cc3e0879 --- /dev/null +++ b/packages/frontend/editor-ui/src/plugins/codemirror/typescript/worker/linter.test.ts @@ -0,0 +1,96 @@ +import { mock, mockDeep } from 'vitest-mock-extended'; +import type * as tsvfs from '@typescript/vfs'; +import ts from 'typescript'; +import { getDiagnostics, tsCategoryToSeverity } from './linter'; + +describe('linter > getDiagnostics', () => { + it('should return diagnostics for a given file', () => { + const env = mockDeep(); + const fileName = 'testFile.ts'; + + const semanticDiagnostics = [ + mock({ + file: { fileName }, + start: 0, + length: 10, + messageText: 'Semantic error', + category: ts.DiagnosticCategory.Error, + code: 1234, + }), + ]; + + const syntacticDiagnostics = [ + mock({ + file: { fileName }, + start: 15, + length: 5, + messageText: 'Syntactic warning', + category: ts.DiagnosticCategory.Warning, + code: 5678, + }), + ]; + + env.languageService.getSemanticDiagnostics.mockReturnValue(semanticDiagnostics); + env.languageService.getSyntacticDiagnostics.mockReturnValue(syntacticDiagnostics); + env.getSourceFile.mockReturnValue({ fileName } as ts.SourceFile); + + const result = getDiagnostics({ env, fileName }); + + expect(result).toHaveLength(2); + expect(result[0].message).toBe('Semantic error'); + expect(result[0].severity).toBe('error'); + expect(result[1].message).toBe('Syntactic warning'); + expect(result[1].severity).toBe('warning'); + }); + + it('should filter out ignored diagnostics', () => { + const env = mockDeep(); + const fileName = 'testFile.ts'; + + const diagnostics = [ + mock({ + file: { fileName }, + start: 0, + length: 10, + messageText: 'No implicit any', + category: ts.DiagnosticCategory.Error, + code: 7006, + }), + mock({ + file: { fileName }, + start: 0, + length: 10, + messageText: 'Cannot find module or its corresponding type declarations.', + category: ts.DiagnosticCategory.Error, + code: 2307, + }), + mock({ + file: { fileName }, + start: 0, + length: 10, + messageText: 'Not ignored error', + category: ts.DiagnosticCategory.Error, + code: 1234, + }), + ]; + + env.languageService.getSemanticDiagnostics.mockReturnValue(diagnostics); + env.languageService.getSyntacticDiagnostics.mockReturnValue([]); + env.getSourceFile.mockReturnValue({ fileName } as ts.SourceFile); + + const result = getDiagnostics({ env, fileName }); + + expect(result).toHaveLength(1); + expect(result[0].message).toBe('Not ignored error'); + }); + + it('should map diagnostic categories to severities correctly', () => { + const diagnostic = mock({ + category: ts.DiagnosticCategory.Error, + code: 1234, + }); + + const severity = tsCategoryToSeverity(diagnostic); + expect(severity).toBe('error'); + }); +}); diff --git a/packages/frontend/editor-ui/src/plugins/codemirror/typescript/worker/linter.ts b/packages/frontend/editor-ui/src/plugins/codemirror/typescript/worker/linter.ts index c53774cbfe..c4cf98d340 100644 --- a/packages/frontend/editor-ui/src/plugins/codemirror/typescript/worker/linter.ts +++ b/packages/frontend/editor-ui/src/plugins/codemirror/typescript/worker/linter.ts @@ -49,8 +49,15 @@ function isDiagnosticWithLocation( } function isIgnoredDiagnostic(diagnostic: ts.Diagnostic) { - // No implicit any - return diagnostic.code === 7006; + switch (diagnostic.code) { + // No implicit any + case 7006: + // Cannot find module or its corresponding type declarations. + case 2307: + return true; + } + + return false; } /**