Skip to content

Commit 6d25d20

Browse files
authored
fix(core): move generator should respect non-relative extends parts (#6427)
* fix(core): move generator should respect non-relative extends parts * fix(core): fix broken test
1 parent 3e6a315 commit 6d25d20

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

packages/workspace/src/generators/move/lib/update-eslintrc-json.spec.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { readJson, readProjectConfiguration, Tree } from '@nrwl/devkit';
1+
import {
2+
readJson,
3+
readProjectConfiguration,
4+
Tree,
5+
updateJson,
6+
} from '@nrwl/devkit';
27
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
38

49
import { Linter } from '../../../utils/lint';
510

611
import { Schema } from '../schema';
712
import { updateEslintrcJson } from './update-eslintrc-json';
813
import { libraryGenerator } from '../../library/library';
9-
import { executionAsyncId } from 'node:async_hooks';
1014

1115
describe('updateEslint', () => {
1216
let tree: Tree;
@@ -58,7 +62,45 @@ describe('updateEslint', () => {
5862
readJson(tree, '/libs/shared/my-destination/.eslintrc.json')
5963
).toEqual(
6064
expect.objectContaining({
61-
extends: '../../../.eslintrc.json',
65+
extends: ['../../../.eslintrc.json'],
66+
})
67+
);
68+
});
69+
70+
it('should preserve .eslintrc.json non-relative extends when project is moved to subdirectory', async () => {
71+
await libraryGenerator(tree, {
72+
name: 'my-lib',
73+
linter: Linter.EsLint,
74+
standaloneConfig: false,
75+
});
76+
updateJson(tree, 'libs/my-lib/.eslintrc.json', (eslintRcJson) => {
77+
eslintRcJson.extends = [
78+
'plugin:@nrwl/nx/react',
79+
'../../.eslintrc.json',
80+
'./customrc.json',
81+
];
82+
return eslintRcJson;
83+
});
84+
85+
// This step is usually handled elsewhere
86+
tree.rename(
87+
'libs/my-lib/.eslintrc.json',
88+
'libs/shared/my-destination/.eslintrc.json'
89+
);
90+
91+
const projectConfig = readProjectConfiguration(tree, 'my-lib');
92+
93+
updateEslintrcJson(tree, schema, projectConfig);
94+
95+
expect(
96+
readJson(tree, '/libs/shared/my-destination/.eslintrc.json')
97+
).toEqual(
98+
expect.objectContaining({
99+
extends: [
100+
'plugin:@nrwl/nx/react',
101+
'../../../.eslintrc.json',
102+
'./customrc.json',
103+
],
62104
})
63105
);
64106
});

packages/workspace/src/generators/move/lib/update-eslintrc-json.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'path';
1+
import { join, relative } from 'path';
22
import {
33
offsetFromRoot,
44
ProjectConfiguration,
@@ -16,10 +16,23 @@ interface PartialEsLintrcOverride {
1616
}
1717

1818
interface PartialEsLintRcJson {
19-
extends: string;
19+
extends: string | string[];
2020
overrides?: PartialEsLintrcOverride[];
2121
}
2222

23+
function offsetFilePath(
24+
project: ProjectConfiguration,
25+
pathToFile: string,
26+
offset: string
27+
): string {
28+
if (!pathToFile.startsWith('..')) {
29+
// not a relative path
30+
return pathToFile;
31+
}
32+
const pathFromRoot = join(project.root, pathToFile);
33+
return join(offset, pathFromRoot);
34+
}
35+
2336
/**
2437
* Update the .eslintrc file of the project if it exists.
2538
*
@@ -41,7 +54,17 @@ export function updateEslintrcJson(
4154
const offset = offsetFromRoot(destination);
4255

4356
updateJson<PartialEsLintRcJson>(tree, eslintRcPath, (eslintRcJson) => {
44-
eslintRcJson.extends = `${offset}.eslintrc.json`;
57+
if (typeof eslintRcJson.extends === 'string') {
58+
eslintRcJson.extends = offsetFilePath(
59+
project,
60+
eslintRcJson.extends,
61+
offset
62+
);
63+
} else {
64+
eslintRcJson.extends = eslintRcJson.extends.map((extend: string) =>
65+
offsetFilePath(project, extend, offset)
66+
);
67+
}
4568

4669
eslintRcJson.overrides?.forEach((o) => {
4770
if (o.parserOptions?.project) {

0 commit comments

Comments
 (0)