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(store): add ESLint plugin to TS overrides when possible #3032

Merged
merged 1 commit into from
May 26, 2021
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
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