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

fix(linter): handle the flat config in workspace rule generators #29253

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
@@ -1,10 +1,19 @@
import { TSESLint } from '@typescript-eslint/utils';
<% if (flatConfig) { %>import { RuleTester } from '@typescript-eslint/rule-tester';
import type { RuleTesterConfig } from '@typescript-eslint/rule-tester';
import { rule, RULE_NAME } from './my-custom-rule';

const ruleTester = new RuleTester({
languageOptions: {
parser: require('@typescript-eslint/parser'),
},
} as RuleTesterConfig);
<% } else { %>import { TSESLint } from '@typescript-eslint/utils';
import { rule, RULE_NAME } from './<%= name %>';

const ruleTester = new TSESLint.RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
});

<% } %>
ruleTester.run(RULE_NAME, rule, {
valid: [`const example = true;`],
invalid: [],
Expand Down
29 changes: 24 additions & 5 deletions packages/eslint/src/generators/workspace-rule/workspace-rule.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import {
addDependenciesToPackageJson,
applyChangesToString,
ChangeType,
formatFiles,
generateFiles,
GeneratorCallback,
joinPathFragments,
logger,
readNxJson,
runTasksInSerial,
Tree,
} from '@nx/devkit';
import { camelize } from '@nx/devkit/src/utils/string-utils';
import { join } from 'path';
import * as ts from 'typescript';
import { workspaceLintPluginDir } from '../../utils/workspace-lint-rules';
import { lintWorkspaceRulesProjectGenerator } from '../workspace-rules-project/workspace-rules-project';
import { useFlatConfig } from '../../utils/flat-config';
import { eslint9__typescriptESLintVersion } from '../../utils/versions';

export interface LintWorkspaceRuleGeneratorOptions {
name: string;
Expand All @@ -23,18 +28,31 @@ export async function lintWorkspaceRuleGenerator(
tree: Tree,
options: LintWorkspaceRuleGeneratorOptions
) {
const tasks: GeneratorCallback[] = [];

const flatConfig = useFlatConfig(tree);

const nxJson = readNxJson(tree);
// Ensure that the workspace rules project has been created
const projectGeneratorCallback = await lintWorkspaceRulesProjectGenerator(
tree,
{
tasks.push(
await lintWorkspaceRulesProjectGenerator(tree, {
skipFormat: true,
addPlugin:
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson.useInferencePlugins !== false,
}
})
);

if (flatConfig) {
tasks.push(
addDependenciesToPackageJson(
tree,
{},
{ '@typescript-eslint/rule-tester': eslint9__typescriptESLintVersion }
)
);
}

const ruleDir = joinPathFragments(
workspaceLintPluginDir,
options.directory ?? ''
Expand All @@ -44,6 +62,7 @@ export async function lintWorkspaceRuleGenerator(
generateFiles(tree, join(__dirname, 'files'), ruleDir, {
tmpl: '',
name: options.name,
flatConfig,
});

const nameCamelCase = camelize(options.name);
Expand Down Expand Up @@ -119,5 +138,5 @@ export async function lintWorkspaceRuleGenerator(
}
`);

return projectGeneratorCallback;
return runTasksInSerial(...tasks);
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ exports[`@nx/eslint:workspace-rules-project should generate the required files 5
"export default {
displayName: 'eslint-rules',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function lintWorkspaceRulesProjectGenerator(
setupFile: 'none',
compiler: 'tsc',
skipFormat: true,
testEnvironment: 'node',
})
);

Expand Down
Loading