forked from ember-tooling/ember-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgjs-gts-parser.js
More file actions
84 lines (77 loc) · 2.6 KB
/
gjs-gts-parser.js
File metadata and controls
84 lines (77 loc) · 2.6 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
const babelParser = require('@babel/eslint-parser');
const { registerParsedFile } = require('../preprocessor/noop');
const {
patchTs,
replaceExtensions,
syncMtsGtsSourceFiles,
typescriptParser,
} = require('./ts-patch');
const { transformForLint, preprocessGlimmerTemplates, convertAst } = require('./transforms');
/**
* implements https://eslint.org/docs/latest/extend/custom-parsers
* 1. transforms gts/gjs files into parseable ts/js without changing the offsets and locations around it
* 2. parses the transformed code and generates the AST for TS ot JS
* 3. preprocesses the templates info and prepares the Glimmer AST
* 4. converts the js/ts AST so that it includes the Glimmer AST at the right locations, replacing the original
*/
/**
*
* @type {import('eslint').ParserModule}
*/
module.exports = {
meta: {
name: 'ember-eslint-parser',
version: '*',
},
parseForESLint(code, options) {
patchTs();
registerParsedFile(options.filePath);
let jsCode = code;
const info = transformForLint(code, options.filePath);
jsCode = info.output;
const isTypescript = options.filePath.endsWith('.gts') || options.filePath.endsWith('.ts');
let useTypescript = true;
if (options.useBabel || !typescriptParser) {
useTypescript = false;
}
let result = null;
const filePath = options.filePath;
if (options.project || options.projectService) {
jsCode = replaceExtensions(jsCode);
}
if (isTypescript && !typescriptParser) {
throw new Error('Please install typescript to process gts');
}
try {
result =
isTypescript || useTypescript
? typescriptParser.parseForESLint(jsCode, {
...options,
ranges: true,
extraFileExtensions: ['.gts', '.gjs'],
filePath,
})
: babelParser.parseForESLint(jsCode, {
...options,
requireConfigFile: false,
ranges: true,
});
if (!info.templateInfos?.length) {
return result;
}
const preprocessedResult = preprocessGlimmerTemplates(info, code);
preprocessedResult.code = code;
const { templateVisitorKeys } = preprocessedResult;
const visitorKeys = { ...result.visitorKeys, ...templateVisitorKeys };
result.isTypescript = isTypescript || useTypescript;
convertAst(result, preprocessedResult, visitorKeys);
if (result.services?.program) {
syncMtsGtsSourceFiles(result.services?.program);
}
return { ...result, visitorKeys };
} catch (e) {
console.error(e);
throw e;
}
},
};