Skip to content

Commit

Permalink
fix(linter): fix migrating projects with the eslint plugin (#23147)
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 -->

Creating a new project with ESLint is mistakenly creating a
`eslint.base.config.js`.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Creating a new project with ESLint only creates a
`eslint.base.config.js` file when the repo was originally standalone and
the second project is made.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz committed May 13, 2024
1 parent 5fea49a commit 461b901
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('convertToCypressTen', () => {
mockedInstalledCypressVersion.mockReturnValue(9);
});

afterEach(() => {
afterAll(() => {
jest.resetAllMocks();
});

Expand Down
44 changes: 21 additions & 23 deletions packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {
import {
createProjectGraphAsync,
GeneratorCallback,
NxJsonConfiguration,
ProjectConfiguration,
ProjectGraph,
Tree,
} from '@nx/devkit';
import {
readNxJson,
formatFiles,
offsetFromRoot,
Expand Down Expand Up @@ -134,7 +134,8 @@ export async function lintProjectGeneratorInternal(
if (!options.rootProject) {
const projects = {} as any;
getProjects(tree).forEach((v, k) => (projects[k] = v));
if (isMigrationToMonorepoNeeded(projects, tree)) {
const graph = await createProjectGraphAsync();
if (isMigrationToMonorepoNeeded(tree, graph)) {
// we only migrate project configurations that have been created
const filteredProjects = [];
Object.entries(projects).forEach(([name, project]) => {
Expand Down Expand Up @@ -295,10 +296,7 @@ function isBuildableLibraryProject(
* Detect based on the state of lint target configuration of the root project
* if we should migrate eslint configs to monorepo style
*/
function isMigrationToMonorepoNeeded(
projects: Record<string, ProjectConfiguration>,
tree: Tree
): boolean {
function isMigrationToMonorepoNeeded(tree: Tree, graph: ProjectGraph): boolean {
// the base config is already created, migration has been done
if (
tree.exists(baseEsLintConfigFile) ||
Expand All @@ -307,25 +305,25 @@ function isMigrationToMonorepoNeeded(
return false;
}

const configs = Object.values(projects);
if (configs.length === 1) {
return false;
}
const nodes = Object.values(graph.nodes);

// get root project
const rootProject = configs.find((p) => p.root === '.');
if (!rootProject || !rootProject.targets) {
const rootProject = nodes.find((p) => p.data.root === '.');
if (!rootProject || !rootProject.data.targets) {
return false;
}
// check if we're inferring lint target from `@nx/eslint/plugin`
if (hasEslintPlugin(tree)) {
for (const f of ESLINT_CONFIG_FILENAMES) {
if (tree.exists(f)) {
return true;
}

for (const targetConfig of Object.values(rootProject.data.targets ?? {})) {
if (
['@nx/eslint:lint', '@nrwl/linter:eslint', '@nx/linter:eslint'].includes(
targetConfig.executor
) ||
(targetConfig.executor === 'nx:run-commands' &&
targetConfig.options?.command.startsWith('eslint '))
) {
return true;
}
}
// find if root project has lint target
const lintTarget = findLintTarget(rootProject);
return !!lintTarget;

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`NxPlugin e2e-project Generator should setup the eslint builder 1`] = `
"{
"extends": ["../../.eslintrc.base.json"],
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ describe('React:CypressComponentTestConfiguration', () => {
});
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();

projectGraph = {
nodes: {},
dependencies: {},
};
});

afterEach(() => {
afterAll(() => {
jest.clearAllMocks();
});

Expand Down Expand Up @@ -422,7 +427,7 @@ describe('React:CypressComponentTestConfiguration', () => {
).toBeFalsy();
});

it('should not throw error when an invalid --build-target is provided', async () => {
it('should throw error when an invalid --build-target is provided', async () => {
mockedAssertCypressVersion.mockReturnValue();
await applicationGenerator(tree, {
e2eTestRunner: 'none',
Expand All @@ -446,6 +451,7 @@ describe('React:CypressComponentTestConfiguration', () => {
const appConfig = readProjectConfiguration(tree, 'my-app');
appConfig.targets['build'].executor = 'something/else';
updateProjectConfiguration(tree, 'my-app', appConfig);
jest.clearAllMocks();
projectGraph = {
nodes: {
'my-app': {
Expand All @@ -465,13 +471,17 @@ describe('React:CypressComponentTestConfiguration', () => {
},
dependencies: {},
};
await expect(async () => {
await cypressComponentConfigGenerator(tree, {

await expect(
cypressComponentConfigGenerator(tree, {
project: 'some-lib',
generateTests: true,
buildTarget: 'my-app:build',
});
}).resolves;
})
).rejects.toThrow();
expect(require('@nx/devkit').createProjectGraphAsync).toHaveBeenCalledTimes(
1
);
});

it('should setup cypress config files correctly', async () => {
Expand Down

0 comments on commit 461b901

Please sign in to comment.