From f6ff69b0a4bcb0b170b30cbc0605201f2b3ba271 Mon Sep 17 00:00:00 2001 From: RebeccaStevens Date: Thu, 11 Jul 2024 09:48:14 +0000 Subject: [PATCH] fix(immutable-data): ignoreAccessorPattern can now handle NonNullExpressions and ChainExpressions (#849) fix #840 --- src/utils/misc.ts | 8 ++++++-- src/utils/type-guards.ts | 12 ++++++++++++ tests/rules/immutable-data/ts/object/valid.ts | 6 ++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 3a8f3600a..b0992fb9b 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -7,13 +7,15 @@ import { hasID, hasKey, isAssignmentExpression, + isChainExpression, isDefined, isIdentifier, isMemberExpression, isPrivateIdentifier, - isThisExpression, isTSAsExpression, + isTSNonNullExpression, isTSTypeAnnotation, + isThisExpression, isUnaryExpression, isVariableDeclaration, } from "#/utils/type-guards"; @@ -73,7 +75,9 @@ function getNodeIdentifierText( ? context.sourceCode .getText(node.typeAnnotation as TSESTree.Node) .replaceAll(/\s+/gmu, "") - : isTSAsExpression(node) + : isTSAsExpression(node) || + isTSNonNullExpression(node) || + isChainExpression(node) ? getNodeIdentifierText(node.expression, context) : null; diff --git a/src/utils/type-guards.ts b/src/utils/type-guards.ts index a2b0b646e..96c3bd145 100644 --- a/src/utils/type-guards.ts +++ b/src/utils/type-guards.ts @@ -75,6 +75,12 @@ export function isCallExpression( return node.type === AST_NODE_TYPES.CallExpression; } +export function isChainExpression( + node: TSESTree.Node, +): node is TSESTree.ChainExpression { + return node.type === AST_NODE_TYPES.ChainExpression; +} + export function isPropertyDefinition( node: TSESTree.Node, ): node is TSESTree.PropertyDefinition { @@ -303,6 +309,12 @@ export function isTSInterfaceHeritage( return node.type === AST_NODE_TYPES.TSInterfaceHeritage; } +export function isTSNonNullExpression( + node: TSESTree.Node, +): node is TSESTree.TSNonNullExpression { + return node.type === AST_NODE_TYPES.TSNonNullExpression; +} + export function isTSNullKeyword( node: TSESTree.Node, ): node is TSESTree.TSNullKeyword { diff --git a/tests/rules/immutable-data/ts/object/valid.ts b/tests/rules/immutable-data/ts/object/valid.ts index 788f81b12..1510c1df2 100644 --- a/tests/rules/immutable-data/ts/object/valid.ts +++ b/tests/rules/immutable-data/ts/object/valid.ts @@ -282,6 +282,12 @@ const tests: Array>> = [ `, optionsSet: [[{ ignoreAccessorPattern: "mutable*.*" }]], }, + { + code: dedent` + mutable_foo!.baz = "hello world"; + `, + optionsSet: [[{ ignoreAccessorPattern: "mutable*.*" }]], + }, ]; export default tests;