-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgjs-gts-parser.js
More file actions
240 lines (212 loc) · 7.4 KB
/
gjs-gts-parser.js
File metadata and controls
240 lines (212 loc) · 7.4 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
import { createRequire } from 'node:module';
import tsconfigUtils from '@typescript-eslint/tsconfig-utils';
import babelParser from '@babel/eslint-parser/experimental-worker';
import { registerParsedFile } from '../preprocessor/noop.js';
import { patchTs, replaceExtensions, syncMtsGtsSourceFiles, typescriptParser } from './ts-patch.js';
import { toTree, buildGlimmerVisitorKeys } from 'ember-estree';
import { registerGlimmerScopes } from './transforms.js';
const require = createRequire(import.meta.url);
/**
* implements https://eslint.org/docs/latest/extend/custom-parsers
*
* Uses ember-estree's toTree with a pluggable jsParser to:
* 1. Extract <template> regions via content-tag
* 2. Parse JS/TS with TypeScript-ESLint or Babel
* 3. Process Glimmer templates and splice them into the AST
* 4. Register Glimmer scopes for ESLint rules (no-undef, no-unused-vars)
*/
/**
* @param {string} tsconfigPath
* @param {string} rootDir
* @returns {boolean|undefined}
*/
function parseAllowJsFromTsconfig(tsconfigPath, rootDir) {
try {
const parserPath = require.resolve('@typescript-eslint/parser');
// eslint-disable-next-line n/no-unpublished-require
const tsPath = require.resolve('typescript', { paths: [parserPath] });
const ts = require(tsPath);
const parsed = tsconfigUtils.getParsedConfigFile(ts, tsconfigPath, rootDir);
return parsed?.options?.allowJs;
} catch (e) {
// eslint-disable-next-line no-console
console.warn('[ember-eslint-parser] Failed to parse tsconfig:', tsconfigPath, e);
return undefined;
}
}
/**
* @param {Array<boolean|undefined>} values
* @param {string} source
* @returns {boolean|null}
*/
function resolveAllowJs(values, source) {
const filtered = values.filter((val) => typeof val !== 'undefined');
if (filtered.length > 0) {
const uniqueValues = [...new Set(filtered)];
if (uniqueValues.length > 1) {
// eslint-disable-next-line no-console
console.warn(
`[ember-eslint-parser] Conflicting allowJs values in ${source}. Defaulting allowGjs to false.`
);
return false;
} else {
return uniqueValues[0];
}
}
return null;
}
/**
* @param {Array<{getCompilerOptions?: Function}>|undefined} programs
* @returns {boolean|null}
*/
function getAllowJsFromPrograms(programs) {
if (!Array.isArray(programs) || programs.length === 0) return null;
const allowJsValues = programs
.map((p) => p.getCompilerOptions?.())
.filter(Boolean)
.map((opts) => opts.allowJs);
return resolveAllowJs(allowJsValues, 'programs');
}
/**
* @param {boolean|object|undefined} projectService
* @returns {string|null}
*/
function getProjectServiceTsconfigPath(projectService) {
if (!projectService) return null;
if (projectService === true) {
return 'tsconfig.json';
}
if (typeof projectService === 'object') {
if (typeof projectService.allowDefaultProject !== 'undefined') {
// eslint-disable-next-line no-console
console.warn(
'[ember-eslint-parser] projectService.allowDefaultProject is specified. Behavior may differ depending on default project config.'
);
}
return projectService.defaultProject ?? 'tsconfig.json';
}
return null;
}
/**
* Returns the resolved allowJs value based on priority: programs > projectService > project/tsconfig
*/
function getAllowJs(options) {
const allowJsFromPrograms = getAllowJsFromPrograms(options.programs);
if (allowJsFromPrograms !== null) return allowJsFromPrograms;
const rootDir = options.tsconfigRootDir || process.cwd();
const projectServiceTsconfigPath = getProjectServiceTsconfigPath(options.projectService);
if (projectServiceTsconfigPath) {
return parseAllowJsFromTsconfig(projectServiceTsconfigPath, rootDir);
}
let tsconfigPaths = [];
if (Array.isArray(options.project)) {
tsconfigPaths = options.project;
} else if (typeof options.project === 'string') {
tsconfigPaths = [options.project];
} else if (options.project) {
tsconfigPaths = ['tsconfig.json'];
}
if (tsconfigPaths.length > 0) {
const allowJsValues = tsconfigPaths.map((cfg) => parseAllowJsFromTsconfig(cfg, rootDir));
return resolveAllowJs(allowJsValues, 'project');
}
return false;
}
/**
* @type {import('eslint').ParserModule}
*/
export const meta = {
name: 'ember-eslint-parser',
version: '*',
};
export function parseForESLint(code, options) {
const allowGjsWasSet = options.allowGjs !== undefined;
const allowGjs = allowGjsWasSet ? options.allowGjs : getAllowJs(options);
let actualAllowGjs;
if (options.programs || options.projectService || options.project) {
({ allowGjs: actualAllowGjs } = patchTs({ allowGjs }));
}
registerParsedFile(options.filePath);
const isTypescript = options.filePath.endsWith('.gts') || options.filePath.endsWith('.ts');
let useTypescript = true;
if (options.useBabel || !typescriptParser) {
useTypescript = false;
}
if (isTypescript && !typescriptParser) {
throw new Error('Please install typescript to process gts');
}
const filePath = options.filePath;
// scopeManager is captured from jsParser result for use in the visitor
let scopeManager = null;
let jsParserResult = null;
try {
const result = toTree(code, {
filePath,
jsParser(placeholderJS, filename) {
let jsCode = placeholderJS;
if (options.project || options.projectService) {
jsCode = replaceExtensions(jsCode);
}
jsParserResult =
isTypescript || useTypescript
? typescriptParser.parseForESLint(jsCode, {
...options,
ranges: true,
extraFileExtensions: ['.gts', '.gjs'],
filePath: filename,
})
: babelParser.parseForESLint(jsCode, {
...options,
requireConfigFile: false,
ranges: true,
});
scopeManager = jsParserResult.scopeManager;
return jsParserResult;
},
visitor(path) {
if (scopeManager) {
registerGlimmerScopes(path, scopeManager, isTypescript || useTypescript);
}
},
});
if (jsParserResult?.services?.program) {
const programAllowJs = jsParserResult.services.program.getCompilerOptions?.()?.allowJs;
if (
!allowGjsWasSet &&
programAllowJs !== undefined &&
actualAllowGjs !== undefined &&
actualAllowGjs !== programAllowJs
) {
// eslint-disable-next-line no-console
console.warn(
'[ember-eslint-parser] allowJs does not match the actual program. Consider setting allowGjs explicitly.\n' +
` Current: ${allowGjs}, Program: ${programAllowJs}`
);
}
syncMtsGtsSourceFiles(jsParserResult.services.program);
}
const visitorKeys = {
...(jsParserResult?.visitorKeys || {}),
...buildGlimmerVisitorKeys(),
};
return {
ast: result.program,
visitorKeys,
scopeManager,
services: jsParserResult?.services || {},
};
} catch (e) {
// Wrap content-tag parse errors with ESLint-friendly properties
if (e.message?.includes('Parse Error at')) {
const [line, column] = e.message.split(':').slice(-2).map((x) => parseInt(x));
const err = new Error(e.source_code || e.message);
err.lineNumber = line;
err.column = column;
err.fileName = filePath;
throw err;
}
console.error(e);
throw e;
}
}
export default { meta, parseForESLint };