Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,11 @@ func (b *Binder) bindModuleDeclaration(node *ast.Node) {
state := b.declareModuleSymbol(node)
if state != ast.ModuleInstanceStateNonInstantiated {
symbol := node.AsModuleDeclaration().Symbol
if symbol.Flags&(ast.SymbolFlagsFunction|ast.SymbolFlagsClass|ast.SymbolFlagsRegularEnum) != 0 || state != ast.ModuleInstanceStateConstEnumOnly {
// if module was already merged with some function, class or non-const enum, treat it as non-const-enum-only
if (symbol.Flags&(ast.SymbolFlagsFunction|ast.SymbolFlagsClass|ast.SymbolFlagsRegularEnum) == 0) &&
state == ast.ModuleInstanceStateConstEnumOnly &&
symbol.Flags&ast.SymbolFlagsConstEnumOnlyModule == 0 {
symbol.Flags |= ast.SymbolFlagsConstEnumOnlyModule
} else {
Comment on lines +797 to +801
Copy link
Member Author

Choose a reason for hiding this comment

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

The old compiler used a boolean | undefined on every symbol for this. Corsa uses a flag but didn't make the behavior match the tristate. Doing this instead to avoid adding a Tristate to all symbols.

symbol.Flags &^= ast.SymbolFlagsConstEnumOnlyModule
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/checker/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func (r *emitResolver) IsTopLevelValueImportEqualsWithEntityName(node *ast.Node)
return false
}
if ast.IsImportEqualsDeclaration(node) &&
(ast.NodeIsMissing(node.AsImportEqualsDeclaration().ModuleReference) || node.AsImportEqualsDeclaration().ModuleReference.Kind != ast.KindExternalModuleReference) {
(ast.NodeIsMissing(node.AsImportEqualsDeclaration().ModuleReference) || node.AsImportEqualsDeclaration().ModuleReference.Kind == ast.KindExternalModuleReference) {
return false
}

Expand Down
22 changes: 22 additions & 0 deletions internal/transformers/moduletransforms/commonjsmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,14 @@ func (tx *CommonJSModuleTransformer) visitTopLevelVariableStatement(node *ast.Va
propertyAccess,
v.Name().Clone(tx.Factory()),
))
} else if ast.IsIdentifier(v.Name()) {
expression := tx.transformInitializedVariable(v)
Copy link
Member Author

Choose a reason for hiding this comment

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

The old compiler had this func instead which did something special with exports, unlike ConvertVariableDeclarationToAssignmentExpression which is new. Not extremely familiar, though.

if expression != nil {
pushExpression(tx.Visitor().VisitNode(expression))
}
} else {
// For binding patterns, we can't do exports.{pattern} = value
// Just emit the assignment and let appendExportsOfVariableStatement handle the exports
expression := transformers.ConvertVariableDeclarationToAssignmentExpression(tx.EmitContext(), v)
if expression != nil {
pushExpression(tx.Visitor().VisitNode(expression))
Expand All @@ -1096,6 +1103,21 @@ func (tx *CommonJSModuleTransformer) visitTopLevelVariableStatement(node *ast.Va
return tx.visitTopLevelNestedVariableStatement(node)
}

func (tx *CommonJSModuleTransformer) transformInitializedVariable(node *ast.VariableDeclaration) *ast.Expression {
if node.Initializer == nil {
return nil
}
name := node.Name()
propertyAccess := tx.Factory().NewPropertyAccessExpression(
tx.Factory().NewIdentifier("exports"),
nil, /*questionDotToken*/
name,
ast.NodeFlagsNone,
)
tx.EmitContext().AssignCommentAndSourceMapRanges(propertyAccess, name)
return tx.Factory().NewAssignmentExpression(propertyAccess, node.Initializer)
}

// Visits a top-level nested variable statement as it may contain `var` declarations that are hoisted and may still be
// exported with `export {}`.
func (tx *CommonJSModuleTransformer) visitTopLevelNestedVariableStatement(node *ast.VariableStatement) *ast.Node {
Expand Down
20 changes: 11 additions & 9 deletions internal/transformers/tstransforms/importelision.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ func NewImportElisionTransformer(opt *transformers.TransformOptions) *transforme
func (tx *ImportElisionTransformer) visit(node *ast.Node) *ast.Node {
switch node.Kind {
case ast.KindImportEqualsDeclaration:
if !tx.isElisionBlocked(node) && !tx.shouldEmitImportEqualsDeclaration(node.AsImportEqualsDeclaration()) {
return nil
if !tx.isElisionBlocked(node) {
if ast.IsExternalModuleImportEqualsDeclaration(node) {
if !tx.shouldEmitAliasDeclaration(node) {
return nil
}
} else {
if !tx.shouldEmitImportEqualsDeclaration(node.AsImportEqualsDeclaration()) {
return nil
}
}
}
return tx.Visitor().VisitEachChild(node)
case ast.KindImportDeclaration:
Expand Down Expand Up @@ -124,16 +132,10 @@ func (tx *ImportElisionTransformer) shouldEmitAliasDeclaration(node *ast.Node) b
}

func (tx *ImportElisionTransformer) shouldEmitImportEqualsDeclaration(node *ast.ImportEqualsDeclaration) bool {
if !tx.shouldEmitAliasDeclaration(node.AsNode()) {
return false
}
if node.ModuleReference.Kind == ast.KindExternalModuleReference {
return true
}
// preserve old compiler's behavior: emit import declaration (even if we do not consider them referenced) when
// - current file is not external module
// - import declaration is top level and target is value imported by entity name
return tx.currentSourceFile != nil && ast.IsExternalModule(tx.currentSourceFile) && tx.isTopLevelValueImportEqualsWithEntityName(node.AsNode())
return tx.shouldEmitAliasDeclaration(node.AsNode()) || (!ast.IsExternalModule(tx.currentSourceFile) && tx.isTopLevelValueImportEqualsWithEntityName(node.AsNode()))
}

func (tx *ImportElisionTransformer) isReferencedAliasDeclaration(node *ast.Node) bool {
Expand Down
13 changes: 13 additions & 0 deletions testdata/baselines/reference/compiler/exportDestructuring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [tests/cases/compiler/exportDestructuring.ts] ////

//// [exportDestructuring.ts]
const arr = [1, 2];
export const [a, b] = arr;


//// [exportDestructuring.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = exports.a = void 0;
const arr = [1, 2];
[exports.a, exports.b] = arr;
11 changes: 11 additions & 0 deletions testdata/baselines/reference/compiler/exportDestructuring.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [tests/cases/compiler/exportDestructuring.ts] ////

=== exportDestructuring.ts ===
const arr = [1, 2];
>arr : Symbol(arr, Decl(exportDestructuring.ts, 0, 5))

export const [a, b] = arr;
>a : Symbol(a, Decl(exportDestructuring.ts, 1, 14))
>b : Symbol(b, Decl(exportDestructuring.ts, 1, 16))
>arr : Symbol(arr, Decl(exportDestructuring.ts, 0, 5))

14 changes: 14 additions & 0 deletions testdata/baselines/reference/compiler/exportDestructuring.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [tests/cases/compiler/exportDestructuring.ts] ////

=== exportDestructuring.ts ===
const arr = [1, 2];
>arr : number[]
>[1, 2] : number[]
>1 : 1
>2 : 2

export const [a, b] = arr;
>a : number
>b : number
>arr : number[]

2 changes: 2 additions & 0 deletions testdata/baselines/reference/submodule/compiler/aliasBug.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var foo;
})(baz = bar.baz || (bar.baz = {}));
})(bar = foo.bar || (foo.bar = {}));
})(foo || (foo = {}));
var provide = foo;
var booz = foo.bar.baz;
var p = new provide.Provide();
function use() {
var p1; // error here, but should be okay
Expand Down
11 changes: 0 additions & 11 deletions testdata/baselines/reference/submodule/compiler/aliasBug.js.diff

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ var foo;
})(baz = bar.baz || (bar.baz = {}));
})(bar = foo.bar || (foo.bar = {}));
})(foo || (foo = {}));
var provide = foo;
var booz = foo.bar.baz;
var beez = foo.bar;
var m = no;
var m2 = no.mod;
5;
"s";
null;
var r = undefined;
var p = new provide.Provide();
function use() {
beez.baz.boo;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ var m;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const x = require("./chainedImportAlias_file0");
var y = x;
y.m.foo();

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var mOfGloalFile;
}
mOfGloalFile.c = c;
})(mOfGloalFile || (mOfGloalFile = {}));
var exports = mOfGloalFile.c;
var require = mOfGloalFile.c;
new exports();
new require();
var m1;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ var a;
a.b = 10;
})(a || (a = {}));
var f = () => this;
var _this = a; // Error

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ var A2;
})(C = B.C || (B.C = {}));
})(B = A2.B || (A2.B = {}));
})(A2 || (A2 = {}));
var I2 = A2.B;
function foo0(e) {
if (e === 1 /* I.V1 */) {
}
Expand Down
10 changes: 0 additions & 10 deletions testdata/baselines/reference/submodule/compiler/constEnums.js.diff

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ TypeScriptAllInOne.Program.Main();


//// [constructorWithIncompleteTypeAnnotation.js]
var fs = module;
("fs");
var TypeScriptAllInOne;
(function (TypeScriptAllInOne) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
--- old.constructorWithIncompleteTypeAnnotation.js
+++ new.constructorWithIncompleteTypeAnnotation.js
@@= skipped -281, +281 lines =@@


//// [constructorWithIncompleteTypeAnnotation.js]
-var fs = module;
("fs");
@@= skipped -286, +286 lines =@@
var TypeScriptAllInOne;
(function (TypeScriptAllInOne) {
class Program {
Expand All @@ -15,7 +10,7 @@
static Main(...args) {
try {
var bfs = new BasicFeatures();
@@= skipped -15, +11 lines =@@
@@= skipped -10, +7 lines =@@
retValue = bfs.VARIABLES();
if (retValue != 0)
^= {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export = m;
var m3;
(function (m3) {
})(m3 || (m3 = {}));
var m = m3;
module.exports = m;


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ exports.x = void 0;
//// [declFileForExportedImport_1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = void 0;
///<reference path='declFileForExportedImport_0.ts'/>
exports.a = require("./declFileForExportedImport_0");
var y = exports.a.x;
exports.b = exports.a;
var z = exports.b.x;


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var m;
c_1.c = c;
})(c = m.c || (m.c = {}));
})(m || (m = {}));
var a = m.c;
var b = a;
module.exports = b;


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var Translation;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Test = void 0;
const translation_1 = require("./translation");
var TranslationKeyEnum = translation_1.Translation.TranslationKeyEnum;
class Test {
TranslationKeyEnum = TranslationKeyEnum;
print() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
--- old.declarationEmitEnumReferenceViaImportEquals.js
+++ new.declarationEmitEnumReferenceViaImportEquals.js
@@= skipped -44, +44 lines =@@
Object.defineProperty(exports, "__esModule", { value: true });
exports.Test = void 0;
@@= skipped -46, +46 lines =@@
const translation_1 = require("./translation");
-var TranslationKeyEnum = translation_1.Translation.TranslationKeyEnum;
var TranslationKeyEnum = translation_1.Translation.TranslationKeyEnum;
class Test {
- constructor() {
- this.TranslationKeyEnum = TranslationKeyEnum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export {Foo}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Foo = void 0;
var Foo = SomeNonExistingName;
exports.Foo = Foo;


//// [declarationEmitUnknownImport.d.ts]
Expand Down
Loading