-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make getNameOfDeclaration public #15958
Conversation
src/compiler/utilities.ts
Outdated
|
||
export function getNameOfDeclaration(declaration: Declaration): DeclarationName { | ||
if (!declaration) { | ||
return undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mark return type as DeclarationName | undefined
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does getNameOfDeclaration
differ from getDeclarationName
? When should you prefer one over the other?
src/compiler/utilities.ts
Outdated
return undefined; | ||
case SpecialPropertyAssignmentKind.ExportsProperty: | ||
if (lhs.kind === SyntaxKind.Identifier) { | ||
return (lhs as PropertyAccessExpression).name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lhs as Identifier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The answer to both your comments is that this code is closely couple to the result of getSpecialPropertyAssignmentKind
, which returns ExportsProperty
in two cases.
In this case, the code is wrong and should be checking lhs.expression.kind
. I will fix it.
src/compiler/utilities.ts
Outdated
return (lhs as PropertyAccessExpression).name; | ||
} | ||
else { | ||
return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you know lhs.expression
is itself a PropertyAccessExpression
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the special property assignment kind is ExportsProperty
, lhs
is known to be a PropertyAccessExpression
and lhs.expression
is either an Identifier
or PropertyAccessExpression
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Furthermore, in the case that lhs.expression
is Identifier
, lhs.expression
is "exports"
and the name is lhs.name
(from exports.blah = ...
). If it's a property access expression, then lhs.expression.expression
is "module", lhs.expression.name
is "exports", and lhs.name
is the name we're interested in (from module.exports.blah = ...
).
So actually this code is wrong too. Changing it doesn't break any tests either, which is very suspicious. I don't think our checkJs coverage is very good.
Other differences:
|
There are actually 2 |
That one also delegates to |
Seems like the one in |
OK, your comments resulted in a considerably cleaned up and fixed |
`getNameOfDeclaration` now handles a lot of the special property assignment kinds in `getDeclarationName`
I think this is ready to go in. I did get rid of some now-dead special-case code in the binder. |
Move getNameOfDeclaration to the non-internal namespace in utilities.