Skip to content
Draft
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 @@ -65,7 +65,7 @@ describe('addLinting generator', () => {
});

const { devDependencies } = readJson(tree, 'package.json');
expect(devDependencies['@typescript-eslint/utils']).toBe('^8.40.0');
expect(devDependencies['@typescript-eslint/utils']).toBe('^8.57.0');
delete process.env.ESLINT_USE_FLAT_CONFIG;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type Tree,
} from '@nx/devkit';
import { useFlatConfig } from '@nx/eslint/src/utils/flat-config';
import { eslint9__typescriptESLintVersion } from '@nx/eslint/src/utils/versions';
import { versions as eslintPkgVersions } from '@nx/eslint/src/utils/version-utils';
import { versions } from '../../utils/version-utils';
import { isBuildableLibraryProject } from './buildable-project';

Expand All @@ -27,15 +27,20 @@ export function addAngularEsLintDependencies(

if ('typescriptEslintVersion' in compatVersions) {
devDependencies['@typescript-eslint/utils'] = usesEslintFlatConfig
? eslint9__typescriptESLintVersion
? eslintPkgVersions(tree).typescriptESLintVersion
: compatVersions.typescriptEslintVersion;
}

if (isBuildableLibraryProject(tree, projectName)) {
const jsoncEslintParserVersionToInstall =
versions(tree).jsoncEslintParserVersion;
devDependencies['jsonc-eslint-parser'] = jsoncEslintParserVersionToInstall;
devDependencies['jsonc-eslint-parser'] =
compatVersions.jsoncEslintParserVersion;
}

return addDependenciesToPackageJson(tree, {}, devDependencies);
return addDependenciesToPackageJson(
tree,
{},
devDependencies,
undefined,
true
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ describe('convert-to-flat-config generator', () => {
"devDependencies": {
"@nx/eslint": "0.0.1",
"@nx/eslint-plugin": "0.0.1",
"@typescript-eslint/eslint-plugin": "^8.40.0",
"@typescript-eslint/parser": "^8.40.0",
"eslint": "^9.8.0",
"@typescript-eslint/eslint-plugin": "^8.57.0",
"@typescript-eslint/parser": "^8.57.0",
"eslint": "^10.0.0",
"eslint-config-prettier": "^10.0.0",
"typescript-eslint": "^8.40.0"
"typescript-eslint": "^8.57.0"
}
}
"
Expand Down Expand Up @@ -667,11 +667,11 @@ describe('convert-to-flat-config generator', () => {
"devDependencies": {
"@nx/eslint": "0.0.1",
"@nx/eslint-plugin": "0.0.1",
"@typescript-eslint/eslint-plugin": "^8.40.0",
"@typescript-eslint/parser": "^8.40.0",
"eslint": "^9.8.0",
"@typescript-eslint/eslint-plugin": "^8.57.0",
"@typescript-eslint/parser": "^8.57.0",
"eslint": "^10.0.0",
"eslint-config-prettier": "^10.0.0",
"typescript-eslint": "^8.40.0"
"typescript-eslint": "^8.57.0"
}
}
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ import { ConvertToFlatConfigGeneratorSchema } from './schema';
import { findEslintFile } from '../utils/eslint-file';
import { hasEslintPlugin } from '../utils/plugin';
import { join } from 'path';
import {
eslint9__eslintVersion,
eslint9__typescriptESLintVersion,
eslintConfigPrettierVersion,
eslintrcVersion,
eslintVersion,
} from '../../utils/versions';
import { versions } from '../../utils/version-utils';
import { ESLint } from 'eslint';
import { convertEslintJsonToFlatConfig } from './converters/json-converter';

Expand Down Expand Up @@ -262,33 +256,34 @@ function processConvertedConfig(
tree.write(join(root, target), content);

// These dependencies are required for flat configs that are generated by subsequent app/lib generators.
const pkgVersions = versions(tree);
const devDependencies: Record<string, string> = {
eslint: eslint9__eslintVersion,
'eslint-config-prettier': eslintConfigPrettierVersion,
'typescript-eslint': eslint9__typescriptESLintVersion,
'@typescript-eslint/eslint-plugin': eslint9__typescriptESLintVersion,
'@typescript-eslint/parser': eslint9__typescriptESLintVersion,
eslint: pkgVersions.eslintVersion,
'eslint-config-prettier': pkgVersions.eslintConfigPrettierVersion,
'typescript-eslint': pkgVersions.typescriptESLintVersion,
'@typescript-eslint/eslint-plugin': pkgVersions.typescriptESLintVersion,
'@typescript-eslint/parser': pkgVersions.typescriptESLintVersion,
};

if (getDependencyVersionFromPackageJson(tree, '@typescript-eslint/utils')) {
devDependencies['@typescript-eslint/utils'] =
eslint9__typescriptESLintVersion;
pkgVersions.typescriptESLintVersion;
}
if (
getDependencyVersionFromPackageJson(tree, '@typescript-eslint/type-utils')
) {
devDependencies['@typescript-eslint/type-utils'] =
eslint9__typescriptESLintVersion;
pkgVersions.typescriptESLintVersion;
}

// add missing packages
if (addESLintRC) {
devDependencies['@eslint/eslintrc'] = eslintrcVersion;
devDependencies['@eslint/eslintrc'] = pkgVersions.eslintrcVersion;
}

if (addESLintJS) {
devDependencies['@eslint/js'] = eslintVersion;
devDependencies['@eslint/js'] = pkgVersions.eslintJsVersion;
}

addDependenciesToPackageJson(tree, {}, devDependencies);
addDependenciesToPackageJson(tree, {}, devDependencies, undefined, true);
}
11 changes: 7 additions & 4 deletions packages/eslint/src/generators/init/init-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
getGlobalFlatEslintConfiguration,
} from './global-eslint-config';
import { useFlatConfig } from '../../utils/flat-config';
import { eslintVersion, nxVersion } from '../../utils/versions';
import { versions } from '../../utils/version-utils';
import { nxVersion } from '../../utils/versions';
import {
addBlockToFlatConfigExport,
addImportToFlatConfig,
Expand Down Expand Up @@ -69,10 +70,10 @@ export function migrateConfigToMonorepoStyle(
tree,
{},
{
'@eslint/js': eslintVersion,
'@eslint/js': versions(tree).eslintJsVersion,
},
undefined,
keepExistingVersions
keepExistingVersions ?? true
);
tree.write(
tree.exists(`eslint.config.${eslintConfigFormat}`)
Expand Down Expand Up @@ -131,7 +132,9 @@ export function migrateConfigToMonorepoStyle(
{},
{
'@nx/eslint-plugin': nxVersion,
}
},
undefined,
true
);
}

Expand Down
7 changes: 4 additions & 3 deletions packages/eslint/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
updateNxJson,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { eslintVersion, nxVersion } from '../../utils/versions';
import { nxVersion } from '../../utils/versions';
import { versions } from '../../utils/version-utils';
import {
determineEslintConfigFormat,
findEslintFile,
Expand Down Expand Up @@ -155,10 +156,10 @@ export async function initEsLint(
{},
{
'@nx/eslint': nxVersion,
eslint: eslintVersion,
eslint: versions(tree).eslintVersion,
},
undefined,
options.keepExistingVersions
options.keepExistingVersions ?? true
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
BASE_ESLINT_CONFIG_FILENAMES,
} from '../../utils/config-file';
import { hasEslintPlugin } from '../utils/plugin';
import { jsoncEslintParserVersion } from '../../utils/versions';
import { versions } from '../../utils/version-utils';
import { setupRootEsLint } from './setup-root-eslint';
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';

Expand Down Expand Up @@ -184,7 +184,7 @@ export async function lintProjectGeneratorInternal(
addDependenciesToPackageJson(
tree,
{},
{ 'jsonc-eslint-parser': jsoncEslintParserVersion },
{ 'jsonc-eslint-parser': versions(tree).jsoncEslintParserVersion },
undefined,
true
)
Expand Down
67 changes: 35 additions & 32 deletions packages/eslint/src/generators/lint-project/setup-root-eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ import {
type Tree,
} from '@nx/devkit';
import { useFlatConfig } from '../../utils/flat-config';
import {
eslint9__eslintVersion,
eslint9__typescriptESLintVersion,
eslintConfigPrettierVersion,
nxVersion,
typescriptESLintVersion,
} from '../../utils/versions';
import { versions } from '../../utils/version-utils';
import { nxVersion } from '../../utils/versions';
import {
getGlobalEsLintConfiguration,
getGlobalFlatEslintConfiguration,
Expand Down Expand Up @@ -58,18 +53,22 @@ function setUpLegacyRootEslintRc(tree: Tree, options: SetupRootEsLintOptions) {
tree.write('.eslintignore', 'node_modules\n');
}

return !options.skipPackageJson
? addDependenciesToPackageJson(
tree,
{},
{
'@nx/eslint-plugin': nxVersion,
'@typescript-eslint/parser': typescriptESLintVersion,
'@typescript-eslint/eslint-plugin': typescriptESLintVersion,
'eslint-config-prettier': eslintConfigPrettierVersion,
}
)
: () => {};
if (options.skipPackageJson) {
return () => {};
}
const pkgVersions = versions(tree);
return addDependenciesToPackageJson(
tree,
{},
{
'@nx/eslint-plugin': nxVersion,
'@typescript-eslint/parser': pkgVersions.typescriptESLintVersion,
'@typescript-eslint/eslint-plugin': pkgVersions.typescriptESLintVersion,
'eslint-config-prettier': pkgVersions.eslintConfigPrettierVersion,
},
undefined,
true
);
}

function setUpRootFlatConfig(tree: Tree, options: SetupRootEsLintOptions) {
Expand All @@ -81,17 +80,21 @@ function setUpRootFlatConfig(tree: Tree, options: SetupRootEsLintOptions) {
)
);

return !options.skipPackageJson
? addDependenciesToPackageJson(
tree,
{},
{
'@eslint/js': eslint9__eslintVersion,
'@nx/eslint-plugin': nxVersion,
eslint: eslint9__eslintVersion,
'eslint-config-prettier': eslintConfigPrettierVersion,
'typescript-eslint': eslint9__typescriptESLintVersion,
}
)
: () => {};
if (options.skipPackageJson) {
return () => {};
}
const pkgVersions = versions(tree);
return addDependenciesToPackageJson(
tree,
{},
{
'@eslint/js': pkgVersions.eslintJsVersion,
'@nx/eslint-plugin': nxVersion,
eslint: pkgVersions.eslintVersion,
'eslint-config-prettier': pkgVersions.eslintConfigPrettierVersion,
'typescript-eslint': pkgVersions.typescriptESLintVersion,
},
undefined,
true
);
}
45 changes: 25 additions & 20 deletions packages/eslint/src/generators/utils/eslint-file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readJson, type Tree } from '@nx/devkit';
import { readJson, type Tree, updateJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import * as devkitInternals from 'nx/src/devkit-internals';
import {
BASE_ESLINT_CONFIG_FILENAMES,
ESLINT_CONFIG_FILENAMES,
Expand Down Expand Up @@ -122,10 +121,11 @@ describe('@nx/eslint:lint-file', () => {
});

it('should install necessary dependencies', () => {
// mock eslint version
jest.spyOn(devkitInternals, 'readModulePackageJson').mockReturnValue({
packageJson: { name: 'eslint', version: '9.0.0' },
path: '',
// Declare eslint v9 in the workspace.
updateJson(tree, 'package.json', (json) => {
json.devDependencies ??= {};
json.devDependencies.eslint = '^9.0.0';
return json;
});
tree.write('eslint.config.cjs', 'module.exports = {};');
tree.write(
Expand Down Expand Up @@ -154,8 +154,9 @@ module.exports = [
expect(readJson(tree, 'package.json').devDependencies)
.toMatchInlineSnapshot(`
{
"@eslint/compat": "^1.1.1",
"@eslint/eslintrc": "^2.1.1",
"@eslint/compat": "^1.4.1",
"@eslint/eslintrc": "^3.3.0",
"eslint": "^9.0.0",
}
`);
});
Expand Down Expand Up @@ -211,10 +212,11 @@ module.exports = [
});

it('should add wrapped plugin for compat in extends when using eslint v9', () => {
// mock eslint version
jest.spyOn(devkitInternals, 'readModulePackageJson').mockReturnValue({
packageJson: { name: 'eslint', version: '9.0.0' },
path: '',
// Declare eslint v9 in the workspace.
updateJson(tree, 'package.json', (json) => {
json.devDependencies ??= {};
json.devDependencies.eslint = '^9.0.0';
return json;
});
tree.write('eslint.config.cjs', 'module.exports = {};');
tree.write(
Expand Down Expand Up @@ -270,10 +272,11 @@ module.exports = [
});

it('should handle mixed multiple incompatible and compatible plugins and add them to extends in the specified order when using eslint v9', () => {
// mock eslint version
jest.spyOn(devkitInternals, 'readModulePackageJson').mockReturnValue({
packageJson: { name: 'eslint', version: '9.0.0' },
path: '',
// Declare eslint v9 in the workspace.
updateJson(tree, 'package.json', (json) => {
json.devDependencies ??= {};
json.devDependencies.eslint = '^9.0.0';
return json;
});
tree.write('eslint.config.cjs', 'module.exports = {};');
tree.write(
Expand Down Expand Up @@ -341,10 +344,12 @@ module.exports = [
});

it('should not add wrapped plugin for compat in extends when not using eslint v9', () => {
// mock eslint version
jest.spyOn(devkitInternals, 'readModulePackageJson').mockReturnValue({
packageJson: { name: 'eslint', version: '8.0.0' },
path: '',
// Declare eslint v8 in the workspace so the helper picks the
// pre-flat-config branch.
updateJson(tree, 'package.json', (json) => {
json.devDependencies ??= {};
json.devDependencies.eslint = '~8.0.0';
return json;
});
tree.write('eslint.config.cjs', 'module.exports = {};');
tree.write(
Expand Down
Loading
Loading