Skip to content
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

Add module: hybrid #29353

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ namespace ts {

if (!dontResolveAlias && symbol) {
if (!(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable)) && !getDeclarationOfKind(symbol, SyntaxKind.SourceFile)) {
const compilerOptionName = moduleKind >= ModuleKind.ES2015
const compilerOptionName = moduleKind >= ModuleKind.Hybrid
? "allowSyntheticDefaultImports"
: "esModuleInterop";

Expand Down Expand Up @@ -26473,7 +26473,7 @@ namespace ts {
*/
function checkClassNameCollisionWithObject(name: Identifier): void {
if (languageVersion === ScriptTarget.ES5 && name.escapedText === "Object"
&& moduleKind !== ModuleKind.ES2015 && moduleKind !== ModuleKind.ESNext) {
&& moduleKind !== ModuleKind.ES2015 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.Hybrid) {
error(name, Diagnostics.Class_name_cannot_be_Object_when_targeting_ES5_with_module_0, ModuleKind[moduleKind]); // https://github.com/Microsoft/TypeScript/issues/17494
}
}
Expand Down Expand Up @@ -27582,7 +27582,7 @@ namespace ts {
error(node.moduleSpecifier, Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol));
}

if (moduleKind !== ModuleKind.System && moduleKind !== ModuleKind.ES2015 && moduleKind !== ModuleKind.ESNext) {
if (moduleKind !== ModuleKind.System && moduleKind !== ModuleKind.ES2015 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.Hybrid) {
checkExternalEmitHelpers(node, ExternalEmitHelpers.ExportStar);
}
}
Expand Down Expand Up @@ -30706,7 +30706,7 @@ namespace ts {
return grammarErrorOnNode(node.exclamationToken, Diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context);
}

if (compilerOptions.module !== ModuleKind.ES2015 && compilerOptions.module !== ModuleKind.ESNext && compilerOptions.module !== ModuleKind.System && !compilerOptions.noEmit &&
if (compilerOptions.module !== ModuleKind.ES2015 && compilerOptions.module !== ModuleKind.ESNext && compilerOptions.module !== ModuleKind.Hybrid && compilerOptions.module !== ModuleKind.System && !compilerOptions.noEmit &&
!(node.parent.parent.flags & NodeFlags.Ambient) && hasModifier(node.parent.parent, ModifierFlags.Export)) {
checkESModuleMarker(node.name);
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ namespace ts {
umd: ModuleKind.UMD,
es6: ModuleKind.ES2015,
es2015: ModuleKind.ES2015,
esnext: ModuleKind.ESNext
esnext: ModuleKind.ESNext,
hybrid: ModuleKind.Hybrid
}),
affectsModuleResolution: true,
paramType: Diagnostics.KIND,
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4541,7 +4541,8 @@ namespace ts {
let create = (hasExportStarsToExportValues || (compilerOptions.esModuleInterop && hasImportStarOrImportDefault))
&& moduleKind !== ModuleKind.System
&& moduleKind !== ModuleKind.ES2015
&& moduleKind !== ModuleKind.ESNext;
&& moduleKind !== ModuleKind.ESNext
&& moduleKind !== ModuleKind.Hybrid;
if (!create) {
const helpers = getEmitHelpers(node);
if (helpers) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ts {
switch (moduleKind) {
case ModuleKind.ESNext:
case ModuleKind.ES2015:
case ModuleKind.Hybrid:
return transformES2015Module;
case ModuleKind.System:
return transformSystemModule;
Expand Down
34 changes: 29 additions & 5 deletions src/compiler/transformers/module/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,42 @@ namespace ts {
function visitor(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.ImportEqualsDeclaration:
// Elide `import=` as it is not legal with --module ES6
return undefined;
return visitImportAssignment(<ImportEqualsDeclaration>node);
case SyntaxKind.ExportAssignment:
return visitExportAssignment(<ExportAssignment>node);
}

return node;
}

function visitExportAssignment(node: ExportAssignment): VisitResult<ExportAssignment> {
// Elide `export=` as it is not legal with --module ES6
return node.isExportEquals ? undefined : node;
function visitImportAssignment(node: ImportEqualsDeclaration): VisitResult<Node> {
if (isExternalModuleReference(node.moduleReference)) {
// We issue an error for an import= when targeting es6, however we will emit is as a cjs-style `require` for hybrid environments
return setOriginalNode(setTextRange(createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList([
createVariableDeclaration(
node.name,
/*type*/ undefined,
createCall(
createIdentifier("require"),
/*typeArguments*/ undefined,
[node.moduleReference.expression]
)
)],
getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015 ? NodeFlags.Const : 0
)
), node), node);
}
return undefined; // ts transformer should have handled namespacy import assignments
}

function visitExportAssignment(node: ExportAssignment): VisitResult<Node> {
if (!node.isExportEquals) {
return node;
}
// We issue an error for an export= when targeting es6, however we will emit is as a cjs-style `module.export=` for hybrid environments
return setOriginalNode(setTextRange(createExpressionStatement(createAssignment(createPropertyAccess(createIdentifier("module"), "exports"), node.expression)), node), node);
}

//
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ namespace ts {

function visitSourceFile(node: SourceFile) {
const alwaysStrict = getStrictOptionValue(compilerOptions, "alwaysStrict") &&
!(isExternalModule(node) && moduleKind >= ModuleKind.ES2015) &&
!(isExternalModule(node) && (moduleKind >= ModuleKind.ES2015 || moduleKind === ModuleKind.Hybrid)) &&
!isJsonSourceFile(node);

return updateSourceFileNode(
Expand Down Expand Up @@ -2847,6 +2847,7 @@ namespace ts {
|| (isExternalModuleExport(node)
&& moduleKind !== ModuleKind.ES2015
&& moduleKind !== ModuleKind.ESNext
&& moduleKind !== ModuleKind.Hybrid
&& moduleKind !== ModuleKind.System);
}

Expand Down
5 changes: 3 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4603,8 +4603,9 @@ namespace ts {
AMD = 2,
UMD = 3,
System = 4,
ES2015 = 5,
ESNext = 6
Hybrid = 5,
ES2015 = 6,
ESNext = 7,
}

export const enum JsxEmit {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7175,6 +7175,7 @@ namespace ts {
case ModuleKind.AMD:
case ModuleKind.ES2015:
case ModuleKind.ESNext:
case ModuleKind.Hybrid:
return true;
default:
return false;
Expand Down
1 change: 1 addition & 0 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ namespace ts.codefix {
case ModuleKind.AMD:
case ModuleKind.CommonJS:
case ModuleKind.UMD:
case ModuleKind.Hybrid:
return ImportKind.Equals;
case ModuleKind.System:
case ModuleKind.ES2015:
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/config/commandLineParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace ts {
start: undefined,
length: undefined,
}, {
messageText: "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext'.",
messageText: "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext', 'hybrid'.",
category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code,

Expand Down
5 changes: 3 additions & 2 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2527,8 +2527,9 @@ declare namespace ts {
AMD = 2,
UMD = 3,
System = 4,
ES2015 = 5,
ESNext = 6
Hybrid = 5,
ES2015 = 6,
ESNext = 7
}
enum JsxEmit {
None = 0,
Expand Down
5 changes: 3 additions & 2 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2527,8 +2527,9 @@ declare namespace ts {
AMD = 2,
UMD = 3,
System = 4,
ES2015 = 5,
ESNext = 6
Hybrid = 5,
ES2015 = 6,
ESNext = 7
}
enum JsxEmit {
None = 0,
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/es6ExportAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export = a;

//// [es6ExportAssignment.js]
var a = 10;
module.exports = a;
1 change: 1 addition & 0 deletions tests/baselines/reference/es6ExportAssignment2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import * as a from "a";

//// [a.js]
var a = 10;
module.exports = a; // Error: export = not allowed in ES6
//// [b.js]
1 change: 1 addition & 0 deletions tests/baselines/reference/es6ExportEquals.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export = f;

//// [es6ExportEquals.js]
export function f() { }
module.exports = f;


//// [es6ExportEquals.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ tests/cases/compiler/server.ts(2,1): error TS1203: Export assignment cannot be u
import a = require("server");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
void a;

==== tests/cases/compiler/server.ts (1 errors) ====
var a = 10;
export = a;
Expand Down
7 changes: 6 additions & 1 deletion tests/baselines/reference/es6ImportEqualsDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ var a = 10;
export = a;

//// [client.ts]
import a = require("server");
import a = require("server");
void a;


//// [server.js]
var a = 10;
module.exports = a;
//// [client.js]
const a = require("server");
void a;
3 changes: 3 additions & 0 deletions tests/baselines/reference/es6ImportEqualsDeclaration.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import a = require("server");
>a : Symbol(a, Decl(client.ts, 0, 0))

void a;
>a : Symbol(a, Decl(client.ts, 0, 0))

=== tests/cases/compiler/server.ts ===
var a = 10;
>a : Symbol(a, Decl(server.ts, 0, 3))
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/es6ImportEqualsDeclaration.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import a = require("server");
>a : number

void a;
>void a : undefined
>a : number

=== tests/cases/compiler/server.ts ===
var a = 10;
>a : number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var a = /** @class */ (function () {
}
return a;
}());
module.exports = a;
//// [main.js]
import * as a from "./a";
a;
31 changes: 31 additions & 0 deletions tests/baselines/reference/hybridModuleEmitNoImportEqualsError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/hybridModuleEmitNoImportEqualsError.ts] ////

//// [cjsyexporter.ts]
function f(): void {}
export = f;
//// [esmyexporter.ts]
const x = 0;
const y = 0;
export {x, y}
//// [importer.ts]
import f = require("./cjsyexporter");
f();

import {x, y} from "./esmyexporter";
void x;
void y;


//// [cjsyexporter.js]
function f() { }
module.exports = f;
//// [esmyexporter.js]
const x = 0;
const y = 0;
export { x, y };
//// [importer.js]
const f = require("./cjsyexporter");
f();
import { x, y } from "./esmyexporter";
void x;
void y;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/importer.ts ===
import f = require("./cjsyexporter");
>f : Symbol(f, Decl(importer.ts, 0, 0))

f();
>f : Symbol(f, Decl(importer.ts, 0, 0))

import {x, y} from "./esmyexporter";
>x : Symbol(x, Decl(importer.ts, 3, 8))
>y : Symbol(y, Decl(importer.ts, 3, 10))

void x;
>x : Symbol(x, Decl(importer.ts, 3, 8))

void y;
>y : Symbol(y, Decl(importer.ts, 3, 10))

=== tests/cases/compiler/cjsyexporter.ts ===
function f(): void {}
>f : Symbol(f, Decl(cjsyexporter.ts, 0, 0))

export = f;
>f : Symbol(f, Decl(cjsyexporter.ts, 0, 0))

=== tests/cases/compiler/esmyexporter.ts ===
const x = 0;
>x : Symbol(x, Decl(esmyexporter.ts, 0, 5))

const y = 0;
>y : Symbol(y, Decl(esmyexporter.ts, 1, 5))

export {x, y}
>x : Symbol(x, Decl(esmyexporter.ts, 2, 8))
>y : Symbol(y, Decl(esmyexporter.ts, 2, 10))

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/importer.ts ===
import f = require("./cjsyexporter");
>f : () => void

f();
>f() : void
>f : () => void

import {x, y} from "./esmyexporter";
>x : 0
>y : 0

void x;
>void x : undefined
>x : 0

void y;
>void y : undefined
>y : 0

=== tests/cases/compiler/cjsyexporter.ts ===
function f(): void {}
>f : () => void

export = f;
>f : () => void

=== tests/cases/compiler/esmyexporter.ts ===
const x = 0;
>x : 0
>0 : 0

const y = 0;
>y : 0
>0 : 0

export {x, y}
>x : 0
>y : 0

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext'.
error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext', 'hybrid'.


!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext'.
!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext', 'hybrid'.
==== file.ts (0 errors) ====

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext'.
error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext', 'hybrid'.


!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext'.
!!! error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'esnext', 'hybrid'.
==== file.ts (0 errors) ====