Skip to content

Commit 963d2e0

Browse files
JamesHenryFrozenPandaz
authored andcommitted
fix(linter): ensure all spreads are removed from rules before parsing (#23292)
(cherry picked from commit 613fdb0)
1 parent 54fa6d2 commit 963d2e0

File tree

1 file changed

+11
-5
lines changed
  • packages/eslint/src/generators/utils/flat-config

1 file changed

+11
-5
lines changed

packages/eslint/src/generators/utils/flat-config/ast-utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import { Linter } from 'eslint';
88
import * as ts from 'typescript';
99
import { mapFilePath } from './path-utils';
1010

11+
/**
12+
* Supports direct identifiers, and those nested within an object (of arbitrary depth)
13+
* E.g. `...foo` and `...foo.bar.baz.qux` etc
14+
*/
15+
const SPREAD_ELEMENTS_REGEXP = /\s*\.\.\.[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*,?\n?/g;
16+
1117
/**
1218
* Remove all overrides from the config file
1319
*/
@@ -87,11 +93,13 @@ export function hasOverride(
8793
let objSource;
8894
if (ts.isObjectLiteralExpression(node)) {
8995
objSource = node.getFullText();
96+
// strip any spread elements
97+
objSource = objSource.replace(SPREAD_ELEMENTS_REGEXP, '');
9098
} else {
9199
const fullNodeText =
92100
node['expression'].arguments[0].body.expression.getFullText();
93101
// strip any spread elements
94-
objSource = fullNodeText.replace(/\s*\.\.\.[a-zA-Z0-9_]+,?\n?/, '');
102+
objSource = fullNodeText.replace(SPREAD_ELEMENTS_REGEXP, '');
95103
}
96104
const data = parseJson(
97105
objSource
@@ -107,8 +115,6 @@ export function hasOverride(
107115
return false;
108116
}
109117

110-
const STRIP_SPREAD_ELEMENTS = /\s*\.\.\.[a-zA-Z0-9_]+,?\n?/g;
111-
112118
function parseTextToJson(text: string): any {
113119
return parseJson(
114120
text
@@ -153,7 +159,7 @@ export function replaceOverride(
153159
const fullNodeText =
154160
node['expression'].arguments[0].body.expression.getFullText();
155161
// strip any spread elements
156-
objSource = fullNodeText.replace(STRIP_SPREAD_ELEMENTS, '');
162+
objSource = fullNodeText.replace(SPREAD_ELEMENTS_REGEXP, '');
157163
start =
158164
node['expression'].arguments[0].body.expression.properties.pos +
159165
(fullNodeText.length - objSource.length);
@@ -415,7 +421,7 @@ export function removePlugin(
415421
const plugins = parseTextToJson(
416422
pluginsElem.initializer
417423
.getText()
418-
.replace(STRIP_SPREAD_ELEMENTS, '')
424+
.replace(SPREAD_ELEMENTS_REGEXP, '')
419425
);
420426

421427
if (plugins.length > 1) {

0 commit comments

Comments
 (0)