|
16 | 16 | */ |
17 | 17 | // https://sonarsource.github.io/rspec/#/rspec/S125/javascript |
18 | 18 |
|
19 | | -import { Rule, SourceCode } from 'eslint'; |
| 19 | +import { AST, Rule, SourceCode } from 'eslint'; |
20 | 20 | import estree from 'estree'; |
21 | 21 | import type { TSESTree } from '@typescript-eslint/utils'; |
22 | | -import babel from '@babel/eslint-parser'; |
23 | 22 | import { generateMeta } from '../helpers/index.js'; |
24 | 23 | import { meta } from './meta.js'; |
25 | 24 | import { CodeRecognizer, JavaScriptFootPrint } from '../helpers/recognizers/index.js'; |
26 | | - |
27 | | -import babelPresetReact from '@babel/preset-react'; |
28 | | -import babelPresetFlow from '@babel/preset-flow'; |
29 | | -import babelPresetEnv from '@babel/preset-env'; |
30 | | -import babelPluginDecorators from '@babel/plugin-proposal-decorators'; |
| 25 | +import path from 'path'; |
31 | 26 |
|
32 | 27 | const EXCLUDED_STATEMENTS = ['BreakStatement', 'LabeledStatement', 'ContinueStatement']; |
33 | 28 |
|
@@ -93,7 +88,10 @@ export const rule: Rule.RuleModule = { |
93 | 88 | ); |
94 | 89 | groupedComments.forEach(groupComment => { |
95 | 90 | const rawTextTrimmed = groupComment.value.trim(); |
96 | | - if (rawTextTrimmed !== '}' && containsCode(injectMissingBraces(rawTextTrimmed))) { |
| 91 | + if ( |
| 92 | + rawTextTrimmed !== '}' && |
| 93 | + containsCode(injectMissingBraces(rawTextTrimmed), context) |
| 94 | + ) { |
97 | 95 | context.report({ |
98 | 96 | messageId: 'commentedCode', |
99 | 97 | loc: getCommentLocation(groupComment.nodes), |
@@ -143,39 +141,24 @@ function isExclusion(parsedBody: Array<estree.Node>, code: SourceCode) { |
143 | 141 | return false; |
144 | 142 | } |
145 | 143 |
|
146 | | -function containsCode(value: string) { |
147 | | - if (!couldBeJsCode(value)) { |
| 144 | +function containsCode(value: string, context: Rule.RuleContext) { |
| 145 | + if (!couldBeJsCode(value) || !context.languageOptions.parser) { |
148 | 146 | return false; |
149 | 147 | } |
150 | 148 |
|
151 | 149 | try { |
152 | | - const result = babel.parse(value, { |
153 | | - filename: 'some/filePath', |
154 | | - tokens: true, |
155 | | - comment: true, |
156 | | - loc: true, |
157 | | - range: true, |
158 | | - ecmaVersion: 2018, |
159 | | - sourceType: 'module', |
160 | | - codeFrame: false, |
161 | | - ecmaFeatures: { |
162 | | - jsx: true, |
163 | | - globalReturn: false, |
164 | | - legacyDecorators: true, |
165 | | - }, |
166 | | - requireConfigFile: false, |
167 | | - babelOptions: { |
168 | | - targets: 'defaults', |
169 | | - presets: [babelPresetReact, babelPresetFlow, babelPresetEnv], |
170 | | - plugins: [[babelPluginDecorators, { version: '2022-03' }]], |
171 | | - babelrc: false, |
172 | | - configFile: false, |
173 | | - parserOpts: { |
174 | | - allowReturnOutsideFunction: true, |
175 | | - }, |
176 | | - }, |
177 | | - }); |
178 | | - const parseResult = new SourceCode(value, result); |
| 150 | + const options = { |
| 151 | + ...context.languageOptions?.parserOptions, |
| 152 | + filePath: `placeholder${path.extname(context.filename)}`, |
| 153 | + programs: undefined, |
| 154 | + project: undefined, |
| 155 | + }; |
| 156 | + //In case of Vue parser: we will use the JS/TS parser instead of the Vue parser |
| 157 | + const parser = |
| 158 | + context.languageOptions?.parserOptions?.parser ?? context.languageOptions?.parser; |
| 159 | + const result = |
| 160 | + 'parse' in parser ? parser.parse(value, options) : parser.parseForESLint(value, options).ast; |
| 161 | + const parseResult = new SourceCode(value, result as AST.Program); |
179 | 162 | return parseResult.ast.body.length > 0 && !isExclusion(parseResult.ast.body, parseResult); |
180 | 163 | } catch (exception) { |
181 | 164 | return false; |
|
0 commit comments