Skip to content

Commit

Permalink
fix(store): add ESLint plugin to TS overrides when possible (#3032)
Browse files Browse the repository at this point in the history
Closes #3031
  • Loading branch information
timdeschryver authored May 26, 2021
1 parent a02ea9f commit 5102a34
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
74 changes: 74 additions & 0 deletions modules/store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,80 @@ describe('Store ng-add Schematic', () => {
});
});

it('should register the NgRx ESLint Plugin in overrides when it supports TS', async () => {
const options = { ...defaultOptions };

// this is a trimmed down version of the default angular-eslint schematic
const initialConfig = {
overrides: [
{
files: ['*.ts'],
parserOptions: {
project: ['tsconfig.eslint.json'],
createDefaultProgram: true,
},
extends: [
'plugin:@angular-eslint/recommended',
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:@angular-eslint/template/process-inline-templates',
'plugin:prettier/recommended',
],
},
{
files: ['*.html'],
extends: [
'plugin:@angular-eslint/template/recommended',
'plugin:prettier/recommended',
],
rules: {},
},
],
};
appTree.create('.eslintrc.json', JSON.stringify(initialConfig, null, 2));

const tree = await schematicRunner
.runSchematicAsync('ng-add', options, appTree)
.toPromise();

const packageContent = tree.readContent('package.json');
const packageJson = JSON.parse(packageContent);
expect(packageJson.devDependencies['eslint-plugin-ngrx']).toBeDefined();

const eslintContent = tree.readContent(`.eslintrc.json`);
const eslintJson = JSON.parse(eslintContent);
expect(eslintJson).toEqual({
overrides: [
{
files: ['*.ts'],
parserOptions: {
project: ['tsconfig.eslint.json'],
createDefaultProgram: true,
},
plugins: ['ngrx'],
extends: [
'plugin:@angular-eslint/recommended',
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:@angular-eslint/template/process-inline-templates',
'plugin:prettier/recommended',
'plugin:ngrx/recommended',
],
},
{
files: ['*.html'],
extends: [
'plugin:@angular-eslint/template/recommended',
'plugin:prettier/recommended',
],
rules: {},
},
],
});
});

it('should not register the NgRx ESLint Plugin when skipped', async () => {
const options = { ...defaultOptions, skipESLintPlugin: true };

Expand Down
19 changes: 16 additions & 3 deletions modules/store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,16 @@ function addNgRxESLintPlugin() {

try {
const json = JSON.parse(eslint);
json.plugins = [...(json.plugins || []), 'ngrx'];
json.extends = [...(json.extends || []), 'plugin:ngrx/recommended'];
if (json.overrides) {
json.overrides
.filter((override: { files?: string[] }) =>
override.files?.some((file: string) => file.endsWith('*.ts'))
)
.forEach(configureESLintPlugin);
} else {
configureESLintPlugin(json);
}

host.overwrite(eslintConfigPath, JSON.stringify(json, null, 2));

context.logger.info(`
Expand All @@ -162,6 +170,11 @@ ${err.message}
};
}

function configureESLintPlugin(json: any): void {
json.plugins = [...(json.plugins || []), 'ngrx'];
json.extends = [...(json.extends || []), 'plugin:ngrx/recommended'];
}

export default function (options: RootStoreOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
Expand Down Expand Up @@ -189,7 +202,7 @@ export default function (options: RootStoreOptions): Rule {
}

const templateSource = apply(url('./files'), [
filter((_) => (options.minimal ? false : true)),
filter(() => (options.minimal ? false : true)),
applyTemplates({
...stringUtils,
...options,
Expand Down

0 comments on commit 5102a34

Please sign in to comment.