Skip to content

Commit

Permalink
Alias for module.exports.x = x
Browse files Browse the repository at this point in the history
This fixes #40155 in a surprisingly small amount of code.
  • Loading branch information
sandersn committed Aug 24, 2020
1 parent 31fab0f commit 64d6e50
Show file tree
Hide file tree
Showing 28 changed files with 273 additions and 151 deletions.
9 changes: 5 additions & 4 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2805,10 +2805,11 @@ namespace ts {
return symbol;
});
if (symbol) {
const flags = isClassExpression(node.right) ?
SymbolFlags.Property | SymbolFlags.ExportValue | SymbolFlags.Class :
SymbolFlags.Property | SymbolFlags.ExportValue;
declareSymbol(symbol.exports!, symbol, node.left, flags, SymbolFlags.None);
const flags = isIdentifier(node.right) ? SymbolFlags.Alias
: isClassExpression(node.right) ? SymbolFlags.Property | SymbolFlags.ExportValue | SymbolFlags.Class
: SymbolFlags.Property | SymbolFlags.ExportValue;
const excludeFlags = isIdentifier(node.right) ? SymbolFlags.AliasExcludes : SymbolFlags.None;
declareSymbol(symbol.exports!, symbol, node.left, flags, excludeFlags);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5499,6 +5499,7 @@ namespace ts {
export const enum AssignmentDeclarationKind {
None,
/// exports.name = expr
/// module.exports.name = expr
ExportsProperty,
/// module.exports = expr
ModuleExports,
Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/commonJSImportClassTypeReference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/conformance/salsa/commonJSImportClassTypeReference.ts] ////

//// [main.js]
const { K } = require("./mod1");
/** @param {K} k */
function f(k) {
k.values()
}

//// [mod1.js]
class K {
values() {
}
}
exports.K = K;
// export { K }


//// [mod1.js]
"use strict";
var K = /** @class */ (function () {
function K() {
}
K.prototype.values = function () {
};
return K;
}());
exports.K = K;
// export { K }
//// [main.js]
"use strict";
var K = require("./mod1").K;
/** @param {K} k */
function f(k) {
k.values();
}


//// [mod1.d.ts]
export class K {
values(): void;
}
//// [main.d.ts]
export {};
33 changes: 33 additions & 0 deletions tests/baselines/reference/commonJSImportClassTypeReference.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/conformance/salsa/main.js ===
const { K } = require("./mod1");
>K : Symbol(K, Decl(main.js, 0, 7))
>require : Symbol(require)
>"./mod1" : Symbol("tests/cases/conformance/salsa/mod1", Decl(mod1.js, 0, 0))

/** @param {K} k */
function f(k) {
>f : Symbol(f, Decl(main.js, 0, 32))
>k : Symbol(k, Decl(main.js, 2, 11))

k.values()
>k.values : Symbol(K.values, Decl(mod1.js, 0, 9))
>k : Symbol(k, Decl(main.js, 2, 11))
>values : Symbol(K.values, Decl(mod1.js, 0, 9))
}

=== tests/cases/conformance/salsa/mod1.js ===
class K {
>K : Symbol(K, Decl(mod1.js, 0, 0))

values() {
>values : Symbol(K.values, Decl(mod1.js, 0, 9))
}
}
exports.K = K;
>exports.K : Symbol(K, Decl(mod1.js, 3, 1))
>exports : Symbol(K, Decl(mod1.js, 3, 1))
>K : Symbol(K, Decl(mod1.js, 3, 1))
>K : Symbol(K, Decl(mod1.js, 0, 0))

// export { K }

36 changes: 36 additions & 0 deletions tests/baselines/reference/commonJSImportClassTypeReference.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/conformance/salsa/main.js ===
const { K } = require("./mod1");
>K : typeof K
>require("./mod1") : typeof import("tests/cases/conformance/salsa/mod1")
>require : any
>"./mod1" : "./mod1"

/** @param {K} k */
function f(k) {
>f : (k: K) => void
>k : K

k.values()
>k.values() : void
>k.values : () => void
>k : K
>values : () => void
}

=== tests/cases/conformance/salsa/mod1.js ===
class K {
>K : K

values() {
>values : () => void
}
}
exports.K = K;
>exports.K = K : typeof K
>exports.K : typeof K
>exports : typeof import("tests/cases/conformance/salsa/mod1")
>K : typeof K
>K : typeof K

// export { K }

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"containerName": "",
"fileName": "/b.js",
"kind": "alias",
"name": "(alias) (property) f: () => void\nimport f",
"name": "(alias) function f(): void\nimport f",
"textSpan": {
"start": 8,
"length": 1
Expand All @@ -36,16 +36,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "function",
"kind": "keyword"
},
{
"text": " ",
Expand All @@ -55,14 +47,6 @@
"text": "f",
"kind": "aliasName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
Expand All @@ -72,11 +56,7 @@
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "=>",
"text": ":",
"kind": "punctuation"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ function foo() {
module.exports = exports = function (o) {
return (o == null) ? create(base) : defineProperties(Object(o), descriptors);
};
exports.methods = methods;
}
const m = function () {
// I have no idea what to put here
}
exports.methods = m;
}


//// [index.js]
// @ts-nocheck
function foo() {
module.exports = exports = function (o) {
return (o == null) ? create(base) : defineProperties(Object(o), descriptors);
};
exports.methods = methods;
var m = function () {
// I have no idea what to put here
};
exports.methods = m;
}


//// [index.d.ts]
declare function _exports(o: any): any;
declare namespace _exports {
const methods: any;
export { m as methods };
}
export = _exports;
declare function m(): void;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ function foo() {
>o : Symbol(o, Decl(index.js, 2, 41))

};
exports.methods = methods;
>exports : Symbol(methods, Decl(index.js, 4, 6))
>methods : Symbol(methods, Decl(index.js, 4, 6))
const m = function () {
>m : Symbol(m, Decl(index.js, 5, 9))

// I have no idea what to put here
}
exports.methods = m;
>exports : Symbol(methods, Decl(index.js, 7, 5))
>methods : Symbol(methods, Decl(index.js, 7, 5))
>m : Symbol(m, Decl(index.js, 5, 9))
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ function foo() {
>descriptors : error

};
exports.methods = methods;
>exports.methods = methods : error
const m = function () {
>m : () => void
>function () { // I have no idea what to put here } : () => void

// I have no idea what to put here
}
exports.methods = m;
>exports.methods = m : () => void
>exports.methods : any
>exports : any
>methods : any
>methods : error
>m : () => void
}

16 changes: 8 additions & 8 deletions tests/baselines/reference/jsDeclarationsFunctionsCjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ export namespace f {
import self = f;
export { self };
}
export function i(): void;
export function ii(): void;
export function jj(): void;
export function j(): void;
declare class Cls {
}
/**
* @param {{x: string}} a
* @param {{y: typeof module.exports.b}} b
Expand All @@ -146,18 +152,12 @@ export function g(a: {
* @param {{x: string}} a
* @param {{y: typeof module.exports.b}} b
*/
export function h(a: {
declare function hh(a: {
x: string;
}, b: {
y: {
(): void;
cat: string;
};
}): void;
export function i(): void;
export function ii(): void;
export function jj(): void;
export function j(): void;
declare class Cls {
}
export {};
export { hh as h };

This file was deleted.

Loading

0 comments on commit 64d6e50

Please sign in to comment.