Skip to content

Conditional assignments to exports should not mark a js file as a commonjs module #33765

@weswigham

Description

@weswigham

The addition of element accesses on exports broke the uglify-js user suite test. This is a minimal repro:

Code

// @noEmit: true
// @checkJs: true
// @allowJs: true
// @strict: true
// @filename: file1.js
// this file _should_ be a global file

var GlobalThing = { x: 12 };

/**
 * @param {*} type 
 * @param {*} ctor
 * @param {*} exports
 */
function f(type, ctor, exports) {
    if (typeof exports !== "undefined") {
        exports["AST_" + type] = ctor;
    }
}
// @filename: ref.js
GlobalThing.x;

Expected behavior:
file1.js is treated as a globally scoped file and ref.js typechecks.

Actual behavior:
Err in ref.js, no referenced found to GlobalThing in file1.js because file1.js is treated like a module.

cc @sandersn @andrewbranch While this error only appeared once we started to recognize element access export assignments (even if we can't bind them because they're computed names, for some reason), that we flag a reference to a variable named exports within a function body (and a conditional!) as a module also seems questionable.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JavaScriptThe issue relates to JavaScript specifically

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions