fix(48166): skip checking module.exports in a truthiness call expression (#48337)

This commit is contained in:
Oleksandr T 2022-03-28 18:23:20 +03:00 committed by GitHub
parent eb95b32dfb
commit c720ad6ffb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 0 deletions

View File

@ -37679,6 +37679,7 @@ namespace ts {
(condExpr.operatorToken.kind === SyntaxKind.BarBarToken || condExpr.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken)
? condExpr.right
: condExpr;
if (isModuleExportsAccessExpression(location)) return;
const type = checkTruthinessExpression(location);
const isPropertyExpressionCast = isPropertyAccessExpression(location) && isTypeAssertion(location.expression);
if (getFalsyFlags(type) || isPropertyExpressionCast) return;

View File

@ -0,0 +1,17 @@
=== tests/cases/compiler/a.js ===
function fn() {}
>fn : Symbol(fn, Decl(a.js, 0, 0))
if (typeof module === 'object' && module.exports) {
>module : Symbol(module, Decl(a.js, 2, 51))
>module.exports : Symbol(module.exports, Decl(a.js, 0, 0))
>module : Symbol(module, Decl(a.js, 2, 51))
>exports : Symbol(module.exports, Decl(a.js, 0, 0))
module.exports = fn;
>module.exports : Symbol(module.exports, Decl(a.js, 0, 0))
>module : Symbol(export=, Decl(a.js, 2, 51))
>exports : Symbol(export=, Decl(a.js, 2, 51))
>fn : Symbol(fn, Decl(a.js, 0, 0))
}

View File

@ -0,0 +1,22 @@
=== tests/cases/compiler/a.js ===
function fn() {}
>fn : () => void
if (typeof module === 'object' && module.exports) {
>typeof module === 'object' && module.exports : false | (() => void)
>typeof module === 'object' : boolean
>typeof module : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>module : { exports: () => void; }
>'object' : "object"
>module.exports : () => void
>module : { exports: () => void; }
>exports : () => void
module.exports = fn;
>module.exports = fn : () => void
>module.exports : () => void
>module : { exports: () => void; }
>exports : () => void
>fn : () => void
}

View File

@ -0,0 +1,11 @@
// @checkJs: true
// @allowJs: true
// @strict: true
// @noEmit: true
// @filename: a.js
function fn() {}
if (typeof module === 'object' && module.exports) {
module.exports = fn;
}