-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Synthesize namespace records for proper esm interop #19675
Changes from 20 commits
213146d
b5a7ef9
8abc907
2d0c8d3
a725916
e910603
e84b051
f23d41c
ebd4c03
3b33df0
7ff11bb
bcafdba
eaf2fc5
29c731c
4350caf
e20a548
578141d
dfe5675
8c6c313
3d996d7
2f9c240
2361f37
357996b
a682476
6e9e874
a654b82
ef6faf1
a8233b3
f4f4e84
386c54d
2663db7
8068e2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3519,6 +3519,14 @@ | |
"category": "Error", | ||
"code": 7036 | ||
}, | ||
"Enables emit interoperability bewteen commonjs and ES Modules via creation of namespace objects for all imports. Implies `allowSyntheticDefaultImports`.": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. between CommonJS Stick with consistent inner quote marks - we usually use apostrophes/single-quotes. |
||
"category": "Message", | ||
"code": 7037 | ||
}, | ||
"A namespace-style import cannot be called or constructed, and will cause a failure at runtime.": { | ||
"category": "Error", | ||
"code": 7038 | ||
}, | ||
|
||
"You cannot rename this element.": { | ||
"category": "Error", | ||
|
@@ -3886,5 +3894,9 @@ | |
"Install '{0}'": { | ||
"category": "Message", | ||
"code": 95014 | ||
}, | ||
"Replace import with '{0}'.": { | ||
"category": "Message", | ||
"code": 95015 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1586,6 +1586,7 @@ namespace ts { | |
// synthesize 'import "tslib"' declaration | ||
const externalHelpersModuleReference = createLiteral(externalHelpersModuleNameText); | ||
const importDecl = createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, /*importClause*/ undefined); | ||
importDecl.transformFlags |= TransformFlags.NeverApplyImportHelper; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only place that should be setting |
||
externalHelpersModuleReference.parent = importDecl; | ||
importDecl.parent = file; | ||
imports = [externalHelpersModuleReference]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,14 +25,14 @@ namespace ts { | |
if (externalHelpersModuleName) { | ||
const statements: Statement[] = []; | ||
const statementOffset = addPrologue(statements, node.statements); | ||
append(statements, | ||
createImportDeclaration( | ||
/*decorators*/ undefined, | ||
/*modifiers*/ undefined, | ||
createImportClause(/*name*/ undefined, createNamespaceImport(externalHelpersModuleName)), | ||
createLiteral(externalHelpersModuleNameText) | ||
) | ||
const tslibImport = createImportDeclaration( | ||
/*decorators*/ undefined, | ||
/*modifiers*/ undefined, | ||
createImportClause(/*name*/ undefined, createNamespaceImport(externalHelpersModuleName)), | ||
createLiteral(externalHelpersModuleNameText) | ||
); | ||
tslibImport.transformFlags |= TransformFlags.NeverApplyImportHelper; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't set transform flags explicitly. See my related comment in program.ts. |
||
append(statements, tslibImport); | ||
|
||
addRange(statements, visitNodes(node.statements, visitor, isStatement, statementOffset)); | ||
return updateSourceFileNode( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,7 @@ namespace ts { | |
createLiteral(externalHelpersModuleNameText)); | ||
|
||
if (externalHelpersImportDeclaration) { | ||
externalHelpersImportDeclaration.transformFlags |= TransformFlags.NeverApplyImportHelper; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't set transform flags explicitly. See my related comment in program.ts. |
||
externalImports.unshift(externalHelpersImportDeclaration); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3165,6 +3165,7 @@ namespace ts { | |
bindingElement?: BindingElement; // Binding element associated with property symbol | ||
exportsSomeValue?: boolean; // True if module exports some value (not just types) | ||
enumKind?: EnumKind; // Enum declaration classification | ||
originatingImport?: ImportDeclaration | ImportCall; // Import declaration which produced the symbol, present if the symbol is poisoned | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know what do you mean by poisoned, but will a future reader? |
||
lateSymbol?: Symbol; // Late-bound symbol for a computed property | ||
} | ||
|
||
|
@@ -3877,6 +3878,7 @@ namespace ts { | |
typeRoots?: string[]; | ||
/*@internal*/ version?: boolean; | ||
/*@internal*/ watch?: boolean; | ||
ESModuleInterop?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The capitalization of this option doesn't align with other options. Should this instead be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, there's prior work in the form of |
||
|
||
[option: string]: CompilerOptionsValue | JsonSourceFile | undefined; | ||
} | ||
|
@@ -4344,6 +4346,7 @@ namespace ts { | |
ContainsYield = 1 << 24, | ||
ContainsHoistedDeclarationOrCompletion = 1 << 25, | ||
ContainsDynamicImport = 1 << 26, | ||
NeverApplyImportHelper = 1 << 27, // Indicates the node should never be wrapped with an import star helper (because, for example, it import tslib itself) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be on |
||
|
||
// Please leave this as 1 << 29. | ||
// It is the maximum bit we can set before we outgrow the size of a v8 small integer (SMI) on an x86 system. | ||
|
@@ -4383,6 +4386,7 @@ namespace ts { | |
// - Additional bitmasks | ||
TypeScriptClassSyntaxMask = ContainsParameterPropertyAssignments | ContainsPropertyInitializer | ContainsDecorators, | ||
ES2015FunctionSyntaxMask = ContainsCapturedLexicalThis | ContainsDefaultValueAssignments, | ||
StickyFlags = NeverApplyImportHelper, // Flags which once set are retained by `aggregateTransformFlags` instead of overwritten | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need this if |
||
} | ||
|
||
export interface SourceMapRange extends TextRange { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1466,7 +1466,9 @@ namespace ts { | |
* Aggregates the TransformFlags for a Node and its subtree. | ||
*/ | ||
export function aggregateTransformFlags<T extends Node>(node: T): T { | ||
const stickyFlags = node.transformFlags & TransformFlags.StickyFlags; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is a good approach. |
||
aggregateTransformFlagsForNode(node); | ||
node.transformFlags |= stickyFlags; | ||
return node; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
registerCodeFix({ | ||
errorCodes: [Diagnostics.A_namespace_style_import_cannot_be_called_or_constructed_and_will_cause_a_failure_at_runtime.code], | ||
getCodeActions: getActionsForInvalidImport | ||
}); | ||
|
||
function getActionsForInvalidImport(context: CodeFixContext): CodeAction[] | undefined { | ||
const sourceFile = context.sourceFile; | ||
|
||
// This is the whole import statement, eg: | ||
// import * as Bluebird from 'bluebird'; | ||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
const node = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false).parent as ImportDeclaration; | ||
const namespace = getNamespaceDeclarationNode(node) as NamespaceImport; | ||
const opts = context.program.getCompilerOptions(); | ||
const variations: CodeAction[] = []; | ||
if (opts.module === ModuleKind.CommonJS || (!opts.module && opts.target < ScriptTarget.ES2015)) { | ||
// import Bluebird = require("bluebird"); | ||
const replacement = createImportEqualsDeclaration( | ||
/*decorators*/ undefined, | ||
/*modifiers*/ undefined, | ||
namespace.name, | ||
createExternalModuleReference(node.moduleSpecifier) | ||
); | ||
const changeTracker = textChanges.ChangeTracker.fromContext(context); | ||
changeTracker.replaceNode(sourceFile, node, replacement, { useNonAdjustedEndPosition: true }); | ||
const changes = changeTracker.getChanges(); | ||
variations.push({ | ||
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Replace_import_with_0), [changes[0].textChanges[0].newText]), | ||
changes | ||
}); | ||
} | ||
|
||
// import Bluebird from "bluebird"; | ||
const replacement = createImportDeclaration( | ||
/*decorators*/ undefined, | ||
/*modifiers*/ undefined, | ||
createImportClause(namespace.name, /*namedBindings*/ undefined), | ||
node.moduleSpecifier | ||
); | ||
const changeTracker = textChanges.ChangeTracker.fromContext(context); | ||
changeTracker.replaceNode(sourceFile, node, replacement, { useNonAdjustedEndPosition: true }); | ||
const changes = changeTracker.getChanges(); | ||
variations.push({ | ||
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Replace_import_with_0), [changes[0].textChanges[0].newText]), | ||
changes | ||
}); | ||
|
||
return variations; | ||
} | ||
} |
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.
should we make it an error to specify
--ESModuleInterop
with--module
>=ES2015
?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.
When
module
>=ES2015
, it's just a synonym forallowSyntheticDefaultImports
. IMO it can be left as not-an-error, since you may still want it for interop if, eg, you're using babel for emit.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.
but our output does not handle that case, and
--allowSyntheticDefaultImports
already does that..