Skip to content

Commit

Permalink
fix(prefer-immutable-types): support private identifier (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
foray1010 authored Apr 15, 2023
1 parent eaa6779 commit 6349a92
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
52 changes: 27 additions & 25 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isExpressionStatement,
isIdentifier,
isMemberExpression,
isPrivateIdentifier,
isThisExpression,
isTSTypeAnnotation,
isUnaryExpression,
Expand Down Expand Up @@ -48,31 +49,32 @@ function getNodeIdentifierText(
return undefined;
}

const identifierText = isIdentifier(node)
? node.name
: hasID(node) && isDefined(node.id)
? getNodeIdentifierText(node.id, context)
: hasKey(node) && isDefined(node.key)
? getNodeIdentifierText(node.key, context)
: isAssignmentExpression(node)
? getNodeIdentifierText(node.left, context)
: isMemberExpression(node)
? `${getNodeIdentifierText(node.object, context)}.${getNodeIdentifierText(
node.property,
context
)}`
: isThisExpression(node)
? "this"
: isUnaryExpression(node)
? getNodeIdentifierText(node.argument, context)
: isExpressionStatement(node)
? context.getSourceCode().getText(node as TSESTree.Node)
: isTSTypeAnnotation(node)
? context
.getSourceCode()
.getText(node.typeAnnotation as TSESTree.Node)
.replaceAll(/\s+/gmu, "")
: null;
const identifierText =
isIdentifier(node) || isPrivateIdentifier(node)
? node.name
: hasID(node) && isDefined(node.id)
? getNodeIdentifierText(node.id, context)
: hasKey(node) && isDefined(node.key)
? getNodeIdentifierText(node.key, context)
: isAssignmentExpression(node)
? getNodeIdentifierText(node.left, context)
: isMemberExpression(node)
? `${getNodeIdentifierText(node.object, context)}.${getNodeIdentifierText(
node.property,
context
)}`
: isThisExpression(node)
? "this"
: isUnaryExpression(node)
? getNodeIdentifierText(node.argument, context)
: isExpressionStatement(node)
? context.getSourceCode().getText(node as TSESTree.Node)
: isTSTypeAnnotation(node)
? context
.getSourceCode()
.getText(node.typeAnnotation as TSESTree.Node)
.replaceAll(/\s+/gmu, "")
: null;

if (identifierText !== null) {
return identifierText;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ export function isObjectPattern(
return node.type === AST_NODE_TYPES.ObjectPattern;
}

export function isPrivateIdentifier(
node: TSESTree.Node
): node is TSESTree.PrivateIdentifier {
return node.type === AST_NODE_TYPES.PrivateIdentifier;
}

export function isProgram(node: TSESTree.Node): node is TSESTree.Program {
return node.type === AST_NODE_TYPES.Program;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/rules/prefer-immutable-types/ts/variables/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
class Klass {
mutableA: number;
private mutableB: number;
#mutableC: number;
}
`,
optionsSet: [[{ ignoreNamePattern: "^mutable" }]],
Expand All @@ -249,6 +250,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
class Klass {
AMutable: number;
private BMutable: number;
#CMutable: number;
}
`,
optionsSet: [[{ ignoreNamePattern: "Mutable$" }]],
Expand Down

0 comments on commit 6349a92

Please sign in to comment.