@angular-eslint/eslint-plugin 22.0.1-alpha.16 → 22.0.1-alpha.18
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/README.md +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/rules/inject-at-top.d.ts +11 -0
- package/dist/rules/inject-at-top.d.ts.map +1 -0
- package/dist/rules/inject-at-top.js +76 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@ The premade flat configs that combine these rules are provided by the umbrella [
|
|
|
23
23
|
| --- | --- | --- | --- | --- |
|
|
24
24
|
| [`computed-must-return`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/computed-must-return.md) | Ensures that computed() returns a value. Omitting the value is likely a mistake. | | | |
|
|
25
25
|
| [`contextual-lifecycle`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/contextual-lifecycle.md) | Ensures that lifecycle methods are used in a correct context | :white_check_mark: | | |
|
|
26
|
+
| [`inject-at-top`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/inject-at-top.md) | Requires inject() calls to be declared at the top of the class, before any other member | | | |
|
|
26
27
|
| [`no-async-lifecycle-method`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/no-async-lifecycle-method.md) | Angular Lifecycle methods should not be async. Angular does not wait for async lifecycle but the code incorrectly suggests it does. | | | |
|
|
27
28
|
| [`no-attribute-decorator`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/no-attribute-decorator.md) | The @Attribute decorator is used to obtain a single value for an attribute. This is a much less common use case than getting a stream of values (using @Input), so the @Attribute decorator is often mistakenly used when @Input is intended. This rule disallows the usage of @Attribute decorator entirely to prevent these mistakes. | | | |
|
|
28
29
|
| [`no-developer-preview`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/no-developer-preview.md) | Disallow using code which is marked as developer preview | | | |
|
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,9 @@ 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", [], import("./utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
31
|
+
name: string;
|
|
32
|
+
};
|
|
30
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> & {
|
|
31
34
|
name: string;
|
|
32
35
|
};
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuJA,kBAsDE"}
|
package/dist/index.js
CHANGED
|
@@ -81,6 +81,7 @@ const no_experimental_1 = __importStar(require("./rules/no-experimental"));
|
|
|
81
81
|
const no_developer_preview_1 = __importStar(require("./rules/no-developer-preview"));
|
|
82
82
|
const no_implicit_take_until_destroyed_1 = __importStar(require("./rules/no-implicit-take-until-destroyed"));
|
|
83
83
|
const computed_must_return_1 = __importStar(require("./rules/computed-must-return"));
|
|
84
|
+
const inject_at_top_1 = __importStar(require("./rules/inject-at-top"));
|
|
84
85
|
module.exports = {
|
|
85
86
|
rules: {
|
|
86
87
|
[component_class_suffix_1.RULE_NAME]: component_class_suffix_1.default,
|
|
@@ -92,6 +93,7 @@ module.exports = {
|
|
|
92
93
|
[contextual_lifecycle_1.RULE_NAME]: contextual_lifecycle_1.default,
|
|
93
94
|
[directive_class_suffix_1.RULE_NAME]: directive_class_suffix_1.default,
|
|
94
95
|
[directive_selector_1.RULE_NAME]: directive_selector_1.default,
|
|
96
|
+
[inject_at_top_1.RULE_NAME]: inject_at_top_1.default,
|
|
95
97
|
[no_async_lifecycle_method_1.RULE_NAME]: no_async_lifecycle_method_1.default,
|
|
96
98
|
[no_attribute_decorator_1.RULE_NAME]: no_attribute_decorator_1.default,
|
|
97
99
|
[no_developer_preview_1.RULE_NAME]: no_developer_preview_1.default,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type Options = [];
|
|
2
|
+
export type MessageIds = 'injectAtTop';
|
|
3
|
+
export declare const RULE_NAME = "inject-at-top";
|
|
4
|
+
declare const _default: import("@typescript-eslint/utils/ts-eslint").RuleModule<"injectAtTop", [], import("../utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
export default _default;
|
|
8
|
+
export declare const RULE_DOCS_EXTENSION: {
|
|
9
|
+
rationale: string;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=inject-at-top.d.ts.map
|
|
@@ -0,0 +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;AAEzB,MAAM,MAAM,UAAU,GAAG,aAAa,CAAC;AACvC,eAAO,MAAM,SAAS,kBAAkB,CAAC;;;;AAEzC,wBAyFG;AAEH,eAAO,MAAM,mBAAmB;;CAG/B,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RULE_DOCS_EXTENSION = 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
|
+
exports.RULE_NAME = 'inject-at-top';
|
|
8
|
+
exports.default = (0, create_eslint_rule_1.createESLintRule)({
|
|
9
|
+
name: exports.RULE_NAME,
|
|
10
|
+
meta: {
|
|
11
|
+
type: 'problem',
|
|
12
|
+
docs: {
|
|
13
|
+
description: 'Requires inject() calls to be declared at the top of the class, before any other member',
|
|
14
|
+
},
|
|
15
|
+
schema: [],
|
|
16
|
+
messages: {
|
|
17
|
+
injectAtTop: 'Move this inject() 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
|
+
},
|
|
19
|
+
},
|
|
20
|
+
defaultOptions: [],
|
|
21
|
+
create(context) {
|
|
22
|
+
const angularDecoratorsPattern = (0, utils_1.toPattern)([
|
|
23
|
+
'Component',
|
|
24
|
+
'Directive',
|
|
25
|
+
'Injectable',
|
|
26
|
+
'Pipe',
|
|
27
|
+
'Service',
|
|
28
|
+
]);
|
|
29
|
+
function hasEagerInject(node) {
|
|
30
|
+
if (!node) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (node.type === utils_2.AST_NODE_TYPES.CallExpression &&
|
|
34
|
+
node.callee.type === utils_2.AST_NODE_TYPES.Identifier &&
|
|
35
|
+
node.callee.name === 'inject') {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
const isFn = node.type === utils_2.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
39
|
+
node.type === utils_2.AST_NODE_TYPES.FunctionExpression;
|
|
40
|
+
const isIIFE = isFn &&
|
|
41
|
+
node.parent?.type === utils_2.AST_NODE_TYPES.CallExpression &&
|
|
42
|
+
node.parent.callee === node;
|
|
43
|
+
if (node.type === utils_2.AST_NODE_TYPES.ClassExpression || (isFn && !isIIFE)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return Object.values(node)
|
|
47
|
+
.flat()
|
|
48
|
+
.some((child) => child &&
|
|
49
|
+
typeof child === 'object' &&
|
|
50
|
+
'type' in child &&
|
|
51
|
+
child !== node.parent &&
|
|
52
|
+
hasEagerInject(child));
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
[`${utils_1.Selectors.decoratorDefinition(angularDecoratorsPattern)} > ClassBody`](node) {
|
|
56
|
+
let seenNonInject = false;
|
|
57
|
+
for (const member of node.body) {
|
|
58
|
+
if ('static' in member && member.static) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (member.type === utils_2.AST_NODE_TYPES.PropertyDefinition &&
|
|
62
|
+
hasEagerInject(member.value)) {
|
|
63
|
+
if (seenNonInject) {
|
|
64
|
+
context.report({ node: member, messageId: 'injectAtTop' });
|
|
65
|
+
}
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
seenNonInject = true;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
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.",
|
|
76
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angular-eslint/eslint-plugin",
|
|
3
|
-
"version": "22.0.1-alpha.
|
|
3
|
+
"version": "22.0.1-alpha.18",
|
|
4
4
|
"description": "ESLint plugin for Angular applications, following https://angular.dev/style-guide",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"ts-api-utils": "^2.1.0",
|
|
23
|
-
"@angular-eslint/bundled-angular-compiler": "22.0.1-alpha.
|
|
24
|
-
"@angular-eslint/utils": "22.0.1-alpha.
|
|
23
|
+
"@angular-eslint/bundled-angular-compiler": "22.0.1-alpha.18",
|
|
24
|
+
"@angular-eslint/utils": "22.0.1-alpha.18"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@angular-eslint/test-utils": "22.0.1-alpha.
|
|
27
|
+
"@angular-eslint/test-utils": "22.0.1-alpha.18"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@typescript-eslint/utils": "^8.0.0",
|