Skip to content

Commit

Permalink
fix(linter): fix migrating projects with the eslint plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed May 2, 2024
1 parent 2002a78 commit e3a517f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('convertToCypressTen', () => {
mockedInstalledCypressVersion.mockReturnValue(9);
});

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

Expand Down
41 changes: 22 additions & 19 deletions packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type {
import {
createProjectGraphAsync,
GeneratorCallback,
NxJsonConfiguration,
ProjectConfiguration,
ProjectGraph,
Tree,
} from '@nx/devkit';
import {
Expand Down Expand Up @@ -134,7 +136,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 +298,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 +307,28 @@ function isMigrationToMonorepoNeeded(
return false;
}

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

// 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 @@ -50,7 +50,7 @@ describe('React:CypressComponentTestConfiguration', () => {
tree = createTreeWithEmptyWorkspace();
});

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

Expand Down

0 comments on commit e3a517f

Please sign in to comment.