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(angular): install @typescript-eslint/utils when using relevant angular eslint v18 packages #26418

Merged
merged 1 commit into from
Jun 6, 2024
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
9 changes: 9 additions & 0 deletions packages/angular/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@
},
"description": "Update the @angular/cli package version to ~18.0.0.",
"factory": "./src/migrations/update-19-1-0/update-angular-cli"
},
"add-typescript-eslint-utils": {
"cli": "nx",
"version": "19.2.1-beta.0",
"requires": {
"@angular-eslint/eslint-plugin": ">=18.0.0"
},
"description": "Installs the '@typescript-eslint/utils' package when having installed '@angular-eslint/eslint-plugin' or '@angular-eslint/eslint-plugin-template' with version >=18.0.0.",
"factory": "./src/migrations/update-19-2-1/add-typescript-eslint-utils"
}
},
"packageJsonUpdates": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ export function addAngularEsLintDependencies(
tree: Tree,
projectName: string
): GeneratorCallback {
const angularEslintVersionToInstall = versions(tree).angularEslintVersion;
const compatVersions = versions(tree);
const angularEslintVersionToInstall = compatVersions.angularEslintVersion;
const devDependencies = {
'@angular-eslint/eslint-plugin': angularEslintVersionToInstall,
'@angular-eslint/eslint-plugin-template': angularEslintVersionToInstall,
'@angular-eslint/template-parser': angularEslintVersionToInstall,
};

if ('typescriptEslintVersion' in compatVersions) {
devDependencies['@typescript-eslint/utils'] =
compatVersions.typescriptEslintVersion;
}

if (isBuildableLibraryProject(tree, projectName)) {
const jsoncEslintParserVersionToInstall =
versions(tree).jsoncEslintParserVersion;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { readJson, updateJson, type Tree } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import migration, {
typescriptEslintUtilsVersion,
} from './add-typescript-eslint-utils';

describe('add-typescript-eslint-utils migration', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

it.each`
pkgName | version
${'@angular-eslint/eslint-plugin'} | ${'18.0.0'}
${'@angular-eslint/eslint-plugin'} | ${'~18.0.0'}
${'@angular-eslint/eslint-plugin'} | ${'^18.0.0'}
${'@angular-eslint/eslint-plugin'} | ${'^19.0.0'}
${'@angular-eslint/eslint-plugin-template'} | ${'18.0.0'}
${'@angular-eslint/eslint-plugin-template'} | ${'~18.0.0'}
${'@angular-eslint/eslint-plugin-template'} | ${'^18.0.0'}
${'@angular-eslint/eslint-plugin-template'} | ${'^19.0.0'}
`(
'should add "@typescript-eslint/utils" as devDependencies when "$pkgName" is installed with version "$version"',
async ({ pkgName, version }) => {
updateJson(tree, 'package.json', (json) => {
json.devDependencies = {
...json.devDependencies,
[`${[pkgName]}`]: `${version}`,
};
return json;
});

await migration(tree);

const { devDependencies } = readJson(tree, 'package.json');
expect(devDependencies['@typescript-eslint/utils']).toBe(
typescriptEslintUtilsVersion
);
}
);

it.each`
pkgName | version
${'@angular-eslint/eslint-plugin'} | ${'17.0.0'}
${'@angular-eslint/eslint-plugin'} | ${'~17.0.0'}
${'@angular-eslint/eslint-plugin'} | ${'^17.0.0'}
${'@angular-eslint/eslint-plugin-template'} | ${'17.0.0'}
${'@angular-eslint/eslint-plugin-template'} | ${'~17.0.0'}
${'@angular-eslint/eslint-plugin-template'} | ${'^17.0.0'}
`(
'should not add "@typescript-eslint/utils" when "$pkgName" is installed with version "$version"',
async ({ pkgName, version }) => {
updateJson(tree, 'package.json', (json) => {
json.devDependencies = {
...json.devDependencies,
[`${[pkgName]}`]: `${version}`,
};
return json;
});

await migration(tree);

const { devDependencies } = readJson(tree, 'package.json');
expect(devDependencies['@typescript-eslint/utils']).toBeUndefined();
}
);

it('should not add "@typescript-eslint/utils" when "@angular-eslint/eslint-plugin" and "@angular-eslint/eslint-plugin-template" are not installed', async () => {
await migration(tree);

const { devDependencies } = readJson(tree, 'package.json');
expect(devDependencies['@typescript-eslint/utils']).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
addDependenciesToPackageJson,
formatFiles,
type Tree,
} from '@nx/devkit';
import {
getInstalledPackageVersion,
getInstalledPackageVersionInfo,
} from '../../generators/utils/version-utils';

export const typescriptEslintUtilsVersion = '^8.0.0-alpha.28';

export default async function (tree: Tree) {
if (getInstalledPackageVersion(tree, '@typescript-eslint/utils')) {
return;
}

const eslintPluginVersionInfo =
getInstalledPackageVersionInfo(tree, '@angular-eslint/eslint-plugin') ??
getInstalledPackageVersionInfo(
tree,
'@angular-eslint/eslint-plugin-template'
);
if (!eslintPluginVersionInfo || eslintPluginVersionInfo.major < 18) {
return;
}

addDependenciesToPackageJson(
tree,
{},
{ '@typescript-eslint/utils': typescriptEslintUtilsVersion }
);

await formatFiles(tree);
}
10 changes: 8 additions & 2 deletions packages/angular/src/utils/backward-compatible-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ export type PackageVersionNames =
| CompatPackageVersionNames;

export type VersionMap = {
angularV16: Record<CompatPackageVersionNames, string>;
angularV16: Record<
Exclude<CompatPackageVersionNames, 'typescriptEslintVersion'>,
string
>;
angularV17: Record<
Exclude<CompatPackageVersionNames, 'ngUniversalVersion'>,
Exclude<
CompatPackageVersionNames,
'ngUniversalVersion' | 'typescriptEslintVersion'
>,
string
>;
};
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const browserSyncVersion = '^3.0.0';
export const moduleFederationNodeVersion = '~1.0.5';

export const angularEslintVersion = '^18.0.1';
export const typescriptEslintVersion = '^8.0.0-alpha.28';
export const tailwindVersion = '^3.0.2';
export const postcssVersion = '^8.4.5';
export const postcssUrlVersion = '~10.1.3';
Expand Down