Skip to content

Commit 76fa4b8

Browse files
author
James Whitney
committed
Add support for --noEmitHelpers flag
This PR is a Work In Progress that addresses multiple `__extends` being output as described in #1350: Multiple `__extends` being output when `--module amd` is set. The issue still exists as of `v1.5.0 - f53e6a8`. Apparently a fix was created for this in #1356 but according to #2009, a [comment](#2009 (comment)) later indicated that this was never merged in. Further conversation continued in #2487 but did not yield any result. I refer to my earlier recommendation in #1350. > My question is this, would the TypeScript team be open to a flag that > can be passed to tsc that will generate something like the following > ```ts > define(["require", "exports", "__extends", './mammal'], function (require, exports, __extends, Mammal) { > var Human = (function (_super) { > __extends(Human, _super); > function Human() { > _super.apply(this, arguments); > } > return Human; > })(Mammal); > return Human; > }); > ``` To continue with the naming convention I have chosen the flag `--noEmitHelpers`.
1 parent 64f3798 commit 76fa4b8

File tree

10 files changed

+80
-16
lines changed

10 files changed

+80
-16
lines changed

bin/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,7 @@ declare module "typescript" {
10871087
mapRoot?: string;
10881088
module?: ModuleKind;
10891089
noEmit?: boolean;
1090+
noEmitHelpers?: boolean;
10901091
noEmitOnError?: boolean;
10911092
noErrorTruncation?: boolean;
10921093
noImplicitAny?: boolean;

bin/typescriptServices.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,7 @@ declare module ts {
10871087
mapRoot?: string;
10881088
module?: ModuleKind;
10891089
noEmit?: boolean;
1090+
noEmitHelpers?: boolean;
10901091
noEmitOnError?: boolean;
10911092
noErrorTruncation?: boolean;
10921093
noImplicitAny?: boolean;

src/compiler/commandLineParser.ts

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ module ts {
7171
type: "boolean",
7272
description: Diagnostics.Do_not_emit_outputs,
7373
},
74+
{
75+
name: "noEmitHelpers",
76+
type: "boolean"
77+
},
7478
{
7579
name: "noEmitOnError",
7680
type: "boolean",

src/compiler/emitter.ts

+27-15
Original file line numberDiff line numberDiff line change
@@ -5487,6 +5487,9 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
54875487
}
54885488

54895489
write("[\"require\", \"exports\"");
5490+
if (compilerOptions.noEmitHelpers) {
5491+
write(", \"__extends\"");
5492+
}
54905493
if (aliasedModuleNames.length) {
54915494
write(", ");
54925495
write(aliasedModuleNames.join(", "));
@@ -5496,6 +5499,9 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
54965499
write(unaliasedModuleNames.join(", "));
54975500
}
54985501
write("], function (require, exports");
5502+
if (compilerOptions.noEmitHelpers) {
5503+
write(", __extends");
5504+
}
54995505
if (importAliasNames.length) {
55005506
write(", ");
55015507
write(importAliasNames.join(", "));
@@ -5614,24 +5620,30 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
56145620

56155621
// emit prologue directives prior to __extends
56165622
var startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
5617-
// Only Emit __extends function when target ES5.
5618-
// For target ES6 and above, we can emit classDeclaration as is.
5619-
if ((languageVersion < ScriptTarget.ES6) && (!extendsEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitExtends)) {
5620-
writeLines(extendsHelper);
5621-
extendsEmitted = true;
5622-
}
56235623

5624-
if (!decorateEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitDecorate) {
5625-
writeLines(decorateHelper);
5626-
if (compilerOptions.emitDecoratorMetadata) {
5627-
writeLines(metadataHelper);
5624+
// Only emit helpers if the user did not say otherwise.
5625+
if (!compilerOptions.noEmitHelpers) {
5626+
5627+
// Only Emit __extends function when target ES5.
5628+
// For target ES6 and above, we can emit classDeclaration as is.
5629+
if ((languageVersion < ScriptTarget.ES6) && (!extendsEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitExtends)) {
5630+
writeLines(extendsHelper);
5631+
extendsEmitted = true;
5632+
}
5633+
5634+
if (!decorateEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitDecorate) {
5635+
writeLines(decorateHelper);
5636+
if (compilerOptions.emitDecoratorMetadata) {
5637+
writeLines(metadataHelper);
5638+
}
5639+
decorateEmitted = true;
5640+
}
5641+
5642+
if (!paramEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitParam) {
5643+
writeLines(paramHelper);
5644+
paramEmitted = true;
56285645
}
5629-
decorateEmitted = true;
5630-
}
56315646

5632-
if (!paramEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitParam) {
5633-
writeLines(paramHelper);
5634-
paramEmitted = true;
56355647
}
56365648

56375649
if (isExternalModule(node) || compilerOptions.separateCompilation) {

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ module ts {
16571657
mapRoot?: string;
16581658
module?: ModuleKind;
16591659
noEmit?: boolean;
1660+
noEmitHelpers?: boolean;
16601661
noEmitOnError?: boolean;
16611662
noErrorTruncation?: boolean;
16621663
noImplicitAny?: boolean;

src/harness/harness.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,10 @@ module Harness {
990990
}
991991
break;
992992

993+
case 'noemithelpers':
994+
options.noEmitHelpers = !!setting.value;
995+
break;
996+
993997
case 'noemitonerror':
994998
options.noEmitOnError = !!setting.value;
995999
break;
@@ -1477,7 +1481,7 @@ module Harness {
14771481

14781482
// List of allowed metadata names
14791483
var fileMetadataNames = ["filename", "comments", "declaration", "module",
1480-
"nolib", "sourcemap", "target", "out", "outdir", "noemitonerror",
1484+
"nolib", "sourcemap", "target", "out", "outdir", "noemithelpers", "noemitonerror",
14811485
"noimplicitany", "noresolve", "newline", "newlines", "emitbom",
14821486
"errortruncation", "usecasesensitivefilenames", "preserveconstenums",
14831487
"includebuiltfile", "suppressimplicitanyindexerrors", "stripinternal",
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [noEmitHelpers.ts]
2+
3+
class A { }
4+
class B extends A { }
5+
6+
7+
//// [noEmitHelpers.js]
8+
var A = (function () {
9+
function A() {
10+
}
11+
return A;
12+
})();
13+
var B = (function (_super) {
14+
__extends(B, _super);
15+
function B() {
16+
_super.apply(this, arguments);
17+
}
18+
return B;
19+
})(A);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/noEmitHelpers.ts ===
2+
3+
class A { }
4+
>A : Symbol(A, Decl(noEmitHelpers.ts, 0, 0))
5+
6+
class B extends A { }
7+
>B : Symbol(B, Decl(noEmitHelpers.ts, 1, 11))
8+
>A : Symbol(A, Decl(noEmitHelpers.ts, 0, 0))
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/noEmitHelpers.ts ===
2+
3+
class A { }
4+
>A : A
5+
6+
class B extends A { }
7+
>B : B
8+
>A : A
9+

tests/cases/compiler/noEmitHelpers.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @noemithelpers: true
2+
3+
class A { }
4+
class B extends A { }

0 commit comments

Comments
 (0)