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

feat(eslint-plugin): add signals rules #4380

Merged
merged 2 commits into from
Jun 10, 2024
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
52 changes: 16 additions & 36 deletions modules/eslint-plugin/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,42 @@
"config": {
"description": "The config to be used.",
"type": "string",
"default": "recommended",
"default": "all",
"enum": [
"recommended",
"all",
"store-recommended",
"store-all",
"effects-recommended",
"effects-all",
"component-store-recommended",
"component-store-all",
"operators-recommended",
"operators-all"
"component-store",
"effects",
"operators",
"signals",
"store"
],
"x-prompt": {
"message": "Which ESLint configuration would you like to use?",
"type": "list",
"items": [
{
"value": "recommended",
"label": "recommended"
},
{
"value": "all",
"label": "all"
},
{
"value": "store-recommended",
"label": "store-recommended"
},
{
"value": "store-all",
"label": "store-all"
},
{
"value": "effects-recommended",
"label": "effects-recommended"
},
{
"value": "effects-all",
"label": "effects-all"
"value": "component-store",
"label": "component-store"
},
{
"value": "component-store-recommended",
"label": "component-store-recommended"
"value": "effects",
"label": "effects"
},
{
"value": "component-store-all",
"label": "component-store-all"
"value": "operators",
"label": "operators"
},
{
"value": "operators-recommended",
"label": "operators-recommended"
"value": "signals",
"label": "signals"
},
{
"value": "operators-all",
"label": "operators-all"
"value": "store",
"label": "store"
}
]
}
Expand Down
8 changes: 1 addition & 7 deletions modules/eslint-plugin/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Schema {
config:
| 'recommended'
| 'strict'
| 'store'
| 'effects'
| 'component-store'
| 'all';
config: 'all' | 'store' | 'effects' | 'component-store' | 'signals';
}
52 changes: 10 additions & 42 deletions modules/eslint-plugin/scripts/generate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,24 @@ const prettierConfig = resolveConfig.sync(__dirname);
const RULE_MODULE = '@ngrx';
const CONFIG_DIRECTORY = './modules/eslint-plugin/src/configs/';

writeConfig('recommended', (rule) => !rule.meta.docs?.requiresTypeChecking);
writeConfig('all', (_rule) => true);

writeConfig(
'store-recommended',
(rule) =>
rule.meta.ngrxModule === 'store' && !rule.meta.docs?.requiresTypeChecking
);
writeConfig('store-all', (rule) => rule.meta.ngrxModule === 'store');

writeConfig(
'effects-recommended',
(rule) =>
rule.meta.ngrxModule === 'effects' && !rule.meta.docs?.requiresTypeChecking
);
writeConfig('effects-all', (rule) => rule.meta.ngrxModule === 'effects');

writeConfig('store', (rule) => rule.meta.ngrxModule === 'store');
writeConfig('effects', (rule) => rule.meta.ngrxModule === 'effects');
writeConfig(
'component-store-recommended',
(rule) =>
rule.meta.ngrxModule === 'component-store' &&
!rule.meta.docs?.requiresTypeChecking
);

writeConfig(
'component-store-all',
'component-store',
(rule) => rule.meta.ngrxModule === 'component-store'
);

writeConfig(
'operators-recommended',
(rule) =>
rule.meta.ngrxModule === 'operators' &&
!rule.meta.docs?.requiresTypeChecking
);

writeConfig('operators-all', (rule) => rule.meta.ngrxModule === 'operators');
writeConfig('operators', (rule) => rule.meta.ngrxModule === 'operators');
writeConfig('signals', (rule) => rule.meta.ngrxModule === 'signals');

function writeConfig(
configName:
| 'all'
| 'recommended'
| 'store-recommended'
| 'store-all'
| 'effects-recommended'
| 'effects-all'
| 'component-store-recommended'
| 'component-store-all'
| 'operators-recommended'
| 'operators-all',
| 'store'
| 'effects'
| 'component-store'
| 'operators'
| 'signals',
predicate: (rule: NgRxRuleModule<[], string>) => boolean
) {
const rulesForConfig = Object.entries(rulesForGenerate).filter(([_, rule]) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { ESLintUtils, TSESLint } from '@typescript-eslint/utils';
import * as path from 'path';
import rule, {
messageId,
} from '../../../src/rules/signals/signal-state-no-arrays-at-root-level';
import { ruleTester, fromFixture } from '../../utils';

type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>;
type Options = readonly ESLintUtils.InferOptionsTypeFromRule<typeof rule>[];
type RunTests = TSESLint.RunTests<MessageIds, Options>;

const valid: () => RunTests['valid'] = () => [
`const state = signalState({ numbers: [1, 2, 3] });`,
`const state = state([1, 2, 3]);`,
];

const invalid: () => RunTests['invalid'] = () => [
fromFixture(`
const state = signalState([1, 2, 3]);
~~~~~~~~~ [${messageId}]`),
fromFixture(`
class Fixture {
state = signalState([{ foo: 'bar' }]);
~~~~~~~~~~~~~~~~ [${messageId}]
}`),
fromFixture(`
const state = signalState<string[]>([]);
~~ [${messageId}]`),
];

ruleTester().run(path.parse(__filename).name, rule, {
valid: valid(),
invalid: invalid(),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { ESLintUtils, TSESLint } from '@typescript-eslint/utils';
import * as path from 'path';
import rule, {
messageId,
} from '../../../src/rules/signals/with-state-no-arrays-at-root-level';
import { ruleTester, fromFixture } from '../../utils';

type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>;
type Options = readonly ESLintUtils.InferOptionsTypeFromRule<typeof rule>[];
type RunTests = TSESLint.RunTests<MessageIds, Options>;

const valid: () => RunTests['valid'] = () => [
`const store = withState({ foo: 'bar' })`,
`const Store = signalStore(withState(initialState));`,
`
const initialState = {};
const Store = signalStore(withState(initialState));
`,
];

const invalid: () => RunTests['invalid'] = () => [
fromFixture(`
const store = withState([1, 2, 3]);
~~~~~~~~~ [${messageId}]`),
fromFixture(`
class Fixture {
store = withState([{ foo: 'bar' }]);
~~~~~~~~~~~~~~~~ [${messageId}]
}`),
fromFixture(`
const store = withState<string[]>([]);
~~ [${messageId}]`),
fromFixture(`
const initialState = [];
const store = withState(initialState);
~~~~~~~~~~~~ [${messageId}]`),
];

ruleTester().run(path.parse(__filename).name, rule, {
valid: valid(),
invalid: invalid(),
});
12 changes: 6 additions & 6 deletions modules/eslint-plugin/spec/schematics/ng-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const schematicRunner = new SchematicTestRunner(
path.join(__dirname, '../../schematics/collection.json')
);

test('registers the plugin with the recommended config', async () => {
test('registers the plugin with the all config', async () => {
const appTree = new UnitTestTree(Tree.empty());

const initialConfig = {};
Expand All @@ -21,7 +21,7 @@ test('registers the plugin with the recommended config', async () => {
const eslintContent = appTree.readContent(`.eslintrc.json`);
const eslintJson = JSON.parse(eslintContent);
expect(eslintJson).toEqual({
overrides: [{ files: ['*.ts'], extends: [`plugin:@ngrx/recommended`] }],
overrides: [{ files: ['*.ts'], extends: [`plugin:@ngrx/all`] }],
});
});

Expand All @@ -31,7 +31,7 @@ test('registers the plugin with a different config', async () => {
const initialConfig = {};
appTree.create('./.eslintrc.json', JSON.stringify(initialConfig, null, 2));

const options = { config: 'recommended' };
const options = { config: 'store' };
await schematicRunner.runSchematic('ng-add', options, appTree);

const eslintContent = appTree.readContent(`.eslintrc.json`);
Expand Down Expand Up @@ -110,7 +110,7 @@ test('registers the plugin in overrides when it supports TS', async () => {
},
{
files: ['*.ts'],
extends: [`plugin:@ngrx/recommended`],
extends: [`plugin:@ngrx/all`],
},
],
});
Expand All @@ -120,7 +120,7 @@ test('does not add the plugin if it is already added manually', async () => {
const appTree = new UnitTestTree(Tree.empty());

const initialConfig = {
extends: ['plugin:@ngrx/recommended'],
extends: ['plugin:@ngrx/all'],
};
appTree.create('.eslintrc.json', JSON.stringify(initialConfig, null, 2));

Expand All @@ -137,7 +137,7 @@ test('does not add the plugin if it is already added manually as an override', a
const initialConfig = {
overrides: [
{
extends: ['plugin:@ngrx/recommended'],
extends: ['plugin:@ngrx/all'],
},
],
};
Expand Down
2 changes: 2 additions & 0 deletions modules/eslint-plugin/src/configs/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"@ngrx/prefer-effect-callback-in-block-statement": "error",
"@ngrx/use-effects-lifecycle-interface": "error",
"@ngrx/prefer-concat-latest-from": "error",
"@ngrx/signal-state-no-arrays-at-root-level": "error",
"@ngrx/with-state-no-arrays-at-root-level": "error",
"@ngrx/avoid-combining-selectors": "error",
"@ngrx/avoid-dispatching-multiple-actions-sequentially": "error",
"@ngrx/avoid-duplicate-actions-in-reducer": "error",
Expand Down
2 changes: 2 additions & 0 deletions modules/eslint-plugin/src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default (
'@ngrx/prefer-effect-callback-in-block-statement': 'error',
'@ngrx/use-effects-lifecycle-interface': 'error',
'@ngrx/prefer-concat-latest-from': 'error',
'@ngrx/signal-state-no-arrays-at-root-level': 'error',
'@ngrx/with-state-no-arrays-at-root-level': 'error',
'@ngrx/avoid-combining-selectors': 'error',
'@ngrx/avoid-dispatching-multiple-actions-sequentially': 'error',
'@ngrx/avoid-duplicate-actions-in-reducer': 'error',
Expand Down
14 changes: 0 additions & 14 deletions modules/eslint-plugin/src/configs/component-store-all.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default (
},
},
{
name: 'ngrx/component-store-all',
name: 'ngrx/component-store',
languageOptions: {
parser,
},
Expand Down
11 changes: 0 additions & 11 deletions modules/eslint-plugin/src/configs/effects-recommended.json

This file was deleted.

35 changes: 0 additions & 35 deletions modules/eslint-plugin/src/configs/effects-recommended.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@
"@ngrx/prefer-action-creator-in-of-type": "error",
"@ngrx/prefer-effect-callback-in-block-statement": "error",
"@ngrx/use-effects-lifecycle-interface": "error"
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default (
},
},
{
name: 'ngrx/effects-all',
name: 'ngrx/effects',
languageOptions: {
parser,
parserOptions: {
Expand Down
Loading