@angular-eslint/eslint-plugin 22.2.1-alpha.11 → 22.2.1-alpha.12
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.
package/dist/index.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ declare const _default: {
|
|
|
27
27
|
"directive-selector": import("@typescript-eslint/utils/ts-eslint").RuleModule<import("./rules/directive-selector").MessageIds, import("./rules/directive-selector").Options, import("./utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
28
28
|
name: string;
|
|
29
29
|
};
|
|
30
|
-
"inject-at-top": import("@typescript-eslint/utils/ts-eslint").RuleModule<"injectAtTop",
|
|
30
|
+
"inject-at-top": import("@typescript-eslint/utils/ts-eslint").RuleModule<"injectAtTop", import("./rules/inject-at-top").Options, import("./utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
31
31
|
name: string;
|
|
32
32
|
};
|
|
33
33
|
"no-async-lifecycle-method": import("@typescript-eslint/utils/ts-eslint").RuleModule<"noAsyncLifecycleMethod", [], import("./utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
export type Options = [
|
|
1
|
+
export type Options = [
|
|
2
|
+
{
|
|
3
|
+
readonly additionalInjectFunctions?: readonly string[];
|
|
4
|
+
}
|
|
5
|
+
];
|
|
2
6
|
export type MessageIds = 'injectAtTop';
|
|
3
7
|
export declare const RULE_NAME = "inject-at-top";
|
|
4
|
-
declare const _default: import("@typescript-eslint/utils/ts-eslint").RuleModule<"injectAtTop",
|
|
8
|
+
declare const _default: import("@typescript-eslint/utils/ts-eslint").RuleModule<"injectAtTop", Options, import("../utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
5
9
|
name: string;
|
|
6
10
|
};
|
|
7
11
|
export default _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-at-top.d.ts","sourceRoot":"","sources":["../../src/rules/inject-at-top.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,OAAO,GAAG,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"inject-at-top.d.ts","sourceRoot":"","sources":["../../src/rules/inject-at-top.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,QAAQ,CAAC,yBAAyB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;KACxD;CACF,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,aAAa,CAAC;AACvC,eAAO,MAAM,SAAS,kBAAkB,CAAC;;;;AAIzC,wBAyHG;AAEH,eAAO,MAAM,mBAAmB;;CAG/B,CAAC"}
|
|
@@ -5,6 +5,7 @@ const utils_1 = require("@angular-eslint/utils");
|
|
|
5
5
|
const utils_2 = require("@typescript-eslint/utils");
|
|
6
6
|
const create_eslint_rule_1 = require("../utils/create-eslint-rule");
|
|
7
7
|
exports.RULE_NAME = 'inject-at-top';
|
|
8
|
+
const DEFAULT_OPTIONS = { additionalInjectFunctions: [] };
|
|
8
9
|
exports.default = (0, create_eslint_rule_1.createESLintRule)({
|
|
9
10
|
name: exports.RULE_NAME,
|
|
10
11
|
meta: {
|
|
@@ -12,13 +13,28 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
|
|
|
12
13
|
docs: {
|
|
13
14
|
description: 'Requires inject() calls to be declared at the top of the class, before any other member',
|
|
14
15
|
},
|
|
15
|
-
schema: [
|
|
16
|
+
schema: [
|
|
17
|
+
{
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
additionalInjectFunctions: {
|
|
21
|
+
type: 'array',
|
|
22
|
+
items: {
|
|
23
|
+
type: 'string',
|
|
24
|
+
},
|
|
25
|
+
description: 'A list of additional functions which perform injection, such as your own `injectTranslations()` helper. Each entry is a regular expression which must match the whole function name, so plain names such as `injectTranslations` work as well as patterns such as `inject[A-Z].*`.',
|
|
26
|
+
uniqueItems: true,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
},
|
|
31
|
+
],
|
|
16
32
|
messages: {
|
|
17
|
-
injectAtTop: 'Move this
|
|
33
|
+
injectAtTop: 'Move this {{functionName}}() to the top of the class. Class fields are initialized in the order they are written, so anything declared above this line cannot safely use the service yet.',
|
|
18
34
|
},
|
|
19
|
-
defaultOptions: [],
|
|
35
|
+
defaultOptions: [DEFAULT_OPTIONS],
|
|
20
36
|
},
|
|
21
|
-
create(context) {
|
|
37
|
+
create(context, [{ additionalInjectFunctions = [] }]) {
|
|
22
38
|
const angularDecoratorsPattern = (0, utils_1.toPattern)([
|
|
23
39
|
'Component',
|
|
24
40
|
'Directive',
|
|
@@ -26,14 +42,18 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
|
|
|
26
42
|
'Pipe',
|
|
27
43
|
'Service',
|
|
28
44
|
]);
|
|
29
|
-
|
|
45
|
+
const injectFunctionsPattern = (0, utils_1.toPattern)([
|
|
46
|
+
'inject',
|
|
47
|
+
...additionalInjectFunctions,
|
|
48
|
+
]);
|
|
49
|
+
function findEagerInjectName(node) {
|
|
30
50
|
if (!node) {
|
|
31
|
-
return
|
|
51
|
+
return null;
|
|
32
52
|
}
|
|
33
53
|
if (node.type === utils_2.AST_NODE_TYPES.CallExpression &&
|
|
34
54
|
node.callee.type === utils_2.AST_NODE_TYPES.Identifier &&
|
|
35
|
-
node.callee.name
|
|
36
|
-
return
|
|
55
|
+
injectFunctionsPattern.test(node.callee.name)) {
|
|
56
|
+
return node.callee.name;
|
|
37
57
|
}
|
|
38
58
|
const isFn = node.type === utils_2.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
39
59
|
node.type === utils_2.AST_NODE_TYPES.FunctionExpression;
|
|
@@ -41,15 +61,20 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
|
|
|
41
61
|
node.parent?.type === utils_2.AST_NODE_TYPES.CallExpression &&
|
|
42
62
|
node.parent.callee === node;
|
|
43
63
|
if (node.type === utils_2.AST_NODE_TYPES.ClassExpression || (isFn && !isIIFE)) {
|
|
44
|
-
return
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
for (const child of Object.values(node).flat()) {
|
|
67
|
+
if (child &&
|
|
68
|
+
typeof child === 'object' &&
|
|
69
|
+
'type' in child &&
|
|
70
|
+
child !== node.parent) {
|
|
71
|
+
const name = findEagerInjectName(child);
|
|
72
|
+
if (name) {
|
|
73
|
+
return name;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
45
76
|
}
|
|
46
|
-
return
|
|
47
|
-
.flat()
|
|
48
|
-
.some((child) => child &&
|
|
49
|
-
typeof child === 'object' &&
|
|
50
|
-
'type' in child &&
|
|
51
|
-
child !== node.parent &&
|
|
52
|
-
hasEagerInject(child));
|
|
77
|
+
return null;
|
|
53
78
|
}
|
|
54
79
|
return {
|
|
55
80
|
[`${utils_1.Selectors.decoratorDefinition(angularDecoratorsPattern)} > ClassBody`](node) {
|
|
@@ -58,12 +83,18 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
|
|
|
58
83
|
if ('static' in member && member.static) {
|
|
59
84
|
continue;
|
|
60
85
|
}
|
|
61
|
-
if (member.type === utils_2.AST_NODE_TYPES.PropertyDefinition
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
86
|
+
if (member.type === utils_2.AST_NODE_TYPES.PropertyDefinition) {
|
|
87
|
+
const functionName = findEagerInjectName(member.value);
|
|
88
|
+
if (functionName) {
|
|
89
|
+
if (seenNonInject) {
|
|
90
|
+
context.report({
|
|
91
|
+
node: member,
|
|
92
|
+
messageId: 'injectAtTop',
|
|
93
|
+
data: { functionName },
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
continue;
|
|
65
97
|
}
|
|
66
|
-
continue;
|
|
67
98
|
}
|
|
68
99
|
seenNonInject = true;
|
|
69
100
|
}
|
|
@@ -72,5 +103,5 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
|
|
|
72
103
|
},
|
|
73
104
|
});
|
|
74
105
|
exports.RULE_DOCS_EXTENSION = {
|
|
75
|
-
rationale: "Class fields are initialized in the order they are declared, so a field that calls inject() only creates its value when the initializer runs. Anything declared above that line which tries to read the injected service will see undefined, and the error message (usually something like 'Cannot read properties of undefined') points at the consumer, not at the field that was initialized out of order. TypeScript catches the obvious shape of this bug when a later-declared field is referenced directly from an earlier initializer, but it does not trace through a getter body or a method call, so the moment the read of this.someService happens behind that indirection the compiler lets it through and the failure only shows up at runtime. The scenario is deceptively easy to introduce: a getter that reads an injected service is defined below the inject() call, someone later adds a small convenience field above the getter that references it, and the class breaks at construction time even though nothing about the change looks suspicious. Keeping every inject() call at the very top of the class removes the ordering concern entirely, because every dependency is guaranteed to exist before any other field, getter, method, or constructor statement runs.",
|
|
106
|
+
rationale: "Class fields are initialized in the order they are declared, so a field that calls inject() only creates its value when the initializer runs. Anything declared above that line which tries to read the injected service will see undefined, and the error message (usually something like 'Cannot read properties of undefined') points at the consumer, not at the field that was initialized out of order. TypeScript catches the obvious shape of this bug when a later-declared field is referenced directly from an earlier initializer, but it does not trace through a getter body or a method call, so the moment the read of this.someService happens behind that indirection the compiler lets it through and the failure only shows up at runtime. The scenario is deceptively easy to introduce: a getter that reads an injected service is defined below the inject() call, someone later adds a small convenience field above the getter that references it, and the class breaks at construction time even though nothing about the change looks suspicious. Keeping every inject() call at the very top of the class removes the ordering concern entirely, because every dependency is guaranteed to exist before any other field, getter, method, or constructor statement runs. Codebases often wrap inject() in their own helpers, for example an injectTranslations() function that injects a translation service and derives a scoped accessor from it; such a helper has exactly the same ordering constraint, so it can be declared through the additionalInjectFunctions option to be treated like inject() itself.",
|
|
76
107
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angular-eslint/eslint-plugin",
|
|
3
|
-
"version": "22.2.1-alpha.
|
|
3
|
+
"version": "22.2.1-alpha.12",
|
|
4
4
|
"description": "ESLint plugin for Angular applications, following https://angular.dev/style-guide",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
"LICENSE"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@angular-eslint/bundled-angular-compiler": "22.2.1-alpha.
|
|
23
|
-
"@angular-eslint/utils": "22.2.1-alpha.
|
|
22
|
+
"@angular-eslint/bundled-angular-compiler": "22.2.1-alpha.12",
|
|
23
|
+
"@angular-eslint/utils": "22.2.1-alpha.12",
|
|
24
24
|
"ts-api-utils": "^2.1.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@angular-eslint/test-utils": "22.2.1-alpha.
|
|
27
|
+
"@angular-eslint/test-utils": "22.2.1-alpha.12"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@typescript-eslint/utils": "^8.0.0",
|