Skip to content

Commit 5102a34

Browse files
fix(store): add ESLint plugin to TS overrides when possible (#3032)
Closes #3031
1 parent a02ea9f commit 5102a34

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

modules/store/schematics/ng-add/index.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,80 @@ describe('Store ng-add Schematic', () => {
170170
});
171171
});
172172

173+
it('should register the NgRx ESLint Plugin in overrides when it supports TS', async () => {
174+
const options = { ...defaultOptions };
175+
176+
// this is a trimmed down version of the default angular-eslint schematic
177+
const initialConfig = {
178+
overrides: [
179+
{
180+
files: ['*.ts'],
181+
parserOptions: {
182+
project: ['tsconfig.eslint.json'],
183+
createDefaultProgram: true,
184+
},
185+
extends: [
186+
'plugin:@angular-eslint/recommended',
187+
'eslint:recommended',
188+
'plugin:@typescript-eslint/recommended',
189+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
190+
'plugin:@angular-eslint/template/process-inline-templates',
191+
'plugin:prettier/recommended',
192+
],
193+
},
194+
{
195+
files: ['*.html'],
196+
extends: [
197+
'plugin:@angular-eslint/template/recommended',
198+
'plugin:prettier/recommended',
199+
],
200+
rules: {},
201+
},
202+
],
203+
};
204+
appTree.create('.eslintrc.json', JSON.stringify(initialConfig, null, 2));
205+
206+
const tree = await schematicRunner
207+
.runSchematicAsync('ng-add', options, appTree)
208+
.toPromise();
209+
210+
const packageContent = tree.readContent('package.json');
211+
const packageJson = JSON.parse(packageContent);
212+
expect(packageJson.devDependencies['eslint-plugin-ngrx']).toBeDefined();
213+
214+
const eslintContent = tree.readContent(`.eslintrc.json`);
215+
const eslintJson = JSON.parse(eslintContent);
216+
expect(eslintJson).toEqual({
217+
overrides: [
218+
{
219+
files: ['*.ts'],
220+
parserOptions: {
221+
project: ['tsconfig.eslint.json'],
222+
createDefaultProgram: true,
223+
},
224+
plugins: ['ngrx'],
225+
extends: [
226+
'plugin:@angular-eslint/recommended',
227+
'eslint:recommended',
228+
'plugin:@typescript-eslint/recommended',
229+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
230+
'plugin:@angular-eslint/template/process-inline-templates',
231+
'plugin:prettier/recommended',
232+
'plugin:ngrx/recommended',
233+
],
234+
},
235+
{
236+
files: ['*.html'],
237+
extends: [
238+
'plugin:@angular-eslint/template/recommended',
239+
'plugin:prettier/recommended',
240+
],
241+
rules: {},
242+
},
243+
],
244+
});
245+
});
246+
173247
it('should not register the NgRx ESLint Plugin when skipped', async () => {
174248
const options = { ...defaultOptions, skipESLintPlugin: true };
175249

modules/store/schematics/ng-add/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ function addNgRxESLintPlugin() {
136136

137137
try {
138138
const json = JSON.parse(eslint);
139-
json.plugins = [...(json.plugins || []), 'ngrx'];
140-
json.extends = [...(json.extends || []), 'plugin:ngrx/recommended'];
139+
if (json.overrides) {
140+
json.overrides
141+
.filter((override: { files?: string[] }) =>
142+
override.files?.some((file: string) => file.endsWith('*.ts'))
143+
)
144+
.forEach(configureESLintPlugin);
145+
} else {
146+
configureESLintPlugin(json);
147+
}
148+
141149
host.overwrite(eslintConfigPath, JSON.stringify(json, null, 2));
142150

143151
context.logger.info(`
@@ -162,6 +170,11 @@ ${err.message}
162170
};
163171
}
164172

173+
function configureESLintPlugin(json: any): void {
174+
json.plugins = [...(json.plugins || []), 'ngrx'];
175+
json.extends = [...(json.extends || []), 'plugin:ngrx/recommended'];
176+
}
177+
165178
export default function (options: RootStoreOptions): Rule {
166179
return (host: Tree, context: SchematicContext) => {
167180
options.path = getProjectPath(host, options);
@@ -189,7 +202,7 @@ export default function (options: RootStoreOptions): Rule {
189202
}
190203

191204
const templateSource = apply(url('./files'), [
192-
filter((_) => (options.minimal ? false : true)),
205+
filter(() => (options.minimal ? false : true)),
193206
applyTemplates({
194207
...stringUtils,
195208
...options,

0 commit comments

Comments
 (0)