fix(linter): handle configuration without "rules" in migration (#26317)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #26306
This commit is contained in:
parent
b97a295fad
commit
adc1d70744
@ -76,6 +76,52 @@ describe('update-19-1-0-migrate-no-extra-semi', () => {
|
||||
`);
|
||||
});
|
||||
|
||||
it('should update top level config that extends @nx/typescript and "rules" is not defined', async () => {
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
plugins: ['@nx'],
|
||||
extends: ['@nx/typescript'],
|
||||
});
|
||||
|
||||
await migrate(tree);
|
||||
|
||||
expect(readJson(tree, '.eslintrc.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": [
|
||||
"@nx/typescript",
|
||||
],
|
||||
"plugins": [
|
||||
"@nx",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
}
|
||||
`);
|
||||
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
plugins: ['@nx'],
|
||||
extends: ['plugin:@nx/typescript'], // alt syntax
|
||||
});
|
||||
|
||||
await migrate(tree);
|
||||
|
||||
expect(readJson(tree, '.eslintrc.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": [
|
||||
"plugin:@nx/typescript",
|
||||
],
|
||||
"plugins": [
|
||||
"@nx",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should update top level config that extends @nx/javascript', async () => {
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
plugins: ['@nx'],
|
||||
@ -124,6 +170,52 @@ describe('update-19-1-0-migrate-no-extra-semi', () => {
|
||||
`);
|
||||
});
|
||||
|
||||
it('should update top level config that extends @nx/javascript and "rules" is not defined', async () => {
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
plugins: ['@nx'],
|
||||
extends: ['@nx/javascript'],
|
||||
});
|
||||
|
||||
await migrate(tree);
|
||||
|
||||
expect(readJson(tree, '.eslintrc.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": [
|
||||
"@nx/javascript",
|
||||
],
|
||||
"plugins": [
|
||||
"@nx",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
}
|
||||
`);
|
||||
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
plugins: ['@nx'],
|
||||
extends: ['plugin:@nx/javascript'], // alt syntax
|
||||
});
|
||||
|
||||
await migrate(tree);
|
||||
|
||||
expect(readJson(tree, '.eslintrc.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": [
|
||||
"plugin:@nx/javascript",
|
||||
],
|
||||
"plugins": [
|
||||
"@nx",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should not update top level config that already defines the rules', async () => {
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
plugins: ['@nx'],
|
||||
@ -263,6 +355,69 @@ describe('update-19-1-0-migrate-no-extra-semi', () => {
|
||||
`);
|
||||
});
|
||||
|
||||
it('should update overrides config that extends @nx/typescript and "rules" is not defined', async () => {
|
||||
writeJson(tree, 'path/to/.eslintrc.json', {
|
||||
overrides: [
|
||||
{
|
||||
files: ['*.ts'],
|
||||
extends: ['@nx/typescript'],
|
||||
},
|
||||
{
|
||||
files: ['*.tsx'],
|
||||
extends: ['plugin:@nx/typescript'], // alt syntax
|
||||
},
|
||||
{
|
||||
// Should be untouched
|
||||
files: ['*.js'],
|
||||
plugins: ['@nx'],
|
||||
rules: {},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await migrate(tree);
|
||||
|
||||
expect(readJson(tree, 'path/to/.eslintrc.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"overrides": [
|
||||
{
|
||||
"extends": [
|
||||
"@nx/typescript",
|
||||
],
|
||||
"files": [
|
||||
"*.ts",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
},
|
||||
{
|
||||
"extends": [
|
||||
"plugin:@nx/typescript",
|
||||
],
|
||||
"files": [
|
||||
"*.tsx",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
},
|
||||
{
|
||||
"files": [
|
||||
"*.js",
|
||||
],
|
||||
"plugins": [
|
||||
"@nx",
|
||||
],
|
||||
"rules": {},
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should update overrides config that extends @nx/javascript', async () => {
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
overrides: [
|
||||
@ -328,6 +483,69 @@ describe('update-19-1-0-migrate-no-extra-semi', () => {
|
||||
`);
|
||||
});
|
||||
|
||||
it('should update overrides config that extends @nx/javascript and "rules" is not defined', async () => {
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
overrides: [
|
||||
{
|
||||
files: ['*.js'],
|
||||
extends: ['@nx/javascript'],
|
||||
},
|
||||
{
|
||||
files: ['*.jsx'],
|
||||
extends: ['plugin:@nx/javascript'], // alt syntax
|
||||
},
|
||||
{
|
||||
// Should be untouched
|
||||
files: ['*.js'],
|
||||
plugins: ['@nx'],
|
||||
rules: {},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await migrate(tree);
|
||||
|
||||
expect(readJson(tree, '.eslintrc.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"overrides": [
|
||||
{
|
||||
"extends": [
|
||||
"@nx/javascript",
|
||||
],
|
||||
"files": [
|
||||
"*.js",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
},
|
||||
{
|
||||
"extends": [
|
||||
"plugin:@nx/javascript",
|
||||
],
|
||||
"files": [
|
||||
"*.jsx",
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-extra-semi": "error",
|
||||
"no-extra-semi": "off",
|
||||
},
|
||||
},
|
||||
{
|
||||
"files": [
|
||||
"*.js",
|
||||
],
|
||||
"plugins": [
|
||||
"@nx",
|
||||
],
|
||||
"rules": {},
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should not update overrides config that already defines the rules', async () => {
|
||||
writeJson(tree, '.eslintrc.json', {
|
||||
overrides: [
|
||||
|
||||
@ -59,11 +59,13 @@ function addNoExtraSemiExplicitly(json: Record<string, any>): boolean {
|
||||
) {
|
||||
return wasUpdated;
|
||||
}
|
||||
if (!json.rules['@typescript-eslint/no-extra-semi']) {
|
||||
if (!json.rules?.['@typescript-eslint/no-extra-semi']) {
|
||||
json.rules ??= {};
|
||||
json.rules['@typescript-eslint/no-extra-semi'] = 'error';
|
||||
wasUpdated = true;
|
||||
}
|
||||
if (!json.rules['no-extra-semi']) {
|
||||
if (!json.rules?.['no-extra-semi']) {
|
||||
json.rules ??= {};
|
||||
json.rules['no-extra-semi'] = 'off';
|
||||
wasUpdated = true;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user