Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b9d6c28
feat: add no-property-in-node rule
JoshuaKGoldberg Feb 5, 2024
8cfaf6d
Add ☑️ emoji for recommended-type-checked
JoshuaKGoldberg Feb 5, 2024
4d163e1
tests: valid before invalid
JoshuaKGoldberg Feb 5, 2024
d62539d
Also check for whether the node has a 'type'
JoshuaKGoldberg Feb 5, 2024
3575644
Added docs and example to isAstNodeType
JoshuaKGoldberg Feb 5, 2024
e42c075
Expanded rule details
JoshuaKGoldberg Feb 5, 2024
2a94dcb
Add more valid test cases
JoshuaKGoldberg Feb 5, 2024
4d5d332
Merge branch 'main'
JoshuaKGoldberg Feb 5, 2024
755cc08
Fixed test path to fixtures
JoshuaKGoldberg Feb 5, 2024
d76b08a
Use parserOptions.project: true for eslint-remote-tester on TS files
JoshuaKGoldberg Feb 5, 2024
5913127
nit: avoid shadowing name for typePart
JoshuaKGoldberg Feb 5, 2024
d45695d
<!-- omit from toc -->
JoshuaKGoldberg Feb 5, 2024
532925d
Downgraded to typescript-eslint@v5
JoshuaKGoldberg Feb 6, 2024
6b60ab0
Also remove @typescript-eslint/utils
JoshuaKGoldberg Feb 6, 2024
89aafda
Or rather, make @typescript-eslint/utils a -D
JoshuaKGoldberg Feb 6, 2024
1d570e8
Remove ts-api-utils too
JoshuaKGoldberg Feb 6, 2024
1ec0c2a
Removed recommended-type-checked
JoshuaKGoldberg Feb 6, 2024
de370b0
Removed README.md section too
JoshuaKGoldberg Feb 6, 2024
18b205a
Removed eslint-remote-tester.config.js parserOptions.project too
JoshuaKGoldberg Feb 6, 2024
8d68dc9
Redid README.md presets table
JoshuaKGoldberg Feb 6, 2024
d8dc1a0
Added all-type-checked
JoshuaKGoldberg Feb 6, 2024
d8cc468
Removed file notice
JoshuaKGoldberg Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expanded rule details
  • Loading branch information
JoshuaKGoldberg committed Feb 5, 2024
commit e42c07531dbf659accf790a7e907601b2de5c924
38 changes: 27 additions & 11 deletions docs/rules/no-property-in-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
When working with a node of type `ESTree.Node` or `TSESTree.Node`, it can be tempting to use the `'in'` operator to narrow the node's type.
`'in'` narrowing is susceptible to confusing behavior from quirks of ASTs, such as node properties sometimes being omitted from nodes and other times explicitly being set to `null` or `undefined`.

Using direct property checks is generally considered preferable.
Instead, checking a node's `type` property is generally considered preferable.

## Rule Details

Expand All @@ -18,21 +18,37 @@ Examples of **incorrect** code for this rule:
```ts
/* eslint eslint-plugin/no-property-in-node: error */

declare const node: TSESTree.Parameter;

if ('optional' in node) {
node.optional;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: { /* ... */ },
create(context) {
return {
'ClassDeclaration, FunctionDeclaration'(node) {
if ('superClass' in node) {
console.log("This is a class declaration:", node);
}
},
};
},
};
```

Examples of **correct** code for this rule:

```ts
/* eslint eslint-plugin/no-property-in-node: error */

declare const node: TSESTree.Parameter;

if (node.type !== TSESTree.AST_NODE_TYPES.TSParameterProperty) {
node.optional;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: { /* ... */ },
create(context) {
return {
'ClassDeclaration, FunctionDeclaration'(node) {
if (node.type === 'ClassDeclaration') {
console.log("This is a class declaration:", node);
}
},
};
},
};
```