⬅️ Back to Table of Contents
📄 no-unnecessary-parameter-property-assignment.ts
📊 Analysis Summary
Metric |
Count |
🔧 Functions |
9 |
📦 Imports |
7 |
📊 Variables & Constants |
2 |
📚 Table of Contents
🛠️ File Location:
📂 packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
📦 Imports
Name |
Source |
TSESTree |
@typescript-eslint/utils |
DefinitionType |
@typescript-eslint/scope-manager |
AST_NODE_TYPES |
@typescript-eslint/utils |
ASTUtils |
@typescript-eslint/utils |
createRule |
../util |
getStaticStringValue |
../util |
nullThrows |
../util |
Variables & Constants
Name |
Type |
Kind |
Value |
Exported |
UNNECESSARY_OPERATORS |
Set<string> |
const |
new Set(['??=', '&&=', '=', '||=']) |
✗ |
reportInfoStack |
`{ |
|
|
|
assignedBeforeConstructor: Set; |
|
|
|
|
assignedBeforeUnnecessary: Set; |
|
|
|
|
unnecessaryAssignments: { |
|
|
|
|
name: string; |
|
|
|
|
node: TSESTree.AssignmentExpression; |
|
|
|
|
}[]; |
|
|
|
|
}[]| const | []` |
✗ |
|
|
|
Functions
isThisMemberExpression(node: TSESTree.Node): node is TSESTree.MemberExpression
Code
function isThisMemberExpression(
node: TSESTree.Node,
): node is TSESTree.MemberExpression {
return (
node.type === AST_NODE_TYPES.MemberExpression &&
node.object.type === AST_NODE_TYPES.ThisExpression
);
}
- Parameters:
node: TSESTree.Node
- Return Type:
node is TSESTree.MemberExpression
getPropertyName(node: TSESTree.Node): string | null
Code
function getPropertyName(node: TSESTree.Node): string | null {
if (!isThisMemberExpression(node)) {
return null;
}
if (node.property.type === AST_NODE_TYPES.Identifier) {
return node.property.name;
}
if (node.computed) {
return getStaticStringValue(node.property);
}
return null;
}
- Parameters:
node: TSESTree.Node
- Return Type:
string | null
- Calls:
isThisMemberExpression
getStaticStringValue (from ../util)
`findParentFunction(node: TSESTree.Node | undefined): | TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| undefined`
Code
function findParentFunction(
node: TSESTree.Node | undefined,
):
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| undefined {
if (
!node ||
node.type === AST_NODE_TYPES.FunctionDeclaration ||
node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression
) {
return node;
}
return findParentFunction(node.parent);
}
- Parameters:
node: TSESTree.Node | undefined
- Return Type:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| undefined
- Calls:
findParentFunction
findParentPropertyDefinition(node: TSESTree.Node | undefined): TSESTree.PropertyDefinition | undefined
Code
function findParentPropertyDefinition(
node: TSESTree.Node | undefined,
): TSESTree.PropertyDefinition | undefined {
if (!node || node.type === AST_NODE_TYPES.PropertyDefinition) {
return node;
}
return findParentPropertyDefinition(node.parent);
}
- Parameters:
node: TSESTree.Node | undefined
- Return Type:
TSESTree.PropertyDefinition | undefined
- Calls:
findParentPropertyDefinition
isConstructorFunctionExpression(node: TSESTree.Node | undefined): node is TSESTree.FunctionExpression
Code
function isConstructorFunctionExpression(
node: TSESTree.Node | undefined,
): node is TSESTree.FunctionExpression {
return (
node?.type === AST_NODE_TYPES.FunctionExpression &&
ASTUtils.isConstructor(node.parent)
);
}
- Parameters:
node: TSESTree.Node | undefined
- Return Type:
node is TSESTree.FunctionExpression
- Calls:
ASTUtils.isConstructor
isReferenceFromParameter(node: TSESTree.Identifier): boolean
Code
function isReferenceFromParameter(node: TSESTree.Identifier): boolean {
const scope = context.sourceCode.getScope(node);
const rightRef = scope.references.find(
ref => ref.identifier.name === node.name,
);
return rightRef?.resolved?.defs.at(0)?.type === DefinitionType.Parameter;
}
- Parameters:
node: TSESTree.Identifier
- Return Type:
boolean
- Calls:
context.sourceCode.getScope
scope.references.find
rightRef?.resolved?.defs.at
isParameterPropertyWithName(node: TSESTree.Parameter, name: string): boolean
Code
function isParameterPropertyWithName(
node: TSESTree.Parameter,
name: string,
): boolean {
return (
node.type === AST_NODE_TYPES.TSParameterProperty &&
((node.parameter.type === AST_NODE_TYPES.Identifier && // constructor (public foo) {}
node.parameter.name === name) ||
(node.parameter.type === AST_NODE_TYPES.AssignmentPattern && // constructor (public foo = 1) {}
node.parameter.left.type === AST_NODE_TYPES.Identifier &&
node.parameter.left.name === name))
);
}
- Parameters:
node: TSESTree.Parameter
name: string
- Return Type:
boolean
getIdentifier(node: TSESTree.Node): TSESTree.Identifier | null
Code
function getIdentifier(node: TSESTree.Node): TSESTree.Identifier | null {
if (node.type === AST_NODE_TYPES.Identifier) {
return node;
}
if (
node.type === AST_NODE_TYPES.TSAsExpression ||
node.type === AST_NODE_TYPES.TSNonNullExpression
) {
return getIdentifier(node.expression);
}
return null;
}
- Parameters:
node: TSESTree.Node
- Return Type:
TSESTree.Identifier | null
- Calls:
getIdentifier
isArrowIIFE(node: TSESTree.Node): boolean
Code
function isArrowIIFE(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
node.parent.type === AST_NODE_TYPES.CallExpression
);
}
- Parameters:
node: TSESTree.Node
- Return Type:
boolean