Skip to content

Commit

Permalink
chore: enable eslint-plugin/no-property-in-node internally (#9261)
Browse files Browse the repository at this point in the history
* chore: enable eslint-plugin/no-property-in-node internally

* commit yarn.lock changes
  • Loading branch information
JoshuaKGoldberg committed Jun 8, 2024
1 parent 629035f commit ab2916c
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/lib/rules/no-property-in-node.js b/lib/rules/no-property-in-node.js
index f3e2e0c829685213c7807b34158237f59142173c..11e54e8f6d69e0f06b27bb83ed96a96690b5ed9a 100644
--- a/lib/rules/no-property-in-node.js
+++ b/lib/rules/no-property-in-node.js
@@ -3,6 +3,7 @@
const typedNodeSourceFileTesters = [
/@types[/\\]estree[/\\]index\.d\.ts/,
/@typescript-eslint[/\\]types[/\\]dist[/\\]generated[/\\]ast-spec\.d\.ts/,
+ /packages[/\\]types[/\\]dist[/\\]generated[/\\]ast-spec\.d\.ts/,
];

/**
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ export default tseslint.config(
'packages/eslint-plugin/src/rules/**/*.{ts,tsx,cts,mts}',
],
rules: {
'eslint-plugin/no-property-in-node': 'error',
'eslint-plugin/require-meta-docs-description': [
'error',
{ pattern: '^(Enforce|Require|Disallow) .+[^. ]$' },
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@
"tmp": "0.2.1",
"tsx": "^4.7.2",
"typescript": "5.4.5",
"nx@18.3.5": "patch:nx@npm%3A18.3.5#./.yarn/patches/nx-npm-18.3.5-58c0bedf91.patch"
"nx@18.3.5": "patch:nx@npm%3A18.3.5#./.yarn/patches/nx-npm-18.3.5-58c0bedf91.patch",
"eslint-plugin-eslint-plugin@^5.5.0": "patch:eslint-plugin-eslint-plugin@npm%3A5.5.1#./.yarn/patches/eslint-plugin-eslint-plugin-npm-5.5.1-4206c2506d.patch"
},
"packageManager": "yarn@3.8.2"
}
20 changes: 11 additions & 9 deletions packages/eslint-plugin/src/rules/adjacent-overload-signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ type RuleNode =
| TSESTree.TSInterfaceBody
| TSESTree.TSModuleBlock
| TSESTree.TSTypeLiteral;

type Member =
| TSESTree.ClassElement
| TSESTree.ProgramStatement
| TSESTree.TypeElement;

type MemberDeclaration =
| TSESTree.DefaultExportDeclarations
| TSESTree.NamedExportDeclarations;

export default createRule({
name: 'adjacent-overload-signatures',
meta: {
Expand All @@ -32,7 +37,7 @@ export default createRule({
create(context) {
interface Method {
name: string;
static: boolean;
static?: boolean;
callSignature: boolean;
type: MemberNameType;
}
Expand All @@ -42,9 +47,9 @@ export default createRule({
* @param member the member being processed.
* @returns the name and attribute of the member or null if it's a member not relevant to the rule.
*/
function getMemberMethod(member: TSESTree.Node): Method | null {
const isStatic = 'static' in member && !!member.static;

function getMemberMethod(
member: Member | MemberDeclaration,
): Method | null {
switch (member.type) {
case AST_NODE_TYPES.ExportDefaultDeclaration:
case AST_NODE_TYPES.ExportNamedDeclaration: {
Expand All @@ -64,35 +69,32 @@ export default createRule({
}
return {
name,
static: isStatic,
callSignature: false,
type: MemberNameType.Normal,
};
}
case AST_NODE_TYPES.TSMethodSignature:
return {
...getNameFromMember(member, context.sourceCode),
static: isStatic,
static: !!member.static,
callSignature: false,
};
case AST_NODE_TYPES.TSCallSignatureDeclaration:
return {
name: 'call',
static: isStatic,
callSignature: true,
type: MemberNameType.Normal,
};
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return {
name: 'new',
static: isStatic,
callSignature: false,
type: MemberNameType.Normal,
};
case AST_NODE_TYPES.MethodDefinition:
return {
...getNameFromMember(member, context.sourceCode),
static: isStatic,
static: !!member.static,
callSignature: false,
};
}
Expand Down
21 changes: 11 additions & 10 deletions packages/eslint-plugin/src/rules/class-literal-property-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ const printNodeModifiers = (
const isSupportedLiteral = (
node: TSESTree.Node,
): node is TSESTree.LiteralExpression => {
if (node.type === AST_NODE_TYPES.Literal) {
return true;
}
switch (node.type) {
case AST_NODE_TYPES.Literal:
return true;

if (
node.type === AST_NODE_TYPES.TaggedTemplateExpression ||
node.type === AST_NODE_TYPES.TemplateLiteral
) {
return ('quasi' in node ? node.quasi.quasis : node.quasis).length === 1;
}
case AST_NODE_TYPES.TaggedTemplateExpression:
return node.quasi.quasis.length === 1;

return false;
case AST_NODE_TYPES.TemplateLiteral:
return node.quasis.length === 1;

default:
return false;
}
};

export default createRule<Options, MessageIds>({
Expand Down
9 changes: 8 additions & 1 deletion packages/eslint-plugin/src/rules/default-param-last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ export default createRule({
* @private
*/
function isOptionalParam(node: TSESTree.Parameter): boolean {
return 'optional' in node && node.optional;
return (
(node.type === AST_NODE_TYPES.ArrayPattern ||
node.type === AST_NODE_TYPES.AssignmentPattern ||
node.type === AST_NODE_TYPES.Identifier ||
node.type === AST_NODE_TYPES.ObjectPattern ||
node.type === AST_NODE_TYPES.RestElement) &&
node.optional
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This is due to some really funky type conversions between different node types.
* This is done intentionally based on the internal implementation of the base indent rule.
*/
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, eslint-plugin/no-property-in-node */

import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
Expand Down
8 changes: 8 additions & 0 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This rule was feature-frozen before we enabled no-property-in-node.
/* eslint-disable eslint-plugin/no-property-in-node */

import type { JSONSchema, TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import naturalCompare from 'natural-compare';
Expand Down Expand Up @@ -1064,6 +1067,11 @@ export default createRule<Options, MessageIds>({
// https://github.com/typescript-eslint/typescript-eslint/issues/5439
/* eslint-disable @typescript-eslint/no-non-null-assertion */
return {
'ClassDeclaration, FunctionDeclaration'(node): void {
if ('superClass' in node) {
// ...
}
},
ClassDeclaration(node): void {
validateMembersOrder(
node.body.body,
Expand Down
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This rule was feature-frozen before we enabled no-property-in-node.
/* eslint-disable eslint-plugin/no-property-in-node */

import { PatternVisitor } from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, TSESLint } from '@typescript-eslint/utils';
Expand Down
16 changes: 13 additions & 3 deletions packages/eslint-plugin/src/rules/no-extraneous-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export default createRule<Options, MessageIds>({
return;
}

const reportNode = 'id' in parent && parent.id ? parent.id : parent;
const reportNode =
parent.type === AST_NODE_TYPES.ClassDeclaration && parent.id
? parent.id
: parent;
if (node.body.length === 0) {
if (allowEmpty) {
return;
Expand All @@ -105,7 +108,10 @@ export default createRule<Options, MessageIds>({
let onlyConstructor = true;

for (const prop of node.body) {
if ('kind' in prop && prop.kind === 'constructor') {
if (
prop.type === AST_NODE_TYPES.MethodDefinition &&
prop.kind === 'constructor'
) {
if (
prop.value.params.some(
param => param.type === AST_NODE_TYPES.TSParameterProperty,
Expand All @@ -116,7 +122,11 @@ export default createRule<Options, MessageIds>({
}
} else {
onlyConstructor = false;
if ('static' in prop && !prop.static) {
if (
(prop.type === AST_NODE_TYPES.PropertyDefinition ||
prop.type === AST_NODE_TYPES.MethodDefinition) &&
!prop.static
) {
onlyStatic = false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/no-inferrable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ export default createRule<Options, MessageIds>({

return (
isFunctionCall(unwrappedInit, 'BigInt') ||
(unwrappedInit.type === AST_NODE_TYPES.Literal &&
'bigint' in unwrappedInit)
unwrappedInit.type === AST_NODE_TYPES.Literal
);
}

Expand Down
23 changes: 18 additions & 5 deletions packages/eslint-plugin/src/rules/no-misused-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ export default createRule({
* @param returnType type to be compared
*/
function isMatchingParentType(
parent: TSESTree.Node | undefined,
parent:
| TSESTree.TSInterfaceDeclaration
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression
| TSESTree.Identifier
| undefined,
returnType: TSESTree.TSTypeAnnotation | undefined,
): boolean {
if (
parent &&
'id' in parent &&
parent.id &&
parent.id.type === AST_NODE_TYPES.Identifier
(parent.type === AST_NODE_TYPES.ClassDeclaration ||
parent.type === AST_NODE_TYPES.ClassExpression ||
parent.type === AST_NODE_TYPES.TSInterfaceDeclaration) &&
parent.id
) {
return getTypeReferenceName(returnType) === parent.id.name;
}
Expand Down Expand Up @@ -93,7 +99,14 @@ export default createRule({
node: TSESTree.MethodDefinition,
): void {
if (node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression) {
if (isMatchingParentType(node.parent.parent, node.value.returnType)) {
if (
isMatchingParentType(
node.parent.parent as
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression,
node.value.returnType,
)
) {
context.report({
node,
messageId: 'errorMessageClass',
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default createRule<Options, MessageIds>({
if (
(def.name.parent.type === AST_NODE_TYPES.ArrayPattern ||
refUsedInArrayPatterns) &&
'name' in def.name &&
def.name.type === AST_NODE_TYPES.Identifier &&
options.destructuredArrayIgnorePattern?.test(def.name.name)
) {
continue;
Expand All @@ -240,7 +240,7 @@ export default createRule<Options, MessageIds>({
}
// skip ignored parameters
if (
'name' in def.name &&
def.name.type === AST_NODE_TYPES.Identifier &&
options.caughtErrorsIgnorePattern?.test(def.name.name)
) {
continue;
Expand All @@ -254,7 +254,7 @@ export default createRule<Options, MessageIds>({
}
// skip ignored parameters
if (
'name' in def.name &&
def.name.type === AST_NODE_TYPES.Identifier &&
options.argsIgnorePattern?.test(def.name.name)
) {
continue;
Expand All @@ -270,7 +270,7 @@ export default createRule<Options, MessageIds>({
} else {
// skip ignored variables
if (
'name' in def.name &&
def.name.type === AST_NODE_TYPES.Identifier &&
options.varsIgnorePattern?.test(def.name.name)
) {
continue;
Expand Down
7 changes: 5 additions & 2 deletions packages/eslint-plugin/src/rules/no-useless-constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ function checkAccessibility(node: TSESTree.MethodDefinition): boolean {
case 'public':
if (
node.parent.type === AST_NODE_TYPES.ClassBody &&
'superClass' in node.parent.parent &&
node.parent.parent.superClass
(
node.parent.parent as
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression
).superClass
) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-non-null-assertion, eslint-plugin/no-property-in-node */
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/prefer-as-const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default createRule({
if (
valueNode.type === AST_NODE_TYPES.Literal &&
typeNode.type === AST_NODE_TYPES.TSLiteralType &&
'raw' in typeNode.literal &&
typeNode.literal.type === AST_NODE_TYPES.Literal &&
valueNode.raw === typeNode.literal.raw
) {
if (canFix) {
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-destructuring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ export default createRule<Options, MessageIds>({
? baseRules
: baseRulesWithoutFix();
if (
'typeAnnotation' in leftNode &&
(leftNode.type === AST_NODE_TYPES.ArrayPattern ||
leftNode.type === AST_NODE_TYPES.Identifier ||
leftNode.type === AST_NODE_TYPES.ObjectPattern) &&
leftNode.typeAnnotation !== undefined &&
!enforceForDeclarationWithTypeAnnotation
) {
Expand Down
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9808,7 +9808,7 @@ __metadata:
languageName: node
linkType: hard

"eslint-plugin-eslint-plugin@npm:^5.5.0":
"eslint-plugin-eslint-plugin@npm:5.5.1":
version: 5.5.1
resolution: "eslint-plugin-eslint-plugin@npm:5.5.1"
dependencies:
Expand All @@ -9820,6 +9820,18 @@ __metadata:
languageName: node
linkType: hard

"eslint-plugin-eslint-plugin@patch:eslint-plugin-eslint-plugin@npm%3A5.5.1#./.yarn/patches/eslint-plugin-eslint-plugin-npm-5.5.1-4206c2506d.patch::locator=%40typescript-eslint%2Ftypescript-eslint%40workspace%3A.":
version: 5.5.1
resolution: "eslint-plugin-eslint-plugin@patch:eslint-plugin-eslint-plugin@npm%3A5.5.1#./.yarn/patches/eslint-plugin-eslint-plugin-npm-5.5.1-4206c2506d.patch::version=5.5.1&hash=0c6729&locator=%40typescript-eslint%2Ftypescript-eslint%40workspace%3A."
dependencies:
eslint-utils: ^3.0.0
estraverse: ^5.3.0
peerDependencies:
eslint: ">=7.0.0"
checksum: d46fb241fbcff5c7a9ec44c6f19930ad7bd2394c0cadb75a97bb151c86fc506da9dfbbcf34ec41151abf2024151945a0dd6ba9b031a3d75e87e58e7f267bf488
languageName: node
linkType: hard

"eslint-plugin-import@npm:^2.29.1":
version: 2.29.1
resolution: "eslint-plugin-import@npm:2.29.1"
Expand Down

0 comments on commit ab2916c

Please sign in to comment.