forked from ember-tooling/ember-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransforms.js
More file actions
345 lines (311 loc) · 10.3 KB
/
transforms.js
File metadata and controls
345 lines (311 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { createRequire } from 'node:module';
import { Preprocessor } from 'content-tag';
import { isKeyword as glimmerIsKeyword } from '@glimmer/syntax';
import { glimmerVisitorKeys } from 'ember-estree';
import { Reference, Scope, Variable, Definition } from 'eslint-scope';
import htmlTags from 'html-tags';
import svgTags from 'svg-tags';
import { mathmlTagNames } from 'mathml-tag-names';
const htmlTagsSet = new Set(htmlTags);
const svgTagsSet = new Set(svgTags);
const mathMLTagsSet = new Set(mathmlTagNames);
const require = createRequire(import.meta.url);
let TypescriptScope = null;
try {
const parserPath = require.resolve('@typescript-eslint/parser');
// eslint-disable-next-line n/no-unpublished-require
const scopeManagerPath = require.resolve('@typescript-eslint/scope-manager', {
paths: [parserPath],
});
TypescriptScope = require(scopeManagerPath);
} catch {
// not available
}
// ── Scope helpers ─────────────────────────────────────────────────────
function findParentScope(scopeManager, nodePath) {
let scope = null;
let path = nodePath;
while (path) {
scope = scopeManager.acquire(path.node, true);
if (scope) {
return scope;
}
path = path.parentPath;
}
return null;
}
function findVarInParentScopes(scopeManager, nodePath, name) {
let defScope = null;
let currentScope = null;
let path = nodePath;
while (path) {
const s = scopeManager.acquire(path.node, true);
if (s) {
if (!currentScope) currentScope = s;
if (s.set.has(name)) {
defScope = s;
break;
}
}
path = path.parentPath;
}
if (!defScope) {
return { scope: currentScope };
}
return { scope: currentScope, variable: defScope.set.get(name) };
}
function registerNodeInScope(node, scope, variable) {
const ref = new Reference(node, scope, Reference.READ);
if (variable) {
variable.references.push(ref);
ref.resolved = variable;
} else {
let s = scope;
while (s.upper) {
s = s.upper;
}
s.through.push(ref);
}
scope.references.push(ref);
}
function isUpperCase(char) {
return char.toUpperCase() === char;
}
function registerBlockParams(node, path, scopeManager, isTypescript) {
const blockParamNodes = node.blockParamNodes || [];
if (blockParamNodes.length === 0) return;
const upperScope = findParentScope(scopeManager, path);
const scope = isTypescript
? new TypescriptScope.BlockScope(scopeManager, upperScope, node)
: new Scope(scopeManager, 'block', upperScope, node);
const declaredVariables = scopeManager.declaredVariables || scopeManager.__declaredVariables;
const vars = [];
declaredVariables.set(node, vars);
const virtualJSParentNode = {
type: 'FunctionDeclaration',
params: blockParamNodes,
range: node.range,
loc: node.loc,
parent: path.parent,
};
for (const [i, b] of blockParamNodes.entries()) {
const v = new Variable(b.name, scope);
v.identifiers.push(b);
scope.variables.push(v);
scope.set.set(b.name, v);
vars.push(v);
const virtualJSNode = {
type: 'Identifier',
name: b.name,
range: b.range,
loc: b.loc,
parent: virtualJSParentNode,
};
v.defs.push(new Definition('Parameter', virtualJSNode, node, node, i, 'Block Param'));
v.defs.push(new Definition('Parameter', b, node, node, i, 'Block Param'));
}
}
function registerPathExpression(node, path, scopeManager) {
if (node.head.type !== 'VarHead') return;
const name = node.head.name;
if (glimmerIsKeyword(name)) return;
const { scope, variable } = findVarInParentScopes(scopeManager, path, name) || {};
if (scope) {
node.head.parent = node;
registerNodeInScope(node.head, scope, variable);
}
}
function registerElementNode(node, path, scopeManager) {
const n = node.parts[0];
const { scope, variable } = findVarInParentScopes(scopeManager, path, n.name) || {};
const ignore =
n.name === 'this' ||
n.name.startsWith(':') ||
n.name.startsWith('@') ||
!scope ||
n.name.includes('-');
const registerUndef =
isUpperCase(n.name[0]) ||
node.name.includes('.') ||
(!htmlTagsSet.has(node.name) && !svgTagsSet.has(node.name) && !mathMLTagsSet.has(node.name));
if (!ignore && (variable || registerUndef)) {
registerNodeInScope(n, scope, variable);
}
}
// ── Visitor builders ──────────────────────────────────────────────────
/**
* Build Glimmer visitors for toTree that register scopes during traversal.
* Uses a getter for scopeManager so it's available after the parser callback runs.
* @param {function} getScopeManager - returns the scopeManager (may be null initially)
* @param {boolean} isTypescript
* @returns {object} visitors for toTree
*/
export function buildGlimmerVisitors(getScopeManager, isTypescript) {
return {
GlimmerPathExpression(node, path) {
const sm = getScopeManager();
if (sm) registerPathExpression(node, path, sm);
},
GlimmerElementNode(node, path) {
const sm = getScopeManager();
if (sm) registerElementNode(node, path, sm);
},
GlimmerBlockParams(node, path) {
const sm = getScopeManager();
if (sm) registerBlockParams(node, path, sm, isTypescript);
},
};
}
// ── registerGlimmerScopes (fallback for JS/oxc path) ──────────────────
function traverse(visitorKeys, node, visitor) {
const allVisitorKeys = { ...visitorKeys, ...glimmerVisitorKeys };
const queue = [];
queue.push({
node,
parent: null,
parentKey: null,
parentPath: null,
context: {},
});
while (queue.length > 0) {
const currentPath = queue.pop();
visitor(currentPath);
if (!currentPath.node) continue;
const keys = allVisitorKeys[currentPath.node.type];
if (!keys) continue;
for (const visitorKey of keys) {
const child = currentPath.node[visitorKey];
if (!child) {
continue;
} else if (Array.isArray(child)) {
for (const item of child) {
queue.push({
node: item,
parent: currentPath.node,
context: currentPath.context,
parentKey: visitorKey,
parentPath: currentPath,
});
}
} else {
queue.push({
node: child,
parent: currentPath.node,
context: currentPath.context,
parentKey: visitorKey,
parentPath: currentPath,
});
}
}
}
}
/**
* Full AST traversal for scope registration — used as fallback for JS/oxc path.
* For the TS path, toTree's visitor API handles this during splicing.
*/
export function registerGlimmerScopes(result) {
// eslint-disable-next-line complexity
traverse(result.visitorKeys, result.ast, (path) => {
const node = path.node;
if (!node) return;
if (node.type === 'GlimmerPathExpression') {
registerPathExpression(node, path, result.scopeManager);
}
if (node.type === 'GlimmerElementNode') {
registerElementNode(node, path, result.scopeManager);
}
if ('blockParams' in node && node.type?.startsWith('Glimmer')) {
registerBlockParams(node, path, result.scopeManager, result.isTypescript);
}
});
}
/**
* Scope registration for the HBS parser. Unlike the gjs/gts path, we do not
* register references for free identifiers (`{{path}}`, `<Tag>`) — all
* template locals are treated as runtime-defined, so no-undef stays quiet.
* Only block params from `as |x|` constructs are declared.
*/
export function registerHBSScopes(result) {
traverse(result.visitorKeys, result.ast, (path) => {
const node = path.node;
if (!node) return;
if ('blockParams' in node && node.type.startsWith('Glimmer')) {
registerBlockParams(node, path, result.scopeManager, false);
}
});
}
// ── transformForLint (used by ts-patch.js) ────────────────────────────
export const replaceRange = function replaceRange(s, start, end, substitute) {
return s.slice(0, start) + substitute + s.slice(end);
};
const processor = new Preprocessor();
class EmberParserError extends Error {
constructor(message, fileName, location) {
super(message);
this.location = location;
this.fileName = fileName;
Object.defineProperty(this, 'name', {
configurable: true,
enumerable: false,
value: new.target.name,
});
}
get index() {
return this.location.start.offset;
}
get lineNumber() {
return this.location.start.line;
}
get column() {
return this.location.start.column;
}
}
function createError(code, message, fileName, start, end = start) {
return new EmberParserError(message, fileName, { end, start });
}
/**
* Transform code for TypeScript virtual file system.
* Replaces <template> regions with backtick/static-block placeholders.
* Used by ts-patch.js for type-aware linting.
*/
export function transformForLint(code, fileName) {
let result;
try {
result = processor.parse(code);
} catch (e) {
if (e.message.includes('Parse Error at')) {
const [line, column] = e.message
.split(':')
.slice(-2)
.map((x) => parseInt(x));
throw createError(code, e.source_code, fileName, { line, column });
}
throw e;
}
// Build placeholder JS inline (same format as ember-estree's toPlaceholderJS)
let jsCode = code;
for (const tplInfo of [...result].reverse()) {
const content = tplInfo.contents.replace(/`/g, '\\`').replace(/\$/g, '\\$');
const start = tplInfo.range.startUtf16Codepoint;
const end = tplInfo.range.endUtf16Codepoint;
const tplLength = end - start;
let replacement;
if (tplInfo.type === 'class-member') {
const spaces = tplLength - content.length - 10; // "static{`" + "`}" = 10
replacement = `static{\`${content}${' '.repeat(Math.max(0, spaces))}\`}`;
} else {
const spaces = tplLength - content.length - 2; // "`" + "`" = 2
replacement = `\`${content}${' '.repeat(Math.max(0, spaces))}\``;
}
jsCode = replaceRange(jsCode, start, end, replacement);
}
/* istanbul ignore next */
if (jsCode.length !== code.length) {
throw new Error('bad transform');
}
return {
templateInfos: result,
output: jsCode,
};
}
export { traverse, glimmerVisitorKeys };