Skip to content

Commit 5ad2a3d

Browse files
authored
Reinstate "unused @glint-expect-error directive" (#889)
1 parent 1ed1e8f commit 5ad2a3d

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

packages/core/src/transform/diagnostics/augmentation.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type ts from 'typescript';
22
import { Diagnostic } from './index.js';
33
import GlimmerASTMappingTree, { MappingSource } from '../template/glimmer-ast-mapping-tree.js';
44
import TransformedModule from '../template/transformed-module.js';
5+
56
export function augmentDiagnostics<T extends Diagnostic>(
7+
ts: typeof import('typescript'),
8+
sourceFile: ts.SourceFile,
69
transformedModule: TransformedModule,
710
diagnostics: T[],
811
): T[] {
@@ -26,7 +29,11 @@ export function augmentDiagnostics<T extends Diagnostic>(
2629
return rangeWithMappingAndSource.mapping || null;
2730
};
2831

29-
const augmentedDiagnostics: T[] = [];
32+
const unusedExpectErrors = new Set(
33+
transformedModule.directives.filter((d) => d.kind === 'expect-error'),
34+
);
35+
36+
const augmentedDiagnostics: Diagnostic[] = [];
3037

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

4350
if (appliedDirective) {
51+
if (appliedDirective.kind === 'expect-error') {
52+
unusedExpectErrors.delete(appliedDirective);
53+
}
4454
// Filter out this diagnostic; its covered by a directive.
4555
continue;
4656
}
4757
}
4858

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

62+
for (const unusedExpectError of unusedExpectErrors) {
63+
augmentedDiagnostics.push({
64+
category: ts.DiagnosticCategory.Error,
65+
code: 2578,
66+
file: sourceFile,
67+
start: unusedExpectError.location.start,
68+
length: unusedExpectError.location.end - unusedExpectError.location.start,
69+
messageText: `Unused '@glint-expect-error' directive.`,
70+
});
71+
}
72+
73+
// @ts-expect-error not sure how to fix
5374
return augmentedDiagnostics;
5475
}
5576

packages/tsserver-plugin/src/typescript-server-plugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ function getSemanticDiagnostics<T>(
202202
return tsDiagnostics;
203203
}
204204

205-
const augmentedTsDiagnostics = augmentDiagnostics(transformedModule, tsDiagnostics);
205+
const augmentedTsDiagnostics = augmentDiagnostics(
206+
ts,
207+
sourceFile,
208+
transformedModule,
209+
tsDiagnostics,
210+
);
206211

207212
return augmentedTsDiagnostics;
208213
};

test-packages/package-test-core/__tests__/language-server/diagnostics.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,43 @@ describe('Language Server: Diagnostics (ts plugin)', () => {
637637
`);
638638
});
639639

640+
test('unused @glint-expect-error triggers a diagnostic', async () => {
641+
const componentA = stripIndent`
642+
import Component from '@glimmer/component';
643+
644+
export default class ComponentA extends Component {
645+
<template>
646+
{{! @glint-expect-error }}
647+
<Component />
648+
</template>
649+
}
650+
`;
651+
652+
const diagnostics = await requestDiagnostics(
653+
'ts-template-imports-app/src/ephemeral-index.gts',
654+
'glimmer-ts',
655+
componentA,
656+
);
657+
658+
expect(diagnostics).toMatchInlineSnapshot(`
659+
[
660+
{
661+
"category": "error",
662+
"code": 2578,
663+
"end": {
664+
"line": 5,
665+
"offset": 31,
666+
},
667+
"start": {
668+
"line": 5,
669+
"offset": 5,
670+
},
671+
"text": "Unused '@glint-expect-error' directive.",
672+
},
673+
]
674+
`);
675+
});
676+
640677
test('svg does not produce false positives', async () => {
641678
const code = stripIndent`
642679
import Component from '@glimmer/component';

0 commit comments

Comments
 (0)