Skip to content

Make direct assignments to cjs exports considered literal contexts #39816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 9, 2022
Merged
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
25 changes: 17 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7012,9 +7012,13 @@ namespace ts {
else {
// A Class + Property merge is made for a `module.exports.Member = class {}`, and it doesn't serialize well as either a class _or_ a property symbol - in fact, _it behaves like an alias!_
// `var` is `FunctionScopedVariable`, `const` and `let` are `BlockScopedVariable`, and `module.exports.thing =` is `Property`
const flags = !(symbol.flags & SymbolFlags.BlockScopedVariable) ? undefined
: isConstVariable(symbol) ? NodeFlags.Const
: NodeFlags.Let;
const flags = !(symbol.flags & SymbolFlags.BlockScopedVariable)
? symbol.parent?.valueDeclaration && isSourceFile(symbol.parent?.valueDeclaration)
? NodeFlags.Const // exports are immutable in es6, which is what we emulate and check; so it's safe to mark all exports as `const` (there's no difference to consumers, but it allows unique symbol type declarations)
: undefined
: isConstVariable(symbol)
? NodeFlags.Const
: NodeFlags.Let;
const name = (needsPostExportDefault || !(symbol.flags & SymbolFlags.Property)) ? localName : getUnusedName(localName, symbol);
let textRange: Node | undefined = symbol.declarations && find(symbol.declarations, d => isVariableDeclaration(d));
if (textRange && isVariableDeclarationList(textRange.parent) && textRange.parent.declarations.length === 1) {
Expand Down Expand Up @@ -9178,7 +9182,10 @@ namespace ts {
if (containsSameNamedThisProperty(expression.left, expression.right)) {
return anyType;
}
const type = resolvedSymbol ? getTypeOfSymbol(resolvedSymbol) : getWidenedLiteralType(checkExpressionCached(expression.right));
const isDirectExport = kind === AssignmentDeclarationKind.ExportsProperty && (isPropertyAccessExpression(expression.left) || isElementAccessExpression(expression.left)) && (isModuleExportsAccessExpression(expression.left.expression) || (isIdentifier(expression.left.expression) && isExportsIdentifier(expression.left.expression)));
const type = resolvedSymbol ? getTypeOfSymbol(resolvedSymbol)
: isDirectExport ? getRegularTypeOfLiteralType(checkExpressionCached(expression.right))
: getWidenedLiteralType(checkExpressionCached(expression.right));
if (type.flags & TypeFlags.Object &&
kind === AssignmentDeclarationKind.ModuleExports &&
symbol.escapedName === InternalSymbolName.ExportEquals) {
Expand Down Expand Up @@ -16364,9 +16371,11 @@ namespace ts {

function getESSymbolLikeTypeForNode(node: Node) {
if (isValidESSymbolDeclaration(node)) {
const symbol = getSymbolOfNode(node);
const links = getSymbolLinks(symbol);
return links.uniqueESSymbolType || (links.uniqueESSymbolType = createUniqueESSymbolType(symbol));
const symbol = isCommonJsExportPropertyAssignment(node) ? getSymbolOfNode((node as BinaryExpression).left) : getSymbolOfNode(node);
if (symbol) {
const links = getSymbolLinks(symbol);
return links.uniqueESSymbolType || (links.uniqueESSymbolType = createUniqueESSymbolType(symbol));
}
}
return esSymbolType;
}
Expand Down Expand Up @@ -34150,7 +34159,7 @@ namespace ts {

function checkExpressionForMutableLocation(node: Expression, checkMode: CheckMode | undefined, contextualType?: Type, forceTuple?: boolean): Type {
const type = checkExpression(node, checkMode, forceTuple);
return isConstContext(node) ? getRegularTypeOfLiteralType(type) :
return isConstContext(node) || isCommonJsExportedExpression(node) ? getRegularTypeOfLiteralType(type) :
isTypeAssertion(node) ? type :
getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(arguments.length === 2 ? getContextualType(node) : contextualType, node));
}
Expand Down
17 changes: 14 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1520,10 +1520,21 @@ namespace ts {
&& node.parent.parent.kind === SyntaxKind.VariableStatement;
}

export function isValidESSymbolDeclaration(node: Node): node is VariableDeclaration | PropertyDeclaration | SignatureDeclaration {
return isVariableDeclaration(node) ? isVarConst(node) && isIdentifier(node.name) && isVariableDeclarationInVariableStatement(node) :
export function isCommonJsExportedExpression(node: Node) {
if (!isInJSFile(node)) return false;
return (isObjectLiteralExpression(node.parent) && isBinaryExpression(node.parent.parent) && getAssignmentDeclarationKind(node.parent.parent) === AssignmentDeclarationKind.ModuleExports) ||
isCommonJsExportPropertyAssignment(node.parent);
}

export function isCommonJsExportPropertyAssignment(node: Node) {
if (!isInJSFile(node)) return false;
return (isBinaryExpression(node) && getAssignmentDeclarationKind(node) === AssignmentDeclarationKind.ExportsProperty);
}

export function isValidESSymbolDeclaration(node: Node): boolean {
return (isVariableDeclaration(node) ? isVarConst(node) && isIdentifier(node.name) && isVariableDeclarationInVariableStatement(node) :
isPropertyDeclaration(node) ? hasEffectiveReadonlyModifier(node) && hasStaticModifier(node) :
isPropertySignature(node) && hasEffectiveReadonlyModifier(node);
isPropertySignature(node) && hasEffectiveReadonlyModifier(node)) || isCommonJsExportPropertyAssignment(node);
}

export function introducesArgumentsExoticObject(node: Node) {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/assignmentToVoidZero1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ exports.y = 2;


//// [assignmentToVoidZero1.d.ts]
export var x: number;
export var y: number;
export const x: 1;
export const y: 2;
16 changes: 8 additions & 8 deletions tests/baselines/reference/assignmentToVoidZero1.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
// #38552
exports.y = exports.x = void 0;
>exports.y = exports.x = void 0 : undefined
>exports.y : number
>exports.y : 2
>exports : typeof import("tests/cases/conformance/salsa/assignmentToVoidZero1")
>y : number
>y : 2
>exports.x = void 0 : undefined
>exports.x : number
>exports.x : 1
>exports : typeof import("tests/cases/conformance/salsa/assignmentToVoidZero1")
>x : number
>x : 1
>void 0 : undefined
>0 : 0

exports.x = 1;
>exports.x = 1 : 1
>exports.x : number
>exports.x : 1
>exports : typeof import("tests/cases/conformance/salsa/assignmentToVoidZero1")
>x : number
>x : 1
>1 : 1

exports.y = 2;
>exports.y = 2 : 2
>exports.y : number
>exports.y : 2
>exports : typeof import("tests/cases/conformance/salsa/assignmentToVoidZero1")
>y : number
>y : 2
>2 : 2

2 changes: 1 addition & 1 deletion tests/baselines/reference/assignmentToVoidZero2.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ assignmentToVoidZero2_1.j + assignmentToVoidZero2_1.k;


//// [assignmentToVoidZero2.d.ts]
export var j: number;
export const j: 1;
//// [importer.d.ts]
export {};
8 changes: 4 additions & 4 deletions tests/baselines/reference/assignmentToVoidZero2.types
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
=== tests/cases/conformance/salsa/assignmentToVoidZero2.js ===
exports.j = 1;
>exports.j = 1 : 1
>exports.j : number
>exports.j : 1
>exports : typeof import("tests/cases/conformance/salsa/assignmentToVoidZero2")
>j : number
>j : 1
>1 : 1

exports.k = void 0;
Expand Down Expand Up @@ -76,11 +76,11 @@ c.p + c.q

=== tests/cases/conformance/salsa/importer.js ===
import { j, k } from './assignmentToVoidZero2'
>j : number
>j : 1
>k : any

j + k
>j + k : any
>j : number
>j : 1
>k : any

4 changes: 2 additions & 2 deletions tests/baselines/reference/commonJsUnusedLocals.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const x = 0;

exports.y = 1;
>exports.y = 1 : 1
>exports.y : number
>exports.y : 1
>exports : typeof import("/a")
>y : number
>y : 1
>1 : 1

8 changes: 4 additions & 4 deletions tests/baselines/reference/commonjsAccessExports.types
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
=== /a.js ===
exports.x = 0;
>exports.x = 0 : 0
>exports.x : number
>exports.x : 0
>exports : typeof import("/a")
>x : number
>x : 0
>0 : 0

exports.x;
>exports.x : number
>exports.x : 0
>exports : typeof import("/a")
>x : number
>x : 0

// Works nested
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export {

//// [index.d.ts]
declare module "versions.static" {
var _default: {
const _default: {
"@a/b": string;
"@a/c": string;
};
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/jsDeclarationsCrossfileMerge.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ module.exports = m.default;
>module.exports : typeof m.default
>module : { exports: typeof m.default; }
>exports : typeof m.default
>m.default : { (): void; memberName: string; }
>m.default : { (): void; memberName: "thing"; }
>m : typeof m
>default : { (): void; memberName: string; }
>default : { (): void; memberName: "thing"; }

module.exports.memberName = "thing";
>module.exports.memberName = "thing" : "thing"
>module.exports.memberName : string
>module.exports.memberName : "thing"
>module.exports : typeof m.default
>module : { exports: typeof m.default; }
>exports : typeof m.default
>memberName : string
>memberName : "thing"
>"thing" : "thing"

=== tests/cases/conformance/jsdoc/declarations/exporter.js ===
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/jsDeclarationsDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ exports.default = func;


//// [index1.d.ts]
declare var _default: 12;
declare const _default: 12;
export default _default;
//// [index2.d.ts]
export default function foo(): typeof foo;
Expand All @@ -137,7 +137,7 @@ declare class Bar extends Fab {
import Fab from "./index3";
//// [index5.d.ts]
type _default = string | number;
declare var _default: 12;
declare const _default: 12;
export default _default;
//// [index6.d.ts]
declare function func(): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ module.exports.additional = 20;

//// [index.d.ts]
export const member: number;
export const additional: number;
export const additional: 20;
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class Foo {
}

module.exports = new Foo();
>module.exports = new Foo() : { member: number; additional: number; }
>module.exports : { member: number; additional: number; }
>module : { exports: { member: number; additional: number; }; }
>exports : { member: number; additional: number; }
>module.exports = new Foo() : { member: number; additional: 20; }
>module.exports : { member: number; additional: 20; }
>module : { exports: { member: number; additional: 20; }; }
>exports : { member: number; additional: 20; }
>new Foo() : Foo
>Foo : typeof Foo

module.exports.additional = 20;
>module.exports.additional = 20 : 20
>module.exports.additional : number
>module.exports : { member: number; additional: number; }
>module : { exports: { member: number; additional: number; }; }
>exports : { member: number; additional: number; }
>additional : number
>module.exports.additional : 20
>module.exports : { member: number; additional: 20; }
>module : { exports: { member: number; additional: 20; }; }
>exports : { member: number; additional: 20; }
>additional : 20
>20 : 20

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export var dummy = 1

//// [mod1.d.ts]
/** @typedef {number} Dotted.Name */
export var dummy: number;
export const dummy: number;
export namespace Dotted {
type Name = number;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/baselines/reference/jsExportAssignmentNonMutableLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [file.js]
const customSymbol = Symbol("custom");

// This is a common pattern in Node’s built-in modules:
module.exports = {
customSymbol,
};

exports.customSymbol2 = Symbol("custom");



//// [file.d.ts]
export const customSymbol2: unique symbol;
export const customSymbol: unique symbol;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/file.js ===
const customSymbol = Symbol("custom");
>customSymbol : Symbol(customSymbol, Decl(file.js, 0, 5))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))

// This is a common pattern in Node’s built-in modules:
module.exports = {
>module.exports : Symbol(module.exports, Decl(file.js, 0, 0))
>module : Symbol(module, Decl(file.js, 0, 38))
>exports : Symbol(module.exports, Decl(file.js, 0, 0))

customSymbol,
>customSymbol : Symbol(customSymbol, Decl(file.js, 3, 18))

};

exports.customSymbol2 = Symbol("custom");
>exports.customSymbol2 : Symbol(customSymbol2, Decl(file.js, 5, 2))
>exports : Symbol(customSymbol2, Decl(file.js, 5, 2))
>customSymbol2 : Symbol(customSymbol2, Decl(file.js, 5, 2))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/file.js ===
const customSymbol = Symbol("custom");
>customSymbol : unique symbol
>Symbol("custom") : unique symbol
>Symbol : SymbolConstructor
>"custom" : "custom"

// This is a common pattern in Node’s built-in modules:
module.exports = {
>module.exports = { customSymbol,} : typeof module.exports
>module.exports : typeof module.exports
>module : { exports: typeof module.exports; }
>exports : typeof module.exports
>{ customSymbol,} : { customSymbol: symbol; }

customSymbol,
>customSymbol : symbol

};

exports.customSymbol2 = Symbol("custom");
>exports.customSymbol2 = Symbol("custom") : unique symbol
>exports.customSymbol2 : unique symbol
>exports : typeof import("tests/cases/compiler/file")
>customSymbol2 : unique symbol
>Symbol("custom") : unique symbol
>Symbol : SymbolConstructor
>"custom" : "custom"

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
=== /x.js ===
module.exports.x = 1;
>module.exports.x = 1 : 1
>module.exports.x : number
>module.exports.x : 1
>module.exports : typeof import("/y")
>module : { exports: typeof import("/y"); }
>exports : typeof import("/y")
>x : number
>x : 1
>1 : 1

module.exports = require("./y.js");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var a = 10;
=== tests/cases/compiler/node_modules/c.js ===
exports.a = 10;
>exports.a = 10 : 10
>exports.a : number
>exports.a : 10
>exports : typeof import("tests/cases/compiler/node_modules/c")
>a : number
>a : 10
>10 : 10

c = 10;
Expand Down
Loading