Skip to content

Add 'name' property to Identifier#17329

Merged
5 commits merged intomasterfrom
idName
Jul 25, 2017
Merged

Add 'name' property to Identifier#17329
5 commits merged intomasterfrom
idName

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Jul 20, 2017

id.name will be more conventent than calling ts.unescapeLeadingUnderscores(id.text).

@ghost ghost requested a review from weswigham July 20, 2017 19:20
@DanielRosenwasser
Copy link
Copy Markdown
Member

This is pretty awkward when so many places that reference identifiers are named name. So you'll write something like

myFunc.name.name

@ghost
Copy link
Copy Markdown
Author

ghost commented Jul 20, 2017

Any alternative suggestions?

@DanielRosenwasser
Copy link
Copy Markdown
Member

DanielRosenwasser commented Jul 20, 2017

You could potentially just specialize getText() instead. Or unescapedText.

@weswigham
Copy link
Copy Markdown
Member

weswigham commented Jul 21, 2017

If we're trying to get some API compatibility back, why not rename identifier.text to identifier.escapedText, then make identifier.text be the unescaped version? It means our consumers could still have hidden underscore related costs; but we still won't. Should probably also make escapeIdentifier and unescapeIdentifier identity functions if we do this (since old code will be passing .text into them).

@ghost
Copy link
Copy Markdown
Author

ghost commented Jul 21, 2017

@weswigham Implemented that suggestion. Had to fix a few invalid casts along the way, since previously (node as Identifier).text also applied to LiteralExpressions and vice versa.
Also discovered a few issues along the way: #17345, #17348, #17347, #17346.
I'll do a similar PR for symbol.name after this is in. I guess you should also revert your PR to tsutils.

Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to prevent our pre 2.5 API consumers from suddenly having bugs relating to over-unescaping things, we should also likely make escapeIdentifier and unescapeIdentifier equal to x => x. But aside from that, I think this keeps us good internally while keeping out API very backcompat. Probably want someone else to glance at this, but I like it.

Comment thread src/services/services.ts

get unescapedText(): string {
get text(): string {
return unescapeLeadingUnderscores(this.escapedText);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we cache this since the node is supposed to be immutable? Potentially return (delete this.text, (this as any).text = unescapeLeadingUnderscores(this.escapedText)) or using an internal field. Not sure which is better.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could that wait for another PR? I would want to see the performance difference between testing for _ every time we call .text vs adding an extra field to every Identifier.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this.text would be the worst you could do. V8 will deoptimize the whole object and your optimization becomes a deoptimization.

If you really need to optimize here (I doubt that), you can use Object.definePropery to override the getter with the computed value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Andy-MS Yeah, sure; it can wait until its needed.

@weswigham
Copy link
Copy Markdown
Member

@Andy-MS Re: PR to tsutils - that PR will still function as intended (across all TS versions!) if we make escapeIdentifier and unescapeIdentifier into identity functions now. I don't think it should be reverted since it also likely fixed a bunch of bugs WRT not unescaping things in their code base, and is still needed to do just that in old versions of TS where .text is escaped.

@ghost ghost merged commit eadd084 into master Jul 25, 2017
@ghost ghost deleted the idName branch July 25, 2017 20:16
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
eos3tion referenced this pull request in domchen/typescript-plus Nov 3, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants