@glint/ember-tsc 1.0.1-unstable.0a00188

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/README.md +49 -0
  2. package/bin/ember-tsc.js +4 -0
  3. package/bin/glint-language-server.js +2 -0
  4. package/lib/cli/run-volar-tsc.d.ts +2 -0
  5. package/lib/cli/run-volar-tsc.d.ts.map +1 -0
  6. package/lib/cli/run-volar-tsc.js +30 -0
  7. package/lib/cli/run-volar-tsc.js.map +1 -0
  8. package/lib/config/config.d.ts +15 -0
  9. package/lib/config/config.d.ts.map +1 -0
  10. package/lib/config/config.js +21 -0
  11. package/lib/config/config.js.map +1 -0
  12. package/lib/config/environment.d.ts +26 -0
  13. package/lib/config/environment.d.ts.map +1 -0
  14. package/lib/config/environment.js +96 -0
  15. package/lib/config/environment.js.map +1 -0
  16. package/lib/config/index.d.ts +17 -0
  17. package/lib/config/index.d.ts.map +1 -0
  18. package/lib/config/index.js +26 -0
  19. package/lib/config/index.js.map +1 -0
  20. package/lib/config/loader.d.ts +25 -0
  21. package/lib/config/loader.d.ts.map +1 -0
  22. package/lib/config/loader.js +110 -0
  23. package/lib/config/loader.js.map +1 -0
  24. package/lib/config/types.cjs +3 -0
  25. package/lib/config/types.cjs.map +1 -0
  26. package/lib/config/types.d.cts +60 -0
  27. package/lib/config/types.d.cts.map +1 -0
  28. package/lib/environment-ember-template-imports/-private/environment/common.d.ts +13 -0
  29. package/lib/environment-ember-template-imports/-private/environment/common.d.ts.map +1 -0
  30. package/lib/environment-ember-template-imports/-private/environment/common.js +2 -0
  31. package/lib/environment-ember-template-imports/-private/environment/common.js.map +1 -0
  32. package/lib/environment-ember-template-imports/-private/environment/index.d.ts +3 -0
  33. package/lib/environment-ember-template-imports/-private/environment/index.d.ts.map +1 -0
  34. package/lib/environment-ember-template-imports/-private/environment/index.js +76 -0
  35. package/lib/environment-ember-template-imports/-private/environment/index.js.map +1 -0
  36. package/lib/environment-ember-template-imports/-private/environment/preprocess.d.ts +4 -0
  37. package/lib/environment-ember-template-imports/-private/environment/preprocess.d.ts.map +1 -0
  38. package/lib/environment-ember-template-imports/-private/environment/preprocess.js +73 -0
  39. package/lib/environment-ember-template-imports/-private/environment/preprocess.js.map +1 -0
  40. package/lib/environment-ember-template-imports/-private/environment/transform.d.ts +4 -0
  41. package/lib/environment-ember-template-imports/-private/environment/transform.d.ts.map +1 -0
  42. package/lib/environment-ember-template-imports/-private/environment/transform.js +134 -0
  43. package/lib/environment-ember-template-imports/-private/environment/transform.js.map +1 -0
  44. package/lib/index.d.ts +7 -0
  45. package/lib/index.d.ts.map +1 -0
  46. package/lib/index.js +6 -0
  47. package/lib/index.js.map +1 -0
  48. package/lib/plugins/g-compiler-errors.d.ts +12 -0
  49. package/lib/plugins/g-compiler-errors.d.ts.map +1 -0
  50. package/lib/plugins/g-compiler-errors.js +58 -0
  51. package/lib/plugins/g-compiler-errors.js.map +1 -0
  52. package/lib/plugins/g-template-tag-symbols.d.ts +11 -0
  53. package/lib/plugins/g-template-tag-symbols.d.ts.map +1 -0
  54. package/lib/plugins/g-template-tag-symbols.js +48 -0
  55. package/lib/plugins/g-template-tag-symbols.js.map +1 -0
  56. package/lib/plugins/utils.d.ts +25 -0
  57. package/lib/plugins/utils.d.ts.map +1 -0
  58. package/lib/plugins/utils.js +63 -0
  59. package/lib/plugins/utils.js.map +1 -0
  60. package/lib/transform/diagnostics/augmentation.d.ts +4 -0
  61. package/lib/transform/diagnostics/augmentation.d.ts.map +1 -0
  62. package/lib/transform/diagnostics/augmentation.js +223 -0
  63. package/lib/transform/diagnostics/augmentation.js.map +1 -0
  64. package/lib/transform/diagnostics/index.d.ts +5 -0
  65. package/lib/transform/diagnostics/index.d.ts.map +1 -0
  66. package/lib/transform/diagnostics/index.js +2 -0
  67. package/lib/transform/diagnostics/index.js.map +1 -0
  68. package/lib/transform/index.d.ts +4 -0
  69. package/lib/transform/index.d.ts.map +1 -0
  70. package/lib/transform/index.js +2 -0
  71. package/lib/transform/index.js.map +1 -0
  72. package/lib/transform/template/code-features.d.ts +30 -0
  73. package/lib/transform/template/code-features.d.ts.map +1 -0
  74. package/lib/transform/template/code-features.js +26 -0
  75. package/lib/transform/template/code-features.js.map +1 -0
  76. package/lib/transform/template/glimmer-ast-mapping-tree.d.ts +80 -0
  77. package/lib/transform/template/glimmer-ast-mapping-tree.d.ts.map +1 -0
  78. package/lib/transform/template/glimmer-ast-mapping-tree.js +132 -0
  79. package/lib/transform/template/glimmer-ast-mapping-tree.js.map +1 -0
  80. package/lib/transform/template/inlining/index.d.ts +16 -0
  81. package/lib/transform/template/inlining/index.d.ts.map +1 -0
  82. package/lib/transform/template/inlining/index.js +21 -0
  83. package/lib/transform/template/inlining/index.js.map +1 -0
  84. package/lib/transform/template/inlining/tagged-strings.d.ts +8 -0
  85. package/lib/transform/template/inlining/tagged-strings.d.ts.map +1 -0
  86. package/lib/transform/template/inlining/tagged-strings.js +140 -0
  87. package/lib/transform/template/inlining/tagged-strings.js.map +1 -0
  88. package/lib/transform/template/map-template-contents.d.ts +121 -0
  89. package/lib/transform/template/map-template-contents.d.ts.map +1 -0
  90. package/lib/transform/template/map-template-contents.js +287 -0
  91. package/lib/transform/template/map-template-contents.js.map +1 -0
  92. package/lib/transform/template/rewrite-module.d.ts +22 -0
  93. package/lib/transform/template/rewrite-module.d.ts.map +1 -0
  94. package/lib/transform/template/rewrite-module.js +265 -0
  95. package/lib/transform/template/rewrite-module.js.map +1 -0
  96. package/lib/transform/template/scope-stack.d.ts +13 -0
  97. package/lib/transform/template/scope-stack.d.ts.map +1 -0
  98. package/lib/transform/template/scope-stack.js +28 -0
  99. package/lib/transform/template/scope-stack.js.map +1 -0
  100. package/lib/transform/template/template-to-typescript.d.ts +19 -0
  101. package/lib/transform/template/template-to-typescript.d.ts.map +1 -0
  102. package/lib/transform/template/template-to-typescript.js +1095 -0
  103. package/lib/transform/template/template-to-typescript.js.map +1 -0
  104. package/lib/transform/template/transformed-module.d.ts +111 -0
  105. package/lib/transform/template/transformed-module.d.ts.map +1 -0
  106. package/lib/transform/template/transformed-module.js +287 -0
  107. package/lib/transform/template/transformed-module.js.map +1 -0
  108. package/lib/transform/util.d.ts +7 -0
  109. package/lib/transform/util.d.ts.map +1 -0
  110. package/lib/transform/util.js +15 -0
  111. package/lib/transform/util.js.map +1 -0
  112. package/lib/volar/ember-language-plugin.d.ts +14 -0
  113. package/lib/volar/ember-language-plugin.d.ts.map +1 -0
  114. package/lib/volar/ember-language-plugin.js +91 -0
  115. package/lib/volar/ember-language-plugin.js.map +1 -0
  116. package/lib/volar/gts-virtual-code.d.ts +83 -0
  117. package/lib/volar/gts-virtual-code.d.ts.map +1 -0
  118. package/lib/volar/gts-virtual-code.js +210 -0
  119. package/lib/volar/gts-virtual-code.js.map +1 -0
  120. package/lib/volar/language-server.d.ts +2 -0
  121. package/lib/volar/language-server.d.ts.map +1 -0
  122. package/lib/volar/language-server.js +214 -0
  123. package/lib/volar/language-server.js.map +1 -0
  124. package/lib/volar/script-snapshot.d.ts +17 -0
  125. package/lib/volar/script-snapshot.d.ts.map +1 -0
  126. package/lib/volar/script-snapshot.js +24 -0
  127. package/lib/volar/script-snapshot.js.map +1 -0
  128. package/package.json +114 -0
  129. package/src/cli/run-volar-tsc.ts +36 -0
  130. package/src/config/config.ts +33 -0
  131. package/src/config/environment.ts +128 -0
  132. package/src/config/index.ts +30 -0
  133. package/src/config/loader.ts +143 -0
  134. package/src/config/types.cts +85 -0
  135. package/src/environment-ember-template-imports/-private/environment/common.ts +14 -0
  136. package/src/environment-ember-template-imports/-private/environment/index.ts +83 -0
  137. package/src/environment-ember-template-imports/-private/environment/preprocess.ts +90 -0
  138. package/src/environment-ember-template-imports/-private/environment/transform.ts +202 -0
  139. package/src/index.ts +9 -0
  140. package/src/plugins/g-compiler-errors.ts +67 -0
  141. package/src/plugins/g-template-tag-symbols.ts +54 -0
  142. package/src/plugins/utils.ts +86 -0
  143. package/src/transform/diagnostics/augmentation.ts +333 -0
  144. package/src/transform/diagnostics/index.ts +5 -0
  145. package/src/transform/index.ts +4 -0
  146. package/src/transform/template/code-features.ts +30 -0
  147. package/src/transform/template/glimmer-ast-mapping-tree.ts +173 -0
  148. package/src/transform/template/inlining/index.ts +33 -0
  149. package/src/transform/template/inlining/tagged-strings.ts +187 -0
  150. package/src/transform/template/map-template-contents.ts +501 -0
  151. package/src/transform/template/rewrite-module.ts +372 -0
  152. package/src/transform/template/scope-stack.ts +34 -0
  153. package/src/transform/template/template-to-typescript.ts +1476 -0
  154. package/src/transform/template/transformed-module.ts +431 -0
  155. package/src/transform/util.ts +24 -0
  156. package/src/volar/ember-language-plugin.ts +108 -0
  157. package/src/volar/gts-virtual-code.ts +249 -0
  158. package/src/volar/language-server.ts +250 -0
  159. package/src/volar/script-snapshot.ts +27 -0
  160. package/types/-private/dsl/globals.d.ts +204 -0
  161. package/types/-private/dsl/index.d.ts +50 -0
  162. package/types/-private/dsl/integration-declarations.d.ts +143 -0
  163. package/types/-private/intrinsics/action.d.ts +45 -0
  164. package/types/-private/intrinsics/concat.d.ts +6 -0
  165. package/types/-private/intrinsics/each-in.d.ts +24 -0
  166. package/types/-private/intrinsics/each.d.ts +17 -0
  167. package/types/-private/intrinsics/fn.d.ts +44 -0
  168. package/types/-private/intrinsics/get.d.ts +31 -0
  169. package/types/-private/intrinsics/input.d.ts +24 -0
  170. package/types/-private/intrinsics/link-to.d.ts +31 -0
  171. package/types/-private/intrinsics/log.d.ts +6 -0
  172. package/types/-private/intrinsics/mount.d.ts +9 -0
  173. package/types/-private/intrinsics/mut.d.ts +14 -0
  174. package/types/-private/intrinsics/on.d.ts +21 -0
  175. package/types/-private/intrinsics/outlet.d.ts +8 -0
  176. package/types/-private/intrinsics/textarea.d.ts +16 -0
  177. package/types/-private/intrinsics/unbound.d.ts +10 -0
  178. package/types/-private/intrinsics/unique-id.d.ts +5 -0
  179. package/types/globals/index.d.ts +3 -0
  180. package/types/silent-error.d.ts +4 -0
@@ -0,0 +1,83 @@
1
+ import { GlintEnvironmentConfig, GlintSpecialFormConfig } from '@glint/ember-tsc/config-types';
2
+ import { preprocess } from './preprocess.js';
3
+ import { transform } from './transform.js';
4
+
5
+ export default function emberTemplateImportsEnvironment(
6
+ options: Record<string, unknown>,
7
+ ): GlintEnvironmentConfig {
8
+ let additionalSpecialForms =
9
+ typeof options['additionalSpecialForms'] === 'object'
10
+ ? (options['additionalSpecialForms'] as GlintSpecialFormConfig)
11
+ : {};
12
+
13
+ const additionalGlobalSpecialForms = additionalSpecialForms.globals ?? {};
14
+
15
+ const additionalGlobals = Array.isArray(options['additionalGlobals'])
16
+ ? options['additionalGlobals']
17
+ : [];
18
+
19
+ return {
20
+ tags: {
21
+ '@glint/ember-tsc/environment-ember-template-imports/-private/tag': {
22
+ hbs: {
23
+ typesModule: '@glint/ember-tsc/-private/dsl',
24
+ specialForms: {
25
+ globals: {
26
+ if: 'if',
27
+ unless: 'if-not',
28
+ yield: 'yield',
29
+ component: 'bind-invokable',
30
+ modifier: 'bind-invokable',
31
+ helper: 'bind-invokable',
32
+ ...additionalGlobalSpecialForms,
33
+ },
34
+ imports: {
35
+ '@ember/helper': {
36
+ array: 'array-literal',
37
+ hash: 'object-literal',
38
+ ...additionalSpecialForms.imports?.['@ember/helper'],
39
+ },
40
+ ...additionalSpecialForms.imports,
41
+ },
42
+ },
43
+ globals: [
44
+ 'action',
45
+ 'component',
46
+ 'debugger',
47
+ 'each',
48
+ 'each-in',
49
+ 'has-block',
50
+ 'has-block-params',
51
+ 'helper',
52
+ 'if',
53
+ 'in-element',
54
+ 'let',
55
+ 'log',
56
+ 'modifier',
57
+ 'mount',
58
+ 'mut',
59
+ 'outlet',
60
+ 'unbound',
61
+ 'unless',
62
+ 'with',
63
+ 'yield',
64
+ ...Object.keys(additionalGlobalSpecialForms),
65
+ ...additionalGlobals,
66
+ ],
67
+ },
68
+ },
69
+ },
70
+ extensions: {
71
+ '.gts': {
72
+ kind: 'typed-script',
73
+ preprocess,
74
+ transform,
75
+ },
76
+ '.gjs': {
77
+ kind: 'untyped-script',
78
+ preprocess,
79
+ transform,
80
+ },
81
+ },
82
+ };
83
+ }
@@ -0,0 +1,90 @@
1
+ import { GlintExtensionPreprocess } from '@glint/ember-tsc/config-types';
2
+ import { GLOBAL_TAG, PreprocessData, TemplateLocation } from './common.js';
3
+
4
+ const TEMPLATE_START = `[${GLOBAL_TAG}\``;
5
+ const TEMPLATE_END = '`]';
6
+
7
+ import { Preprocessor } from 'content-tag';
8
+ const p = new Preprocessor();
9
+
10
+ export const preprocess: GlintExtensionPreprocess<PreprocessData> = (source, path) => {
11
+ // NOTE: https://github.com/embroider-build/content-tag/issues/45
12
+ // All indicies are byte-index, not char-index.
13
+ let templates = p.parse(source, { filename: path });
14
+
15
+ let templateLocations: Array<TemplateLocation> = [];
16
+ let contents = '';
17
+ let sourceOffsetBytes = 0;
18
+ let deltaBytes = 0;
19
+
20
+ let sourceBuffer = getBuffer(source);
21
+
22
+ for (let template of templates) {
23
+ let startTagLengthBytes = template.startRange.end - template.startRange.start;
24
+ let endTagLengthBytes = template.endRange.end - template.endRange.start;
25
+ let startTagOffsetBytes = template.startRange.start;
26
+ let endTagOffsetBytes = template.endRange.start;
27
+ let transformedStartBytes = startTagOffsetBytes - deltaBytes;
28
+ /**
29
+ * TODO: we want content-tag to manage all this for us, as managing indicies
30
+ * can be error-prone.
31
+ *
32
+ * SEE: https://github.com/embroider-build/content-tag/issues/39#issuecomment-1832443310
33
+ */
34
+ let prefixingSegment = sourceBuffer.subarray(sourceOffsetBytes, startTagOffsetBytes);
35
+ contents = contents.concat(prefixingSegment.toString());
36
+ contents = contents.concat(TEMPLATE_START);
37
+
38
+ // For TEMPLATE_START & TEMPLATE_END, characters === bytes
39
+ deltaBytes += startTagLengthBytes - TEMPLATE_START.length;
40
+
41
+ let transformedEnd = endTagOffsetBytes - deltaBytes + TEMPLATE_END.length;
42
+ let templateContentSegment = sourceBuffer.subarray(
43
+ startTagOffsetBytes + startTagLengthBytes,
44
+ endTagOffsetBytes,
45
+ );
46
+ let templateContentSegmentString = templateContentSegment.toString();
47
+ let escapedTemplateContentSegment = templateContentSegmentString
48
+ .replaceAll('$', '\\$')
49
+ .replaceAll('`', '\\`');
50
+ deltaBytes += templateContentSegmentString.length - escapedTemplateContentSegment.length;
51
+
52
+ contents = contents.concat(escapedTemplateContentSegment);
53
+ contents = contents.concat(TEMPLATE_END);
54
+ deltaBytes += endTagLengthBytes - TEMPLATE_END.length;
55
+
56
+ sourceOffsetBytes = endTagOffsetBytes + endTagLengthBytes;
57
+ templateLocations.push({
58
+ startTagOffset: byteToCharIndex(source, startTagOffsetBytes),
59
+ endTagOffset: byteToCharIndex(source, endTagOffsetBytes),
60
+ startTagLength: byteToCharIndex(source, startTagLengthBytes),
61
+ endTagLength: byteToCharIndex(source, endTagLengthBytes),
62
+ transformedStart: byteToCharIndex(contents, transformedStartBytes),
63
+ transformedEnd: byteToCharIndex(contents, transformedEnd),
64
+ });
65
+ }
66
+
67
+ contents = contents.concat(sourceBuffer.subarray(sourceOffsetBytes).toString());
68
+ return {
69
+ contents,
70
+ data: {
71
+ templateLocations,
72
+ },
73
+ };
74
+ };
75
+
76
+ function byteToCharIndex(str: string, byteOffset: number): number {
77
+ const buf = getBuffer(str);
78
+ return buf.subarray(0, byteOffset).toString().length;
79
+ }
80
+
81
+ const BufferMap = new Map();
82
+
83
+ function getBuffer(str: string): Buffer {
84
+ let buf = BufferMap.get(str);
85
+ if (!buf) {
86
+ buf = Buffer.from(str);
87
+ BufferMap.set(str, buf);
88
+ }
89
+ return buf;
90
+ }
@@ -0,0 +1,202 @@
1
+ import { GlintExtensionTransform } from '@glint/ember-tsc/config-types';
2
+ import type ts from 'typescript';
3
+ import { GLOBAL_TAG, PreprocessData, TemplateLocation } from './common.js';
4
+
5
+ type TSLib = typeof ts;
6
+
7
+ export const transform: GlintExtensionTransform<PreprocessData> = (
8
+ data,
9
+ { ts, context, setEmitMetadata },
10
+ ) => {
11
+ let f = ts.factory;
12
+ let { templateLocations } = data;
13
+ if (!templateLocations.length) return (sf) => sf;
14
+
15
+ return function visit(node: ts.Node): ts.Node {
16
+ let visitedNode = ts.visitEachChild(node, visit, context);
17
+ let transformedNode = transformNode(visitedNode);
18
+ return repairAncestry(transformedNode);
19
+ };
20
+
21
+ function transformNode(node: ts.Node): ts.Node {
22
+ if (ts.isSourceFile(node)) {
23
+ // Add `import { hbs as __T } from 'ember-template-imports'` to the file
24
+ return addTagImport(f, node);
25
+ } else if (isETIDefaultTemplate(ts, node)) {
26
+ // Annotate that this template is a default export
27
+ setEmitMetadata(node.expression, { prepend: 'export default ' });
28
+ return node;
29
+ } else if (isETIDefaultSatisfiesTemplate(ts, node)) {
30
+ // Annotate that this template is a default export
31
+ setEmitMetadata(node.expression.expression, { prepend: 'export default ' });
32
+ return node;
33
+ } else if (isETITemplateExpression(ts, node)) {
34
+ // Convert '[__T`foo`]' as an expression to just '__T`foo`'
35
+ let location = findTemplateLocation(templateLocations, node);
36
+
37
+ let template = node.elements[0];
38
+ setEmitMetadata(template, {
39
+ templateLocation: {
40
+ start: location.startTagOffset,
41
+ end: location.endTagOffset + location.endTagLength,
42
+ contentStart: location.startTagOffset + location.startTagLength,
43
+ contentEnd: location.endTagOffset,
44
+ },
45
+ });
46
+ return template;
47
+ } else if (isETITemplateProperty(ts, node)) {
48
+ // Convert '[__T`foo`]' in a class body to 'static { __T`foo` }'
49
+ let location = findTemplateLocation(templateLocations, node);
50
+ let template = node.name.expression;
51
+
52
+ setEmitMetadata(template, {
53
+ prepend: 'static { ',
54
+ append: ' }',
55
+ templateLocation: {
56
+ start: location.startTagOffset,
57
+ end: location.endTagOffset + location.endTagLength,
58
+ contentStart: location.startTagOffset + location.startTagLength,
59
+ contentEnd: location.endTagOffset,
60
+ },
61
+ });
62
+
63
+ return buildStaticBlockForTemplate(f, template);
64
+ }
65
+
66
+ return node;
67
+ }
68
+ };
69
+
70
+ // Many location operations in the TS AST rely on having an unbroken chain
71
+ // of `.parent` values fron a given node up to its containing `SourceFile`,
72
+ // but its transformation framework does not maintain these by default,
73
+ // so we explicitly reconnect nodes as we go.
74
+ function repairAncestry(node: ts.Node, parent: ts.Node = node.parent): ts.Node {
75
+ // If the node already has a parent AND it's correct, we don't need
76
+ // to descend further.
77
+ if (parent && node.parent === parent) return node;
78
+
79
+ Object.assign(node, { parent });
80
+
81
+ node.forEachChild((child) => {
82
+ repairAncestry(child, node);
83
+ });
84
+
85
+ return node;
86
+ }
87
+
88
+ function addTagImport(f: ts.NodeFactory, sourceFile: ts.SourceFile): ts.SourceFile {
89
+ return f.updateSourceFile(sourceFile, [
90
+ f.createImportDeclaration(
91
+ [],
92
+ f.createImportClause(
93
+ false,
94
+ undefined,
95
+ f.createNamedImports([
96
+ f.createImportSpecifier(false, f.createIdentifier('hbs'), f.createIdentifier(GLOBAL_TAG)),
97
+ ]),
98
+ ),
99
+ f.createStringLiteral('@glint/ember-tsc/environment-ember-template-imports/-private/tag'),
100
+ ),
101
+ ...sourceFile.statements,
102
+ ]);
103
+ }
104
+
105
+ type ETITemplateLiteral = ts.TaggedTemplateExpression & {
106
+ template: ts.NoSubstitutionTemplateLiteral;
107
+ };
108
+
109
+ type ETITemplateExpression = ts.ArrayLiteralExpression & {
110
+ elements: [ETITemplateLiteral];
111
+ };
112
+
113
+ type ETITemplateProperty = ts.PropertyDeclaration & {
114
+ name: ts.ComputedPropertyName & { expression: ETITemplateLiteral };
115
+ };
116
+
117
+ type ETIDefaultTemplate = ts.ExpressionStatement & {
118
+ expression: ETITemplateLiteral;
119
+ };
120
+
121
+ type ETIDefaultSatisfiesTemplate = ts.ExpressionStatement & {
122
+ expression: ts.SatisfiesExpression & { expression: ETITemplateLiteral };
123
+ };
124
+
125
+ /**
126
+ * Implicit default export:
127
+ *
128
+ * ( <template></template> )
129
+ * ^ ExpressionStatement
130
+ *
131
+ * ( <template></template> satisfies ... )
132
+ * ^ SatisfiesExpression
133
+ *
134
+ * But!
135
+ *
136
+ * ( const X = <template></template> satisfies ... )
137
+ * ^ VariableStatement
138
+ *
139
+ * So when we check for a wrapping SatisfiesExpression, we need to also make sure
140
+ * the parent node is not a variable Statement.
141
+ */
142
+ function isETIDefaultTemplate(ts: TSLib, node: ts.Node): node is ETIDefaultTemplate {
143
+ return ts.isExpressionStatement(node) && isETITemplateLiteral(ts, node.expression);
144
+ }
145
+
146
+ function isETIDefaultSatisfiesTemplate(
147
+ ts: TSLib,
148
+ node: ts.Node,
149
+ ): node is ETIDefaultSatisfiesTemplate {
150
+ return (
151
+ ts.isExpressionStatement(node) &&
152
+ ts.isSatisfiesExpression(node.expression) &&
153
+ isETITemplateLiteral(ts, node.expression.expression)
154
+ );
155
+ }
156
+
157
+ function isETITemplateProperty(ts: TSLib, node: ts.Node): node is ETITemplateProperty {
158
+ return (
159
+ ts.isPropertyDeclaration(node) &&
160
+ ts.isComputedPropertyName(node.name) &&
161
+ isETITemplateLiteral(ts, node.name.expression)
162
+ );
163
+ }
164
+
165
+ function isETITemplateExpression(ts: TSLib, node: ts.Node): node is ETITemplateExpression {
166
+ return (
167
+ ts.isArrayLiteralExpression(node) &&
168
+ node.elements.length === 1 &&
169
+ isETITemplateLiteral(ts, node.elements[0])
170
+ );
171
+ }
172
+
173
+ function isETITemplateLiteral(ts: TSLib, node: ts.Node): node is ETITemplateLiteral {
174
+ return (
175
+ ts.isTaggedTemplateExpression(node) &&
176
+ ts.isNoSubstitutionTemplateLiteral(node.template) &&
177
+ ts.isIdentifier(node.tag) &&
178
+ node.tag.text === GLOBAL_TAG
179
+ );
180
+ }
181
+
182
+ function findTemplateLocation(
183
+ locations: Array<TemplateLocation>,
184
+ node: ETITemplateExpression | ETITemplateProperty,
185
+ ): TemplateLocation {
186
+ let location = locations.find((loc) => loc.transformedStart === node.getStart());
187
+
188
+ if (!location) {
189
+ throw new Error('Internal error: missing location info for template');
190
+ }
191
+
192
+ return location;
193
+ }
194
+
195
+ function buildStaticBlockForTemplate(
196
+ f: ts.NodeFactory,
197
+ template: ts.TaggedTemplateExpression,
198
+ ): ts.Node {
199
+ return f.createClassStaticBlockDeclaration(
200
+ f.createBlock([f.createExpressionStatement(template)]),
201
+ );
202
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { GlintConfig, loadConfig, findConfig } from './config/index.js';
2
+ import { createEmberLanguagePlugin } from './volar/ember-language-plugin.js';
3
+
4
+ import { VirtualGtsCode } from './volar/gts-virtual-code.js';
5
+ import { augmentDiagnostics } from './transform/diagnostics/augmentation.js';
6
+
7
+ export { loadConfig, findConfig, createEmberLanguagePlugin, VirtualGtsCode, augmentDiagnostics };
8
+
9
+ export type { GlintConfig };
@@ -0,0 +1,67 @@
1
+ import type { LanguageServicePlugin } from '@volar/language-service';
2
+ import type * as vscode from 'vscode-languageserver-protocol';
3
+ import { getEmbeddedInfo } from './utils.js';
4
+
5
+ /**
6
+ * This LanguageServicePlugin surfaces compiler/syntax errors as diagnostics
7
+ * within .gts/.gjs files, e.g. if there is an unclosed html tag or `{{` curly brace,
8
+ * the entire `<template>` tag region will be highlighted as an error.
9
+ *
10
+ * @GLINT_FEATURE_DIAGNOSTICS
11
+ * @GLINT_FEATURE_DIAGNOSTICS_LANGUAGE_SERVER
12
+ * @GLINT_FEATURE_DIAGNOSTICS_LANGUAGE_SERVER_GTS_COMPILER_ERRORS
13
+ */
14
+ export function create(): LanguageServicePlugin {
15
+ return {
16
+ name: 'g-compiler-errors',
17
+ capabilities: {
18
+ diagnosticProvider: {
19
+ interFileDependencies: false,
20
+ workspaceDiagnostics: false,
21
+ },
22
+ },
23
+ create(context) {
24
+ return {
25
+ provideDiagnostics(document) {
26
+ // `gts` is the ID of the root virtual code within the virtual/embedded code tree of our GtsVirtualCode.
27
+ // It is the untransformed .gts file; we use this file to generate diagnostics for
28
+ // `<template>` tag compiler errors because the diagnostic regions within the errors
29
+ // we get back from the compiler are already relative to the root/outer .gts file
30
+ // rather than relative to start of the template tag.
31
+ const info = getEmbeddedInfo(context, document, 'gts');
32
+
33
+ if (!info) {
34
+ return;
35
+ }
36
+
37
+ const virtualCode = info.root;
38
+
39
+ const transformedModule = virtualCode.transformedModule;
40
+ if (!transformedModule) {
41
+ return;
42
+ }
43
+
44
+ const parseErrors: vscode.Diagnostic[] = [];
45
+
46
+ for (const error of transformedModule.errors) {
47
+ const start = document.positionAt(error.location.start);
48
+ const end = document.positionAt(error.location.end);
49
+
50
+ parseErrors.push({
51
+ range: {
52
+ start: start,
53
+ end: end,
54
+ },
55
+ severity: 1 satisfies typeof vscode.DiagnosticSeverity.Error,
56
+ code: 9999,
57
+ source: 'glint',
58
+ message: error.message,
59
+ });
60
+ }
61
+
62
+ return parseErrors;
63
+ },
64
+ };
65
+ },
66
+ };
67
+ }
@@ -0,0 +1,54 @@
1
+ import type { LanguageServicePlugin } from '@volar/language-service';
2
+ import type * as vscode from 'vscode-languageserver-protocol';
3
+ import { getEmbeddedInfo } from './utils.js';
4
+
5
+ /**
6
+ * Provides symbols for `<template>` tags within .gts/.gjs files.
7
+ * This among other things faciliates code folding of the `<template>` region.
8
+ *
9
+ * Note that this ONLY provides symbols for the `<template>` tag and none of the
10
+ * contents of the template tag. Symbols inside or outside the template tag are actually
11
+ * generated by the syntactic TS LanguageServicePlugin (search `createTypeScriptSyntacticPlugin`).
12
+ */
13
+ export function create(): LanguageServicePlugin {
14
+ return {
15
+ name: 'g-template-symbols',
16
+ capabilities: {
17
+ documentSymbolProvider: true,
18
+ },
19
+ create(context) {
20
+ return {
21
+ provideDocumentSymbols(document) {
22
+ const virtualCode = getEmbeddedInfo(context, document, 'gts')?.root;
23
+
24
+ if (!virtualCode) {
25
+ return;
26
+ }
27
+
28
+ const result: vscode.DocumentSymbol[] = [];
29
+ const { transformedModule } = virtualCode;
30
+
31
+ if (transformedModule) {
32
+ const templateSymbols = transformedModule.templateSymbols();
33
+ for (const templateSymbol of templateSymbols) {
34
+ result.push({
35
+ name: 'template',
36
+ kind: 2 satisfies typeof vscode.SymbolKind.Module,
37
+ range: {
38
+ start: document.positionAt(templateSymbol.start),
39
+ end: document.positionAt(templateSymbol.end),
40
+ },
41
+ selectionRange: {
42
+ start: document.positionAt(templateSymbol.start),
43
+ end: document.positionAt(templateSymbol.startTagEnd),
44
+ },
45
+ });
46
+ }
47
+ }
48
+
49
+ return result;
50
+ },
51
+ };
52
+ },
53
+ };
54
+ }
@@ -0,0 +1,86 @@
1
+ // based on https://github.com/vuejs/language-tools/blob/master/packages/language-service/lib/utils.ts
2
+
3
+ import {
4
+ type LanguageServiceContext,
5
+ type SourceScript,
6
+ type TextDocument,
7
+ type VirtualCode,
8
+ } from '@volar/language-service';
9
+ import { URI } from 'vscode-uri';
10
+ import { VirtualGtsCode } from '../volar/gts-virtual-code.js';
11
+
12
+ type EmbeddedInfo = {
13
+ sourceScript: Required<SourceScript<URI>>;
14
+ virtualCode: VirtualCode;
15
+ root: VirtualGtsCode;
16
+ };
17
+
18
+ /**
19
+ * Helper function that accepts a document URI and, if it matches the format
20
+ * of Volar's special URI format for representing embedded codes within a virtual code,
21
+ * will look up and return the embedded code and its root VirtualGtsCode.
22
+ *
23
+ * A example Volar embedded document URI would be something like:
24
+ *
25
+ * volar-embedded-content://gts/file%253A%252F%252F%252FUsers%252Fmachty%252Fcode%252Fglint%252Ftest-packages%252Fts-template-imports-app%252Fsrc%252Fempty-fixture.gts
26
+ *
27
+ * Note that the hostname/authority part of the URI is the embedded code ID, e.g. `gts`. These correspond
28
+ * to the IDs we assign to parsed embedded virtual codes contained within the root virtual code,
29
+ * e.g. `ts` corresponds to the TypeScript representation of .gts files where all `<template>` tags
30
+ * have been converted to type-checkable TS.
31
+ */
32
+ export function getEmbeddedInfo(
33
+ context: LanguageServiceContext,
34
+ document: TextDocument,
35
+ desiredEmbeddedCodeId?: string | ((id: string) => boolean),
36
+ desiredDocumentLanguageId?: string | ((id: string) => boolean),
37
+ ): EmbeddedInfo | undefined {
38
+ const uri = URI.parse(document.uri);
39
+ const decoded = context.decodeEmbeddedDocumentUri(uri);
40
+ if (!decoded) {
41
+ return;
42
+ }
43
+
44
+ const [documentUri, embeddedCodeId] = decoded;
45
+
46
+ if (desiredEmbeddedCodeId) {
47
+ if (typeof desiredEmbeddedCodeId === 'string') {
48
+ if (embeddedCodeId !== desiredEmbeddedCodeId) {
49
+ return;
50
+ }
51
+ } else if (!desiredEmbeddedCodeId(embeddedCodeId)) {
52
+ return;
53
+ }
54
+ }
55
+
56
+ if (desiredDocumentLanguageId) {
57
+ if (typeof desiredDocumentLanguageId === 'string') {
58
+ if (document.languageId !== desiredDocumentLanguageId) {
59
+ return;
60
+ }
61
+ } else if (!desiredDocumentLanguageId(document.languageId)) {
62
+ return;
63
+ }
64
+ }
65
+
66
+ const sourceScript = context.language.scripts.get(documentUri);
67
+ if (!sourceScript?.generated) {
68
+ return;
69
+ }
70
+
71
+ const virtualCode = sourceScript.generated.embeddedCodes.get(embeddedCodeId);
72
+ if (!virtualCode) {
73
+ return;
74
+ }
75
+
76
+ const root = sourceScript.generated.root;
77
+ if (!(root instanceof VirtualGtsCode)) {
78
+ return;
79
+ }
80
+
81
+ return {
82
+ sourceScript: sourceScript as Required<SourceScript<URI>>,
83
+ virtualCode,
84
+ root,
85
+ };
86
+ }