@ethlete/eslint-plugin 0.1.0-next.0 → 0.1.0-next.1

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.
Files changed (41) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/package.json +3 -4
  3. package/src/configs/recommended.js +362 -0
  4. package/src/index.js +116 -0
  5. package/src/rules/angular-decorator-property-order.js +217 -0
  6. package/src/rules/enforce-routing-view-naming.js +84 -0
  7. package/src/rules/guard-return-newline.js +81 -0
  8. package/src/rules/inject-member-accessibility.js +286 -0
  9. package/src/rules/no-angular-router-api.js +158 -0
  10. package/src/rules/no-angular-seo-services.js +82 -0
  11. package/src/rules/no-direct-dom-manipulation.js +160 -0
  12. package/src/rules/no-document-cookie.js +55 -0
  13. package/src/rules/no-dom-query.js +81 -0
  14. package/src/rules/no-inject-chain.js +44 -0
  15. package/src/rules/no-legacy-angular-decorators.js +210 -0
  16. package/src/rules/no-locale-id.js +57 -0
  17. package/src/rules/no-member-alias.js +151 -0
  18. package/src/rules/no-native-observers.js +102 -0
  19. package/src/rules/no-pipe-logic.js +76 -0
  20. package/src/rules/no-public-property.js +39 -0
  21. package/src/rules/no-readonly-signal.js +119 -0
  22. package/src/rules/no-rxjs-in-effect.js +88 -0
  23. package/src/rules/no-screaming-case-local.js +113 -0
  24. package/src/rules/no-subscribe-in-pipe.js +89 -0
  25. package/src/rules/no-subscribe-with-body.js +95 -0
  26. package/src/rules/no-trivial-return-type.js +104 -0
  27. package/src/rules/no-trivial-wrapper-method.js +120 -0
  28. package/src/rules/no-type-only-import.js +79 -0
  29. package/src/rules/no-unused-class-member.js +209 -0
  30. package/src/rules/no-window-location.js +109 -0
  31. package/src/rules/prefer-clone-equal.js +112 -0
  32. package/src/rules/prefer-concise-angular-host-directives.js +225 -0
  33. package/src/rules/prefer-concise-angular-style-metadata.js +108 -0
  34. package/src/rules/prefer-element-dimensions.js +128 -0
  35. package/src/rules/prefer-linked-signal.js +124 -0
  36. package/src/rules/prefer-match-media.js +101 -0
  37. package/src/rules/prefer-rxjs-timer.js +82 -0
  38. package/src/rules/prefer-scroll-state.js +158 -0
  39. package/src/rules/prefer-viewport-size.js +75 -0
  40. package/src/rules/require-dollar-suffix.js +123 -0
  41. package/src/rules/require-view-encapsulation-none.js +101 -0
@@ -0,0 +1,82 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * Prefer RxJS timer/interval/fromEvent operators over native browser APIs.
6
+ *
7
+ * RxJS operators integrate with takeUntilDestroyed() for automatic cleanup,
8
+ * whereas native APIs require manual cleanup and are easy to leak.
9
+ *
10
+ * BAD:
11
+ * setTimeout(() => this.refresh(), 500); // leaks if component destroyed first
12
+ * setInterval(() => this.poll(), 2000); // leaks
13
+ * element.addEventListener('click', handler); // must be removed manually
14
+ * element.removeEventListener('click', handler);
15
+ *
16
+ * GOOD:
17
+ * timer(500).pipe(takeUntilDestroyed()).subscribe(() => this.refresh());
18
+ * interval(2000).pipe(takeUntilDestroyed()).subscribe(() => this.poll());
19
+ * fromEvent(element, 'click').pipe(takeUntilDestroyed()).subscribe(handler);
20
+ */
21
+
22
+ /** @type {import('eslint').Rule.RuleModule} */
23
+ const preferRxjsTimer = {
24
+ meta: {
25
+ type: 'suggestion',
26
+ docs: {
27
+ description: 'Prefer RxJS timer / interval / fromEvent over native browser timer and event APIs.',
28
+ recommended: true,
29
+ },
30
+ messages: {
31
+ preferTimer:
32
+ "Use RxJS 'timer(delay)' from 'rxjs' instead of 'setTimeout()'. Combine with 'takeUntilDestroyed()' for automatic cleanup.",
33
+ preferInterval:
34
+ "Use RxJS 'interval(period)' from 'rxjs' instead of 'setInterval()'. Combine with 'takeUntilDestroyed()' for automatic cleanup.",
35
+ preferUnsubscribe:
36
+ "Remove '{{name}}()' — cancel the timer / interval by calling 'subscription.unsubscribe()' or use 'takeUntilDestroyed()' / 'take(1)'.",
37
+ preferFromEvent:
38
+ "Use RxJS 'fromEvent(target, event)' from 'rxjs' instead of '.addEventListener()'. Combine with 'takeUntilDestroyed()' for automatic cleanup.",
39
+ preferFromEventRemove:
40
+ "Remove '.removeEventListener()' — use 'fromEvent()' with 'takeUntilDestroyed()' to avoid ever needing to remove listeners manually.",
41
+ },
42
+ schema: [],
43
+ },
44
+ create(context) {
45
+ return {
46
+ CallExpression(node) {
47
+ const { callee } = node;
48
+
49
+ // ── Global timer calls ────────────────────────────────────────────────
50
+ if (callee.type === 'Identifier') {
51
+ const { name } = callee;
52
+ if (name === 'setTimeout') {
53
+ context.report({ node, messageId: 'preferTimer' });
54
+ return;
55
+ }
56
+ if (name === 'setInterval') {
57
+ context.report({ node, messageId: 'preferInterval' });
58
+ return;
59
+ }
60
+ if (name === 'clearTimeout' || name === 'clearInterval') {
61
+ context.report({ node, messageId: 'preferUnsubscribe', data: { name } });
62
+ return;
63
+ }
64
+ }
65
+
66
+ // ── Member calls: .addEventListener / .removeEventListener ────────────
67
+ if (callee.type === 'MemberExpression' && callee.property.type === 'Identifier') {
68
+ const methodName = callee.property.name;
69
+ if (methodName === 'addEventListener') {
70
+ context.report({ node, messageId: 'preferFromEvent' });
71
+ return;
72
+ }
73
+ if (methodName === 'removeEventListener') {
74
+ context.report({ node, messageId: 'preferFromEventRemove' });
75
+ }
76
+ }
77
+ },
78
+ };
79
+ },
80
+ };
81
+
82
+ module.exports = preferRxjsTimer;
@@ -0,0 +1,158 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * Prefers signalElementScrollState / signalHostElementScrollState from @ethlete/core
6
+ * over imperatively reading scroll position properties inside reactive contexts,
7
+ * and disallows adding raw scroll event listeners in favour of the same signal utilities.
8
+ *
9
+ * The signal utilities:
10
+ * - Set up a scroll event listener that keeps a reactive signal in sync
11
+ * - Clean up automatically when the component is destroyed
12
+ * - Provide debouncing and direction tracking built in
13
+ *
14
+ * NOTE: The property-read check is a WARNING — imperative one-shot reads of
15
+ * scrollTop/scrollLeft are fine in event handlers and animations; only flag
16
+ * inside effect() / computed().
17
+ *
18
+ * BAD (reactive context):
19
+ * effect(() => {
20
+ * const top = this.el.nativeElement.scrollTop; // stale snapshot ❌
21
+ * });
22
+ *
23
+ * BAD (scroll listeners — always wrong):
24
+ * el.addEventListener('scroll', fn); // ❌
25
+ * fromEvent(el, 'scroll').subscribe(fn); // ❌
26
+ * el.onscroll = fn; // ❌
27
+ *
28
+ * GOOD:
29
+ * scrollState = signalHostElementScrollState(); // from @ethlete/core
30
+ * scrollDir = signalHostElementLastScrollDirection(); // from @ethlete/core
31
+ * // Or use <et-scrollable> / ScrollableComponent for list scrolling
32
+ */
33
+
34
+ /** Scroll position properties read from DOM elements. */
35
+ const SCROLL_PROPS = new Set(['scrollTop', 'scrollLeft', 'scrollY', 'scrollX']);
36
+
37
+ /**
38
+ * Returns the name of the wrapping reactive context ('effect' | 'computed') if
39
+ * the node is inside one, crossing at least one function boundary.
40
+ * @param {import('eslint').Rule.Node} node
41
+ * @returns {string | null}
42
+ */
43
+ const getReactiveContext = (node) => {
44
+ let crossedFunctionBoundary = false;
45
+ let current = node.parent;
46
+
47
+ while (current) {
48
+ if (current.type === 'ArrowFunctionExpression' || current.type === 'FunctionExpression') {
49
+ crossedFunctionBoundary = true;
50
+ }
51
+
52
+ if (
53
+ crossedFunctionBoundary &&
54
+ current.type === 'CallExpression' &&
55
+ current.callee.type === 'Identifier' &&
56
+ (current.callee.name === 'effect' || current.callee.name === 'computed')
57
+ ) {
58
+ return current.callee.name;
59
+ }
60
+
61
+ current = current.parent;
62
+ }
63
+
64
+ return null;
65
+ };
66
+
67
+ /** @type {import('eslint').Rule.RuleModule} */
68
+ const preferScrollState = {
69
+ meta: {
70
+ type: 'suggestion',
71
+ docs: {
72
+ description:
73
+ "Prefer 'signalElementScrollState()' / 'signalHostElementScrollState()' from '@ethlete/core' over reading scroll position properties inside reactive contexts.",
74
+ recommended: true,
75
+ },
76
+ messages: {
77
+ preferScrollState:
78
+ "Avoid reading '{{prop}}' inside {{context}}() — it creates a stale snapshot. Use 'signalElementScrollState(elementRef)' or 'signalHostElementScrollState()' from '@ethlete/core' instead. For scroll direction, use 'signalElementLastScrollDirection()' / 'signalHostElementLastScrollDirection()'.",
79
+ noScrollListener:
80
+ "Do not add 'scroll' event listeners directly. Use 'signalElementScrollState(elementRef)' / 'signalHostElementScrollState()' for scroll position or state, 'signalElementLastScrollDirection(elementRef)' / 'signalHostElementLastScrollDirection()' for scroll direction, or the 'ScrollableComponent' (et-scrollable) from '@ethlete/cdk' for scrollable lists. All are imported from '@ethlete/core' or '@ethlete/cdk'.",
81
+ },
82
+ schema: [],
83
+ },
84
+ create(context) {
85
+ return {
86
+ CallExpression(node) {
87
+ const { callee } = node;
88
+ const args = node.arguments;
89
+
90
+ // ── el.addEventListener('scroll', fn) / window.addEventListener('scroll', fn) ──
91
+ if (
92
+ callee.type === 'MemberExpression' &&
93
+ callee.property.type === 'Identifier' &&
94
+ callee.property.name === 'addEventListener' &&
95
+ args[0]?.type === 'Literal' &&
96
+ args[0].value === 'scroll'
97
+ ) {
98
+ context.report({ node, messageId: 'noScrollListener' });
99
+ return;
100
+ }
101
+
102
+ // ── fromEvent(el, 'scroll') ──────────────────────────────────────────
103
+ if (
104
+ callee.type === 'Identifier' &&
105
+ callee.name === 'fromEvent' &&
106
+ args[1]?.type === 'Literal' &&
107
+ args[1].value === 'scroll'
108
+ ) {
109
+ context.report({ node, messageId: 'noScrollListener' });
110
+ return;
111
+ }
112
+
113
+ // ── renderer.listen(el, 'scroll', fn) ───────────────────────────────
114
+ if (
115
+ callee.type === 'MemberExpression' &&
116
+ callee.property.type === 'Identifier' &&
117
+ callee.property.name === 'listen' &&
118
+ args[1]?.type === 'Literal' &&
119
+ args[1].value === 'scroll'
120
+ ) {
121
+ context.report({ node, messageId: 'noScrollListener' });
122
+ }
123
+ },
124
+
125
+ AssignmentExpression(node) {
126
+ // ── el.onscroll = fn ────────────────────────────────────────────────
127
+ const { left } = node;
128
+ if (
129
+ left.type === 'MemberExpression' &&
130
+ left.property.type === 'Identifier' &&
131
+ left.property.name === 'onscroll'
132
+ ) {
133
+ context.report({ node, messageId: 'noScrollListener' });
134
+ }
135
+ },
136
+
137
+ MemberExpression(node) {
138
+ if (node.property.type !== 'Identifier') return;
139
+ if (!SCROLL_PROPS.has(node.property.name)) return;
140
+
141
+ // Only flag reads (not assignments like el.scrollTop = 0).
142
+ const parent = node.parent;
143
+ if (parent && parent.type === 'AssignmentExpression' && parent.left === node) return;
144
+
145
+ const reactiveCtx = getReactiveContext(node);
146
+ if (!reactiveCtx) return;
147
+
148
+ context.report({
149
+ node,
150
+ messageId: 'preferScrollState',
151
+ data: { prop: node.property.name, context: reactiveCtx },
152
+ });
153
+ },
154
+ };
155
+ },
156
+ };
157
+
158
+ module.exports = preferScrollState;
@@ -0,0 +1,75 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * Prefers injectViewportSize() from @ethlete/core over reading raw viewport
6
+ * dimensions from the global window object.
7
+ *
8
+ * injectViewportSize() returns a reactive signal that:
9
+ * - Updates automatically when the viewport is resized
10
+ * - Works on the server (SSR-safe)
11
+ * - Participates in Angular's change detection
12
+ *
13
+ * BAD:
14
+ * const w = window.innerWidth;
15
+ * document.defaultView?.innerHeight
16
+ *
17
+ * GOOD:
18
+ * const viewport = injectViewportSize(); // from @ethlete/core
19
+ * const w = viewport().width;
20
+ * const h = viewport().height;
21
+ */
22
+
23
+ /** Properties on window that report viewport dimensions. */
24
+ const VIEWPORT_PROPERTIES = new Set([
25
+ 'innerWidth',
26
+ 'innerHeight',
27
+ 'outerWidth',
28
+ 'outerHeight',
29
+ 'screen', // window.screen.width / window.screen.height
30
+ ]);
31
+
32
+ /** @type {import('eslint').Rule.RuleModule} */
33
+ const preferViewportSize = {
34
+ meta: {
35
+ type: 'suggestion',
36
+ docs: {
37
+ description: "Prefer 'injectViewportSize()' from '@ethlete/core' over direct window dimension properties.",
38
+ recommended: true,
39
+ },
40
+ messages: {
41
+ preferViewportSize:
42
+ "Avoid reading '{{prop}}' directly from window. Use 'injectViewportSize()' from '@ethlete/core' — it returns a reactive signal that stays up to date as the viewport resizes.",
43
+ },
44
+ schema: [],
45
+ },
46
+ create(context) {
47
+ return {
48
+ MemberExpression(node) {
49
+ if (node.property.type !== 'Identifier') return;
50
+ if (!VIEWPORT_PROPERTIES.has(node.property.name)) return;
51
+
52
+ // Only flag when the object is clearly a window-like reference.
53
+ const obj = node.object;
54
+ const objText = context.sourceCode.getText(obj).toLowerCase();
55
+
56
+ const isWindowRef =
57
+ objText === 'window' ||
58
+ objText.endsWith('.defaultview') ||
59
+ objText.endsWith('?.defaultview') ||
60
+ // document.defaultView without optional chain
61
+ /defaultview$/.test(objText);
62
+
63
+ if (!isWindowRef) return;
64
+
65
+ context.report({
66
+ node,
67
+ messageId: 'preferViewportSize',
68
+ data: { prop: node.property.name },
69
+ });
70
+ },
71
+ };
72
+ },
73
+ };
74
+
75
+ module.exports = preferViewportSize;
@@ -0,0 +1,123 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * Requires observable variables and class properties to be suffixed with `$`.
6
+ *
7
+ * Detection is heuristic — a binding is assumed to be an Observable if its
8
+ * initializer matches one of:
9
+ * - a `.pipe(...)` call (most reliable signal)
10
+ * - `new Subject / BehaviorSubject / ReplaySubject / AsyncSubject / Observable()`
11
+ * - a call to a known RxJS creator: from, of, interval, timer, fromEvent,
12
+ * fromEventPattern, combineLatest, merge, concat, forkJoin, race, zip,
13
+ * defer, iif, throwError, using, partition
14
+ *
15
+ * ❌ const myData = someObs.pipe(map(...));
16
+ * ❌ const clicks = fromEvent(el, 'click');
17
+ * ❌ class Foo { stream = this.src.pipe(filter(...)); }
18
+ *
19
+ * ✅ const myData$ = someObs.pipe(map(...));
20
+ * ✅ const clicks$ = fromEvent(el, 'click');
21
+ * ✅ class Foo { stream$ = this.src.pipe(filter(...)); }
22
+ */
23
+
24
+ const RXJS_CREATORS = new Set([
25
+ 'from',
26
+ 'of',
27
+ 'interval',
28
+ 'timer',
29
+ 'fromEvent',
30
+ 'fromEventPattern',
31
+ 'combineLatest',
32
+ 'merge',
33
+ 'concat',
34
+ 'forkJoin',
35
+ 'race',
36
+ 'zip',
37
+ 'defer',
38
+ 'iif',
39
+ 'throwError',
40
+ 'using',
41
+ 'partition',
42
+ ]);
43
+
44
+ const RXJS_SUBJECT_CTORS = new Set(['Subject', 'BehaviorSubject', 'ReplaySubject', 'AsyncSubject', 'Observable']);
45
+
46
+ /**
47
+ * Returns true if the expression looks like an Observable.
48
+ * @param {any} node
49
+ */
50
+ const isObservableInit = (node) => {
51
+ if (!node) return false;
52
+
53
+ // someObs.pipe(...)
54
+ if (
55
+ node.type === 'CallExpression' &&
56
+ node.callee.type === 'MemberExpression' &&
57
+ node.callee.property.type === 'Identifier' &&
58
+ node.callee.property.name === 'pipe'
59
+ ) {
60
+ return true;
61
+ }
62
+
63
+ // from(...), of(...), interval(...), etc.
64
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && RXJS_CREATORS.has(node.callee.name)) {
65
+ return true;
66
+ }
67
+
68
+ // new Subject(), new BehaviorSubject(...), etc.
69
+ if (node.type === 'NewExpression' && node.callee.type === 'Identifier' && RXJS_SUBJECT_CTORS.has(node.callee.name)) {
70
+ return true;
71
+ }
72
+
73
+ return false;
74
+ };
75
+
76
+ /** @type {import('eslint').Rule.RuleModule} */
77
+ const rule = {
78
+ meta: {
79
+ type: 'suggestion',
80
+ docs: {
81
+ description: 'Require observable variables and class properties to use the $ suffix.',
82
+ },
83
+ schema: [],
84
+ messages: {
85
+ missingSuffix: "Observable '{{ name }}' must end with '$' (e.g. '{{ name }}$').",
86
+ },
87
+ },
88
+ create(context) {
89
+ /**
90
+ * @param {string} name
91
+ * @param {any} reportNode
92
+ * @param {any} initNode
93
+ */
94
+ const checkBinding = (name, reportNode, initNode) => {
95
+ if (!isObservableInit(initNode)) return;
96
+ if (name.endsWith('$')) return;
97
+
98
+ context.report({
99
+ node: reportNode,
100
+ messageId: 'missingSuffix',
101
+ data: { name },
102
+ });
103
+ };
104
+
105
+ return {
106
+ // const myObs = ...
107
+ VariableDeclarator(node) {
108
+ const n = /** @type {any} */ (node);
109
+ if (n.id.type !== 'Identifier') return;
110
+ checkBinding(n.id.name, n.id, n.init);
111
+ },
112
+
113
+ // class Foo { myObs = ... }
114
+ PropertyDefinition(node) {
115
+ const n = /** @type {any} */ (node);
116
+ if (n.key.type !== 'Identifier') return;
117
+ checkBinding(n.key.name, n.key, n.value);
118
+ },
119
+ };
120
+ },
121
+ };
122
+
123
+ module.exports = rule;
@@ -0,0 +1,101 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * Requires @Component decorators to explicitly set `encapsulation: ViewEncapsulation.None`.
6
+ *
7
+ * The Ethlete styleguide mandates `ViewEncapsulation.None` for all components
8
+ * because styles are managed globally via design tokens and CSS custom properties.
9
+ * Emulated or ShadowDom encapsulation would interfere with this system.
10
+ *
11
+ * ❌ Missing the property entirely (default is Emulated):
12
+ * @Component({ selector: 'my-cmp', template: '' })
13
+ *
14
+ * ❌ Explicitly set to Emulated:
15
+ * @Component({ encapsulation: ViewEncapsulation.Emulated, ... })
16
+ *
17
+ * ❌ ShadowDom:
18
+ * @Component({ encapsulation: ViewEncapsulation.ShadowDom, ... })
19
+ *
20
+ * ✅ Correct:
21
+ * @Component({ encapsulation: ViewEncapsulation.None, ... })
22
+ */
23
+
24
+ /** @type {import('eslint').Rule.RuleModule} */
25
+ const requireViewEncapsulationNone = {
26
+ meta: {
27
+ type: 'suggestion',
28
+ docs: {
29
+ description: 'Require `encapsulation: ViewEncapsulation.None` in all @Component decorators.',
30
+ },
31
+ messages: {
32
+ missing: 'Add `encapsulation: ViewEncapsulation.None` to @Component. The default (Emulated) is not allowed.',
33
+ notNone: '`encapsulation` must be `ViewEncapsulation.None`. Got `ViewEncapsulation.{{value}}`.',
34
+ },
35
+ schema: [],
36
+ },
37
+ create(context) {
38
+ /**
39
+ * Returns true if the node is a @Component decorator call.
40
+ * @param {import('eslint').Rule.Node} node
41
+ */
42
+ const isComponentDecorator = (node) => {
43
+ const dec = /** @type {any} */ (node);
44
+ if (dec.type !== 'Decorator') return false;
45
+ const expr = dec.expression;
46
+ if (expr.type === 'CallExpression') return expr.callee.type === 'Identifier' && expr.callee.name === 'Component';
47
+ if (expr.type === 'Identifier') return expr.name === 'Component';
48
+ return false;
49
+ };
50
+
51
+ return {
52
+ Decorator(node) {
53
+ if (!isComponentDecorator(node)) return;
54
+
55
+ const dec = /** @type {any} */ (node);
56
+ const expr = dec.expression;
57
+ if (expr.type !== 'CallExpression') return;
58
+
59
+ const args = expr.arguments;
60
+ if (!args || args.length === 0) return;
61
+
62
+ const metadata = args[0];
63
+ if (!metadata || metadata.type !== 'ObjectExpression') return;
64
+
65
+ // Look for an `encapsulation` property
66
+ const encProp = metadata.properties.find(
67
+ (/** @type {any} */ p) =>
68
+ p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'encapsulation',
69
+ );
70
+
71
+ if (!encProp) {
72
+ // Property absent — default is Emulated, which is not allowed
73
+ context.report({ node: metadata, messageId: 'missing' });
74
+ return;
75
+ }
76
+
77
+ // Must be ViewEncapsulation.None
78
+ const val = /** @type {any} */ (encProp).value;
79
+ if (
80
+ val.type === 'MemberExpression' &&
81
+ val.object.type === 'Identifier' &&
82
+ val.object.name === 'ViewEncapsulation' &&
83
+ val.property.type === 'Identifier'
84
+ ) {
85
+ if (val.property.name !== 'None') {
86
+ context.report({
87
+ node: val,
88
+ messageId: 'notNone',
89
+ data: { value: val.property.name },
90
+ });
91
+ }
92
+ // .None — valid, do nothing
93
+ }
94
+ // If it's not a MemberExpression at all we don't flag it
95
+ // (dynamic or unusual expression — let TypeScript catch it)
96
+ },
97
+ };
98
+ },
99
+ };
100
+
101
+ module.exports = requireViewEncapsulationNone;