Skip to content

Commit 5564fc2

Browse files
authored
Reinstate GJS Support (#877)
1 parent 640446d commit 5564fc2

File tree

13 files changed

+161
-19
lines changed

13 files changed

+161
-19
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ export function augmentDiagnostics<T extends Diagnostic>(
1818
const start = diagnostic.start;
1919
const end = start + diagnostic.length;
2020

21-
// TODO: de-hardwire "disregard.gts" and consider the case of two-file components where
22-
// the hardcoded source file name might be the hbs file.
2321
const rangeWithMappingAndSource = transformedModule.getTransformedRange(
24-
'disregard.gts',
22+
transformedModule.originalFileName,
2523
start,
2624
end,
2725
);

packages/core/src/transform/template/rewrite-module.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type RewriteInput = { script: SourceFile; template?: SourceFile };
3333
//
3434
// 1. It doesn't break Organize Imports command
3535
// 2. It doesn't introduce any keywords/variables that'll show up in auto-complete suggestions
36-
const EXTENSION_FIXING_HEADER_HACK = `
36+
const EXTENSION_FIXING_HEADER_HACK_GTS = `
3737
// @ts-expect-error
3838
({} as typeof import('./__glint-hacky-nonexistent.gts'));
3939
@@ -42,6 +42,15 @@ const EXTENSION_FIXING_HEADER_HACK = `
4242
4343
`;
4444

45+
const EXTENSION_FIXING_HEADER_HACK_GJS = `
46+
// @ts-expect-error
47+
(/** @type {typeof import("./__glint-hacky-nonexistent.gts")} */ ({}))
48+
49+
// @ts-expect-error
50+
(/** @type {typeof import("./__glint-hacky-nonexistent.gjs")} */ ({}))
51+
52+
`;
53+
4554
/**
4655
* Given the script and/or template that together comprise a component module,
4756
* returns a `TransformedModule` representing the combined result, with the
@@ -70,7 +79,7 @@ export function rewriteModule(
7079
let sparseSpans = completeCorrelatedSpans(partialSpans);
7180
let { contents, correlatedSpans } = calculateTransformedSource(script, sparseSpans);
7281

73-
return new TransformedModule(contents, errors, directives, correlatedSpans);
82+
return new TransformedModule(contents, errors, directives, correlatedSpans, script.filename);
7483
}
7584

7685
/**
@@ -94,7 +103,9 @@ function calculateCorrelatedSpans(
94103
originalStart: 0,
95104
originalLength: 0,
96105
insertionPoint: 0,
97-
transformedSource: EXTENSION_FIXING_HEADER_HACK,
106+
transformedSource: environment.isUntypedScript(script.filename)
107+
? EXTENSION_FIXING_HEADER_HACK_GJS
108+
: EXTENSION_FIXING_HEADER_HACK_GTS,
98109
},
99110
];
100111

packages/core/src/transform/template/transformed-module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default class TransformedModule {
6868
public readonly errors: ReadonlyArray<TransformError>,
6969
public readonly directives: ReadonlyArray<Directive>,
7070
public readonly correlatedSpans: Array<CorrelatedSpan>,
71+
public readonly originalFileName: string,
7172
) {}
7273

7374
public toDebugString(): string {

packages/core/src/volar/gts-virtual-code.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ export class VirtualGtsCode implements VirtualCode {
129129
};
130130

131131
const contents = snapshot.getText(0, length);
132-
133-
let script = { filename: 'disregard.gts', contents };
132+
const isJavascript =
133+
this.languageId === 'glimmer-js' || this.languageId === 'javascript.glimmer';
134+
const embeddedLanguageId = isJavascript ? 'javascript' : 'typescript';
135+
const filename = isJavascript ? 'root.gjs' : 'root.gts';
136+
let script = { filename, contents };
134137
let template = undefined;
135138

136139
const transformedModule = rewriteModule(
@@ -149,7 +152,7 @@ export class VirtualGtsCode implements VirtualCode {
149152
{
150153
embeddedCodes: [],
151154
id: 'ts',
152-
languageId: 'typescript',
155+
languageId: embeddedLanguageId,
153156
mappings,
154157
snapshot: new ScriptSnapshot(transformedModule.transformedContents),
155158
directives: transformedModule.directives,
@@ -213,7 +216,7 @@ export class VirtualGtsCode implements VirtualCode {
213216
{
214217
embeddedCodes: [],
215218
id: 'ts',
216-
languageId: 'typescript',
219+
languageId: embeddedLanguageId,
217220
mappings: [
218221
{
219222
// Hacked hardwired values for now.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function getCompletionsAtPosition<T>(
144144
const transformedModule: TransformedModule = root?.transformedModule;
145145
const completions = getCompletionsAtPosition(fileName, position, options, formattingSettings);
146146
const transformedRange = transformedModule?.getTransformedRange(
147-
'disregard.gts',
147+
transformedModule.originalFileName,
148148
position,
149149
position,
150150
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Component from '@glimmer/component';
2+
3+
export default class Greeting extends Component {
4+
message = 'Hello';
5+
6+
<template>
7+
{{this.message}}, {{@target}}!
8+
</template>
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Component from '@glimmer/component';
2+
import Colocated from './colocated-folder';
3+
import Greeting from './GreetingUntyped';
4+
5+
export default class Other extends Component {
6+
message = 'Hello';
7+
8+
<template>
9+
<Greeting @target="World" />
10+
<Colocated @target="World" />
11+
</template>
12+
}

packages/vscode/__fixtures__/ember-app-loose-and-gts/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"target": "es2019",
55
"module": "es2015",
66
"moduleResolution": "bundler",
7-
"skipLibCheck": true
7+
"skipLibCheck": true,
8+
"checkJs": true
89
},
910
"glint": {
1011
"environment": ["ember-loose", "ember-template-imports"]

packages/vscode/__tests__/helpers/async.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export function sleep(ms: number): Promise<void> {
22
return new Promise((resolve) => setTimeout(resolve, ms));
33
}
44

5-
export async function waitUntil(callback: () => unknown): Promise<void> {
5+
export async function waitUntil(callback: () => unknown, message?: string): Promise<void> {
66
let start = Date.now();
77
while (Date.now() - start < 15_000) {
88
if (await callback()) {
@@ -12,5 +12,5 @@ export async function waitUntil(callback: () => unknown): Promise<void> {
1212
await sleep(500);
1313
}
1414

15-
throw new Error(`waitUntil condition never came true`);
15+
throw new Error(`waitUntil condition never came true (${message})`);
1616
}

packages/vscode/__tests__/ts-plugin-tests/smoketest-ember-app-loose-and-gts.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Smoke test: Loose Mode + GTS with TS Plugin Mode', () => {
3434
let scriptEditor = await window.showTextDocument(scriptURI, { viewColumn: ViewColumn.One });
3535

3636
// Wait for a diagnostic to appear in the template
37-
await waitUntil(() => languages.getDiagnostics(scriptURI).length);
37+
await waitUntil(() => languages.getDiagnostics(scriptURI).length, 'diagnostic to appear');
3838

3939
expect(languages.getDiagnostics(scriptURI)).toMatchObject([
4040
{
@@ -51,7 +51,10 @@ describe('Smoke test: Loose Mode + GTS with TS Plugin Mode', () => {
5151
});
5252

5353
// Wait for the diagnostic to disappear
54-
await waitUntil(() => languages.getDiagnostics(scriptURI).length == 0);
54+
await waitUntil(
55+
() => languages.getDiagnostics(scriptURI).length == 0,
56+
'diagnostic to disappear',
57+
);
5558
});
5659
});
5760
});

0 commit comments

Comments
 (0)