Skip to content

Remove escaped names of well known symbols from string completions#19401

Merged
rbuckton merged 3 commits into
masterfrom
fix19349
Oct 30, 2017
Merged

Remove escaped names of well known symbols from string completions#19401
rbuckton merged 3 commits into
masterfrom
fix19349

Conversation

@rbuckton
Copy link
Copy Markdown
Contributor

@rbuckton rbuckton commented Oct 22, 2017

When reporting completions for string literals we were incorrectly including the internal escaped names of ES symbols, such as __@iterator or __@species.

Fixes #19349

@rbuckton rbuckton requested review from a user and weswigham October 22, 2017 00:24
Comment thread src/compiler/utilities.ts Outdated
return (identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? "_" + identifier : identifier) as __String;
}

export function isEscapedNameOfWellKnownSymbol(escapedName: __String) {
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.

With #15473, this won't have anything to do with well-known symbols.

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.

But for now, it's perfectly accurate.

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.

can we move this to services/utilities instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can move it. I'll rename it later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, I'll just inline it into getCompletionEntryDisplayNameForSymbol since we do similar tests in there.

Comment thread src/compiler/utilities.ts Outdated
return (identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? "_" + identifier : identifier) as __String;
}

export function isEscapedNameOfWellKnownSymbol(escapedName: __String) {
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.

But for now, it's perfectly accurate.

Comment thread src/services/completions.ts Outdated
if (!uniqueNames.has(id)) {
if (symbolToOriginInfoMap && symbolToOriginInfoMap[getUniqueSymbolId(symbol, typeChecker)]) {
entry.hasAction = true;
if (!isEscapedNameOfWellKnownSymbol(symbol.escapedName)) {
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.

if (isEscapedNameOfWellKnownSymbol(symbol.escapedName)) {
  continue;
}

to avoid the extra nesting? Likewise some of the conditions on the other nested if statements could be inverted and used with a continue to reduce the nesting a bit here.


// If the symbol is for a member of an object type and is the internal name of an ES
// symbol, it is not a valid entry. Internal names for ES symbols start with "__@"
if (symbol.flags & SymbolFlags.ClassMember) {
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.

I don't think internal names are specific to ClassMembers?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ESSymbol named members are. ClassMember is a misnomer. It includes Property, Method, and Accessor, which are all of the ESSymbol-nameable members.

// symbol, it is not a valid entry. Internal names for ES symbols start with "__@"
if (symbol.flags & SymbolFlags.ClassMember) {
const escapedName = symbol.escapedName as string;
if (escapedName.length >= 3 &&
Copy link
Copy Markdown
Member

@weswigham weswigham Oct 23, 2017

Choose a reason for hiding this comment

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

There's a isReservedMemberName inside checker.ts that we should just move to utilities and reuse (the negation of) here.

Copy link
Copy Markdown
Contributor Author

@rbuckton rbuckton Oct 23, 2017

Choose a reason for hiding this comment

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

isReservedMemberName explicitly does not remove ES symbols, only __call, __new, etc. since those aren't actually members. ES symbols, however, are members. Also, isReservedMemberName is already executed as part of the call to getApparentProperties on line 287.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wonder if SymbolObject.prototype.name should return undefined for internal names, though I'm not sure if that would have other undesirable effects.

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.

isReservedMemberName explicitly does not remove ES symbols

I misread it, OK. Would still be nice to have all these escaped name inspection snippets in one place... doesn't really matter, though.

I wonder if SymbolObject.prototype.name should return undefined for internal names, though I'm not sure if that would have other undesirable effects.

Ideally we (internally) should never use .name on an internally named symbol, since we (internally) swapped everything to .__escapedName before re-implementing .name (with proper escaping) for external compatibility. Would making it return undefined on internal symbol names likely break any external consumers? If no, I'm all for it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I might look at the .name thing some other time.

@rbuckton
Copy link
Copy Markdown
Contributor Author

@DanielRosenwasser, any other comments?

@rbuckton rbuckton merged commit 91c37f7 into master Oct 30, 2017
@rbuckton rbuckton deleted the fix19349 branch October 30, 2017 19:37
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
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