-
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 8 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 |
---|---|---|
|
@@ -390,6 +390,12 @@ namespace ts { | |
category: Diagnostics.Module_Resolution_Options, | ||
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking | ||
}, | ||
{ | ||
name: "strictESM", | ||
type: "boolean", | ||
category: Diagnostics.Module_Resolution_Options, | ||
description: Diagnostics.Create_namespace_objects_for_ECMAScript_imports_which_are_neither_callable_nor_constructable_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.
we want that to advertise that as much as we can. |
||
}, | ||
{ | ||
name: "preserveSymlinks", | ||
type: "boolean", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3326,6 +3326,10 @@ | |
"category": "Message", | ||
"code": 6186 | ||
}, | ||
"Import is called or constructed, which is not valid ES2015 module usage, and will fail at runtime.": { | ||
"category": "Error", | ||
"code": 7004 | ||
}, | ||
"Variable '{0}' implicitly has an '{1}' type.": { | ||
"category": "Error", | ||
"code": 7005 | ||
|
@@ -3438,6 +3442,10 @@ | |
"category": "Error", | ||
"code": 7036 | ||
}, | ||
"Create namespace objects for ECMAScript imports which are neither callable nor constructable, 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. this seems hard to follow.. we a more user friendly description.. something that has interop, ES modules and commonjs. @DanielRosenwasser thoughts? 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've changed it to 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. Create namespace objects when using ECMAScript imports on modules that do not use ECMAScript exports. 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. I changed it to |
||
"category": "Message", | ||
"code": 7037 | ||
}, | ||
|
||
"You cannot rename this element.": { | ||
"category": "Error", | ||
|
@@ -3797,5 +3805,9 @@ | |
"Convert to default import": { | ||
"category": "Message", | ||
"code": 95013 | ||
}, | ||
"Replace import with '{0}'.": { | ||
"category": "Message", | ||
"code": 95014 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1593,6 +1593,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 |
---|---|---|
|
@@ -3102,6 +3102,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? |
||
} | ||
|
||
/* @internal */ | ||
|
@@ -3799,6 +3800,7 @@ namespace ts { | |
typeRoots?: string[]; | ||
/*@internal*/ version?: boolean; | ||
/*@internal*/ watch?: boolean; | ||
strictESM?: boolean; | ||
|
||
[option: string]: CompilerOptionsValue | JsonSourceFile | undefined; | ||
} | ||
|
@@ -4266,6 +4268,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. | ||
|
@@ -4305,6 +4308,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.Import_is_called_or_constructed_which_is_not_valid_ES2015_module_usage_and_will_fail_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.
This is not a strict option per se.. this is an interop flag.. i would not use
strict
prefix since we have already given it a different meaning with--strict
.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.
🚲 🏠 🕐 Alright,
--standardESM
then?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.
Spoke with daniel, gunna try
--ESMInterop