Skip to content

Commit d1bc6b1

Browse files
committed
Bind module.export = {Thing} with alias symbols
1 parent 9ed608b commit d1bc6b1

File tree

38 files changed

+463
-145
lines changed

38 files changed

+463
-145
lines changed

src/compiler/binder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,11 @@ namespace ts {
28332833
return;
28342834
}
28352835

2836+
if (isObjectLiteralExpression(assignedExpression) && every(assignedExpression.properties, isShorthandPropertyAssignment)) {
2837+
forEach(assignedExpression.properties, bindExportAssignedObjectMemberAlias);
2838+
return;
2839+
}
2840+
28362841
// 'module.exports = expr' assignment
28372842
const flags = exportAssignmentIsAlias(node)
28382843
? SymbolFlags.Alias // An export= with an EntityNameExpression or a ClassExpression exports all meanings of that identifier or class
@@ -2841,6 +2846,10 @@ namespace ts {
28412846
setValueDeclaration(symbol, node);
28422847
}
28432848

2849+
function bindExportAssignedObjectMemberAlias(node: ShorthandPropertyAssignment) {
2850+
declareSymbol(file.symbol.exports!, file.symbol, node, SymbolFlags.Alias | SymbolFlags.Assignment, SymbolFlags.None);
2851+
}
2852+
28442853
function bindThisPropertyAssignment(node: BindablePropertyAssignmentExpression | PropertyAccessExpression | LiteralLikeElementAccessExpression) {
28452854
Debug.assert(isInJSFile(node));
28462855
// private identifiers *must* be declared (even in JS files)

src/compiler/checker.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6807,21 +6807,17 @@ namespace ts {
68076807
), ModifierFlags.None);
68086808
break;
68096809
}
6810-
// At present, the below case should be entirely unhit, as, generally speaking, the below case is *usually* bound
6811-
// such that the `BinaryExpression` is the declaration rather than the specific, nested binding element
6812-
// (because we don't seek to emit an alias in these forms yet). As such, the `BinaryExpression` switch case
6813-
// will be what actually handles this form. _However_, in anticipation of binding the below with proper
6814-
// alias symbols, I'm _pretty comfortable_ including the case here, even though it is not yet live.
6810+
// We don't know how to serialize this (nested?) binding element
6811+
Debug.failBadSyntaxKind(node.parent?.parent || node, "Unhandled binding element grandparent kind in declaration serialization");
6812+
break;
6813+
case SyntaxKind.ShorthandPropertyAssignment:
68156814
if (node.parent?.parent?.kind === SyntaxKind.BinaryExpression) {
68166815
// module.exports = { SomeClass }
68176816
serializeExportSpecifier(
68186817
unescapeLeadingUnderscores(symbol.escapedName),
68196818
targetName
68206819
);
6821-
break;
68226820
}
6823-
// We don't know how to serialize this (nested?) binding element
6824-
Debug.failBadSyntaxKind(node.parent?.parent || node, "Unhandled binding element grandparent kind in declaration serialization");
68256821
break;
68266822
case SyntaxKind.VariableDeclaration:
68276823
// commonjs require: const x = require('y')

tests/baselines/reference/findAllRefsCommonJsRequire3.baseline.jsonc

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"containerName": "",
1414
"fileName": "/b.js",
1515
"kind": "alias",
16-
"name": "(alias) (property) f: () => void\nimport f",
16+
"name": "(alias) function f(): void\nimport f",
1717
"textSpan": {
1818
"start": 8,
1919
"length": 1
@@ -36,16 +36,8 @@
3636
"kind": "space"
3737
},
3838
{
39-
"text": "(",
40-
"kind": "punctuation"
41-
},
42-
{
43-
"text": "property",
44-
"kind": "text"
45-
},
46-
{
47-
"text": ")",
48-
"kind": "punctuation"
39+
"text": "function",
40+
"kind": "keyword"
4941
},
5042
{
5143
"text": " ",
@@ -55,14 +47,6 @@
5547
"text": "f",
5648
"kind": "aliasName"
5749
},
58-
{
59-
"text": ":",
60-
"kind": "punctuation"
61-
},
62-
{
63-
"text": " ",
64-
"kind": "space"
65-
},
6650
{
6751
"text": "(",
6852
"kind": "punctuation"
@@ -72,11 +56,7 @@
7256
"kind": "punctuation"
7357
},
7458
{
75-
"text": " ",
76-
"kind": "space"
77-
},
78-
{
79-
"text": "=>",
59+
"text": ":",
8060
"kind": "punctuation"
8161
},
8262
{

tests/baselines/reference/jsDeclarationsCommonjsRelativePath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ module.exports = { Thing }
1717
export class Thing {
1818
}
1919
//// [reexport.d.ts]
20+
export { Thing };
2021
import Thing_1 = require("./thing");
2122
import Thing = Thing_1.Thing;
22-
export { Thing };

tests/baselines/reference/jsDeclarationsCommonjsRelativePath.symbols

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const Thing = require('./thing').Thing
99

1010
module.exports = { Thing }
1111
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/reexport", Decl(reexport.js, 0, 0))
12-
>module : Symbol(export=, Decl(reexport.js, 1, 38))
13-
>exports : Symbol(export=, Decl(reexport.js, 1, 38))
12+
>module : Symbol(module, Decl(reexport.js, 1, 38))
13+
>exports : Symbol("tests/cases/conformance/jsdoc/declarations/reexport", Decl(reexport.js, 0, 0))
1414
>Thing : Symbol(Thing, Decl(reexport.js, 2, 18))
1515

1616
=== tests/cases/conformance/jsdoc/declarations/thing.js ===
@@ -20,7 +20,7 @@ class Thing {}
2020

2121
module.exports = { Thing }
2222
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/thing", Decl(thing.js, 0, 0))
23-
>module : Symbol(export=, Decl(thing.js, 1, 14))
24-
>exports : Symbol(export=, Decl(thing.js, 1, 14))
23+
>module : Symbol(module, Decl(thing.js, 1, 14))
24+
>exports : Symbol("tests/cases/conformance/jsdoc/declarations/thing", Decl(thing.js, 0, 0))
2525
>Thing : Symbol(Thing, Decl(thing.js, 2, 18))
2626

tests/baselines/reference/jsDeclarationsCommonjsRelativePath.types

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
const Thing = require('./thing').Thing
66
>Thing : typeof Thing
77
>require('./thing').Thing : typeof Thing
8-
>require('./thing') : { Thing: typeof Thing; }
8+
>require('./thing') : typeof import("tests/cases/conformance/jsdoc/declarations/thing")
99
>require : any
1010
>'./thing' : "./thing"
1111
>Thing : typeof Thing
1212

1313
module.exports = { Thing }
14-
>module.exports = { Thing } : { Thing: typeof Thing; }
15-
>module.exports : { Thing: typeof Thing; }
16-
>module : { "\"tests/cases/conformance/jsdoc/declarations/reexport\"": { Thing: typeof Thing; }; }
17-
>exports : { Thing: typeof Thing; }
14+
>module.exports = { Thing } : typeof import("tests/cases/conformance/jsdoc/declarations/reexport")
15+
>module.exports : typeof import("tests/cases/conformance/jsdoc/declarations/reexport")
16+
>module : { "\"tests/cases/conformance/jsdoc/declarations/reexport\"": typeof import("tests/cases/conformance/jsdoc/declarations/reexport"); }
17+
>exports : typeof import("tests/cases/conformance/jsdoc/declarations/reexport")
1818
>{ Thing } : { Thing: typeof Thing; }
1919
>Thing : typeof Thing
2020

@@ -26,10 +26,10 @@ class Thing {}
2626
>Thing : Thing
2727

2828
module.exports = { Thing }
29-
>module.exports = { Thing } : { Thing: typeof Thing; }
30-
>module.exports : { Thing: typeof Thing; }
31-
>module : { "\"tests/cases/conformance/jsdoc/declarations/thing\"": { Thing: typeof Thing; }; }
32-
>exports : { Thing: typeof Thing; }
29+
>module.exports = { Thing } : typeof import("tests/cases/conformance/jsdoc/declarations/thing")
30+
>module.exports : typeof import("tests/cases/conformance/jsdoc/declarations/thing")
31+
>module : { "\"tests/cases/conformance/jsdoc/declarations/thing\"": typeof import("tests/cases/conformance/jsdoc/declarations/thing"); }
32+
>exports : typeof import("tests/cases/conformance/jsdoc/declarations/thing")
3333
>{ Thing } : { Thing: typeof Thing; }
3434
>Thing : typeof Thing
3535

tests/baselines/reference/jsDeclarationsDocCommentsOnConsts.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ function b() {
2020

2121
module.exports = {x, b}
2222
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/index1", Decl(index1.js, 0, 0))
23-
>module : Symbol(export=, Decl(index1.js, 12, 1))
24-
>exports : Symbol(export=, Decl(index1.js, 12, 1))
23+
>module : Symbol(module, Decl(index1.js, 12, 1))
24+
>exports : Symbol("tests/cases/conformance/jsdoc/declarations/index1", Decl(index1.js, 0, 0))
2525
>x : Symbol(x, Decl(index1.js, 14, 18))
2626
>b : Symbol(b, Decl(index1.js, 14, 20))
2727

tests/baselines/reference/jsDeclarationsDocCommentsOnConsts.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ function b() {
2323
}
2424

2525
module.exports = {x, b}
26-
>module.exports = {x, b} : { x: (a: any) => string; b: () => number; }
27-
>module.exports : { x: (a: any) => string; b: () => number; }
28-
>module : { "\"tests/cases/conformance/jsdoc/declarations/index1\"": { x: (a: any) => string; b: () => number; }; }
29-
>exports : { x: (a: any) => string; b: () => number; }
26+
>module.exports = {x, b} : typeof import("tests/cases/conformance/jsdoc/declarations/index1")
27+
>module.exports : typeof import("tests/cases/conformance/jsdoc/declarations/index1")
28+
>module : { "\"tests/cases/conformance/jsdoc/declarations/index1\"": typeof import("tests/cases/conformance/jsdoc/declarations/index1"); }
29+
>exports : typeof import("tests/cases/conformance/jsdoc/declarations/index1")
3030
>{x, b} : { x: (a: any) => string; b: () => number; }
3131
>x : (a: any) => string
3232
>b : () => number

tests/baselines/reference/jsDeclarationsExportForms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ import * as ns from "./cls";
176176
export { ns as classContainer };
177177
import * as ns from "./cls";
178178
//// [cjs.d.ts]
179-
import ns = require("./cls");
180179
export { ns };
180+
import ns = require("./cls");
181181
//// [cjs2.d.ts]
182182
export = ns;
183183
import ns = require("./cls");

tests/baselines/reference/jsDeclarationsExportForms.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const ns = require("./cls");
5050

5151
module.exports = { ns };
5252
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/cjs", Decl(cjs.js, 0, 0))
53-
>module : Symbol(export=, Decl(cjs.js, 0, 28))
54-
>exports : Symbol(export=, Decl(cjs.js, 0, 28))
53+
>module : Symbol(module, Decl(cjs.js, 0, 28))
54+
>exports : Symbol("tests/cases/conformance/jsdoc/declarations/cjs", Decl(cjs.js, 0, 0))
5555
>ns : Symbol(ns, Decl(cjs.js, 1, 18))
5656

5757
=== tests/cases/conformance/jsdoc/declarations/cjs2.js ===

0 commit comments

Comments
 (0)