@angular-eslint/eslint-plugin 19.0.0-alpha.2 → 19.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ type Options = [
3
+ {
4
+ preferReadonlySignalProperties: boolean;
5
+ preferInputSignals: boolean;
6
+ preferQuerySignals: boolean;
7
+ useTypeChecking: boolean;
8
+ additionalSignalCreationFunctions: string[];
9
+ }
10
+ ];
11
+ export type MessageIds = 'preferInputSignals' | 'preferQuerySignals' | 'preferReadonlySignalProperties' | 'suggestAddReadonlyModifier';
12
+ export declare const RULE_NAME = "prefer-signals";
13
+ declare const _default: ESLintUtils.RuleModule<MessageIds, Options, import("../utils/create-eslint-rule").RuleDocs, ESLintUtils.RuleListener>;
14
+ export default _default;
15
+ //# sourceMappingURL=prefer-signals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefer-signals.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-signals.ts"],"names":[],"mappings":"AAKA,OAAO,EAAkB,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAGvE,KAAK,OAAO,GAAG;IACb;QACE,8BAA8B,EAAE,OAAO,CAAC;QACxC,kBAAkB,EAAE,OAAO,CAAC;QAC5B,kBAAkB,EAAE,OAAO,CAAC;QAC5B,eAAe,EAAE,OAAO,CAAC;QACzB,iCAAiC,EAAE,MAAM,EAAE,CAAC;KAC7C;CACF,CAAC;AA4BF,MAAM,MAAM,UAAU,GAClB,oBAAoB,GACpB,oBAAoB,GACpB,gCAAgC,GAChC,4BAA4B,CAAC;AACjC,eAAO,MAAM,SAAS,mBAAmB,CAAC;;AAE1C,wBA6KG"}
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RULE_NAME = void 0;
4
+ const utils_1 = require("@angular-eslint/utils");
5
+ const utils_2 = require("@typescript-eslint/utils");
6
+ const create_eslint_rule_1 = require("../utils/create-eslint-rule");
7
+ const DEFAULT_OPTIONS = {
8
+ preferReadonlySignalProperties: true,
9
+ preferInputSignals: true,
10
+ preferQuerySignals: true,
11
+ useTypeChecking: false,
12
+ additionalSignalCreationFunctions: [],
13
+ };
14
+ const KNOWN_SIGNAL_TYPES = new Set([
15
+ 'InputSignal',
16
+ 'ModelSignal',
17
+ 'Signal',
18
+ 'WritableSignal',
19
+ ]);
20
+ const KNOWN_SIGNAL_CREATION_FUNCTIONS = new Set([
21
+ 'computed',
22
+ 'contentChild',
23
+ 'contentChildren',
24
+ 'input',
25
+ 'model',
26
+ 'signal',
27
+ 'toSignal',
28
+ 'viewChild',
29
+ 'viewChildren',
30
+ ]);
31
+ exports.RULE_NAME = 'prefer-signals';
32
+ exports.default = (0, create_eslint_rule_1.createESLintRule)({
33
+ name: exports.RULE_NAME,
34
+ meta: {
35
+ type: 'suggestion',
36
+ docs: {
37
+ description: 'Use readonly signals instead of `@Input()`, `@ViewChild()` and other legacy decorators',
38
+ recommended: 'recommended',
39
+ },
40
+ hasSuggestions: true,
41
+ schema: [
42
+ {
43
+ type: 'object',
44
+ properties: {
45
+ preferReadonlySignalProperties: {
46
+ type: 'boolean',
47
+ default: DEFAULT_OPTIONS.preferReadonlySignalProperties,
48
+ },
49
+ preferInputSignals: {
50
+ type: 'boolean',
51
+ default: DEFAULT_OPTIONS.preferInputSignals,
52
+ },
53
+ preferQuerySignals: {
54
+ type: 'boolean',
55
+ default: DEFAULT_OPTIONS.preferQuerySignals,
56
+ },
57
+ useTypeChecking: {
58
+ type: 'boolean',
59
+ default: DEFAULT_OPTIONS.useTypeChecking,
60
+ },
61
+ additionalSignalCreationFunctions: {
62
+ type: 'array',
63
+ items: { type: 'string' },
64
+ default: DEFAULT_OPTIONS.additionalSignalCreationFunctions,
65
+ },
66
+ },
67
+ additionalProperties: false,
68
+ },
69
+ ],
70
+ messages: {
71
+ preferInputSignals: 'Use `InputSignal`s (e.g. via `input()`) for Component input properties rather than the legacy `@Input()` decorator',
72
+ preferQuerySignals: 'Use the `{{function}}` function instead of the `{{decorator}}` decorator',
73
+ preferReadonlySignalProperties: 'Properties declared using signals should be marked as `readonly` since they should not be reassigned',
74
+ suggestAddReadonlyModifier: 'Add `readonly` modifier',
75
+ },
76
+ },
77
+ defaultOptions: [{ ...DEFAULT_OPTIONS }],
78
+ create(context, [{ preferReadonlySignalProperties = DEFAULT_OPTIONS.preferReadonlySignalProperties, preferInputSignals = DEFAULT_OPTIONS.preferInputSignals, preferQuerySignals = DEFAULT_OPTIONS.preferQuerySignals, additionalSignalCreationFunctions = DEFAULT_OPTIONS.additionalSignalCreationFunctions, useTypeChecking = DEFAULT_OPTIONS.useTypeChecking, },]) {
79
+ let services;
80
+ const listener = {};
81
+ if (preferReadonlySignalProperties) {
82
+ listener[`PropertyDefinition:not([readonly=true])`] = (node) => {
83
+ let shouldBeReadonly = false;
84
+ if (node.typeAnnotation) {
85
+ // Use the type annotation to determine
86
+ // whether the property is a signal.
87
+ if (node.typeAnnotation.typeAnnotation.type ===
88
+ utils_2.AST_NODE_TYPES.TSTypeReference) {
89
+ const type = node.typeAnnotation.typeAnnotation;
90
+ if (type.typeArguments &&
91
+ type.typeName.type === utils_2.AST_NODE_TYPES.Identifier &&
92
+ KNOWN_SIGNAL_TYPES.has(type.typeName.name)) {
93
+ shouldBeReadonly = true;
94
+ }
95
+ }
96
+ }
97
+ else {
98
+ // There is no type annotation, so try to
99
+ // use the value assigned to the property
100
+ // to determine whether it would be a signal.
101
+ if (node.value?.type === utils_2.AST_NODE_TYPES.CallExpression) {
102
+ let callee = node.value.callee;
103
+ // Some signal-creating functions have a `.required`
104
+ // member. For example, `input.required()`.
105
+ if (callee.type === utils_2.AST_NODE_TYPES.MemberExpression) {
106
+ if (callee.property.type === utils_2.AST_NODE_TYPES.Identifier &&
107
+ callee.property.name !== 'required') {
108
+ return;
109
+ }
110
+ callee = callee.object;
111
+ }
112
+ if (callee.type === utils_2.AST_NODE_TYPES.Identifier &&
113
+ (KNOWN_SIGNAL_CREATION_FUNCTIONS.has(callee.name) ||
114
+ additionalSignalCreationFunctions.includes(callee.name))) {
115
+ shouldBeReadonly = true;
116
+ }
117
+ }
118
+ if (!shouldBeReadonly && useTypeChecking && node.value) {
119
+ services ??= utils_2.ESLintUtils.getParserServices(context);
120
+ const name = services
121
+ .getTypeAtLocation(node.value)
122
+ .getSymbol()?.name;
123
+ shouldBeReadonly =
124
+ name !== undefined && KNOWN_SIGNAL_TYPES.has(name);
125
+ }
126
+ }
127
+ if (shouldBeReadonly) {
128
+ context.report({
129
+ node: node.key,
130
+ messageId: 'preferReadonlySignalProperties',
131
+ suggest: [
132
+ {
133
+ messageId: 'suggestAddReadonlyModifier',
134
+ fix: (fixer) => fixer.insertTextBefore(node.key, 'readonly '),
135
+ },
136
+ ],
137
+ });
138
+ }
139
+ };
140
+ }
141
+ if (preferInputSignals) {
142
+ listener[utils_1.Selectors.INPUT_DECORATOR] = (node) => {
143
+ context.report({
144
+ node,
145
+ messageId: 'preferInputSignals',
146
+ });
147
+ };
148
+ }
149
+ if (preferQuerySignals) {
150
+ listener['Decorator[expression.callee.name=/^(ContentChild|ContentChildren|ViewChild|ViewChildren)$/]'] = (node) => {
151
+ if (node.expression.type === utils_2.AST_NODE_TYPES.CallExpression &&
152
+ node.expression.callee.type === utils_2.AST_NODE_TYPES.Identifier) {
153
+ const decoratorName = node.expression.callee.name;
154
+ context.report({
155
+ node,
156
+ messageId: 'preferQuerySignals',
157
+ data: {
158
+ function: decoratorName.slice(0, 1).toLowerCase() +
159
+ decoratorName.slice(1),
160
+ decorator: decoratorName,
161
+ },
162
+ });
163
+ }
164
+ };
165
+ }
166
+ return listener;
167
+ },
168
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angular-eslint/eslint-plugin",
3
- "version": "19.0.0-alpha.2",
3
+ "version": "19.0.0-alpha.4",
4
4
  "description": "ESLint plugin for Angular applications, following https://angular.dev/style-guide",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -18,11 +18,11 @@
18
18
  "LICENSE"
19
19
  ],
20
20
  "dependencies": {
21
- "@angular-eslint/bundled-angular-compiler": "19.0.0-alpha.2",
22
- "@angular-eslint/utils": "19.0.0-alpha.2"
21
+ "@angular-eslint/bundled-angular-compiler": "19.0.0-alpha.4",
22
+ "@angular-eslint/utils": "19.0.0-alpha.4"
23
23
  },
24
24
  "devDependencies": {
25
- "@angular-eslint/test-utils": "19.0.0-alpha.2"
25
+ "@angular-eslint/test-utils": "19.0.0-alpha.4"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@typescript-eslint/utils": "^7.11.0 || ^8.0.0",