Skip to content

Commit

Permalink
fix(linter): ensure config.rules is spread into rules in flat config …
Browse files Browse the repository at this point in the history
…migration (#23263)

(cherry picked from commit d5945bd)
  • Loading branch information
JamesHenry authored and FrozenPandaz committed May 9, 2024
1 parent 9eea479 commit e3afd8a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -85,12 +89,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -130,12 +138,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -174,12 +186,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
{ ignores: ['ignore/me'] },
];
Expand Down Expand Up @@ -221,12 +237,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -275,12 +295,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -320,12 +344,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -364,12 +392,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -429,12 +461,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -494,12 +530,16 @@ module.exports = [
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ describe('convertEslintJsonToFlatConfig', () => {
"**/*.ts",
"**/*.tsx"
],
rules: {}
rules: {
...config.rules
}
})),
...compat.config({ env: { jest: true } }).map(config => ({
...config,
Expand All @@ -112,7 +114,9 @@ describe('convertEslintJsonToFlatConfig', () => {
"**/*.spec.js",
"**/*.spec.jsx"
],
rules: {}
rules: {
...config.rules
}
})),
{ ignores: ["src/ignore/to/keep.ts"] },
{ ignores: ["something/else"] }
Expand Down Expand Up @@ -221,7 +225,10 @@ describe('convertEslintJsonToFlatConfig', () => {
...compat.config({ parser: "jsonc-eslint-parser" }).map(config => ({
...config,
files: ["**/*.json"],
rules: { "@nx/dependency-checks": "error" }
rules: {
...config.rules,
"@nx/dependency-checks": "error"
}
})),
{ ignores: [".next/**/*"] },
{ ignores: ["something/else"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,16 @@ describe('convert-to-flat-config generator', () => {
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down Expand Up @@ -424,12 +428,16 @@ describe('convert-to-flat-config generator', () => {
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
rules: {
...config.rules,
},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
rules: {
...config.rules,
},
})),
];
"
Expand Down
21 changes: 20 additions & 1 deletion packages/eslint/src/generators/utils/flat-config/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,26 @@ export function generateFlatOverride(
];
addTSObjectProperty(objectLiteralElements, 'files', files);
addTSObjectProperty(objectLiteralElements, 'excludedFiles', excludedFiles);
addTSObjectProperty(objectLiteralElements, 'rules', rules);

// Apply rules (and spread ...config.rules into it as the first assignment)
addTSObjectProperty(objectLiteralElements, 'rules', rules || {});
const rulesObjectAST = objectLiteralElements.pop() as ts.PropertyAssignment;
const rulesObjectInitializer =
rulesObjectAST.initializer as ts.ObjectLiteralExpression;
const spreadAssignment = ts.factory.createSpreadAssignment(
ts.factory.createIdentifier('config.rules')
);
const updatedRulesProperties = [
spreadAssignment,
...rulesObjectInitializer.properties,
];
objectLiteralElements.push(
ts.factory.createPropertyAssignment(
'rules',
ts.factory.createObjectLiteralExpression(updatedRulesProperties, true)
)
);

if (parserOptions) {
addTSObjectProperty(objectLiteralElements, 'languageSettings', {
parserOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ describe('updateEslint', () => {
"**/*.spec.tsx",
"**/*.spec.js",
"**/*.spec.jsx"
]
],
rules: {
...config.rules
}
})),
{ ignores: [".next/**/*"] }
];
Expand Down

0 comments on commit e3afd8a

Please sign in to comment.