Skip to content

Commit

Permalink
fix(linter): enable analyzeSourceFile as needed when generating new l…
Browse files Browse the repository at this point in the history
…int project (#18769)
  • Loading branch information
jaysoo authored Aug 24, 2023
1 parent a146fcc commit 4846ae5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
46 changes: 46 additions & 0 deletions packages/linter/src/generators/lint-project/lint-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
addProjectConfiguration,
readProjectConfiguration,
updateJson,
Tree,
readJson,
} from '@nx/devkit';

import { Linter } from '../utils/linter';
Expand Down Expand Up @@ -175,4 +177,48 @@ describe('@nx/linter:lint-project', () => {
"
`);
});

it('should update nx.json to enable source analysis when using npm.json preset', async () => {
updateJson(tree, 'nx.json', (json) => {
// npm preset disables source analysis
json.extends = 'nx/presets/npm.json';
return json;
});

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
eslintFilePatterns: ['libs/buildable-lib/**/*.ts'],
project: 'buildable-lib',
setParserOptionsProject: false,
});

expect(readJson(tree, 'nx.json').pluginsConfig['@nx/js']).toEqual({
analyzeSourceFiles: true,
});
});

it('should update nx.json to enable source analysis when it is disabled', async () => {
updateJson(tree, 'nx.json', (json) => {
// npm preset disables source analysis
json.pluginsConfig = {
'@nx/js': {
analyzeSourceFiles: false,
},
};
return json;
});

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
eslintFilePatterns: ['libs/buildable-lib/**/*.ts'],
project: 'buildable-lib',
setParserOptionsProject: false,
});

expect(readJson(tree, 'nx.json').pluginsConfig['@nx/js']).toEqual({
analyzeSourceFiles: true,
});
});
});
54 changes: 42 additions & 12 deletions packages/linter/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { ProjectConfiguration, Tree } from '@nx/devkit';
import type {
NxJsonConfiguration,
ProjectConfiguration,
Tree,
} from '@nx/devkit';
import {
formatFiles,
offsetFromRoot,
readJson,
readProjectConfiguration,
updateJson,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
Expand Down Expand Up @@ -108,6 +114,19 @@ export async function lintProjectGenerator(
);
}

// Buildable libs need source analysis enabled for linting `package.json`.
if (
isBuildableLibraryProject(projectConfig) &&
!isJsAnalyzeSourceFilesEnabled(tree)
) {
updateJson(tree, 'nx.json', (json) => {
json.pluginsConfig ??= {};
json.pluginsConfig['@nx/js'] ??= {};
json.pluginsConfig['@nx/js'].analyzeSourceFiles = true;
return json;
});
}

updateProjectConfiguration(tree, options.project, projectConfig);

if (!options.skipFormat) {
Expand Down Expand Up @@ -163,19 +182,18 @@ function createEsLintConfiguration(
files: ['*.js', '*.jsx'],
rules: {},
},
...(isBuildableLibraryProject(projectConfig)
? [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'error',
} as Linter.RulesRecord,
},
]
: []),
];

if (isBuildableLibraryProject(projectConfig)) {
overrides.push({
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'error',
},
});
}

if (useFlatConfig(tree)) {
const isCompatNeeded = addDependencyChecks;
const nodes = [];
Expand Down Expand Up @@ -204,6 +222,18 @@ function createEsLintConfiguration(
}
}

function isJsAnalyzeSourceFilesEnabled(tree: Tree): boolean {
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
const jsPluginConfig = nxJson.pluginsConfig?.['@nx/js'] as {
analyzeSourceFiles?: boolean;
};

return (
jsPluginConfig?.analyzeSourceFiles ??
nxJson.extends !== 'nx/presets/npm.json'
);
}

function isBuildableLibraryProject(
projectConfig: ProjectConfiguration
): boolean {
Expand Down

1 comment on commit 4846ae5

@vercel
Copy link

@vercel vercel bot commented on 4846ae5 Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.