Skip to content

Commit

Permalink
[Refactor] display-name: use unwrapTSAsExpression
Browse files Browse the repository at this point in the history
also add `isTSAsExpression` AST util
  • Loading branch information
ljharb committed Jul 29, 2024
1 parent 983b88d commit 4d99f6e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module.exports = {
if (!component) {
return;
}
markDisplayNameAsDeclared(component.node.type === 'TSAsExpression' ? component.node.expression : component.node);
markDisplayNameAsDeclared(astUtil.unwrapTSAsExpression(component.node));
},

'FunctionExpression, FunctionDeclaration, ArrowFunctionExpression'(node) {
Expand Down
8 changes: 6 additions & 2 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,18 @@ function isAssignmentLHS(node) {
);
}

function isTSAsExpression(node) {
return node && node.type === 'TSAsExpression';
}

/**
* Extracts the expression node that is wrapped inside a TS type assertion
*
* @param {ASTNode} node - potential TS node
* @returns {ASTNode} - unwrapped expression node
*/
function unwrapTSAsExpression(node) {
if (node && node.type === 'TSAsExpression') return node.expression;
return node;
return isTSAsExpression(node) ? node.expression : node;
}

function isTSTypeReference(node) {
Expand Down Expand Up @@ -450,6 +453,7 @@ module.exports = {
isFunctionLike,
inConstructor,
isNodeFirstInLine,
isTSAsExpression,
unwrapTSAsExpression,
traverseReturns,
isTSTypeReference,
Expand Down

0 comments on commit 4d99f6e

Please sign in to comment.