Skip to content

Commit

Permalink
feat: no-property-in-node add additionalNodeTypeFiles option (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Jun 25, 2024
1 parent 04e397b commit 6cdef14
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/rules/no-property-in-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const typedNodeSourceFileTesters = [
const defaultTypedNodeSourceFileTesters = [
/@types[/\\]estree[/\\]index\.d\.ts/,
/@typescript-eslint[/\\]types[/\\]dist[/\\]generated[/\\]ast-spec\.d\.ts/,
];
Expand All @@ -26,9 +26,10 @@ const typedNodeSourceFileTesters = [
* ```
*
* @param {import('typescript').Type} type
* @param {RegExp[]} typedNodeSourceFileTesters
* @returns Whether the type seems to include a known ESTree or TSESTree AST node.
*/
function isAstNodeType(type) {
function isAstNodeType(type, typedNodeSourceFileTesters) {
return (type.types || [type])
.filter((typePart) => typePart.getProperty('type'))
.flatMap(
Expand All @@ -55,13 +56,33 @@ module.exports = {
requiresTypeChecking: true,
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-property-in-node.md',
},
schema: [],
schema: [
{
type: 'object',
properties: {
additionalNodeTypeFiles: {
description:
'Any additional regular expressions to consider source files defining AST Node types.',
elements: { type: 'string' },
type: 'array',
},
},
additionalProperties: false,
},
],
messages: {
in: 'Prefer checking specific node properties instead of a broad `in`.',
},
},

create(context) {
const typedNodeSourceFileTesters = [
...defaultTypedNodeSourceFileTesters,
...(context.options[0]?.additionalNodeTypeFiles?.map(
(filePath) => new RegExp(filePath),
) ?? []),
];

return {
'BinaryExpression[operator=in]'(node) {
// TODO: Switch this to ESLintUtils.getParserServices with typescript-eslint@>=6
Expand All @@ -77,7 +98,7 @@ module.exports = {
const tsNode = services.esTreeNodeToTSNodeMap.get(node.right);
const type = checker.getTypeAtLocation(tsNode);

if (isAstNodeType(type)) {
if (isAstNodeType(type, typedNodeSourceFileTesters)) {
context.report({ messageId: 'in', node });
}
},
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/rules/no-property-in-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ ruleTester.run('no-property-in-node', rule, {
},
};
`,
{
code: `
interface Node {
type: string;
}
declare const node: Node;
'a' in node;
export {};
`,
options: [
{
additionalNodeTypeFiles: [/not-found/],
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -163,5 +178,29 @@ ruleTester.run('no-property-in-node', rule, {
},
],
},
{
code: `
interface Node {
type: string;
}
declare const node: Node;
'a' in node;
export {};
`,
options: [
{
additionalNodeTypeFiles: [/lib[/\\]fixtures[/\\]estree\.ts/],
},
],
errors: [
{
column: 9,
line: 6,
endColumn: 20,
endLine: 6,
messageId: 'in',
},
],
},
],
});

0 comments on commit 6cdef14

Please sign in to comment.