You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reviewers will reject re-implementations of existing helpers.
33
+
34
+
**Anti-pattern:** Iterating `Object.keys(node)` to enumerate child nodes. Use the existing `childrenOf` helper instead:
35
+
```typescript
36
+
// ❌ Do not do this
37
+
for (const key ofObject.keys(node)) { ... }
38
+
39
+
// ✅ Use this
40
+
for (const child ofchildrenOf(node)) { ... }
41
+
```
42
+
43
+
If you write a **new** utility function that only operates on `estree`/`TSESTree` nodes with no rule-specific domain logic, place it in `helpers/ast.ts` (or `helpers/vue.ts`, `helpers/react.ts` for domain-specific utilities) — not inside the individual rule file. Functions buried in a rule file are invisible to future implementers.
31
44
32
45
## Finding Helpers
33
46
@@ -37,3 +50,21 @@ ls packages/jsts/src/rules/helpers/
37
50
```
38
51
39
52
Read helper implementations to understand their usage and parameters.
53
+
54
+
## Helper Contracts
55
+
56
+
### childrenOf
57
+
58
+
`childrenOf(node)` always returns `estree.Node[]`. Do not write custom type guard predicates or `Array.isArray()` checks on its return values — they are dead code:
Copy file name to clipboardExpand all lines: .claude/skills/rule-implementation/SKILL.md
+272Lines changed: 272 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -131,3 +131,275 @@ After modifying configuration or metadata files, regenerate the derived files:
131
131
132
132
-`npm run generate-meta` - Regenerate rule metadata after changes to `meta.ts` or `config.ts`
133
133
-`npm run generate-java-rule-classes` - Regenerate Java rule classes (required after `config.ts` changes)
134
+
135
+
---
136
+
137
+
## AST Traversal Strategy
138
+
139
+
### Prefer ESLint selector-based node collection over recursive walks
140
+
141
+
When implementing a rule, use ESLint's selector-based dispatch instead of walking the AST manually:
142
+
143
+
```typescript
144
+
// ❌ Recursive walk — expensive and brittle
145
+
create(context) {
146
+
return {
147
+
FunctionDeclaration(node) {
148
+
const returns =collectReturns(node.body); // recursive walk inside
149
+
}
150
+
};
151
+
}
152
+
153
+
// ✅ Selector + side-table — ESLint dispatches once per node
154
+
create(context) {
155
+
const returnsByFunction =newMap();
156
+
return {
157
+
ReturnStatement(node) {
158
+
const fn =getEnclosingFunction(node);
159
+
if (fn) returnsByFunction.set(fn, [...(returnsByFunction.get(fn) ?? []), node]);
160
+
},
161
+
'FunctionDeclaration:exit'(node) {
162
+
const returns =returnsByFunction.get(node) ?? [];
163
+
// analyse returns
164
+
}
165
+
};
166
+
}
167
+
```
168
+
169
+
Selector-based approaches let ESLint dispatch to your handler exactly once per matching node at no extra traversal cost, and automatically handle all node types including future additions to the language.
170
+
171
+
### When recursive traversal is unavoidable, use visitorKeys — not switch
172
+
173
+
If you must traverse a subtree recursively inside a helper (e.g. `containsAssignment`, `nodeHasReturn`), enumerate child properties generically using ESLint's `visitorKeys` instead of a manual `switch`:
if (Array.isArray(child)) returnchild.some(c=>c&&containsReturn(casestree.Node));
193
+
if (child&&typeofchild==='object') returncontainsReturn(childasestree.Node);
194
+
returnfalse;
195
+
});
196
+
}
197
+
```
198
+
199
+
---
200
+
201
+
## TypeScript Type Safety
202
+
203
+
### Unsafe type assertions must be guarded or explained
204
+
205
+
Do not cast AST nodes silently. A cast is safe only if it holds for every node type the upstream rule can report on.
206
+
207
+
```typescript
208
+
// ❌ Silent cast — crashes if upstream rule ever reports a non-Identifier node
209
+
const identifier =tsNodeasTSESTree.Identifier;
210
+
211
+
// ✅ Type guard
212
+
if (tsNode.type!=='Identifier') return;
213
+
const identifier =tsNode; // narrowed
214
+
215
+
// ✅ Explanatory comment when guard is impractical
216
+
// Safe: prefer-single-call only reports Identifier property nodes; computed access is excluded.
217
+
const identifier =tsNodeasTSESTree.Identifier;
218
+
```
219
+
220
+
### Enumerate all upstream report sites before casting reportDescriptor.node
221
+
222
+
When writing a decorator, inspect **every**`context.report()` call in the upstream rule to determine all possible node types. A cast that only covers the common case will crash on edge cases.
This audit is the natural follow-on to Step 1 ("Find All Report Calls") in the FP tracing workflow above.
233
+
234
+
### Use specific node types in function signatures
235
+
236
+
Declare parameters with the most specific `estree`/`TSESTree` type the function actually requires. Using the base `estree.Node` hides the contract and disables type-checker assistance:
237
+
238
+
```typescript
239
+
// ❌ Too broad
240
+
function isInDirectionalContext(node:estree.Node):boolean { ... }
241
+
242
+
// ✅ Specific
243
+
function isInDirectionalContext(node:estree.CallExpression):boolean { ... }
244
+
```
245
+
246
+
---
247
+
248
+
## TypeScript Compiler API
249
+
250
+
### Use mutual assignability to test type equivalence
251
+
252
+
One-directional `isTypeAssignableTo(A, B)` only proves that `A` is a structural subtype of `B`. It returns `true` even when `B` is much wider, causing wrong-trigger suppressions.
253
+
254
+
```typescript
255
+
// ❌ One-directional — false matches when B has extra optional fields
256
+
if (checker.isTypeAssignableTo(typeA, typeB)) { suppress(); }
257
+
258
+
// ✅ Mutual — true only when both types are structurally equivalent
259
+
if (checker.isTypeAssignableTo(typeA, typeB) &&checker.isTypeAssignableTo(typeB, typeA)) {
260
+
suppress();
261
+
}
262
+
```
263
+
264
+
### Use bitwise operators for TypeFlags comparisons
265
+
266
+
`ts.TypeFlags` values are bitmasks. A type can have multiple flags set simultaneously. Using `===` against a single flag value fails whenever any other flag is also set:
267
+
268
+
```typescript
269
+
// ❌ Fails when multiple flags are set (common with union types)
270
+
if (type.flags===ts.TypeFlags.Any) { ... }
271
+
272
+
// ✅ Bitwise mask — true whenever the flag is set, regardless of other flags
273
+
if (type.flags&ts.TypeFlags.Any) { ... }
274
+
if (type.flags& (ts.TypeFlags.Any|ts.TypeFlags.Unknown)) { ... }
275
+
```
276
+
277
+
---
278
+
279
+
## FP Remediation Quality
280
+
281
+
### Do not suppress reports that the rule is correct to raise
282
+
283
+
Not every complaint is a genuine false positive. Two cases to watch for:
284
+
285
+
**strictNullChecks disabled:** Do not add an exemption that suppresses non-null assertion (`!`) reports when `strictNullChecks` is off. Without `strictNullChecks`, `!` is a no-op — but suppressing the report hides future migration debt. Let the rule fire and guide developers to write proper null guards.
286
+
287
+
**Rule goal conflicts:** Before implementing an exception, re-read the upstream rule's documentation. If the exception would allow exactly the pattern the rule was designed to catch, the report is not a false positive. Do not proceed with the fix; flag it for human review instead.
288
+
289
+
### Cover the full API family when adding an exception
290
+
291
+
When adding a suppression exception for one method, identify all semantically equivalent siblings in the same API family and include them in the same guard:
292
+
293
+
```typescript
294
+
// ❌ Only covers Object.keys — Object.getOwnPropertyNames users still get FPs
295
+
if (isCallingMethod(node, 1, 'keys')) { suppress(); }
296
+
297
+
// ✅ Covers the full family
298
+
if (isCallingMethod(node, 1, 'keys', 'getOwnPropertyNames', 'getOwnPropertySymbols')) {
299
+
suppress();
300
+
}
301
+
```
302
+
303
+
Add test cases and update rule documentation for each covered variant.
304
+
305
+
---
306
+
307
+
## Decorator Design
308
+
309
+
### When all identification strategies fail, preserve the report
310
+
311
+
When a decorator uses multiple strategies to identify the owning component, and all strategies fail, preserve the original report. Do not fall back to `context.sourceCode.ast` (the entire file AST) as a catch-all scope:
context.report(reportDescriptor); // original report is more likely correct
322
+
return;
323
+
}
324
+
```
325
+
326
+
### Scope suppression decisions to the component, not the file
327
+
328
+
Never cache a suppression decision at the file level. If one component matches, a file-level cache incorrectly suppresses reports for other components in the same file:
329
+
330
+
```typescript
331
+
// ❌ File-level cache — contaminates all components in the file
332
+
let fileHasSuppressibleProps =false;
333
+
return {
334
+
'Program:exit'() {
335
+
if (fileHasSuppressibleProps) return; // suppresses everything in file
336
+
}
337
+
};
338
+
339
+
// ✅ Per-report decision scoped to the component
340
+
(context, descriptor) => {
341
+
const owner =findOwner(descriptor.node);
342
+
if (owner&&isSuppressible(descriptor.node, owner)) return;
343
+
context.report(descriptor);
344
+
}
345
+
```
346
+
347
+
### Resolve spread expressions before suppressing
348
+
349
+
Do not suppress a report simply because a JSX spread attribute `{...expr}` is present. Attempt to resolve `expr` statically first:
350
+
351
+
```typescript
352
+
// ❌ Suppresses on mere presence of spread — may produce false negatives
353
+
if (node.openingElement.attributes.some(a=>a.type==='JSXSpreadAttribute')) return;
0 commit comments