Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): migrate to create-nodes-v2 #26302

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
type TargetConfiguration,
type Tree,
} from '@nx/devkit';
import { createNodes, EslintPluginOptions } from '../../plugins/plugin';
import { migrateExecutorToPluginV1 } from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
import { createNodesV2, EslintPluginOptions } from '../../plugins/plugin';
import { migrateExecutorToPlugin } from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
import { targetOptionsToCliMap } from './lib/target-options-map';
import { interpolate } from 'nx/src/tasks-runner/utils';

Expand All @@ -19,26 +19,26 @@ export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync();

const migratedProjectsModern =
await migrateExecutorToPluginV1<EslintPluginOptions>(
await migrateExecutorToPlugin<EslintPluginOptions>(
tree,
projectGraph,
'@nx/eslint:lint',
'@nx/eslint/plugin',
(targetName) => ({ targetName }),
postTargetTransformer,
createNodes,
createNodesV2,
options.project
);

const migratedProjectsLegacy =
await migrateExecutorToPluginV1<EslintPluginOptions>(
await migrateExecutorToPlugin<EslintPluginOptions>(
tree,
projectGraph,
'@nrwl/linter:eslint',
'@nx/eslint/plugin',
(targetName) => ({ targetName }),
postTargetTransformer,
createNodes,
createNodesV2,
options.project
);

Expand Down
12 changes: 6 additions & 6 deletions packages/eslint/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
Tree,
updateNxJson,
} from '@nx/devkit';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { eslintVersion, nxVersion } from '../../utils/versions';
import { findEslintFile } from '../utils/eslint-file';
import { createNodes } from '../../plugins/plugin';
import { createNodesV2 } from '../../plugins/plugin';
import { hasEslintPlugin } from '../utils/plugin';

export interface LinterInitOptions {
Expand Down Expand Up @@ -73,11 +73,11 @@ export async function initEsLint(
];

if (rootEslintFile && options.addPlugin && !hasPlugin) {
await addPluginV1(
await addPlugin(
tree,
graph,
'@nx/eslint/plugin',
createNodes,
createNodesV2,
{
targetName: lintTargetNames,
},
Expand All @@ -94,11 +94,11 @@ export async function initEsLint(
updateProductionFileset(tree);

if (options.addPlugin) {
await addPluginV1(
await addPlugin(
tree,
graph,
'@nx/eslint/plugin',
createNodes,
createNodesV2,
{
targetName: lintTargetNames,
},
Expand Down
59 changes: 35 additions & 24 deletions packages/eslint/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { CreateNodesContext } from '@nx/devkit';
import { CreateNodesContextV2 } from '@nx/devkit';
import { minimatch } from 'minimatch';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { createNodes } from './plugin';
import { createNodesV2 } from './plugin';
import { mkdirSync, rmdirSync } from 'fs';

jest.mock('nx/src/utils/cache-directory', () => ({
...jest.requireActual('nx/src/utils/cache-directory'),
projectGraphCacheDirectory: 'tmp/project-graph-cache',
}));

describe('@nx/eslint/plugin', () => {
let context: CreateNodesContext;
let context: CreateNodesContextV2;
let tempFs: TempFs;
let configFiles: string[] = [];

beforeEach(async () => {
mkdirSync('tmp/project-graph-cache', { recursive: true });
tempFs = new TempFs('eslint-plugin');
context = {
nxJsonConfiguration: {
Expand All @@ -24,14 +32,14 @@ describe('@nx/eslint/plugin', () => {
},
},
workspaceRoot: tempFs.tempDir,
configFiles: [],
};
});

afterEach(() => {
jest.resetModules();
tempFs.cleanup();
tempFs = null;
rmdirSync('tmp/project-graph-cache', { recursive: true });
});

it('should not create any nodes when there are no eslint configs', async () => {
Expand Down Expand Up @@ -617,27 +625,30 @@ describe('@nx/eslint/plugin', () => {

function createFiles(fileSys: Record<string, string>) {
tempFs.createFilesSync(fileSys);
// @ts-expect-error update otherwise readonly property for testing
context.configFiles = getMatchingFiles(Object.keys(fileSys));
configFiles = getMatchingFiles(Object.keys(fileSys));
}
});

function getMatchingFiles(allConfigFiles: string[]): string[] {
return allConfigFiles.filter((file) =>
minimatch(file, createNodes[0], { dot: true })
);
}
function getMatchingFiles(allConfigFiles: string[]): string[] {
return allConfigFiles.filter((file) =>
minimatch(file, createNodesV2[0], { dot: true })
);
}

async function invokeCreateNodesOnMatchingFiles(
context: CreateNodesContext,
targetName: string
) {
const aggregateProjects: Record<string, any> = {};
for (const file of context.configFiles) {
const nodes = await createNodes[1](file, { targetName }, context);
Object.assign(aggregateProjects, nodes.projects);
async function invokeCreateNodesOnMatchingFiles(
context: CreateNodesContextV2,
targetName: string
) {
const aggregateProjects: Record<string, any> = {};
const results = await createNodesV2[1](
configFiles,
{ targetName },
context
);
for (const [, nodes] of results) {
Object.assign(aggregateProjects, nodes.projects);
}
return {
projects: aggregateProjects,
};
}
return {
projects: aggregateProjects,
};
}
});
Loading