@angular-eslint/schematics 18.0.0-alpha.4 → 18.0.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const devkit_1 = require("@nx/devkit");
4
4
  const utils_1 = require("../utils");
5
5
  exports.default = (0, devkit_1.convertNxGenerator)(async (tree, options) => {
6
- const projectName = (0, utils_1.determineTargetProjectName__NX)(tree, options.project);
6
+ const projectName = (0, utils_1.determineTargetProjectName)(tree, options.project);
7
7
  if (!projectName) {
8
8
  throw new Error('\n' +
9
9
  `
@@ -13,6 +13,6 @@ E.g. npx ng g @angular-eslint/schematics:add-eslint-to-project {{YOUR_PROJECT_NA
13
13
  `.trim());
14
14
  }
15
15
  // Update the lint builder and config in angular.json
16
- (0, utils_1.addESLintTargetToProject__NX)(tree, projectName, 'lint');
17
- (0, utils_1.createESLintConfigForProject__NX)(tree, projectName, options.setParserOptionsProject ?? false);
16
+ (0, utils_1.addESLintTargetToProject)(tree, projectName, 'lint');
17
+ (0, utils_1.createESLintConfigForProject)(tree, projectName, options.setParserOptionsProject ?? false);
18
18
  });
@@ -10,6 +10,6 @@ exports.default = (0, devkit_1.convertNxGenerator)(async (tree, options) => {
10
10
  const applicationGenerator = (0, ngcli_adapter_1.wrapAngularDevkitSchematic)('@schematics/angular', 'application');
11
11
  await applicationGenerator(tree, angularOptions);
12
12
  // Update the lint builder and config in angular.json
13
- (0, utils_1.addESLintTargetToProject__NX)(tree, options.name, 'lint');
14
- (0, utils_1.createESLintConfigForProject__NX)(tree, options.name, options.setParserOptionsProject ?? false);
13
+ (0, utils_1.addESLintTargetToProject)(tree, options.name, 'lint');
14
+ (0, utils_1.createESLintConfigForProject)(tree, options.name, options.setParserOptionsProject ?? false);
15
15
  });
@@ -10,6 +10,6 @@ exports.default = (0, devkit_1.convertNxGenerator)(async (tree, options) => {
10
10
  const libraryGenerator = (0, ngcli_adapter_1.wrapAngularDevkitSchematic)('@schematics/angular', 'library');
11
11
  await libraryGenerator(tree, angularOptions);
12
12
  // Update the lint builder and config in angular.json
13
- (0, utils_1.addESLintTargetToProject__NX)(tree, options.name, 'lint');
14
- (0, utils_1.createESLintConfigForProject__NX)(tree, options.name, options.setParserOptionsProject ?? false);
13
+ (0, utils_1.addESLintTargetToProject)(tree, options.name, 'lint');
14
+ (0, utils_1.createESLintConfigForProject)(tree, options.name, options.setParserOptionsProject ?? false);
15
15
  });
@@ -1,3 +1,5 @@
1
1
  import type { Rule } from '@angular-devkit/schematics';
2
+ export declare const FIXED_ESLINT_V8_VERSION = "8.57.0";
3
+ export declare const FIXED_TYPESCRIPT_ESLINT_V7_VERSION = "7.11.0";
2
4
  export default function (): Rule;
3
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ng-add/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAA0B,MAAM,4BAA4B,CAAC;AAiJ/E,MAAM,CAAC,OAAO,cAAc,IAAI,CAwB/B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ng-add/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAA0B,MAAM,4BAA4B,CAAC;AAc/E,eAAO,MAAM,uBAAuB,WAAW,CAAC;AAChD,eAAO,MAAM,kCAAkC,WAAW,CAAC;AA+K3D,MAAM,CAAC,OAAO,cAAc,IAAI,CAuB/B"}
@@ -1,11 +1,18 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FIXED_TYPESCRIPT_ESLINT_V7_VERSION = exports.FIXED_ESLINT_V8_VERSION = void 0;
3
7
  const schematics_1 = require("@angular-devkit/schematics");
4
8
  const tasks_1 = require("@angular-devkit/schematics/tasks");
9
+ const semver_1 = __importDefault(require("semver"));
5
10
  const utils_1 = require("../utils");
11
+ exports.FIXED_ESLINT_V8_VERSION = '8.57.0';
12
+ exports.FIXED_TYPESCRIPT_ESLINT_V7_VERSION = '7.11.0';
6
13
  // eslint-disable-next-line @typescript-eslint/no-var-requires
7
14
  const packageJSON = require('../../package.json');
8
- function addAngularESLintPackages() {
15
+ function addAngularESLintPackages(json, useFlatConfig) {
9
16
  return (host, context) => {
10
17
  if (!host.exists('package.json')) {
11
18
  throw new Error('Could not find a `package.json` file at the root of your workspace');
@@ -13,50 +20,67 @@ function addAngularESLintPackages() {
13
20
  if (host.exists('tsconfig.base.json')) {
14
21
  throw new Error('\nError: Angular CLI v10.1.0 and later (and no `tsconfig.base.json`) is required in order to run this schematic. Please update your workspace and try again.\n');
15
22
  }
16
- const projectPackageJSON = host.read('package.json').toString('utf-8');
17
- const json = JSON.parse(projectPackageJSON);
18
- json.devDependencies = json.devDependencies || {};
19
- json.devDependencies['eslint'] =
20
- `^${packageJSON.devDependencies['eslint']}`;
21
23
  json.scripts = json.scripts || {};
22
24
  json.scripts['lint'] = json.scripts['lint'] || 'ng lint';
23
- /**
24
- * @angular-eslint packages
25
- */
26
- json.devDependencies['@angular-eslint/builder'] = packageJSON.version;
27
- json.devDependencies['@angular-eslint/eslint-plugin'] = packageJSON.version;
28
- json.devDependencies['@angular-eslint/eslint-plugin-template'] =
29
- packageJSON.version;
30
- /**
31
- * It seems in certain versions of Angular CLI `ng add` will automatically add the
32
- * @angular-eslint/schematics package to the dependencies section, so clean that up
33
- * at this point
34
- */
35
- if (json.dependencies?.['@angular-eslint/schematics']) {
36
- delete json.dependencies['@angular-eslint/schematics'];
25
+ if (useFlatConfig) {
26
+ applyDevDependenciesForFlatConfig(json);
27
+ }
28
+ else {
29
+ applyDevDependenciesForESLintRC(json);
37
30
  }
38
- json.devDependencies['@angular-eslint/schematics'] = packageJSON.version;
39
- json.devDependencies['@angular-eslint/template-parser'] =
40
- packageJSON.version;
41
- /**
42
- * @typescript-eslint packages
43
- */
44
- const typescriptESLintVersion = packageJSON.devDependencies['@typescript-eslint/utils'];
45
- json.devDependencies['@typescript-eslint/eslint-plugin'] =
46
- typescriptESLintVersion;
47
- json.devDependencies['@typescript-eslint/parser'] = typescriptESLintVersion;
48
31
  json.devDependencies = (0, utils_1.sortObjectByKeys)(json.devDependencies);
49
32
  host.overwrite('package.json', JSON.stringify(json, null, 2));
50
33
  context.addTask(new tasks_1.NodePackageInstallTask());
51
34
  context.logger.info(`
52
- All @angular-eslint dependencies have been successfully installed 🎉
35
+ All angular-eslint dependencies have been successfully installed 🎉
53
36
 
54
37
  Please see https://github.com/angular-eslint/angular-eslint for how to add ESLint configuration to your project.
55
38
  `);
56
39
  return host;
57
40
  };
58
41
  }
59
- function applyESLintConfigIfSingleProjectWithNoExistingTSLint() {
42
+ function applyDevDependenciesForESLintRC(json) {
43
+ json.devDependencies['eslint'] = exports.FIXED_ESLINT_V8_VERSION;
44
+ /**
45
+ * @angular-eslint packages
46
+ */
47
+ json.devDependencies['@angular-eslint/builder'] = packageJSON.version;
48
+ json.devDependencies['@angular-eslint/eslint-plugin'] = packageJSON.version;
49
+ json.devDependencies['@angular-eslint/eslint-plugin-template'] =
50
+ packageJSON.version;
51
+ json.devDependencies['@angular-eslint/schematics'] = packageJSON.version;
52
+ json.devDependencies['@angular-eslint/template-parser'] = packageJSON.version;
53
+ /**
54
+ * @typescript-eslint packages
55
+ */
56
+ json.devDependencies['@typescript-eslint/eslint-plugin'] =
57
+ exports.FIXED_TYPESCRIPT_ESLINT_V7_VERSION;
58
+ json.devDependencies['@typescript-eslint/parser'] =
59
+ exports.FIXED_TYPESCRIPT_ESLINT_V7_VERSION;
60
+ }
61
+ function applyDevDependenciesForFlatConfig(json) {
62
+ json.devDependencies['eslint'] = `^${packageJSON.devDependencies['eslint']}`;
63
+ /**
64
+ * angular-eslint packages
65
+ */
66
+ json.devDependencies['angular-eslint'] = packageJSON.version;
67
+ // Clean up individual packages from devDependencies
68
+ delete json.devDependencies['@angular-eslint/builder'];
69
+ delete json.devDependencies['@angular-eslint/eslint-plugin'];
70
+ delete json.devDependencies['@angular-eslint/eslint-plugin-template'];
71
+ delete json.devDependencies['@angular-eslint/schematics'];
72
+ delete json.devDependencies['@angular-eslint/template-parser'];
73
+ /**
74
+ * typescript-eslint
75
+ */
76
+ const typescriptESLintVersion = packageJSON.devDependencies['@typescript-eslint/utils'];
77
+ json.devDependencies['typescript-eslint'] = typescriptESLintVersion;
78
+ // Clean up individual packages from devDependencies
79
+ delete json.devDependencies['@typescript-eslint/parser'];
80
+ delete json.devDependencies['@typescript-eslint/eslint-plugin'];
81
+ delete json.devDependencies['@typescript-eslint/utils'];
82
+ }
83
+ function applyESLintConfigIfSingleProjectWithNoExistingTSLint(useFlatConfig) {
60
84
  return (host, context) => {
61
85
  const angularJson = (0, utils_1.readJsonInTree)(host, 'angular.json');
62
86
  if (!angularJson || !angularJson.projects) {
@@ -73,7 +97,12 @@ function applyESLintConfigIfSingleProjectWithNoExistingTSLint() {
73
97
  const projectNames = Object.keys(angularJson.projects);
74
98
  if (projectNames.length === 0) {
75
99
  return (0, schematics_1.chain)([
76
- (0, utils_1.updateJsonInTree)('.eslintrc.json', () => (0, utils_1.createRootESLintConfig)(null)),
100
+ useFlatConfig
101
+ ? (host) => {
102
+ host.create('eslint.config.js', (0, utils_1.createStringifiedRootESLintConfig)(null));
103
+ return host;
104
+ }
105
+ : (0, utils_1.updateJsonInTree)('.eslintrc.json', () => (0, utils_1.createRootESLintConfig)(null)),
77
106
  (0, utils_1.updateJsonInTree)('angular.json', (json) => (0, utils_1.updateSchematicCollections)(json)),
78
107
  ]);
79
108
  }
@@ -108,23 +137,18 @@ Please see https://github.com/angular-eslint/angular-eslint for more information
108
137
  }
109
138
  function default_1() {
110
139
  return (host, context) => {
140
+ const workspacePackageJSON = host.read('package.json').toString('utf-8');
141
+ const json = JSON.parse(workspacePackageJSON);
142
+ /**
143
+ * Until eslint v9, typescript-eslint v8 and flat config stabilize completely, allow the user to set a previous version of eslint
144
+ * as a signal for what kind of config and dependencies to set up.
145
+ */
146
+ json.devDependencies = json.devDependencies || {};
147
+ const existingESLintVersion = json.devDependencies['eslint'];
148
+ const useFlatConfig = !(existingESLintVersion && semver_1.default.major(existingESLintVersion) < 9);
111
149
  return (0, schematics_1.chain)([
112
- addAngularESLintPackages(),
113
- applyESLintConfigIfSingleProjectWithNoExistingTSLint(),
114
- () => {
115
- const additionalGitignoreEntries = `.nx/cache
116
- .nx/workspace-data
117
- `;
118
- if (!host.exists('.gitignore')) {
119
- host.create('.gitignore', additionalGitignoreEntries);
120
- return;
121
- }
122
- const gitIgnore = host.read('.gitignore')?.toString();
123
- if (gitIgnore?.includes('.nx')) {
124
- return;
125
- }
126
- host.overwrite('.gitignore', `${gitIgnore}\n${additionalGitignoreEntries}`);
127
- },
150
+ addAngularESLintPackages(json, useFlatConfig),
151
+ applyESLintConfigIfSingleProjectWithNoExistingTSLint(useFlatConfig),
128
152
  ])(host, context);
129
153
  };
130
154
  }
package/dist/utils.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- /**
2
- * Some utils taken from various parts of Nx:
3
- * https://github.com/nrwl/nx
4
- *
5
- * Thanks, Nrwl folks!
6
- */
7
1
  import type { Path } from '@angular-devkit/core';
8
2
  import type { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
9
3
  import type { Tree as NxTree } from '@nx/devkit';
@@ -30,8 +24,7 @@ export declare function getTargetsConfigFromProject(projectConfig: {
30
24
  } & {
31
25
  targets?: TargetsConfig;
32
26
  }): TargetsConfig | null;
33
- export declare function addESLintTargetToProject__NX(tree: NxTree, projectName: string, targetName: 'eslint' | 'lint'): void;
34
- export declare function addESLintTargetToProject(projectName: string, targetName: 'eslint' | 'lint'): Rule;
27
+ export declare function addESLintTargetToProject(tree: NxTree, projectName: string, targetName: 'eslint' | 'lint'): void;
35
28
  /**
36
29
  * Utility to act on all files in a tree that are not ignored by git.
37
30
  */
@@ -59,19 +52,14 @@ export declare function createRootESLintConfig(prefix: string | null): {
59
52
  };
60
53
  }[];
61
54
  };
62
- export declare function createESLintConfigForProject__NX(tree: NxTree, projectName: string, setParserOptionsProject: boolean): void;
63
- export declare function createESLintConfigForProject(projectName: string, setParserOptionsProject: boolean): Rule;
55
+ export declare function createStringifiedRootESLintConfig(prefix: string | null): string;
56
+ export declare function createESLintConfigForProject(tree: NxTree, projectName: string, setParserOptionsProject: boolean): void;
64
57
  export declare function sortObjectByKeys(obj: Record<string, unknown>): Record<string, unknown>;
65
58
  /**
66
59
  * To make certain schematic usage conversion more ergonomic, if the user does not specify a project
67
60
  * and only has a single project in their angular.json we will just go ahead and use that one.
68
61
  */
69
- export declare function determineTargetProjectName__NX(tree: NxTree, maybeProject?: string): string | null;
70
- /**
71
- * To make certain schematic usage conversion more ergonomic, if the user does not specify a project
72
- * and only has a single project in their angular.json we will just go ahead and use that one.
73
- */
74
- export declare function determineTargetProjectName(tree: Tree, maybeProject?: string): string | null;
62
+ export declare function determineTargetProjectName(tree: NxTree, maybeProject?: string): string | null;
75
63
  /**
76
64
  * See `schematicCollections` docs here:
77
65
  * https://github.com/angular/angular-cli/blob/8431b3f0769b5f95b9e13807a09293d820c4b017/docs/specifications/schematic-collections-config.md
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAEjD,OAAO,KAAK,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,4BAA4B,CAAC;AAE/E,OAAO,KAAK,EAAE,IAAI,IAAI,MAAM,EAAwB,MAAM,YAAY,CAAC;AAYvE;;;;;GAKG;AAEH,wBAAgB,cAAc,CAAC,CAAC,GAAG,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,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AAC,uBAS1E;AAED,wBAAgB,uBAAuB,CACrC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAChC,iBAAiB,EAAE,MAAM,EACzB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,uBAUvC"}
package/dist/utils.js CHANGED
@@ -3,11 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.updateSchematicDefaults = exports.updateSchematicCollections = exports.determineTargetProjectName = exports.determineTargetProjectName__NX = exports.sortObjectByKeys = exports.createESLintConfigForProject = exports.createESLintConfigForProject__NX = exports.createRootESLintConfig = exports.visitNotIgnoredFiles = exports.addESLintTargetToProject = exports.addESLintTargetToProject__NX = exports.getTargetsConfigFromProject = exports.updateJsonInTree = exports.readJsonInTree = void 0;
6
+ exports.updateSchematicDefaults = exports.updateSchematicCollections = exports.determineTargetProjectName = exports.sortObjectByKeys = exports.createESLintConfigForProject = exports.createStringifiedRootESLintConfig = exports.createRootESLintConfig = exports.visitNotIgnoredFiles = exports.addESLintTargetToProject = exports.getTargetsConfigFromProject = exports.updateJsonInTree = exports.readJsonInTree = void 0;
7
7
  const core_1 = require("@angular-devkit/core");
8
8
  const schematics_1 = require("@angular-devkit/schematics");
9
9
  const devkit_1 = require("@nx/devkit");
10
10
  const ignore_1 = __importDefault(require("ignore"));
11
+ const semver_1 = __importDefault(require("semver"));
11
12
  const strip_json_comments_1 = __importDefault(require("strip-json-comments"));
12
13
  const DEFAULT_PREFIX = 'app';
13
14
  /**
@@ -48,11 +49,6 @@ function updateJsonInTree(path, callback) {
48
49
  };
49
50
  }
50
51
  exports.updateJsonInTree = updateJsonInTree;
51
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
52
- function getWorkspacePath(host) {
53
- const possibleFiles = ['/workspace.json', '/angular.json', '/.angular.json'];
54
- return possibleFiles.filter((path) => host.exists(path))[0];
55
- }
56
52
  function getTargetsConfigFromProject(projectConfig) {
57
53
  if (!projectConfig) {
58
54
  return null;
@@ -67,25 +63,9 @@ function getTargetsConfigFromProject(projectConfig) {
67
63
  return null;
68
64
  }
69
65
  exports.getTargetsConfigFromProject = getTargetsConfigFromProject;
70
- function offsetFromRoot(fullPathToSourceDir) {
71
- const parts = (0, core_1.normalize)(fullPathToSourceDir).split('/');
72
- let offset = '';
73
- for (let i = 0; i < parts.length; ++i) {
74
- offset += '../';
75
- }
76
- return offset;
77
- }
78
66
  function serializeJson(json) {
79
67
  return `${JSON.stringify(json, null, 2)}\n`;
80
68
  }
81
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
82
- function updateWorkspaceInTree(callback) {
83
- return (host, context) => {
84
- const path = getWorkspacePath(host);
85
- host.overwrite(path, serializeJson(callback(readJsonInTree(host, path), context, host)));
86
- return host;
87
- };
88
- }
89
69
  function readProjectConfiguration(tree, projectName) {
90
70
  const angularJSON = (0, devkit_1.readJson)(tree, 'angular.json');
91
71
  return angularJSON.projects[projectName];
@@ -95,7 +75,7 @@ function updateProjectConfiguration(tree, projectName, projectConfig) {
95
75
  angularJSON.projects[projectName] = projectConfig;
96
76
  (0, devkit_1.writeJson)(tree, 'angular.json', angularJSON);
97
77
  }
98
- function addESLintTargetToProject__NX(tree, projectName, targetName) {
78
+ function addESLintTargetToProject(tree, projectName, targetName) {
99
79
  const existingProjectConfig = readProjectConfiguration(tree, projectName);
100
80
  let lintFilePatternsRoot = '';
101
81
  // Default Angular CLI project at the root of the workspace
@@ -118,31 +98,6 @@ function addESLintTargetToProject__NX(tree, projectName, targetName) {
118
98
  existingProjectConfig.architect[targetName] = eslintTargetConfig;
119
99
  updateProjectConfiguration(tree, projectName, existingProjectConfig);
120
100
  }
121
- exports.addESLintTargetToProject__NX = addESLintTargetToProject__NX;
122
- function addESLintTargetToProject(projectName, targetName) {
123
- return updateWorkspaceInTree((workspaceJson) => {
124
- const existingProjectConfig = workspaceJson.projects[projectName];
125
- let lintFilePatternsRoot = '';
126
- // Default Angular CLI project at the root of the workspace
127
- if (existingProjectConfig.root === '') {
128
- lintFilePatternsRoot = existingProjectConfig.sourceRoot || 'src';
129
- }
130
- else {
131
- lintFilePatternsRoot = existingProjectConfig.root;
132
- }
133
- const eslintTargetConfig = {
134
- builder: '@angular-eslint/builder:lint',
135
- options: {
136
- lintFilePatterns: [
137
- `${lintFilePatternsRoot}/**/*.ts`,
138
- `${lintFilePatternsRoot}/**/*.html`,
139
- ],
140
- },
141
- };
142
- existingProjectConfig.architect[targetName] = eslintTargetConfig;
143
- return workspaceJson;
144
- });
145
- }
146
101
  exports.addESLintTargetToProject = addESLintTargetToProject;
147
102
  /**
148
103
  * Utility to act on all files in a tree that are not ignored by git.
@@ -235,39 +190,54 @@ function createRootESLintConfig(prefix) {
235
190
  };
236
191
  }
237
192
  exports.createRootESLintConfig = createRootESLintConfig;
238
- function createProjectESLintConfig(projectRoot, projectType, prefix, setParserOptionsProject, hasE2e) {
239
- return {
240
- extends: `${offsetFromRoot(projectRoot)}.eslintrc.json`,
241
- ignorePatterns: ['!**/*'],
242
- overrides: [
243
- {
244
- files: ['*.ts'],
245
- ...(setParserOptionsProject
246
- ? {
247
- parserOptions: {
248
- project: setESLintProjectBasedOnProjectType(projectRoot, projectType, hasE2e),
249
- },
250
- }
251
- : null),
252
- rules: {
253
- '@angular-eslint/directive-selector': [
254
- 'error',
255
- { type: 'attribute', prefix, style: 'camelCase' },
256
- ],
257
- '@angular-eslint/component-selector': [
258
- 'error',
259
- { type: 'element', prefix, style: 'kebab-case' },
260
- ],
261
- },
262
- },
263
- {
264
- files: ['*.html'],
265
- rules: {},
266
- },
267
- ],
268
- };
193
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
194
+ function createStringifiedRootESLintConfig(prefix) {
195
+ let codeRules;
196
+ if (prefix) {
197
+ codeRules = {
198
+ '@angular-eslint/directive-selector': [
199
+ 'error',
200
+ { type: 'attribute', prefix, style: 'camelCase' },
201
+ ],
202
+ '@angular-eslint/component-selector': [
203
+ 'error',
204
+ { type: 'element', prefix, style: 'kebab-case' },
205
+ ],
206
+ };
207
+ }
208
+ else {
209
+ codeRules = {};
210
+ }
211
+ return `// @ts-check
212
+ const eslint = require("@eslint/js");
213
+ const tseslint = require("typescript-eslint");
214
+ const angular = require("angular-eslint");
215
+
216
+ module.exports = tseslint.config(
217
+ {
218
+ files: ["**/*.ts"],
219
+ extends: [
220
+ eslint.configs.recommended,
221
+ ...tseslint.configs.recommended,
222
+ ...tseslint.configs.stylistic,
223
+ ...angular.configs.tsRecommended,
224
+ ],
225
+ processor: angular.processInlineTemplates,
226
+ rules: ${JSON.stringify(codeRules, null, 2)},
227
+ },
228
+ {
229
+ files: ["**/*.html"],
230
+ extends: [
231
+ ...angular.configs.templateRecommended,
232
+ ...angular.configs.templateAccessibility,
233
+ ],
234
+ rules: {},
235
+ }
236
+ );
237
+ `;
269
238
  }
270
- function createProjectESLintConfig__NX(projectRoot, projectType, prefix, setParserOptionsProject, hasE2e) {
239
+ exports.createStringifiedRootESLintConfig = createStringifiedRootESLintConfig;
240
+ function createProjectESLintConfig(projectRoot, projectType, prefix, setParserOptionsProject, hasE2e) {
271
241
  return {
272
242
  extends: `${(0, devkit_1.offsetFromRoot)(projectRoot)}.eslintrc.json`,
273
243
  ignorePatterns: ['!**/*'],
@@ -299,59 +269,105 @@ function createProjectESLintConfig__NX(projectRoot, projectType, prefix, setPars
299
269
  ],
300
270
  };
301
271
  }
302
- function createESLintConfigForProject__NX(tree, projectName, setParserOptionsProject) {
272
+ function createStringifiedProjectESLintConfig(projectRoot, projectType, prefix, setParserOptionsProject, hasE2e) {
273
+ return `// @ts-check
274
+ const tseslint = require("typescript-eslint");
275
+ const rootConfig = require("${(0, devkit_1.offsetFromRoot)(projectRoot)}eslint.config.js");
276
+
277
+ module.exports = tseslint.config(
278
+ ...rootConfig,
279
+ {
280
+ files: ["**/*.ts"],${setParserOptionsProject
281
+ ? `
282
+ languageOptions: {
283
+ parserOptions: {
284
+ projectService: true,
285
+ },
286
+ },`
287
+ : ''}
288
+ rules: {
289
+ "@angular-eslint/directive-selector": [
290
+ "error",
291
+ {
292
+ type: "attribute",
293
+ prefix: "${prefix}",
294
+ style: "camelCase",
295
+ },
296
+ ],
297
+ "@angular-eslint/component-selector": [
298
+ "error",
299
+ {
300
+ type: "element",
301
+ prefix: "${prefix}",
302
+ style: "kebab-case",
303
+ },
304
+ ],
305
+ },
306
+ },
307
+ {
308
+ files: ["**/*.html"],
309
+ rules: {},
310
+ }
311
+ );
312
+ `;
313
+ }
314
+ function createESLintConfigForProject(tree, projectName, setParserOptionsProject) {
303
315
  const existingProjectConfig = readProjectConfiguration(tree, projectName);
304
316
  const targets = existingProjectConfig.architect || existingProjectConfig.targets;
305
317
  const { root: projectRoot, projectType, prefix } = existingProjectConfig;
306
318
  const hasE2e = !!targets?.e2e;
319
+ /**
320
+ * In order to support both flat config and eslintrc we need to dynamically figure out
321
+ * what the user should be using based on:
322
+ * - their existing files
323
+ * - their eslint version
324
+ */
325
+ let useFlatConfig = true;
326
+ const alreadyHasRootFlatConfig = tree.exists('eslint.config.js');
327
+ const alreadyHasRootESLintRC = tree.exists('.eslintrc.json');
328
+ if (alreadyHasRootFlatConfig) {
329
+ useFlatConfig = true;
330
+ }
331
+ else if (alreadyHasRootESLintRC) {
332
+ useFlatConfig = false;
333
+ }
334
+ else {
335
+ // If no existing config file, check if they are using at least eslint 9 (when flat config became the default)
336
+ let eslintVersion;
337
+ try {
338
+ eslintVersion = JSON.parse(tree.read('package.json', 'utf-8'))
339
+ .devDependencies?.['eslint'];
340
+ // eslint-disable-next-line no-empty
341
+ }
342
+ catch { }
343
+ if (eslintVersion) {
344
+ const v = semver_1.default.minVersion(eslintVersion);
345
+ useFlatConfig = semver_1.default.gte(v.raw, '9.0.0');
346
+ }
347
+ }
307
348
  /**
308
349
  * If the root is an empty string it must be the initial project created at the
309
350
  * root by the Angular CLI's workspace schematic
310
351
  */
311
352
  if (projectRoot === '') {
312
- return createRootESLintConfigFile__NX(tree, prefix || DEFAULT_PREFIX);
353
+ return createRootESLintConfigFile(tree, prefix || DEFAULT_PREFIX, useFlatConfig);
313
354
  }
314
- // If, for whatever reason, the root .eslintrc.json doesn't exist yet, create it
315
- if (!tree.exists('.eslintrc.json')) {
316
- createRootESLintConfigFile__NX(tree, prefix || DEFAULT_PREFIX);
355
+ // If, for whatever reason, the root eslint.config.js/.eslintrc.json doesn't exist yet, create it
356
+ if (!alreadyHasRootESLintRC && !alreadyHasRootFlatConfig) {
357
+ createRootESLintConfigFile(tree, prefix || DEFAULT_PREFIX, useFlatConfig);
358
+ }
359
+ if (useFlatConfig) {
360
+ tree.write((0, core_1.join)((0, core_1.normalize)(projectRoot), 'eslint.config.js'), createStringifiedProjectESLintConfig(projectRoot, projectType || 'library', prefix || DEFAULT_PREFIX, setParserOptionsProject, hasE2e));
361
+ }
362
+ else {
363
+ (0, devkit_1.writeJson)(tree, (0, core_1.join)((0, core_1.normalize)(projectRoot), '.eslintrc.json'), createProjectESLintConfig(projectRoot, projectType || 'library', prefix || DEFAULT_PREFIX, setParserOptionsProject, hasE2e));
317
364
  }
318
- (0, devkit_1.writeJson)(tree, (0, core_1.join)((0, core_1.normalize)(projectRoot), '.eslintrc.json'), createProjectESLintConfig__NX(projectRoot, projectType || 'library', prefix || DEFAULT_PREFIX, setParserOptionsProject, hasE2e));
319
- }
320
- exports.createESLintConfigForProject__NX = createESLintConfigForProject__NX;
321
- function createESLintConfigForProject(projectName, setParserOptionsProject) {
322
- return (tree) => {
323
- const angularJSON = readJsonInTree(tree, 'angular.json');
324
- const { root: projectRoot, projectType, prefix, } = angularJSON.projects[projectName];
325
- const hasE2e = determineTargetProjectHasE2E(angularJSON, projectName);
326
- /**
327
- * If the root is an empty string it must be the initial project created at the
328
- * root by the Angular CLI's workspace schematic
329
- */
330
- if (projectRoot === '') {
331
- return createRootESLintConfigFile(projectName);
332
- }
333
- return (0, schematics_1.chain)([
334
- // If, for whatever reason, the root .eslintrc.json doesn't exist yet, create it
335
- tree.exists('.eslintrc.json')
336
- ? () => undefined
337
- : createRootESLintConfigFile(projectName),
338
- updateJsonInTree((0, core_1.join)((0, core_1.normalize)(projectRoot), '.eslintrc.json'), () => createProjectESLintConfig(projectRoot, projectType, prefix, setParserOptionsProject, hasE2e)),
339
- ]);
340
- };
341
365
  }
342
366
  exports.createESLintConfigForProject = createESLintConfigForProject;
343
- function createRootESLintConfigFile(projectName) {
344
- return (tree) => {
345
- const angularJSON = readJsonInTree(tree, getWorkspacePath(tree));
346
- let lintPrefix = null;
347
- if (angularJSON.projects?.[projectName]) {
348
- const { prefix } = angularJSON.projects[projectName];
349
- lintPrefix = prefix;
350
- }
351
- return updateJsonInTree('.eslintrc.json', () => createRootESLintConfig(lintPrefix));
352
- };
353
- }
354
- function createRootESLintConfigFile__NX(tree, prefix) {
367
+ function createRootESLintConfigFile(tree, prefix, useFlatConfig) {
368
+ if (useFlatConfig) {
369
+ return tree.write('eslint.config.js', createStringifiedRootESLintConfig(prefix));
370
+ }
355
371
  return (0, devkit_1.writeJson)(tree, '.eslintrc.json', createRootESLintConfig(prefix));
356
372
  }
357
373
  function sortObjectByKeys(obj) {
@@ -365,22 +381,6 @@ function sortObjectByKeys(obj) {
365
381
  }, {});
366
382
  }
367
383
  exports.sortObjectByKeys = sortObjectByKeys;
368
- /**
369
- * To make certain schematic usage conversion more ergonomic, if the user does not specify a project
370
- * and only has a single project in their angular.json we will just go ahead and use that one.
371
- */
372
- function determineTargetProjectName__NX(tree, maybeProject) {
373
- if (maybeProject) {
374
- return maybeProject;
375
- }
376
- const workspaceJson = (0, devkit_1.readJson)(tree, 'angular.json');
377
- const projects = Object.keys(workspaceJson.projects);
378
- if (projects.length === 1) {
379
- return projects[0];
380
- }
381
- return null;
382
- }
383
- exports.determineTargetProjectName__NX = determineTargetProjectName__NX;
384
384
  /**
385
385
  * To make certain schematic usage conversion more ergonomic, if the user does not specify a project
386
386
  * and only has a single project in their angular.json we will just go ahead and use that one.
@@ -389,7 +389,7 @@ function determineTargetProjectName(tree, maybeProject) {
389
389
  if (maybeProject) {
390
390
  return maybeProject;
391
391
  }
392
- const workspaceJson = readJsonInTree(tree, getWorkspacePath(tree));
392
+ const workspaceJson = (0, devkit_1.readJson)(tree, 'angular.json');
393
393
  const projects = Object.keys(workspaceJson.projects);
394
394
  if (projects.length === 1) {
395
395
  return projects[0];
@@ -397,15 +397,6 @@ function determineTargetProjectName(tree, maybeProject) {
397
397
  return null;
398
398
  }
399
399
  exports.determineTargetProjectName = determineTargetProjectName;
400
- /**
401
- * Checking if the target project has e2e setup
402
- * Method will check if angular project architect has e2e configuration to determine if e2e setup
403
- */
404
- function determineTargetProjectHasE2E(
405
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
406
- angularJSON, projectName) {
407
- return !!getTargetsConfigFromProject(angularJSON.projects[projectName])?.e2e;
408
- }
409
400
  /**
410
401
  * See `schematicCollections` docs here:
411
402
  * https://github.com/angular/angular-cli/blob/8431b3f0769b5f95b9e13807a09293d820c4b017/docs/specifications/schematic-collections-config.md
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angular-eslint/schematics",
3
- "version": "18.0.0-alpha.4",
3
+ "version": "18.0.0-alpha.9",
4
4
  "description": "Angular Schematics for angular-eslint",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -31,11 +31,11 @@
31
31
  ]
32
32
  },
33
33
  "ng-add": {
34
- "save": "devDependencies"
34
+ "save": false
35
35
  },
36
36
  "dependencies": {
37
- "@angular-eslint/eslint-plugin": "18.0.0-alpha.4",
38
- "@angular-eslint/eslint-plugin-template": "18.0.0-alpha.4",
37
+ "@angular-eslint/eslint-plugin": "18.0.0-alpha.9",
38
+ "@angular-eslint/eslint-plugin-template": "18.0.0-alpha.9",
39
39
  "@nx/devkit": "^19.0.6",
40
40
  "ignore": "5.3.1",
41
41
  "nx": "^19.0.6",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@types/tmp": "0.2.6",
47
- "@typescript-eslint/utils": "8.0.0-alpha.16",
47
+ "@typescript-eslint/utils": "8.0.0-alpha.17",
48
48
  "eslint": "9.3.0"
49
49
  },
50
50
  "peerDependencies": {