Skip to content

Commit c720ad6

Browse files
authored
fix(48166): skip checking module.exports in a truthiness call expression (microsoft#48337)
1 parent eb95b32 commit c720ad6

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

src/compiler/checker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37679,6 +37679,7 @@ namespace ts {
3767937679
(condExpr.operatorToken.kind === SyntaxKind.BarBarToken || condExpr.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken)
3768037680
? condExpr.right
3768137681
: condExpr;
37682+
if (isModuleExportsAccessExpression(location)) return;
3768237683
const type = checkTruthinessExpression(location);
3768337684
const isPropertyExpressionCast = isPropertyAccessExpression(location) && isTypeAssertion(location.expression);
3768437685
if (getFalsyFlags(type) || isPropertyExpressionCast) return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/a.js ===
2+
function fn() {}
3+
>fn : Symbol(fn, Decl(a.js, 0, 0))
4+
5+
if (typeof module === 'object' && module.exports) {
6+
>module : Symbol(module, Decl(a.js, 2, 51))
7+
>module.exports : Symbol(module.exports, Decl(a.js, 0, 0))
8+
>module : Symbol(module, Decl(a.js, 2, 51))
9+
>exports : Symbol(module.exports, Decl(a.js, 0, 0))
10+
11+
module.exports = fn;
12+
>module.exports : Symbol(module.exports, Decl(a.js, 0, 0))
13+
>module : Symbol(export=, Decl(a.js, 2, 51))
14+
>exports : Symbol(export=, Decl(a.js, 2, 51))
15+
>fn : Symbol(fn, Decl(a.js, 0, 0))
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/a.js ===
2+
function fn() {}
3+
>fn : () => void
4+
5+
if (typeof module === 'object' && module.exports) {
6+
>typeof module === 'object' && module.exports : false | (() => void)
7+
>typeof module === 'object' : boolean
8+
>typeof module : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
9+
>module : { exports: () => void; }
10+
>'object' : "object"
11+
>module.exports : () => void
12+
>module : { exports: () => void; }
13+
>exports : () => void
14+
15+
module.exports = fn;
16+
>module.exports = fn : () => void
17+
>module.exports : () => void
18+
>module : { exports: () => void; }
19+
>exports : () => void
20+
>fn : () => void
21+
}
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @checkJs: true
2+
// @allowJs: true
3+
// @strict: true
4+
// @noEmit: true
5+
// @filename: a.js
6+
7+
function fn() {}
8+
9+
if (typeof module === 'object' && module.exports) {
10+
module.exports = fn;
11+
}

0 commit comments

Comments
 (0)