Skip to content

Create synthetic default exports for dynamic imports#17492

Merged
weswigham merged 7 commits into
microsoft:masterfrom
weswigham:synthetic-dynamic-imports
Aug 9, 2017
Merged

Create synthetic default exports for dynamic imports#17492
weswigham merged 7 commits into
microsoft:masterfrom
weswigham:synthetic-dynamic-imports

Conversation

@weswigham
Copy link
Copy Markdown
Member

Fixes #17444

If synthetic default imports is on, we now intersect an anonymous object with a default member equal to the module with the module type itself iff there is no module default member already.

Comment thread src/compiler/checker.ts Outdated
memberTable.set(InternalSymbolName.Default, newSymbol);
const anonymousSymbol = createSymbol(SymbolFlags.TypeLiteral, InternalSymbolName.Type);
const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
const resolved = resolveStructuredTypeMembers(defaultContainingObject);
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.

resolveStructuredTypeMembers is a no-op since createAnonymousType sets the members propery.

Comment thread src/compiler/checker.ts Outdated
const anonymousSymbol = createSymbol(SymbolFlags.TypeLiteral, InternalSymbolName.Type);
const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
const resolved = resolveStructuredTypeMembers(defaultContainingObject);
resolved.properties = [newSymbol];
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.

This shouldn't be necessary since createAnonymousType sets properties for you.

Comment thread src/compiler/checker.ts Outdated
if (!defaultSymbol) {
const memberTable = createSymbolTable();
const newSymbol = createSymbol(SymbolFlags.Alias, InternalSymbolName.Default);
getSymbolLinks(newSymbol).target = resolveSymbol(symbol);
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.

The getSymbolLinks call is unnecessary here. newSymbol is already a TransientSymbol which is its own SymbolLinks.

Comment thread src/compiler/checker.ts Outdated
const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
const resolved = resolveStructuredTypeMembers(defaultContainingObject);
resolved.properties = [newSymbol];
getSymbolLinks(anonymousSymbol).type = defaultContainingObject;
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.

As with newSymbol above, anonymousSymbol is a TransientSymbol which is its own SymbolLinks.

Comment thread src/compiler/checker.ts Outdated
if (allowSyntheticDefaultImports && type && type !== unknownType) {
const synthType = type as SyntheticDefaultModuleType;
if (!synthType.syntheticType) {
const defaultSymbol = getPropertyOfType(type, InternalSymbolName.Default);
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.

defaultSymbol is only used once, consider inlining this into the if condition.

Comment thread src/compiler/checker.ts Outdated
const resolved = resolveStructuredTypeMembers(defaultContainingObject);
resolved.properties = [newSymbol];
getSymbolLinks(anonymousSymbol).type = defaultContainingObject;
const newType = getIntersectionType([type, defaultContainingObject]);
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.

newType is only used once, consider inlining the variable.

@weswigham weswigham force-pushed the synthetic-dynamic-imports branch from 0230d36 to 7c72c4f Compare August 2, 2017 22:25
@weswigham
Copy link
Copy Markdown
Member Author

@rbuckton done.

Copy link
Copy Markdown
Contributor

@rbuckton rbuckton left a comment

Choose a reason for hiding this comment

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

One minor comment, then it looks good to merge.

Comment thread src/compiler/checker.ts Outdated
return createPromiseReturnType(node, anyType);
}

function syntheticDefaultType(type: Type, symbol: Symbol): Type {
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 you use a more descriptive name like getTypeWithSyntheticDefaultImport? At a very high level this behaves similarly to getTypeWithThisArgument and getTypeWithDefault and as such should share a similar nomenclature.

@weswigham weswigham merged commit 43e758e into microsoft:master Aug 9, 2017
@weswigham weswigham deleted the synthetic-dynamic-imports branch August 9, 2017 00:01
@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