Skip to content

Commit

Permalink
fix(linter): handle configuration without "rules" in migration (#26317)
Browse files Browse the repository at this point in the history
<!-- 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
  • Loading branch information
leosvelperez authored Jun 3, 2024
1 parent b97a295 commit adc1d70
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit adc1d70

Please sign in to comment.