Skip to content

Commit d7c379c

Browse files
committed
try, fail, add lots of notes
1 parent ea5eb0e commit d7c379c

File tree

2 files changed

+68
-21
lines changed

2 files changed

+68
-21
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,6 @@ describe('Language Server: Diagnostics', () => {
454454
).toEqual([]);
455455
expect(
456456
await server.sendDocumentDiagnosticRequestNormalized(project.fileURI('component-a.gts'))
457-
).toMatchInlineSnapshot(`[expect unused directive]`);
457+
).toMatchInlineSnapshot(`[TODO should display unused glint-expect-error directive]`);
458458
});
459459
});

packages/core/src/volar/language-server.ts

+67-20
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ import { assert } from '../transform/util.js';
1414
import { ConfigLoader } from '../config/loader.js';
1515
import ts from 'typescript';
1616
import type { TextDocument } from 'vscode-languageserver-textdocument';
17-
import type * as vscode from 'vscode-languageserver-protocol';
17+
import * as vscode from 'vscode-languageserver-protocol';
1818
import { URI } from 'vscode-uri';
1919
import { VirtualGtsCode } from './gts-virtual-code.js';
2020
import { augmentDiagnostic } from '../transform/diagnostics/augmentation.js';
2121
import MappingTree from '../transform/template/mapping-tree.js';
2222
import { Directive, TransformedModule } from '../transform/index.js';
2323
import { Range } from '../transform/template/transformed-module.js';
24+
import { offsetToPosition } from '../language-server/util/position.js';
2425

2526
const connection = createConnection();
2627

@@ -112,10 +113,6 @@ function filterAndAugmentDiagnostics(
112113
return null;
113114
}
114115

115-
if (diagnostics.length == 0) {
116-
return diagnostics;
117-
}
118-
119116
// Lazily fetch and cache the VirtualCode -- this might be a premature optimization
120117
// after the code went through enough changes, so maybe safe to simplify in the future.
121118
let cachedVirtualCode: VirtualGtsCode | null | undefined = undefined;
@@ -161,10 +158,6 @@ function filterAndAugmentDiagnostics(
161158
return augmentDiagnostic(diagnostic as any, mappingForDiagnostic);
162159
});
163160

164-
if (augmentedDiagnostics.length === 0) {
165-
return [];
166-
}
167-
168161
let unusedExpectErrors = new Set<Directive>();
169162
const transformedModule = fetchVirtualCode()?.transformedModule;
170163
if (transformedModule) {
@@ -195,7 +188,6 @@ function filterAndAugmentDiagnostics(
195188
let originalGtsDiagnosticStart = transformedModule?.getOriginalOffset(diagnosticStart);
196189

197190
appliedDirective = transformedModule?.directives.find((directive) => {
198-
const diagnosticStart = document.offsetAt(diagnostic.range.start);
199191
return (
200192
// TODO: when would the filename ever be different? uncomment and fix?
201193
// directive.source.filename === diagnostic.file.fileName &&
@@ -212,16 +204,71 @@ function filterAndAugmentDiagnostics(
212204
}
213205
});
214206

215-
// for (let directive of unusedExpectErrors) {
216-
// allDiagnostics.push(
217-
// createTransformDiagnostic(
218-
// ts,
219-
// directive.source,
220-
// `Unused '@glint-expect-error' directive.`,
221-
// directive.location
222-
// )
223-
// );
224-
// }
207+
for (let directive of unusedExpectErrors) {
208+
209+
// desired methond on transformedModule:
210+
// - it accepts a source offset and finds the transformed offset
211+
// - in which file? there are multiple embeddedCodes in a .gts file
212+
// - root: gts
213+
// - embeddedCodes[0]: ts (IR)
214+
// - embeddedCodes[1, 2, 3]: however many Handlebars templates
215+
//
216+
// transformedModule.correlatedSpans[1].mapping.children[0].children[1].sourceNode
217+
// - {type: 'MustacheCommentStatement', value: ' @glint-expect-error ', loc: SourceSpan}
218+
//
219+
// this is what we want.
220+
//
221+
// OK what is our input?
222+
// the starting point is directive that is left over.
223+
// directive.areaOfEffect.start/end reference to the offset within the .gts file delineating: |{{! @glint-expect-error }}|
224+
//
225+
// directive.source: {
226+
// contents: <GTS source code with untransformed template>
227+
// filename: "disregard.gts"
228+
// }
229+
//
230+
// determineTransformedOffsetAndSpan(
231+
// originalFileName: string,
232+
// originalOffset: number
233+
// )
234+
//
235+
// transformedModule.determineTransformedOffsetAndSpan(directive.source.filename, directive.location.start)
236+
//
237+
// this returns a transformedOffset and correlatedSpan with mapping pointing to the template embedded.
238+
//
239+
240+
allDiagnostics.push(
241+
{
242+
message: `Unused '@glint-expect-error' directive.`,
243+
244+
// this range... should be... for the TS file. Currently we're sending
245+
// a range for the source .gts. That can't be right.
246+
// The info we have is....... we know an unused glint directive exists.
247+
// We need to find a range in the IR .ts file.
248+
//
249+
// 1. need to translate directive.areaOfEffect into the IR .ts file location
250+
// - this is going to be the beginning of line in .gts and end of line in .gts.
251+
// - actually maybe it's not area of effect, but rather the comment node. YES.
252+
// emit.forNode(node, () => {
253+
// emit.text(`// @glint-${kind}`);
254+
// emit.newline();
255+
// });
256+
//
257+
// - can we take the souce and query the CommentNode
258+
// - node: AST.MustacheCommentStatement | AST.CommentStatement
259+
// - what/how do we query now?
260+
//
261+
// 2. need to make sure it fits error boundary
262+
range: vscode.Range.create(
263+
offsetToPosition(document.getText(), directive.areaOfEffect.start),
264+
offsetToPosition(document.getText(), directive.areaOfEffect.end)
265+
),
266+
severity: vscode.DiagnosticSeverity.Error,
267+
code: 0,
268+
source: directive.source.filename, // not sure if this is right
269+
}
270+
);
271+
}
225272

226273
return allDiagnostics;
227274
}

0 commit comments

Comments
 (0)