Skip to content

Commit

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

Root projects are considered standalone projects which shouldn't lint everything if the `src` directory exists.

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

Root projects which have a `lib` directory should be treated the same way.

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

Fixes #
  • Loading branch information
xiongemi authored May 31, 2024
1 parent 2fe4def commit 1f7c0bc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
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

0 comments on commit 1f7c0bc

Please sign in to comment.