Skip to content

Commit 96f6f7f

Browse files
authored
ESLINTJS-60 Remove dependency on Babel for rule S125 (#5074)
1 parent 88f6604 commit 96f6f7f

8 files changed

Lines changed: 53 additions & 46 deletions

File tree

generate-eslint-package-json.mjs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ import { dirname, join } from 'path';
1919
import { fileURLToPath } from 'node:url';
2020

2121
const eslintPluginDependencies = [
22-
'@babel/core',
23-
'@babel/eslint-parser',
24-
'@babel/plugin-proposal-decorators',
25-
'@babel/preset-env',
26-
'@babel/preset-flow',
27-
'@babel/preset-react',
2822
'@eslint-community/regexpp',
2923
'builtin-modules',
3024
'bytes',

its/ruling/src/test/expected/jsts/TypeScript/typescript-S125.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,35 @@
1313
"TypeScript:src/compiler/checker.ts": [
1414
1063,
1515
2774,
16+
21568,
1617
22486
1718
],
1819
"TypeScript:src/compiler/factory.ts": [
20+
4151,
21+
4170,
1922
4208,
2023
4215,
2124
4229,
2225
4236
2326
],
27+
"TypeScript:src/compiler/parser.ts": [
28+
4135,
29+
4146
30+
],
2431
"TypeScript:src/compiler/transformers/es2015.ts": [
32+
664,
2533
716,
2634
735,
2735
1356,
2836
1376
2937
],
38+
"TypeScript:src/compiler/transformers/generators.ts": [
39+
1682
40+
],
3041
"TypeScript:src/compiler/transformers/module/module.ts": [
3142
507,
3243
513,
44+
523,
3345
561,
3446
694,
3547
730

its/ruling/src/test/expected/jsts/ace/javascript-S125.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
2728,
3535
2787,
3636
2840,
37+
3108,
3738
7409,
3839
7817
3940
],

its/ruling/src/test/expected/jsts/console/typescript-S125.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"console:src/actions/databrowser/data.ts": [
33
159
44
],
5-
"console:src/components/CallToAction.tsx.ts": [
6-
1
7-
],
85
"console:src/components/Modal.tsx": [
96
18
107
],
118
"console:src/mutations/SetPlanMutation.ts": [
129
41
1310
],
11+
"console:src/views/FunctionsView/FunctionLogs/Log.tsx": [
12+
6
13+
],
1414
"console:src/views/PermissionsView/PermissionsList/ModelPermissions/ModelPermissionList.tsx": [
1515
13
1616
],
@@ -35,5 +35,8 @@
3535
],
3636
"console:src/views/models/FieldPopup/Constraints.tsx": [
3737
209
38+
],
39+
"console:src/views/playground/PlaygroundView/PlaygroundView.tsx": [
40+
127
3841
]
3942
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"eigen:src/app/Scenes/MyCollection/Screens/ArtworkForm/Form/useArtworkForm.tests.tsx": [
3+
5
4+
]
5+
}

its/ruling/src/test/expected/jsts/paper.js/javascript-S125.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
"paper.js:src/basic/Matrix.js": [
1414
709
1515
],
16+
"paper.js:src/canvas/BlendMode.js": [
17+
121,
18+
130
19+
],
1620
"paper.js:src/path/Curve.js": [
1721
101
1822
],

its/ruling/src/test/expected/jsts/vuetify/typescript-S125.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
71,
2121
78,
2222
81,
23+
89,
2324
96,
2425
101,
2526
106,
@@ -43,8 +44,12 @@
4344
130
4445
],
4546
"vuetify:packages/vuetify/src/components/VOverlay/locationStrategies.ts": [
47+
120,
4648
140
4749
],
50+
"vuetify:packages/vuetify/src/composables/__tests__/theme.spec.ts": [
51+
147
52+
],
4853
"vuetify:packages/vuetify/src/composables/touch.ts": [
4954
26
5055
],

packages/jsts/src/rules/S125/rule.ts

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616
*/
1717
// https://sonarsource.github.io/rspec/#/rspec/S125/javascript
1818

19-
import { Rule, SourceCode } from 'eslint';
19+
import { AST, Rule, SourceCode } from 'eslint';
2020
import estree from 'estree';
2121
import type { TSESTree } from '@typescript-eslint/utils';
22-
import babel from '@babel/eslint-parser';
2322
import { generateMeta } from '../helpers/index.js';
2423
import { meta } from './meta.js';
2524
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';
3126

3227
const EXCLUDED_STATEMENTS = ['BreakStatement', 'LabeledStatement', 'ContinueStatement'];
3328

@@ -93,7 +88,10 @@ export const rule: Rule.RuleModule = {
9388
);
9489
groupedComments.forEach(groupComment => {
9590
const rawTextTrimmed = groupComment.value.trim();
96-
if (rawTextTrimmed !== '}' && containsCode(injectMissingBraces(rawTextTrimmed))) {
91+
if (
92+
rawTextTrimmed !== '}' &&
93+
containsCode(injectMissingBraces(rawTextTrimmed), context)
94+
) {
9795
context.report({
9896
messageId: 'commentedCode',
9997
loc: getCommentLocation(groupComment.nodes),
@@ -143,39 +141,24 @@ function isExclusion(parsedBody: Array<estree.Node>, code: SourceCode) {
143141
return false;
144142
}
145143

146-
function containsCode(value: string) {
147-
if (!couldBeJsCode(value)) {
144+
function containsCode(value: string, context: Rule.RuleContext) {
145+
if (!couldBeJsCode(value) || !context.languageOptions.parser) {
148146
return false;
149147
}
150148

151149
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);
179162
return parseResult.ast.body.length > 0 && !isExclusion(parseResult.ast.body, parseResult);
180163
} catch (exception) {
181164
return false;

0 commit comments

Comments
 (0)