Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/core/src/transform/diagnostics/augmentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type ts from 'typescript';
import { Diagnostic } from './index.js';
import GlimmerASTMappingTree, { MappingSource } from '../template/glimmer-ast-mapping-tree.js';
import TransformedModule from '../template/transformed-module.js';

export function augmentDiagnostics<T extends Diagnostic>(
ts: typeof import('typescript'),
sourceFile: ts.SourceFile,
transformedModule: TransformedModule,
diagnostics: T[],
): T[] {
Expand All @@ -26,7 +29,11 @@ export function augmentDiagnostics<T extends Diagnostic>(
return rangeWithMappingAndSource.mapping || null;
};

const augmentedDiagnostics: T[] = [];
const unusedExpectErrors = new Set(
transformedModule.directives.filter((d) => d.kind === 'expect-error'),
);

const augmentedDiagnostics: Diagnostic[] = [];

for (const diagnostic of diagnostics) {
const augmentedDiagnostic = rewriteMessageText(diagnostic, mappingForDiagnostic);
Expand All @@ -41,15 +48,29 @@ export function augmentDiagnostics<T extends Diagnostic>(
);

if (appliedDirective) {
if (appliedDirective.kind === 'expect-error') {
unusedExpectErrors.delete(appliedDirective);
}
// Filter out this diagnostic; its covered by a directive.
continue;
}
}

// @ts-expect-error not sure how to fix
augmentedDiagnostics.push(augmentedDiagnostic);
}

for (const unusedExpectError of unusedExpectErrors) {
augmentedDiagnostics.push({
category: ts.DiagnosticCategory.Error,
code: 2578,
file: sourceFile,
start: unusedExpectError.location.start,
length: unusedExpectError.location.end - unusedExpectError.location.start,
messageText: `Unused '@glint-expect-error' directive.`,
});
}

// @ts-expect-error not sure how to fix
return augmentedDiagnostics;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/tsserver-plugin/src/typescript-server-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,12 @@ function getSemanticDiagnostics<T>(
return tsDiagnostics;
}

const augmentedTsDiagnostics = augmentDiagnostics(transformedModule, tsDiagnostics);
const augmentedTsDiagnostics = augmentDiagnostics(
ts,
sourceFile,
transformedModule,
tsDiagnostics,
);

return augmentedTsDiagnostics;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,43 @@ describe('Language Server: Diagnostics (ts plugin)', () => {
`);
});

test('unused @glint-expect-error triggers a diagnostic', async () => {
const componentA = stripIndent`
import Component from '@glimmer/component';

export default class ComponentA extends Component {
<template>
{{! @glint-expect-error }}
<Component />
</template>
}
`;

const diagnostics = await requestDiagnostics(
'ts-template-imports-app/src/ephemeral-index.gts',
'glimmer-ts',
componentA,
);

expect(diagnostics).toMatchInlineSnapshot(`
[
{
"category": "error",
"code": 2578,
"end": {
"line": 5,
"offset": 31,
},
"start": {
"line": 5,
"offset": 5,
},
"text": "Unused '@glint-expect-error' directive.",
},
]
`);
});

test('svg does not produce false positives', async () => {
const code = stripIndent`
import Component from '@glimmer/component';
Expand Down