Skip to content

Commit ca36f71

Browse files
Update the README sections on flat configuration and Angular. (#173)
I updated an Angular application following the README and have updated the README for the style guide with important information. The tests for Angular also need to be expanded to validate that our configuration and how we document its use works with Angular workspaces and the VSCode extensions. This will be done in a separate PR. - Update the README sections on flat configuration and Angular. - Fix the `tsconfigRootDir` configuration in the tests. Testing - Verified that the linter passes - Verified that the automated tests pass - Manually verified that the installation instructions in the README work correctly - Manually verified that the shared configurations work correctly
1 parent 440c014 commit ca36f71

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

README.md

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ Install the package for your corresponding language as a development dependency:
4949

5050
## Configuration
5151

52-
After installing the lint configuration packages, follow the configuration instructions for your project language:
52+
After installing the lint configuration packages, determine if the project is using CommonJS or ES modules by default. ES modules will specify ["type": "module"](https://nodejs.org/api/packages.html#type) in `package.json`. The following configuration examples are ES modules. Use the file extension `mjs` for ES modules in CommonJS projects, e.g. `eslint.config.mjs` will be common for Angular.
53+
54+
With flat configuration, the `files` declaration must use a glob to match files in subdirectories. See [Glob-Based Configs](https://eslint.org/docs/latest/use/configure/migration-guide#glob-based-configs).
55+
56+
Follow the instructions for your project's language:
5357
5458
### JavaScript configuration
5559
@@ -66,7 +70,7 @@ export default defineConfig([
6670
6771
### TypeScript configuration
6872
69-
Use `@ni/eslint-config-typescript` configurations in the [ESLint flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new) (`eslint.config.js`). Set the `parserOptions.project` to the project's TypeScript configuration to correctly enable [linting with type information](https://typescript-eslint.io/getting-started/typed-linting).
73+
Use `@ni/eslint-config-typescript` configurations in the [ESLint flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new) (`eslint.config.js`). Set the `languageOptions.parserOptions.project` to the project's TypeScript configuration to correctly enable [linting with type information](https://typescript-eslint.io/getting-started/typed-linting).
7074

7175
```js
7276
import { defineConfig } from 'eslint/config';
@@ -79,7 +83,7 @@ export default defineConfig([
7983
languageOptions: {
8084
parserOptions: {
8185
project: ['./tsconfig.json'],
82-
tsConfigRootDir: import.meta.dirname,
86+
tsconfigRootDir: import.meta.dirname,
8387
},
8488
},
8589
},
@@ -90,38 +94,58 @@ export default defineConfig([
9094
9195
ESLint support for Angular is provided by [`angular-eslint`](https://github.com/angular-eslint/angular-eslint#readme).
9296
93-
1. **For single and multi-project workspaces**, [add angular-eslint](https://github.com/angular-eslint/angular-eslint#quick-start). Remove the `angular-eslint` and `eslint` dependencies from `package.json`.
97+
1. **For single and multi-project workspaces**, [add angular-eslint](https://github.com/angular-eslint/angular-eslint#quick-start).
9498
```bash
9599
ng add angular-eslint
96100
```
97-
2. **For multi-project workspaces**, [configure each project](https://github.com/angular-eslint/angular-eslint#adding-eslint-configuration-to-an-existing-angular-cli-project-which-has-no-existing-linter), and then enable future generated projects to be configured as well.
101+
2. Remove the `angular-eslint`, `typescript-eslint`, and `eslint` development dependencies from `package.json`.
102+
3. **For multi-project workspaces**, [configure each project](https://github.com/angular-eslint/angular-eslint#adding-eslint-configuration-to-an-existing-angular-cli-project-which-has-no-existing-linter), and then enable future generated projects to be configured as well.
98103
```bash
99104
> ng g angular-eslint:add-eslint-to-project <PROJECT NAME>
100105
> ng config cli.schematicCollections "[\"angular-eslint\"]"
101106
```
102-
3. Use the NI configured rules for Angular TypeScript code and Angular templates in the [ESLint flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new) (`eslint.config.js`). Set the `parserOptions.project` configuration to the project's TypeScript configuration to correctly enable [linting with type information](https://typescript-eslint.io/getting-started/typed-linting)..
107+
4. Replace `eslint.config.js` with the following `eslint.config.mjs` [flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new) of rules for Typescript, Angular templates, and JavaScript.
103108
```js
104109
import { defineConfig } from 'eslint/config';
105110
import { angularTypescriptConfig, angularTemplateConfig } from '@ni/eslint-config-angular';
111+
import { javascriptConfig } from '@ni/eslint-config-javascript';
106112
107113
export default defineConfig([
114+
{
115+
// JavaScript rules fail to parse the HTML files that are added below. Therefore, the JavaScript
116+
// configuration must now match the correct files to avoid an error.
117+
files: ['**/*.js'],
118+
extends: javascriptConfig
119+
},
108120
{
109121
files: ['**/*.ts'],
110122
extends: angularTypescriptConfig,
111123
languageOptions: {
112124
parserOptions: {
113-
project: ['./tsconfig.json'],
114-
tsConfigRootDir: import.meta.dirname,
115-
},
116-
},
125+
// The `languageOptions.parserOptions.projectService` option is recommended but does not identify
126+
// tsconfig.*.json files. Use the older `project` configuration instead. The general `tsconfig.json` is
127+
// declared last in the hope that the application and test configurations will apply to their
128+
// applicable files correctly.
129+
// https://typescript-eslint.io/troubleshooting/typed-linting/#project-service-issues
130+
project: ['tsconfig.app.json', 'tsconfig.spec.json', 'tsconfig.json']
131+
// In projects (e.g. libraries) using `parserOptions.project`, Angular requires that the paths be
132+
// relative to the root, but the VSCode extension requires them to be relative to the project
133+
// directory. Set the root to be the project directory to satisfy both. The `parserOptions.projectService`
134+
// configuration would likely resolve this, but is not used for reasons described above.
135+
// https://typescript-eslint.io/blog/project-service
136+
// tsconfigRootDir: import.meta.dirname
137+
}
138+
139+
}
117140
},
118141
{
119142
files: ['**/*.html'],
120-
extends: angularTemplateConfig,
143+
extends: angularTemplateConfig
121144
}
122145
]);
123146
```
124-
4. Evaluate the [project specific rule groups](#evaluate-project-specific-rule-groups) to manually add to your lint configuration. For Angular applications in particular, consider enabling the [`[application-prefix]`](#application-prefix) rule group.
147+
5. Evaluate the [project specific rule groups](#evaluate-project-specific-rule-groups) to manually add to your lint configuration. For Angular applications in particular, consider enabling the [`[application-prefix]`](#application-prefix) rule group.
148+
6. With flat configurations, configurations for subdirectories must be defined at the root except for projects which are already root configurations. ESLint loads only one flat configuration resolved relative to the working directory (e.g. root). Support for defining configurations in subdirectories is experimental behind an active feature flag. The angular-eslint builder does not support this feature flag. See ESLint [issue #18385](https://github.com/eslint/eslint/issues/18385).
125149
126150
### Playwright configuration
127151
@@ -140,7 +164,7 @@ export default defineConfig([
140164
languageOptions: {
141165
parserOptions: {
142166
project: ['./tsconfig.json'],
143-
tsConfigRootDir: import.meta.dirname,
167+
tsconfigRootDir: import.meta.dirname,
144168
},
145169
}
146170
}
@@ -314,7 +338,7 @@ Instead of using the `extends` property, import the configuration packages you n
314338
languageOptions: {
315339
parserOptions: {
316340
project: ['./tsconfig.json'],
317-
tsConfigRootDir: import.meta.dirname,
341+
tsconfigRootDir: import.meta.dirname,
318342
},
319343
}
320344
}

tests/angular/eslint.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ export default defineConfig([
1212
extends: angularTypescriptConfig,
1313
languageOptions: {
1414
parserOptions: {
15-
project: ['./tsconfig.json'],
16-
tsConfigRootDir: import.meta.dirname,
15+
project: ['./tsconfig.json']
1716
},
1817
},
1918
},

tests/playwright/eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig([
1313
languageOptions: {
1414
parserOptions: {
1515
project: ['./tsconfig.json'],
16-
tsConfigRootDir: import.meta.dirname,
16+
tsconfigRootDir: import.meta.dirname,
1717
},
1818
},
1919
rules: {

tests/typescript/eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineConfig([
1010
languageOptions: {
1111
parserOptions: {
1212
project: ['./tsconfig.json'],
13-
tsConfigRootDir: import.meta.dirname,
13+
tsconfigRootDir: import.meta.dirname,
1414
},
1515
},
1616
},

0 commit comments

Comments
 (0)