Skip to content

Commit

Permalink
fix(linter): handle string extends property in config (#19674)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens authored Oct 17, 2023
1 parent aa7625b commit a6d8824
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
47 changes: 46 additions & 1 deletion packages/eslint/src/generators/utils/eslint-file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
addExtendsToLintConfig,
baseEsLintConfigFile,
eslintConfigFileWhitelist,
findEslintFile,
lintConfigHasOverride,
} from './eslint-file';

import { Tree } from '@nx/devkit';
import { Tree, readJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';

describe('@nx/eslint:lint-file', () => {
Expand Down Expand Up @@ -73,4 +74,48 @@ describe('@nx/eslint:lint-file', () => {
).toBe(false);
});
});

describe('addExtendsToLintConfig', () => {
it('should update string extends property to array', () => {
tree.write(
'apps/demo/.eslintrc.json',
JSON.stringify({
extends: '../../.eslintrc',
rules: {},
overrides: [
{
files: ['**/*.ts', '**/*.tsx'],
rules: {
'@typescript-eslint/no-unused-vars': 'off',
},
},
{
files: ['./package.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': [
'error',
{
buildTargets: ['build'],
includeTransitiveDependencies: true,
ignoredFiles: [
'{projectRoot}/remix.config.js',
'{projectRoot}/tailwind.config.js',
],
ignoredDependencies: ['saslprep'],
},
],
},
},
],
ignorePatterns: ['!**/*', 'build/**/*'],
})
);
addExtendsToLintConfig(tree, 'apps/demo', 'plugin:playwright/recommend');
expect(readJson(tree, 'apps/demo/.eslintrc.json').extends).toEqual([
'plugin:playwright/recommend',
'../../.eslintrc',
]);
});
});
});
6 changes: 5 additions & 1 deletion packages/eslint/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ export function addExtendsToLintConfig(
} else {
const fileName = joinPathFragments(root, '.eslintrc.json');
updateJson(tree, fileName, (json) => {
json.extends = [...plugins, ...(json.extends ?? [])];
json.extends ??= [];
json.extends = [
...plugins,
...(Array.isArray(json.extends) ? json.extends : [json.extends]),
];
return json;
});
}
Expand Down

0 comments on commit a6d8824

Please sign in to comment.