Skip to content

Give mapped type properties a synthetic declaration name#18023

Merged
weswigham merged 4 commits into
microsoft:masterfrom
weswigham:declaration-quoted-names
Aug 24, 2017
Merged

Give mapped type properties a synthetic declaration name#18023
weswigham merged 4 commits into
microsoft:masterfrom
weswigham:declaration-quoted-names

Conversation

@weswigham
Copy link
Copy Markdown
Member

Which escapes them appropriately if need be, as is suitable for printing.
Fixes #18012

@weswigham weswigham requested review from a user and mhegazy August 24, 2017 19:34
Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

CC @sandersn could this happen for spread types too?
Also, why can't we just go to the syntheticOrigin and print its name like we normally would?

Comment thread src/compiler/checker.ts Outdated
return "(Anonymous function)";
}
}
if (hasProperty(symbol, "syntheticName")) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would rather see if ((symbol as TransientSymbol).syntheticName), because hasProperty isn't type-checked.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(symbol as TransientSymbol).syntheticName is falsy for "", so it would have to be typeof (symbol as TransientSymbol).syntheticName === "string"

export type Timespans =
'1 hour' | '4 hours' | '12 hours' | '1 day' | '3 days' | '1 week' | '1 month';
const TIMESPANS: {[k in Timespans]: number} = {
'1 hour': 1 * 60 * 60 * 1000,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd rather see the smallest test possible, so long as it failed before this PR and succeeds after.

Comment thread src/compiler/checker.ts Outdated
prop.syntheticOrigin = propertySymbol;
prop.declarations = propertySymbol.declarations;
}
else if (!isIdentifierText((<StringLiteralType>t).value, compilerOptions.target)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: Would be prettier to access value once at the top of this block

Copy link
Copy Markdown
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

I'm not sure this is the correct way to fix the bug.

Comment thread src/compiler/checker.ts Outdated
}
}
if (hasProperty(symbol, "syntheticName")) {
return (symbol as TransientSymbol).syntheticName;
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 you use symbol.syntheticOrigin.name? (or escapedName, not sure which). Even if it doesn't exist, creating a synthetic symbol for syntheticOrigin might make more sense than creating a standalone name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No - the symbol will have no syntheticOrigin, as it has no source type to draw a named declaration from.

Comment thread src/compiler/checker.ts Outdated
function addMemberForKeyType(t: Type, propertySymbol?: Symbol) {
function addMemberForKeyType(t: Type, propertySymbolOrIndex?: Symbol | number) {
let propertySymbol: Symbol;
// forEachType offloads to forEach, which calls with a numeric second argument
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 can't comment on line 5724 because of github's limitations, but I would just change that to foreachType(iterationType, (p,i) => addMemberForKeyType(p)) and remove this whole changed section.

Copy link
Copy Markdown
Member Author

@weswigham weswigham Aug 24, 2017

Choose a reason for hiding this comment

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

We usually avoid inline lambdas (for things which could get called a lot), from what I've been told, since they don't get optimized as well as a named function. Could be false, I dunno.

Comment thread src/compiler/checker.ts Outdated
prop.declarations = propertySymbol.declarations;
}
else if (!isIdentifierText((<StringLiteralType>t).value, compilerOptions.target)) {
prop.syntheticName = `"${escapeString((<StringLiteralType>t).value, CharacterCodes.doubleQuote)}"`;
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.

why use escapeString instead of escapeLeadingUnderscores as in propName?

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.

What is propName in the buggy case, anyway? Why not change propName in this case to be the version that's called with escapeString, because then line 5748 (const prop = createSymbol(...) would do this name lookup that we need for free.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

escapeString escapes the specified quotemark, to make the string suitable for use within quotes. escapeLeadingUnderscores converts a string to a __String by adding extra leading underscore, if needed. Totally different.

@weswigham
Copy link
Copy Markdown
Member Author

weswigham commented Aug 24, 2017

@Andy-MS These symbols have no synthetic origin, since a union of string literal types does point to any NamedDeclarations (specifically property, method, or accessor declarations, I suppose), unlike a homogenous mapped type - which knows that it got its names from the input to the keyof operator.

@weswigham weswigham force-pushed the declaration-quoted-names branch from 75c34d0 to 1215a99 Compare August 24, 2017 23:25
Comment thread src/compiler/checker.ts Outdated
function addMemberForKeyType(t: Type, propertySymbol?: Symbol) {
function addMemberForKeyType(t: Type, propertySymbolOrIndex?: Symbol | number) {
let propertySymbol: Symbol;
// forEachType offloads to forEach, which calls with a numeric second argument
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.

nit:delegates

@weswigham weswigham merged commit f824e72 into microsoft:master Aug 24, 2017
@weswigham weswigham deleted the declaration-quoted-names branch August 24, 2017 23:48
@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.

3 participants