Skip to content

Commit 64d6e50

Browse files
committed
Alias for module.exports.x = x
This fixes #40155 in a surprisingly small amount of code.
1 parent 31fab0f commit 64d6e50

28 files changed

+273
-151
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,10 +2805,11 @@ namespace ts {
28052805
return symbol;
28062806
});
28072807
if (symbol) {
2808-
const flags = isClassExpression(node.right) ?
2809-
SymbolFlags.Property | SymbolFlags.ExportValue | SymbolFlags.Class :
2810-
SymbolFlags.Property | SymbolFlags.ExportValue;
2811-
declareSymbol(symbol.exports!, symbol, node.left, flags, SymbolFlags.None);
2808+
const flags = isIdentifier(node.right) ? SymbolFlags.Alias
2809+
: isClassExpression(node.right) ? SymbolFlags.Property | SymbolFlags.ExportValue | SymbolFlags.Class
2810+
: SymbolFlags.Property | SymbolFlags.ExportValue;
2811+
const excludeFlags = isIdentifier(node.right) ? SymbolFlags.AliasExcludes : SymbolFlags.None;
2812+
declareSymbol(symbol.exports!, symbol, node.left, flags, excludeFlags);
28122813
}
28132814
}
28142815

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5499,6 +5499,7 @@ namespace ts {
54995499
export const enum AssignmentDeclarationKind {
55005500
None,
55015501
/// exports.name = expr
5502+
/// module.exports.name = expr
55025503
ExportsProperty,
55035504
/// module.exports = expr
55045505
ModuleExports,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/conformance/salsa/commonJSImportClassTypeReference.ts] ////
2+
3+
//// [main.js]
4+
const { K } = require("./mod1");
5+
/** @param {K} k */
6+
function f(k) {
7+
k.values()
8+
}
9+
10+
//// [mod1.js]
11+
class K {
12+
values() {
13+
}
14+
}
15+
exports.K = K;
16+
// export { K }
17+
18+
19+
//// [mod1.js]
20+
"use strict";
21+
var K = /** @class */ (function () {
22+
function K() {
23+
}
24+
K.prototype.values = function () {
25+
};
26+
return K;
27+
}());
28+
exports.K = K;
29+
// export { K }
30+
//// [main.js]
31+
"use strict";
32+
var K = require("./mod1").K;
33+
/** @param {K} k */
34+
function f(k) {
35+
k.values();
36+
}
37+
38+
39+
//// [mod1.d.ts]
40+
export class K {
41+
values(): void;
42+
}
43+
//// [main.d.ts]
44+
export {};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/conformance/salsa/main.js ===
2+
const { K } = require("./mod1");
3+
>K : Symbol(K, Decl(main.js, 0, 7))
4+
>require : Symbol(require)
5+
>"./mod1" : Symbol("tests/cases/conformance/salsa/mod1", Decl(mod1.js, 0, 0))
6+
7+
/** @param {K} k */
8+
function f(k) {
9+
>f : Symbol(f, Decl(main.js, 0, 32))
10+
>k : Symbol(k, Decl(main.js, 2, 11))
11+
12+
k.values()
13+
>k.values : Symbol(K.values, Decl(mod1.js, 0, 9))
14+
>k : Symbol(k, Decl(main.js, 2, 11))
15+
>values : Symbol(K.values, Decl(mod1.js, 0, 9))
16+
}
17+
18+
=== tests/cases/conformance/salsa/mod1.js ===
19+
class K {
20+
>K : Symbol(K, Decl(mod1.js, 0, 0))
21+
22+
values() {
23+
>values : Symbol(K.values, Decl(mod1.js, 0, 9))
24+
}
25+
}
26+
exports.K = K;
27+
>exports.K : Symbol(K, Decl(mod1.js, 3, 1))
28+
>exports : Symbol(K, Decl(mod1.js, 3, 1))
29+
>K : Symbol(K, Decl(mod1.js, 3, 1))
30+
>K : Symbol(K, Decl(mod1.js, 0, 0))
31+
32+
// export { K }
33+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/conformance/salsa/main.js ===
2+
const { K } = require("./mod1");
3+
>K : typeof K
4+
>require("./mod1") : typeof import("tests/cases/conformance/salsa/mod1")
5+
>require : any
6+
>"./mod1" : "./mod1"
7+
8+
/** @param {K} k */
9+
function f(k) {
10+
>f : (k: K) => void
11+
>k : K
12+
13+
k.values()
14+
>k.values() : void
15+
>k.values : () => void
16+
>k : K
17+
>values : () => void
18+
}
19+
20+
=== tests/cases/conformance/salsa/mod1.js ===
21+
class K {
22+
>K : K
23+
24+
values() {
25+
>values : () => void
26+
}
27+
}
28+
exports.K = K;
29+
>exports.K = K : typeof K
30+
>exports.K : typeof K
31+
>exports : typeof import("tests/cases/conformance/salsa/mod1")
32+
>K : typeof K
33+
>K : typeof K
34+
35+
// export { K }
36+

tests/baselines/reference/findAllRefsCommonJsRequire2.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/jsDeclarationsExportDoubleAssignmentInClosure.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@ function foo() {
44
module.exports = exports = function (o) {
55
return (o == null) ? create(base) : defineProperties(Object(o), descriptors);
66
};
7-
exports.methods = methods;
8-
}
7+
const m = function () {
8+
// I have no idea what to put here
9+
}
10+
exports.methods = m;
11+
}
12+
913

1014
//// [index.js]
1115
// @ts-nocheck
1216
function foo() {
1317
module.exports = exports = function (o) {
1418
return (o == null) ? create(base) : defineProperties(Object(o), descriptors);
1519
};
16-
exports.methods = methods;
20+
var m = function () {
21+
// I have no idea what to put here
22+
};
23+
exports.methods = m;
1724
}
1825

1926

2027
//// [index.d.ts]
2128
declare function _exports(o: any): any;
2229
declare namespace _exports {
23-
const methods: any;
30+
export { m as methods };
2431
}
2532
export = _exports;
33+
declare function m(): void;

tests/baselines/reference/jsDeclarationsExportDoubleAssignmentInClosure.symbols

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ function foo() {
1616
>o : Symbol(o, Decl(index.js, 2, 41))
1717

1818
};
19-
exports.methods = methods;
20-
>exports : Symbol(methods, Decl(index.js, 4, 6))
21-
>methods : Symbol(methods, Decl(index.js, 4, 6))
19+
const m = function () {
20+
>m : Symbol(m, Decl(index.js, 5, 9))
21+
22+
// I have no idea what to put here
23+
}
24+
exports.methods = m;
25+
>exports : Symbol(methods, Decl(index.js, 7, 5))
26+
>methods : Symbol(methods, Decl(index.js, 7, 5))
27+
>m : Symbol(m, Decl(index.js, 5, 9))
2228
}
29+

tests/baselines/reference/jsDeclarationsExportDoubleAssignmentInClosure.types

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ function foo() {
3030
>descriptors : error
3131

3232
};
33-
exports.methods = methods;
34-
>exports.methods = methods : error
33+
const m = function () {
34+
>m : () => void
35+
>function () { // I have no idea what to put here } : () => void
36+
37+
// I have no idea what to put here
38+
}
39+
exports.methods = m;
40+
>exports.methods = m : () => void
3541
>exports.methods : any
3642
>exports : any
3743
>methods : any
38-
>methods : error
44+
>m : () => void
3945
}
46+

tests/baselines/reference/jsDeclarationsFunctionsCjs.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ export namespace f {
130130
import self = f;
131131
export { self };
132132
}
133+
export function i(): void;
134+
export function ii(): void;
135+
export function jj(): void;
136+
export function j(): void;
137+
declare class Cls {
138+
}
133139
/**
134140
* @param {{x: string}} a
135141
* @param {{y: typeof module.exports.b}} b
@@ -146,18 +152,12 @@ export function g(a: {
146152
* @param {{x: string}} a
147153
* @param {{y: typeof module.exports.b}} b
148154
*/
149-
export function h(a: {
155+
declare function hh(a: {
150156
x: string;
151157
}, b: {
152158
y: {
153159
(): void;
154160
cat: string;
155161
};
156162
}): void;
157-
export function i(): void;
158-
export function ii(): void;
159-
export function jj(): void;
160-
export function j(): void;
161-
declare class Cls {
162-
}
163-
export {};
163+
export { hh as h };

tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespaceCjs.errors.txt

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)