@ethlete/eslint-plugin 0.1.0-beta.5
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/CHANGELOG.md +39 -0
- package/README.md +25 -0
- package/package.json +14 -0
- package/src/configs/recommended.js +403 -0
- package/src/index.js +136 -0
- package/src/rules/angular-decorator-property-order.js +205 -0
- package/src/rules/class-constant-property.js +344 -0
- package/src/rules/class-member-order.js +431 -0
- package/src/rules/enforce-routing-view-naming.js +84 -0
- package/src/rules/guard-return-newline.js +81 -0
- package/src/rules/inject-member-accessibility.js +93 -0
- package/src/rules/internals/angular-member-visibility.js +233 -0
- package/src/rules/internals/implemented-contract-members.js +294 -0
- package/src/rules/no-angular-router-api.js +158 -0
- package/src/rules/no-angular-seo-services.js +82 -0
- package/src/rules/no-direct-dom-manipulation.js +160 -0
- package/src/rules/no-document-cookie.js +55 -0
- package/src/rules/no-dom-query.js +81 -0
- package/src/rules/no-empty-angular-metadata-arrays.js +104 -0
- package/src/rules/no-empty-newlines-between-imports.js +45 -0
- package/src/rules/no-inject-chain.js +44 -0
- package/src/rules/no-leading-underscore-class-member.js +120 -0
- package/src/rules/no-legacy-angular-decorators.js +405 -0
- package/src/rules/no-locale-id.js +57 -0
- package/src/rules/no-member-alias.js +151 -0
- package/src/rules/no-native-observers.js +102 -0
- package/src/rules/no-pipe-logic.js +76 -0
- package/src/rules/no-public-property.js +85 -0
- package/src/rules/no-readonly-signal.js +119 -0
- package/src/rules/no-redundant-internal.js +94 -0
- package/src/rules/no-rxjs-in-effect.js +88 -0
- package/src/rules/no-screaming-case-local.js +113 -0
- package/src/rules/no-standalone-flag.js +95 -0
- package/src/rules/no-subscribe-in-pipe.js +89 -0
- package/src/rules/no-subscribe-with-body.js +95 -0
- package/src/rules/no-trivial-return-type.js +104 -0
- package/src/rules/no-trivial-wrapper-method.js +120 -0
- package/src/rules/no-type-only-import.js +79 -0
- package/src/rules/no-typed-injected-element-ref.js +88 -0
- package/src/rules/no-unused-class-member.js +209 -0
- package/src/rules/no-window-location.js +109 -0
- package/src/rules/prefer-clone-equal.js +112 -0
- package/src/rules/prefer-concise-angular-host-directives.js +225 -0
- package/src/rules/prefer-concise-angular-style-metadata.js +108 -0
- package/src/rules/prefer-element-dimensions.js +128 -0
- package/src/rules/prefer-linked-signal.js +124 -0
- package/src/rules/prefer-match-media.js +101 -0
- package/src/rules/prefer-rxjs-timer.js +82 -0
- package/src/rules/prefer-scroll-state.js +158 -0
- package/src/rules/prefer-viewport-size.js +75 -0
- package/src/rules/require-dollar-suffix.js +123 -0
- package/src/rules/require-on-push-change-detection.js +271 -0
- package/src/rules/require-view-encapsulation-none.js +264 -0
- package/src/rules/template-member-accessibility.js +190 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Disallows trivial wrapper methods that do nothing but forward all their
|
|
6
|
+
* arguments to another call.
|
|
7
|
+
*
|
|
8
|
+
* These methods add an extra layer of indirection without adding any value.
|
|
9
|
+
* Call sites should invoke the target directly.
|
|
10
|
+
*
|
|
11
|
+
* BAD:
|
|
12
|
+
* setValue(val: string) { this.value.set(val); } // just delegates
|
|
13
|
+
* getItem(key: string) { return this.store.get(key); } // just delegates
|
|
14
|
+
* doThing(a: string, b: number) { this.impl.doThing(a, b); }
|
|
15
|
+
*
|
|
16
|
+
* GOOD:
|
|
17
|
+
* // Call this.value.set(val) directly at the call sites.
|
|
18
|
+
* // If the method wraps something non-trivially (transforms args, adds logic), it is fine.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Returns true when the param is a plain identifier (no default, no destructuring, no rest).
|
|
23
|
+
* @param {import('@typescript-eslint/types').TSESTree.Parameter} param
|
|
24
|
+
*/
|
|
25
|
+
const isSimpleParam = (param) => param.type === 'Identifier';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Returns the single CallExpression in the method body when the method is a
|
|
29
|
+
* trivial all-args-forwarding wrapper, or null otherwise.
|
|
30
|
+
*
|
|
31
|
+
* Matches both void wrappers:
|
|
32
|
+
* setValue(val) { this.value.set(val); }
|
|
33
|
+
* and return wrappers:
|
|
34
|
+
* get(key) { return this.store.get(key); }
|
|
35
|
+
*
|
|
36
|
+
* @param {import('eslint').Rule.Node} methodNode MethodDefinition node
|
|
37
|
+
* @returns {import('eslint').Rule.Node | null}
|
|
38
|
+
*/
|
|
39
|
+
const getTrivialCallExpr = (methodNode) => {
|
|
40
|
+
const fnValue = methodNode.value;
|
|
41
|
+
if (!fnValue) return null;
|
|
42
|
+
|
|
43
|
+
const params = fnValue.params;
|
|
44
|
+
// Require at least one parameter — zero-param delegate methods are deliberate API surface.
|
|
45
|
+
if (params.length === 0) return null;
|
|
46
|
+
// All params must be simple identifiers (no destructuring, defaults, or rest).
|
|
47
|
+
if (!params.every(isSimpleParam)) return null;
|
|
48
|
+
|
|
49
|
+
const body = fnValue.body;
|
|
50
|
+
if (!body || body.type !== 'BlockStatement') return null;
|
|
51
|
+
if (body.body.length !== 1) return null;
|
|
52
|
+
|
|
53
|
+
const stmt = body.body[0];
|
|
54
|
+
|
|
55
|
+
let callExpr = null;
|
|
56
|
+
if (stmt.type === 'ExpressionStatement') {
|
|
57
|
+
callExpr = stmt.expression;
|
|
58
|
+
} else if (stmt.type === 'ReturnStatement' && stmt.argument) {
|
|
59
|
+
callExpr = stmt.argument;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!callExpr || callExpr.type !== 'CallExpression') return null;
|
|
63
|
+
if (callExpr.arguments.length !== params.length) return null;
|
|
64
|
+
|
|
65
|
+
// All arguments must be the method's own param names in the same order.
|
|
66
|
+
const allMatch = params.every((param, i) => {
|
|
67
|
+
const arg = callExpr.arguments[i];
|
|
68
|
+
return arg.type === 'Identifier' && arg.name === param.name;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return allMatch ? callExpr : null;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
75
|
+
const noTrivialWrapperMethod = {
|
|
76
|
+
meta: {
|
|
77
|
+
type: 'suggestion',
|
|
78
|
+
docs: {
|
|
79
|
+
description: 'Disallow trivial wrapper methods that only forward all arguments to another call.',
|
|
80
|
+
recommended: true,
|
|
81
|
+
},
|
|
82
|
+
messages: {
|
|
83
|
+
noTrivialWrapperMethod:
|
|
84
|
+
"'{{name}}' is a trivial wrapper — it only forwards all its arguments to '{{target}}'. Remove it and call '{{target}}' directly at each call site.",
|
|
85
|
+
},
|
|
86
|
+
schema: [],
|
|
87
|
+
},
|
|
88
|
+
create(context) {
|
|
89
|
+
return {
|
|
90
|
+
MethodDefinition(node) {
|
|
91
|
+
// Skip constructors, getters, and setters — they commonly delegate for good reasons.
|
|
92
|
+
if (node.kind === 'constructor' || node.kind === 'get' || node.kind === 'set') return;
|
|
93
|
+
|
|
94
|
+
const callExpr = getTrivialCallExpr(node);
|
|
95
|
+
if (!callExpr) return;
|
|
96
|
+
|
|
97
|
+
const methodName = node.key.type === 'Identifier' ? node.key.name : null;
|
|
98
|
+
if (!methodName) return;
|
|
99
|
+
|
|
100
|
+
const callee = callExpr.callee;
|
|
101
|
+
const targetName =
|
|
102
|
+
callee.type === 'MemberExpression'
|
|
103
|
+
? context.sourceCode.getText(callee)
|
|
104
|
+
: callee.type === 'Identifier'
|
|
105
|
+
? callee.name
|
|
106
|
+
: null;
|
|
107
|
+
|
|
108
|
+
if (!targetName) return;
|
|
109
|
+
|
|
110
|
+
context.report({
|
|
111
|
+
node,
|
|
112
|
+
messageId: 'noTrivialWrapperMethod',
|
|
113
|
+
data: { name: methodName, target: targetName },
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
module.exports = noTrivialWrapperMethod;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
5
|
+
const noTypeOnlyImport = {
|
|
6
|
+
meta: {
|
|
7
|
+
type: 'suggestion',
|
|
8
|
+
docs: {
|
|
9
|
+
description: 'Disallow `import type { Foo }` and `import { type Foo }` — use a regular value import instead.',
|
|
10
|
+
recommended: true,
|
|
11
|
+
},
|
|
12
|
+
fixable: 'code',
|
|
13
|
+
messages: {
|
|
14
|
+
noTypeImportDeclaration:
|
|
15
|
+
"Do not use `import type`. Use a regular import instead: `import { {{specifiers}} } from '{{source}}';`.",
|
|
16
|
+
noInlineTypeSpecifier:
|
|
17
|
+
"Do not use inline `type` on import specifiers. Use a regular import instead: `import { {{specifiers}} } from '{{source}}';`.",
|
|
18
|
+
},
|
|
19
|
+
schema: [],
|
|
20
|
+
},
|
|
21
|
+
create(context) {
|
|
22
|
+
return {
|
|
23
|
+
ImportDeclaration(node) {
|
|
24
|
+
const decl = /** @type {any} */ (node);
|
|
25
|
+
if (decl.importKind === 'type') {
|
|
26
|
+
const source = /** @type {string} */ (node.source.value);
|
|
27
|
+
const specifiers = node.specifiers.map((s) => s.local.name).join(', ');
|
|
28
|
+
|
|
29
|
+
context.report({
|
|
30
|
+
node,
|
|
31
|
+
messageId: 'noTypeImportDeclaration',
|
|
32
|
+
data: { specifiers, source },
|
|
33
|
+
fix(fixer) {
|
|
34
|
+
const specifierText = node.specifiers.map((s) => s.local.name).join(', ');
|
|
35
|
+
return fixer.replaceText(node, `import { ${specifierText} } from '${source}';`);
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const specifier of node.specifiers) {
|
|
43
|
+
const spec = /** @type {any} */ (specifier);
|
|
44
|
+
if (specifier.type === 'ImportSpecifier' && spec.importKind === 'type') {
|
|
45
|
+
const source = /** @type {string} */ (node.source.value);
|
|
46
|
+
const allSpecifiers = node.specifiers
|
|
47
|
+
.map((s) => {
|
|
48
|
+
if (s.type === 'ImportSpecifier') {
|
|
49
|
+
const imported = s.imported.type === 'Identifier' ? s.imported.name : s.imported.value;
|
|
50
|
+
return s.local.name === imported ? imported : `${imported} as ${s.local.name}`;
|
|
51
|
+
}
|
|
52
|
+
return s.local.name;
|
|
53
|
+
})
|
|
54
|
+
.join(', ');
|
|
55
|
+
|
|
56
|
+
context.report({
|
|
57
|
+
node: specifier,
|
|
58
|
+
messageId: 'noInlineTypeSpecifier',
|
|
59
|
+
data: { specifiers: allSpecifiers, source },
|
|
60
|
+
fix(fixer) {
|
|
61
|
+
const sourceCode = context.sourceCode;
|
|
62
|
+
const tokenBefore = sourceCode.getTokenBefore(specifier.imported ?? specifier.local);
|
|
63
|
+
if (tokenBefore && tokenBefore.value === 'type') {
|
|
64
|
+
const tokenAfter = sourceCode.getTokenAfter(tokenBefore);
|
|
65
|
+
if (tokenAfter) {
|
|
66
|
+
return fixer.removeRange([tokenBefore.range[0], tokenAfter.range[0]]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
module.exports = noTypeOnlyImport;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
5
|
+
const noTypedInjectedElementRef = {
|
|
6
|
+
meta: {
|
|
7
|
+
type: 'suggestion',
|
|
8
|
+
docs: {
|
|
9
|
+
description:
|
|
10
|
+
'Require that inject(ElementRef) uses a generic type parameter on inject() itself, not on ElementRef.',
|
|
11
|
+
recommended: true,
|
|
12
|
+
},
|
|
13
|
+
messages: {
|
|
14
|
+
missingGeneric:
|
|
15
|
+
'Provide a generic type on inject() itself: `inject<ElementRef<HTMLElement>>(ElementRef)`. Passing a type argument on ElementRef directly is also disallowed.',
|
|
16
|
+
},
|
|
17
|
+
schema: [],
|
|
18
|
+
fixable: 'code',
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
return {
|
|
22
|
+
CallExpression(node) {
|
|
23
|
+
if (node.callee.type !== 'Identifier' || node.callee.name !== 'inject') {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const firstArg = node.arguments[0];
|
|
28
|
+
if (!firstArg) return;
|
|
29
|
+
|
|
30
|
+
const isPlainElementRef = firstArg.type === 'Identifier' && firstArg.name === 'ElementRef';
|
|
31
|
+
const isInstantiatedElementRef =
|
|
32
|
+
firstArg.type === 'TSInstantiationExpression' &&
|
|
33
|
+
firstArg.expression &&
|
|
34
|
+
firstArg.expression.type === 'Identifier' &&
|
|
35
|
+
firstArg.expression.name === 'ElementRef';
|
|
36
|
+
|
|
37
|
+
if (!isPlainElementRef && !isInstantiatedElementRef) return;
|
|
38
|
+
|
|
39
|
+
const src = context.sourceCode;
|
|
40
|
+
const existingTypeParams = node.typeParameters || node.typeArguments;
|
|
41
|
+
const hasInjectTypeParam =
|
|
42
|
+
existingTypeParams && existingTypeParams.params && existingTypeParams.params.length > 0;
|
|
43
|
+
|
|
44
|
+
if (isPlainElementRef && !hasInjectTypeParam) {
|
|
45
|
+
// inject(ElementRef) → inject<ElementRef<HTMLElement>>(ElementRef)
|
|
46
|
+
context.report({
|
|
47
|
+
node,
|
|
48
|
+
messageId: 'missingGeneric',
|
|
49
|
+
fix(fixer) {
|
|
50
|
+
return fixer.insertTextAfter(node.callee, '<ElementRef<HTMLElement>>');
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (isInstantiatedElementRef) {
|
|
57
|
+
// Capture the type args text from the token, e.g. "<HTMLElement>"
|
|
58
|
+
const tokenTypeParamText = src.getText(firstArg.typeParameters || firstArg.typeArguments);
|
|
59
|
+
|
|
60
|
+
if (!hasInjectTypeParam) {
|
|
61
|
+
// inject(ElementRef<HTMLElement>) → inject<ElementRef<HTMLElement>>(ElementRef)
|
|
62
|
+
context.report({
|
|
63
|
+
node,
|
|
64
|
+
messageId: 'missingGeneric',
|
|
65
|
+
fix(fixer) {
|
|
66
|
+
return [
|
|
67
|
+
fixer.insertTextAfter(node.callee, `<ElementRef${tokenTypeParamText}>`),
|
|
68
|
+
fixer.replaceText(firstArg, 'ElementRef'),
|
|
69
|
+
];
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
} else {
|
|
73
|
+
// inject<X>(ElementRef<Y>) — token must not carry type args; keep inject's type param
|
|
74
|
+
context.report({
|
|
75
|
+
node,
|
|
76
|
+
messageId: 'missingGeneric',
|
|
77
|
+
fix(fixer) {
|
|
78
|
+
return fixer.replaceText(firstArg, 'ElementRef');
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
module.exports = noTypedInjectedElementRef;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Disallows class members that are declared but never read within the class body.
|
|
6
|
+
*
|
|
7
|
+
* Only checks members that CANNOT be accessed from outside the class at compile
|
|
8
|
+
* time — so false positives (e.g. template usage) are avoided:
|
|
9
|
+
*
|
|
10
|
+
* - `private` members — always checked. Private members are only accessible
|
|
11
|
+
* within the class body, so if they are never read there they are dead code.
|
|
12
|
+
*
|
|
13
|
+
* - `protected` members — checked only when the class has an Angular decorator
|
|
14
|
+
* other than `@Component` (i.e. `@Directive`, `@Pipe`, `@Injectable`).
|
|
15
|
+
* For `@Component`, protected members may be read in the HTML template file
|
|
16
|
+
* which is invisible to static analysis, so protected is skipped there.
|
|
17
|
+
* For plain (non-Angular) classes, protected members may be used in a
|
|
18
|
+
* subclass in another file, so they are also skipped.
|
|
19
|
+
*
|
|
20
|
+
* Members referenced in `host:` binding expressions in the class decorator are
|
|
21
|
+
* excluded from the check for `protected` (since they are accessed by Angular
|
|
22
|
+
* at runtime via the host binding mechanism).
|
|
23
|
+
*
|
|
24
|
+
* BAD:
|
|
25
|
+
* private document = inject(DOCUMENT); // fetched but never used
|
|
26
|
+
* private unusedFlag = false;
|
|
27
|
+
*
|
|
28
|
+
* GOOD:
|
|
29
|
+
* private document = inject(DOCUMENT);
|
|
30
|
+
* createElement() { return this.document.createElement('div'); }
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
const ANGULAR_DECORATORS = new Set(['Component', 'Directive', 'Pipe', 'Injectable']);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Extract all identifier-like tokens from a template expression string.
|
|
37
|
+
* Used for `host:` binding values like `'myMember ? "on" : "off"'`.
|
|
38
|
+
* @param {string} str
|
|
39
|
+
* @returns {string[]}
|
|
40
|
+
*/
|
|
41
|
+
const extractIdentifiers = (str) => str.match(/\b[a-zA-Z_$][a-zA-Z0-9_$]*\b/g) ?? [];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns info about the Angular decorators applied to a class node.
|
|
45
|
+
* @param {any} classNode
|
|
46
|
+
* @returns {{ isComponent: boolean, isAngularClass: boolean }}
|
|
47
|
+
*/
|
|
48
|
+
const getDecoratorInfo = (classNode) => {
|
|
49
|
+
const decorators = classNode.decorators ?? [];
|
|
50
|
+
let isComponent = false;
|
|
51
|
+
let isAngularClass = false;
|
|
52
|
+
|
|
53
|
+
for (const dec of decorators) {
|
|
54
|
+
const expr = dec.expression;
|
|
55
|
+
const name =
|
|
56
|
+
expr?.type === 'CallExpression' && expr.callee?.type === 'Identifier'
|
|
57
|
+
? expr.callee.name
|
|
58
|
+
: expr?.type === 'Identifier'
|
|
59
|
+
? expr.name
|
|
60
|
+
: null;
|
|
61
|
+
|
|
62
|
+
if (name && ANGULAR_DECORATORS.has(name)) {
|
|
63
|
+
isAngularClass = true;
|
|
64
|
+
if (name === 'Component') isComponent = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { isComponent, isAngularClass };
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns names of class members referenced in `host:` binding value strings
|
|
73
|
+
* found in any decorator argument on the class.
|
|
74
|
+
* @param {any} classNode
|
|
75
|
+
* @returns {Set<string>}
|
|
76
|
+
*/
|
|
77
|
+
const getHostBoundMembers = (classNode) => {
|
|
78
|
+
/** @type {Set<string>} */
|
|
79
|
+
const result = new Set();
|
|
80
|
+
const decorators = classNode.decorators ?? [];
|
|
81
|
+
|
|
82
|
+
for (const dec of decorators) {
|
|
83
|
+
const expr = dec.expression;
|
|
84
|
+
if (expr?.type !== 'CallExpression') continue;
|
|
85
|
+
|
|
86
|
+
const arg = expr.arguments?.[0];
|
|
87
|
+
if (!arg || arg.type !== 'ObjectExpression') continue;
|
|
88
|
+
|
|
89
|
+
const hostProp = arg.properties.find(
|
|
90
|
+
/** @param {any} p */
|
|
91
|
+
(p) => p.type === 'Property' && (p.key?.name === 'host' || p.key?.value === 'host'),
|
|
92
|
+
);
|
|
93
|
+
if (!hostProp || hostProp.value?.type !== 'ObjectExpression') continue;
|
|
94
|
+
|
|
95
|
+
for (const binding of hostProp.value.properties) {
|
|
96
|
+
if (binding.type !== 'Property') continue;
|
|
97
|
+
/** @type {any} */
|
|
98
|
+
const val = binding.value;
|
|
99
|
+
if (val?.type !== 'Literal' || typeof val.value !== 'string') continue;
|
|
100
|
+
for (const id of extractIdentifiers(val.value)) result.add(id);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return result;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
108
|
+
const noUnusedClassMember = {
|
|
109
|
+
meta: {
|
|
110
|
+
type: 'suggestion',
|
|
111
|
+
docs: {
|
|
112
|
+
description: 'Disallow class members that are declared but never read within the class body.',
|
|
113
|
+
recommended: true,
|
|
114
|
+
},
|
|
115
|
+
messages: {
|
|
116
|
+
noUnused:
|
|
117
|
+
"'{{name}}' is declared but its value is never read within this class. " +
|
|
118
|
+
'Remove it, or widen its accessibility if it is intentionally used externally.',
|
|
119
|
+
},
|
|
120
|
+
schema: [],
|
|
121
|
+
},
|
|
122
|
+
create(context) {
|
|
123
|
+
/**
|
|
124
|
+
* Stack of class frames. Each frame tracks which members are candidates and
|
|
125
|
+
* which have been seen in a `this.x` expression.
|
|
126
|
+
*
|
|
127
|
+
* @type {Array<{
|
|
128
|
+
* classBody: import('eslint').Rule.Node,
|
|
129
|
+
* isComponent: boolean,
|
|
130
|
+
* isAngularClass: boolean,
|
|
131
|
+
* hostBound: Set<string>,
|
|
132
|
+
* candidates: Map<string, import('eslint').Rule.Node>,
|
|
133
|
+
* usedNames: Set<string>,
|
|
134
|
+
* }>}
|
|
135
|
+
*/
|
|
136
|
+
const classStack = [];
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @param {any} node
|
|
140
|
+
*/
|
|
141
|
+
const registerMember = (node) => {
|
|
142
|
+
const frame = classStack[classStack.length - 1];
|
|
143
|
+
if (!frame) return;
|
|
144
|
+
// Only register members that belong directly to this class body
|
|
145
|
+
if (node.parent !== frame.classBody) return;
|
|
146
|
+
|
|
147
|
+
if (node.static) return;
|
|
148
|
+
|
|
149
|
+
const key = node.key;
|
|
150
|
+
if (!key || key.type !== 'Identifier') return;
|
|
151
|
+
if (node.type === 'MethodDefinition' && node.kind === 'constructor') return;
|
|
152
|
+
|
|
153
|
+
const name = key.name;
|
|
154
|
+
const accessibility = node.accessibility;
|
|
155
|
+
const { isComponent, isAngularClass, hostBound } = frame;
|
|
156
|
+
|
|
157
|
+
if (accessibility === 'private') {
|
|
158
|
+
frame.candidates.set(name, node);
|
|
159
|
+
} else if (accessibility === 'protected' && isAngularClass && !isComponent) {
|
|
160
|
+
if (!hostBound.has(name)) frame.candidates.set(name, node);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
ClassBody(node) {
|
|
166
|
+
const classNode = node.parent;
|
|
167
|
+
const { isComponent, isAngularClass } = getDecoratorInfo(classNode);
|
|
168
|
+
const hostBound = getHostBoundMembers(classNode);
|
|
169
|
+
classStack.push({
|
|
170
|
+
classBody: node,
|
|
171
|
+
isComponent,
|
|
172
|
+
isAngularClass,
|
|
173
|
+
hostBound,
|
|
174
|
+
candidates: new Map(),
|
|
175
|
+
usedNames: new Set(),
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
'ClassBody:exit'() {
|
|
180
|
+
const frame = classStack.pop();
|
|
181
|
+
if (!frame) return;
|
|
182
|
+
for (const [name, memberNode] of frame.candidates) {
|
|
183
|
+
if (!frame.usedNames.has(name)) {
|
|
184
|
+
context.report({ node: memberNode, messageId: 'noUnused', data: { name } });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
PropertyDefinition: registerMember,
|
|
190
|
+
MethodDefinition: registerMember,
|
|
191
|
+
|
|
192
|
+
MemberExpression(node) {
|
|
193
|
+
// Only care about `this.name` (non-computed direct property access)
|
|
194
|
+
if (node.computed) return;
|
|
195
|
+
if (node.object?.type !== 'ThisExpression') return;
|
|
196
|
+
if (node.property?.type !== 'Identifier') return;
|
|
197
|
+
|
|
198
|
+
// Mark used in the innermost class frame. Arrow functions don't push a
|
|
199
|
+
// new frame so they correctly share the enclosing class's `this`.
|
|
200
|
+
// Regular nested classes push a new frame so their `this.x` won't
|
|
201
|
+
// accidentally mark an outer class member as used.
|
|
202
|
+
const frame = classStack[classStack.length - 1];
|
|
203
|
+
if (frame) frame.usedNames.add(node.property.name);
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
module.exports = noUnusedClassMember;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Disallows reading URL state from window.location properties.
|
|
6
|
+
*
|
|
7
|
+
* window.location reads are:
|
|
8
|
+
* - Not reactive — they return a stale snapshot at call time
|
|
9
|
+
* - Not SSR-safe — window is undefined during server-side rendering
|
|
10
|
+
*
|
|
11
|
+
* @ethlete/core ships reactive signal utilities that replace all URL-reading use cases.
|
|
12
|
+
*
|
|
13
|
+
* BAD:
|
|
14
|
+
* window.location.pathname → injectRoute()
|
|
15
|
+
* window.location.href → injectUrl()
|
|
16
|
+
* window.location.search → injectQueryParams() / injectQueryParam(key)
|
|
17
|
+
* window.location.hash → injectFragment()
|
|
18
|
+
* new URLSearchParams(window.location.search) → injectQueryParams() / injectQueryParam(key)
|
|
19
|
+
*
|
|
20
|
+
* GOOD:
|
|
21
|
+
* import {
|
|
22
|
+
* injectUrl, injectRoute, injectQueryParam, injectQueryParams, injectFragment
|
|
23
|
+
* } from '@ethlete/core';
|
|
24
|
+
*
|
|
25
|
+
* NOTE: window.location.href = url (assignment/navigation) and
|
|
26
|
+
* window.location.hostname / window.location.origin are not flagged.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** URL state properties that have reactive signal equivalents. */
|
|
30
|
+
const LOCATION_STATE_PROPS = new Map([
|
|
31
|
+
['href', "injectUrl() from '@ethlete/core'"],
|
|
32
|
+
['pathname', "injectRoute() from '@ethlete/core'"],
|
|
33
|
+
['search', "injectQueryParams() / injectQueryParam(key) from '@ethlete/core'"],
|
|
34
|
+
['hash', "injectFragment() from '@ethlete/core'"],
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
38
|
+
const noWindowLocation = {
|
|
39
|
+
meta: {
|
|
40
|
+
type: 'suggestion',
|
|
41
|
+
docs: {
|
|
42
|
+
description:
|
|
43
|
+
"Disallow reading URL state from 'window.location.*'. Use reactive signal utilities from '@ethlete/core' instead.",
|
|
44
|
+
recommended: true,
|
|
45
|
+
},
|
|
46
|
+
messages: {
|
|
47
|
+
noWindowLocation:
|
|
48
|
+
"Avoid reading 'window.location.{{prop}}'. Use {{replacement}} instead — it returns a reactive signal that is SSR-safe and updates on navigation.",
|
|
49
|
+
noURLSearchParams:
|
|
50
|
+
"Avoid 'new URLSearchParams(window.location.search)'. Use 'injectQueryParam(key)' or 'injectQueryParams()' from '@ethlete/core' instead — they return reactive signals and are SSR-safe.",
|
|
51
|
+
},
|
|
52
|
+
schema: [],
|
|
53
|
+
},
|
|
54
|
+
create(context) {
|
|
55
|
+
return {
|
|
56
|
+
// ── window.location.{prop} reads ────────────────────────────────────────
|
|
57
|
+
MemberExpression(node) {
|
|
58
|
+
if (node.property.type !== 'Identifier') return;
|
|
59
|
+
const prop = node.property.name;
|
|
60
|
+
|
|
61
|
+
if (!LOCATION_STATE_PROPS.has(prop)) return;
|
|
62
|
+
|
|
63
|
+
// Must be window.location.{prop} — check the object is window.location
|
|
64
|
+
const parent = node.object;
|
|
65
|
+
if (
|
|
66
|
+
parent.type !== 'MemberExpression' ||
|
|
67
|
+
parent.object.type !== 'Identifier' ||
|
|
68
|
+
parent.object.name !== 'window' ||
|
|
69
|
+
parent.property.type !== 'Identifier' ||
|
|
70
|
+
parent.property.name !== 'location'
|
|
71
|
+
) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Skip assignments TO window.location.href (navigation redirect)
|
|
76
|
+
if (prop === 'href' && node.parent.type === 'AssignmentExpression' && node.parent.left === node) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
context.report({
|
|
81
|
+
node,
|
|
82
|
+
messageId: 'noWindowLocation',
|
|
83
|
+
data: { prop, replacement: LOCATION_STATE_PROPS.get(prop) },
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
// ── new URLSearchParams(window.location.search) ─────────────────────────
|
|
88
|
+
NewExpression(node) {
|
|
89
|
+
if (node.callee.type !== 'Identifier' || node.callee.name !== 'URLSearchParams') return;
|
|
90
|
+
const arg = node.arguments[0];
|
|
91
|
+
if (!arg) return;
|
|
92
|
+
if (
|
|
93
|
+
arg.type === 'MemberExpression' &&
|
|
94
|
+
arg.object.type === 'MemberExpression' &&
|
|
95
|
+
arg.object.object.type === 'Identifier' &&
|
|
96
|
+
arg.object.object.name === 'window' &&
|
|
97
|
+
arg.object.property.type === 'Identifier' &&
|
|
98
|
+
arg.object.property.name === 'location' &&
|
|
99
|
+
arg.property.type === 'Identifier' &&
|
|
100
|
+
arg.property.name === 'search'
|
|
101
|
+
) {
|
|
102
|
+
context.report({ node, messageId: 'noURLSearchParams' });
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
module.exports = noWindowLocation;
|