Skip to content

Commit 90f1ddd

Browse files
authored
Fix organize imports (#874)
1 parent 502de14 commit 90f1ddd

File tree

2 files changed

+219
-116
lines changed

2 files changed

+219
-116
lines changed

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ import { calculateCompanionTemplateSpans } from './inlining/companion-file.js';
2121
*/
2222
export type RewriteInput = { script: SourceFile; template?: SourceFile };
2323

24+
// HACK: We prefix every transformed TS file with these non-existent imports
25+
// because it causes TypeScript to consider `.gts` and `.gjs` as possible
26+
// implied extensions when extensions are omitted from import module specifiers,
27+
// i.e. it causes `import FooComponent from './foo';` to work given a `foo.gts` file.
28+
//
29+
// Origin of this hack:
30+
// https://github.com/typed-ember/glint/issues/806#issuecomment-2758616327
31+
//
32+
// This approach has the following desirable properties:
33+
//
34+
// 1. It doesn't break Organize Imports command
35+
// 2. It doesn't introduce any keywords/variables that'll show up in auto-complete suggestions
36+
const EXTENSION_FIXING_HEADER_HACK = `
37+
// @ts-expect-error
38+
({} as typeof import('./__glint-hacky-nonexistent.gts'));
39+
40+
// @ts-expect-error
41+
({} as typeof import('./__glint-hacky-nonexistent.gjs'));
42+
43+
`;
44+
2445
/**
2546
* Given the script and/or template that together comprise a component module,
2647
* returns a `TransformedModule` representing the combined result, with the
@@ -68,25 +89,12 @@ function calculateCorrelatedSpans(
6889
let directives: Array<Directive> = [];
6990
let errors: Array<TransformError> = [];
7091
let partialSpans: Array<PartialCorrelatedSpan> = [
71-
// HACK: We prefix every transformed TS file with these non-existent imports
72-
// because it causes TypeScript to consider `.gts` and `.gjs` as possible
73-
// implied extensions when extensions are omitted from import module specifiers,
74-
// i.e. it causes `import FooComponent from './foo';` to work given a `foo.gts` file.
75-
//
76-
// Origin of this hack:
77-
// https://github.com/typed-ember/glint/issues/806#issuecomment-2758616327
78-
//
79-
// Note that these imports WILL generate diagnostic errors, but because they're
80-
// mapped to zero-length source code spans, they're essentially mapped to nothing,
81-
// and when Volar reverse-transforms the diagnostics back to the original source
82-
// code, they'll be discarded.
8392
{
8493
originalFile: script,
8594
originalStart: 0,
8695
originalLength: 0,
8796
insertionPoint: 0,
88-
transformedSource:
89-
"import __GLINT_GTS_EXTENSION_HACK__ from './__glint-non-existent.gts';\n import __GLINT_GJS_EXTENSION_HACK__ from './__glint-non-existent.gjs';\n",
97+
transformedSource: EXTENSION_FIXING_HEADER_HACK,
9098
},
9199
];
92100

0 commit comments

Comments
 (0)