Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [no-property-in-node] add additionalNodeTypeFiles option #484

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
30 changes: 26 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,34 @@ 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.',
default: '^(enforce|require|disallow)',
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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 +99,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',
},
],
},
],
});
Loading