Skip to content

Commit

Permalink
fix(immutable-data): ignoreAccessorPattern can now handle NonNullExpr…
Browse files Browse the repository at this point in the history
…essions and ChainExpressions (#849)

fix #840
  • Loading branch information
RebeccaStevens committed Jul 11, 2024
1 parent 13e04cc commit f6ff69b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
hasID,
hasKey,
isAssignmentExpression,
isChainExpression,
isDefined,
isIdentifier,
isMemberExpression,
isPrivateIdentifier,
isThisExpression,
isTSAsExpression,
isTSNonNullExpression,
isTSTypeAnnotation,
isThisExpression,
isUnaryExpression,
isVariableDeclaration,
} from "#/utils/type-guards";
Expand Down Expand Up @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions src/utils/type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions tests/rules/immutable-data/ts/object/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ const tests: Array<ValidTestCaseSet<OptionsOf<typeof rule>>> = [
`,
optionsSet: [[{ ignoreAccessorPattern: "mutable*.*" }]],
},
{
code: dedent`
mutable_foo!.baz = "hello world";
`,
optionsSet: [[{ ignoreAccessorPattern: "mutable*.*" }]],
},
];

export default tests;

0 comments on commit f6ff69b

Please sign in to comment.