|
1 | | -const tsconfigUtils = require('@typescript-eslint/tsconfig-utils'); |
2 | | -const babelParser = require('@babel/eslint-parser/experimental-worker'); |
3 | | -const { registerParsedFile } = require('../preprocessor/noop'); |
4 | | -const { |
5 | | - patchTs, |
6 | | - replaceExtensions, |
7 | | - syncMtsGtsSourceFiles, |
8 | | - typescriptParser, |
9 | | -} = require('./ts-patch'); |
10 | | -const { transformForLint, preprocessGlimmerTemplates, convertAst } = require('./transforms'); |
| 1 | +import { createRequire } from 'node:module'; |
| 2 | +import tsconfigUtils from '@typescript-eslint/tsconfig-utils'; |
| 3 | +import babelParser from '@babel/eslint-parser/experimental-worker'; |
| 4 | +import { registerParsedFile } from '../preprocessor/noop.js'; |
| 5 | +import { patchTs, replaceExtensions, syncMtsGtsSourceFiles, typescriptParser } from './ts-patch.js'; |
| 6 | +import { transformForLint, preprocessGlimmerTemplates, convertAst } from './transforms.js'; |
| 7 | + |
| 8 | +const require = createRequire(import.meta.url); |
11 | 9 |
|
12 | 10 | /** |
13 | 11 | * implements https://eslint.org/docs/latest/extend/custom-parsers |
@@ -131,86 +129,86 @@ function getAllowJs(options) { |
131 | 129 | /** |
132 | 130 | * @type {import('eslint').ParserModule} |
133 | 131 | */ |
134 | | -module.exports = { |
135 | | - meta: { |
136 | | - name: 'ember-eslint-parser', |
137 | | - version: '*', |
138 | | - }, |
139 | | - |
140 | | - parseForESLint(code, options) { |
141 | | - const allowGjsWasSet = options.allowGjs !== undefined; |
142 | | - const allowGjs = allowGjsWasSet ? options.allowGjs : getAllowJs(options); |
143 | | - let actualAllowGjs; |
144 | | - // Only patch TypeScript if we actually need it. |
145 | | - if (options.programs || options.projectService || options.project) { |
146 | | - ({ allowGjs: actualAllowGjs } = patchTs({ allowGjs })); |
147 | | - } |
148 | | - registerParsedFile(options.filePath); |
149 | | - let jsCode = code; |
150 | | - const info = transformForLint(code, options.filePath); |
151 | | - jsCode = info.output; |
| 132 | +export const meta = { |
| 133 | + name: 'ember-eslint-parser', |
| 134 | + version: '*', |
| 135 | +}; |
152 | 136 |
|
153 | | - const isTypescript = options.filePath.endsWith('.gts') || options.filePath.endsWith('.ts'); |
154 | | - let useTypescript = true; |
| 137 | +export function parseForESLint(code, options) { |
| 138 | + const allowGjsWasSet = options.allowGjs !== undefined; |
| 139 | + const allowGjs = allowGjsWasSet ? options.allowGjs : getAllowJs(options); |
| 140 | + let actualAllowGjs; |
| 141 | + // Only patch TypeScript if we actually need it. |
| 142 | + if (options.programs || options.projectService || options.project) { |
| 143 | + ({ allowGjs: actualAllowGjs } = patchTs({ allowGjs })); |
| 144 | + } |
| 145 | + registerParsedFile(options.filePath); |
| 146 | + let jsCode = code; |
| 147 | + const info = transformForLint(code, options.filePath); |
| 148 | + jsCode = info.output; |
155 | 149 |
|
156 | | - if (options.useBabel || !typescriptParser) { |
157 | | - useTypescript = false; |
158 | | - } |
| 150 | + const isTypescript = options.filePath.endsWith('.gts') || options.filePath.endsWith('.ts'); |
| 151 | + let useTypescript = true; |
159 | 152 |
|
160 | | - let result = null; |
161 | | - const filePath = options.filePath; |
162 | | - if (options.project || options.projectService) { |
163 | | - jsCode = replaceExtensions(jsCode); |
164 | | - } |
| 153 | + if (options.useBabel || !typescriptParser) { |
| 154 | + useTypescript = false; |
| 155 | + } |
165 | 156 |
|
166 | | - if (isTypescript && !typescriptParser) { |
167 | | - throw new Error('Please install typescript to process gts'); |
168 | | - } |
| 157 | + let result = null; |
| 158 | + const filePath = options.filePath; |
| 159 | + if (options.project || options.projectService) { |
| 160 | + jsCode = replaceExtensions(jsCode); |
| 161 | + } |
169 | 162 |
|
170 | | - try { |
171 | | - result = |
172 | | - isTypescript || useTypescript |
173 | | - ? typescriptParser.parseForESLint(jsCode, { |
174 | | - ...options, |
175 | | - ranges: true, |
176 | | - extraFileExtensions: ['.gts', '.gjs'], |
177 | | - filePath, |
178 | | - }) |
179 | | - : babelParser.parseForESLint(jsCode, { |
180 | | - ...options, |
181 | | - requireConfigFile: false, |
182 | | - ranges: true, |
183 | | - }); |
184 | | - if (!info.templateInfos?.length) { |
185 | | - return result; |
186 | | - } |
187 | | - const preprocessedResult = preprocessGlimmerTemplates(info, code); |
188 | | - preprocessedResult.code = code; |
189 | | - const { templateVisitorKeys } = preprocessedResult; |
190 | | - const visitorKeys = { ...result.visitorKeys, ...templateVisitorKeys }; |
191 | | - result.isTypescript = isTypescript || useTypescript; |
192 | | - convertAst(result, preprocessedResult, visitorKeys); |
193 | | - if (result.services?.program) { |
194 | | - // Compare allowJs with the actual program's compiler options |
195 | | - const programAllowJs = result.services.program.getCompilerOptions?.()?.allowJs; |
196 | | - if ( |
197 | | - !allowGjsWasSet && |
198 | | - programAllowJs !== undefined && |
199 | | - actualAllowGjs !== undefined && |
200 | | - actualAllowGjs !== programAllowJs |
201 | | - ) { |
202 | | - // eslint-disable-next-line no-console |
203 | | - console.warn( |
204 | | - '[ember-eslint-parser] allowJs does not match the actual program. Consider setting allowGjs explicitly.\n' + |
205 | | - ` Current: ${allowGjs}, Program: ${programAllowJs}` |
206 | | - ); |
207 | | - } |
208 | | - syncMtsGtsSourceFiles(result.services.program); |
| 163 | + if (isTypescript && !typescriptParser) { |
| 164 | + throw new Error('Please install typescript to process gts'); |
| 165 | + } |
| 166 | + |
| 167 | + try { |
| 168 | + result = |
| 169 | + isTypescript || useTypescript |
| 170 | + ? typescriptParser.parseForESLint(jsCode, { |
| 171 | + ...options, |
| 172 | + ranges: true, |
| 173 | + extraFileExtensions: ['.gts', '.gjs'], |
| 174 | + filePath, |
| 175 | + }) |
| 176 | + : babelParser.parseForESLint(jsCode, { |
| 177 | + ...options, |
| 178 | + requireConfigFile: false, |
| 179 | + ranges: true, |
| 180 | + }); |
| 181 | + if (!info.templateInfos?.length) { |
| 182 | + return result; |
| 183 | + } |
| 184 | + const preprocessedResult = preprocessGlimmerTemplates(info, code); |
| 185 | + preprocessedResult.code = code; |
| 186 | + const { templateVisitorKeys } = preprocessedResult; |
| 187 | + const visitorKeys = { ...result.visitorKeys, ...templateVisitorKeys }; |
| 188 | + result.isTypescript = isTypescript || useTypescript; |
| 189 | + convertAst(result, preprocessedResult, visitorKeys); |
| 190 | + if (result.services?.program) { |
| 191 | + // Compare allowJs with the actual program's compiler options |
| 192 | + const programAllowJs = result.services.program.getCompilerOptions?.()?.allowJs; |
| 193 | + if ( |
| 194 | + !allowGjsWasSet && |
| 195 | + programAllowJs !== undefined && |
| 196 | + actualAllowGjs !== undefined && |
| 197 | + actualAllowGjs !== programAllowJs |
| 198 | + ) { |
| 199 | + // eslint-disable-next-line no-console |
| 200 | + console.warn( |
| 201 | + '[ember-eslint-parser] allowJs does not match the actual program. Consider setting allowGjs explicitly.\n' + |
| 202 | + ` Current: ${allowGjs}, Program: ${programAllowJs}` |
| 203 | + ); |
209 | 204 | } |
210 | | - return { ...result, visitorKeys }; |
211 | | - } catch (e) { |
212 | | - console.error(e); |
213 | | - throw e; |
| 205 | + syncMtsGtsSourceFiles(result.services.program); |
214 | 206 | } |
215 | | - }, |
216 | | -}; |
| 207 | + return { ...result, visitorKeys }; |
| 208 | + } catch (e) { |
| 209 | + console.error(e); |
| 210 | + throw e; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +export default { meta, parseForESLint }; |
0 commit comments