1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const moduleRequire = require('./module-require').default;
|
9 | const extname = require('path').extname;
|
10 | const fs = require('fs');
|
11 |
|
12 | const log = require('debug')('eslint-plugin-import:parse');
|
13 |
|
14 |
|
15 | function getBabelEslintVisitorKeys(parserPath) {
|
16 | if (parserPath.endsWith('index.js')) {
|
17 | const hypotheticalLocation = parserPath.replace('index.js', 'visitor-keys.js');
|
18 | if (fs.existsSync(hypotheticalLocation)) {
|
19 | const keys = moduleRequire(hypotheticalLocation);
|
20 | return keys.default || keys;
|
21 | }
|
22 | }
|
23 | return null;
|
24 | }
|
25 |
|
26 |
|
27 | function keysFromParser(parserPath, parserInstance, parsedResult) {
|
28 |
|
29 | if (parsedResult && parsedResult.visitorKeys) {
|
30 | return parsedResult.visitorKeys;
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | if (typeof parserPath === 'string' && (/.*babel-eslint.*/).test(parserPath)) {
|
37 | return getBabelEslintVisitorKeys(parserPath);
|
38 | }
|
39 |
|
40 |
|
41 | if (parserInstance && parserInstance.VisitorKeys) {
|
42 | return parserInstance.VisitorKeys;
|
43 | }
|
44 | return null;
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function makeParseReturn(ast, visitorKeys) {
|
51 | if (ast) {
|
52 |
|
53 | ast.visitorKeys = visitorKeys;
|
54 |
|
55 | ast.ast = ast;
|
56 | }
|
57 | return ast;
|
58 | }
|
59 |
|
60 |
|
61 | function stripUnicodeBOM(text) {
|
62 | return text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text;
|
63 | }
|
64 |
|
65 |
|
66 | function transformHashbang(text) {
|
67 | return text.replace(/^#!([^\r\n]+)/u, (_, captured) => `//${captured}`);
|
68 | }
|
69 |
|
70 |
|
71 | function getParserPath(path, context) {
|
72 | const parsers = context.settings['import/parsers'];
|
73 | if (parsers != null) {
|
74 |
|
75 | const extension = (extname(path));
|
76 | for (const parserPath in parsers) {
|
77 | if (parsers[parserPath].indexOf(extension) > -1) {
|
78 |
|
79 | log('using alt parser:', parserPath);
|
80 | return parserPath;
|
81 | }
|
82 | }
|
83 | }
|
84 |
|
85 | return context.parserPath;
|
86 | }
|
87 |
|
88 |
|
89 | function getParser(path, context) {
|
90 | const parserPath = getParserPath(path, context);
|
91 | if (parserPath) {
|
92 | return parserPath;
|
93 | }
|
94 | if (
|
95 | !!context.languageOptions
|
96 | && !!context.languageOptions.parser
|
97 | && typeof context.languageOptions.parser !== 'string'
|
98 | && (
|
99 |
|
100 | typeof context.languageOptions.parser.parse === 'function'
|
101 |
|
102 | || typeof context.languageOptions.parser.parseForESLint === 'function'
|
103 | )
|
104 | ) {
|
105 | return context.languageOptions.parser;
|
106 | }
|
107 |
|
108 | return null;
|
109 | }
|
110 |
|
111 |
|
112 | exports.default = function parse(path, content, context) {
|
113 | if (context == null) { throw new Error('need context to parse properly'); }
|
114 |
|
115 |
|
116 | const languageOptions = context.languageOptions;
|
117 | let parserOptions = languageOptions && languageOptions.parserOptions || context.parserOptions;
|
118 | const parserOrPath = getParser(path, context);
|
119 |
|
120 | if (!parserOrPath) { throw new Error('parserPath or languageOptions.parser is required!'); }
|
121 |
|
122 |
|
123 | parserOptions = Object.assign({}, parserOptions);
|
124 | parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
|
125 |
|
126 |
|
127 | parserOptions.comment = true;
|
128 | parserOptions.attachComment = true;
|
129 | parserOptions.tokens = true;
|
130 |
|
131 |
|
132 | parserOptions.loc = true;
|
133 | parserOptions.range = true;
|
134 |
|
135 |
|
136 |
|
137 | parserOptions.filePath = path;
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | delete parserOptions.EXPERIMENTAL_useProjectService;
|
144 | delete parserOptions.projectService;
|
145 | delete parserOptions.project;
|
146 | delete parserOptions.projects;
|
147 |
|
148 |
|
149 | if (languageOptions && languageOptions.ecmaVersion) {
|
150 | parserOptions.ecmaVersion = languageOptions.ecmaVersion;
|
151 | }
|
152 | if (languageOptions && languageOptions.sourceType) {
|
153 |
|
154 |
|
155 |
|
156 | parserOptions.sourceType = languageOptions.sourceType;
|
157 | }
|
158 |
|
159 |
|
160 | const parser = typeof parserOrPath === 'string' ? moduleRequire(parserOrPath) : parserOrPath;
|
161 |
|
162 |
|
163 |
|
164 | content = transformHashbang(stripUnicodeBOM(String(content)));
|
165 |
|
166 | if (typeof parser.parseForESLint === 'function') {
|
167 | let ast;
|
168 | try {
|
169 | const parserRaw = parser.parseForESLint(content, parserOptions);
|
170 | ast = parserRaw.ast;
|
171 |
|
172 | return makeParseReturn(ast, keysFromParser(parserOrPath, parser, parserRaw));
|
173 | } catch (e) {
|
174 | console.warn();
|
175 | console.warn('Error while parsing ' + parserOptions.filePath);
|
176 |
|
177 | console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
|
178 | }
|
179 | if (!ast || typeof ast !== 'object') {
|
180 | console.warn(
|
181 |
|
182 | '`parseForESLint` from parser `' + (typeof parserOrPath === 'string' ? parserOrPath : 'context.languageOptions.parser') + '` is invalid and will just be ignored'
|
183 | );
|
184 | } else {
|
185 |
|
186 | return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
|
187 | }
|
188 | }
|
189 |
|
190 | const ast = parser.parse(content, parserOptions);
|
191 |
|
192 | return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
|
193 | };
|