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): support lib as standalone src path #26263

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
41 changes: 41 additions & 0 deletions packages/eslint/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,47 @@ describe('@nx/eslint/plugin', () => {
`);
});

it('should create a node for just a package.json and root level eslint config if accompanied by a lib directory', async () => {
createFiles({
'.eslintrc.json': `{}`,
'package.json': `{}`,
'lib/index.ts': `console.log('hello world')`,
});
// NOTE: The command is specifically targeting the src directory in the case of a standalone Nx workspace
expect(await invokeCreateNodesOnMatchingFiles(context, 'lint'))
.toMatchInlineSnapshot(`
{
"projects": {
".": {
"targets": {
"lint": {
"cache": true,
"command": "eslint ./lib",
"inputs": [
"default",
"^default",
"{projectRoot}/eslintrc.json",
"{workspaceRoot}/tools/eslint-rules/**/*",
{
"externalDependencies": [
"eslint",
],
},
],
"options": {
"cwd": ".",
},
"outputs": [
"{options.outputFile}",
],
},
},
},
},
}
`);
});

it('should not create a node for just a package.json and root level eslint config if accompanied by a src directory when all files are ignored (.eslintignore)', async () => {
createFiles({
'.eslintrc.json': `{}`,
Expand Down
24 changes: 16 additions & 8 deletions packages/eslint/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,18 @@ function getProjectsUsingESLintConfig(

// Add a lint target for each child project without an eslint config, with the root level config as an input
for (const projectRoot of childProjectRoots) {
// If there's no src folder, it's not a standalone project, do not add the target at all
const isStandaloneWorkspace =
let standaloneSrcPath: string | undefined;
if (
projectRoot === '.' &&
existsSync(join(context.workspaceRoot, projectRoot, 'src')) &&
existsSync(join(context.workspaceRoot, projectRoot, 'package.json'));
if (projectRoot === '.' && !isStandaloneWorkspace) {
existsSync(join(context.workspaceRoot, projectRoot, 'package.json'))
) {
if (existsSync(join(context.workspaceRoot, projectRoot, 'src'))) {
standaloneSrcPath = 'src';
} else if (existsSync(join(context.workspaceRoot, projectRoot, 'lib'))) {
standaloneSrcPath = 'lib';
}
}
if (projectRoot === '.' && !standaloneSrcPath) {
continue;
}

Expand All @@ -150,7 +156,7 @@ function getProjectsUsingESLintConfig(
projectRoot,
context.workspaceRoot,
options,
isStandaloneWorkspace
standaloneSrcPath
),
};
}
Expand All @@ -164,14 +170,16 @@ function buildEslintTargets(
projectRoot: string,
workspaceRoot: string,
options: EslintPluginOptions,
isStandaloneWorkspace = false
standaloneSrcPath?: string
) {
const isRootProject = projectRoot === '.';

const targets: Record<string, TargetConfiguration> = {};

const targetConfig: TargetConfiguration = {
command: `eslint ${isRootProject && isStandaloneWorkspace ? './src' : '.'}`,
command: `eslint ${
isRootProject && standaloneSrcPath ? `./${standaloneSrcPath}` : '.'
}`,
cache: true,
options: {
cwd: projectRoot,
Expand Down