diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index b4de069542..7108367ec2 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -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) { diff --git a/lib/util/ast.js b/lib/util/ast.js index 5664dcb512..8d0ec3cad8 100644 --- a/lib/util/ast.js +++ b/lib/util/ast.js @@ -339,6 +339,10 @@ function isAssignmentLHS(node) { ); } +function isTSAsExpression(node) { + return node && node.type === 'TSAsExpression'; +} + /** * Extracts the expression node that is wrapped inside a TS type assertion * @@ -346,8 +350,7 @@ function isAssignmentLHS(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) { @@ -450,6 +453,7 @@ module.exports = { isFunctionLike, inConstructor, isNodeFirstInLine, + isTSAsExpression, unwrapTSAsExpression, traverseReturns, isTSTypeReference,