Skip to content

CommonJS emit gives an imported function the wrong this when the callee is wrapped #65414

Open

Description

@trackoor

🔎 Search Terms

commonjs emit parenthesized call this, (0, a_1.f), indirect call wrapped callee, satisfies call this

🕗 Version & Regression Information

Not a regression.

  • Fails: 5.9.3, 6.0.3, 7.0.2, 7.1.0-dev.20261106.1, 7.1.0-dev.20261109.1

⏯ Playground Link

https://www.typescriptlang.org/play/v2/?ts=6.0.3#code/v2/N4IghgxgLglgbgUwGIwDYJALhAegO4D2ATgNYDOADpAjmAHRRkgA0IAZmgk5qPseVQg1GEAgDsOAczoArMuKwhgAHTEACNcpCiAthU5EA8hVjiyWzGpXqNmkGShEY0C2scBXBM1W27YggCSeqjOMFAASghQ7kRi5lhuRJ7eNhpaYKioBHgAqmJECJAAFmAARugAwgQAJgiubBlkXj626ZnZee5N1QAyZQio8ZYNg82pdlwAsjXu6AFiUAhEBBSuHmO+WrUQqGBEYKZia0kbrSA6M+iuWroXYnJaKZvnlwiR8qjuh9cg-rWPLTSICge0kUR+XAATAAGSGQgHjLRyAAePwKkCgCOeXQQABEEBwxMhiBVdmQyCgBtUhmoRk0nmcYJJ-AV8RQChADjAzD8AGx0aFaFoAX1UorELFwhFIlGotAY3BACGRFGIUFp7jE0G56jYAAooEUYGRLJqSP48GJmGo6LaAPqgk1qM0WsQAbQAugBKKxqArRWJuI1kADcanFkr4MsENBgYlqyIVihgejVvrY4dpyx0djotC0IdUUAAnhQEGokGoALxqPU+qsAPmdYnN2TEhaOYhwODUAFEVQhoAhqpYEIgiMW1CEiWp2XHGIlPLWwOoU6qiItqhqtYc1Ma1JzMsO1HgwkUg-uq1fm7VCcOvXRVKI4gR0HQspI9Vp9T7fH+gdaP7Vtemq3nG95hv+UFqN2agIDoYSWHq0LWmAdoAIx0GwXp1k+Zivgg74EJ+Wh6thdbQQBtbkfWIHxgS4HVF6kGURosHwYhy4YVhOFenhL5vh+X4gGRahgGQFa8X+jzUWJElIFJV41qBDFEkxLF-uxCFQJYaGYT+-EfIRQmkWwACEUnQTJZEWRRSk3qpEGsWxPYcTpXH6bxhkEURJEiRmZBcmQHBcJJuEsLJgWwMFMChQpdl0WBanMTBrnabp3EGVq+GCcRwlkV6AAGyKFZR1nYcVpX2Spd7qc5qVwelHk8WolWqCAwqsE06DagoPAgKqZBhDqVSfDoEqYOhrCDcN4g9OBABy7g6KUSxYFN9gDIOhwAMoghuo3LRNG3ddtOp7XsUDzUSS0rWtk2dSA45DX1kLCkAA

(Playground v2, 6.0.3, module: commonjs; press Run.)

💻 Code

// a.ts
export function f(this: unknown) { return this; }

// index.ts
import { f } from "./a";
type F = () => unknown;
console.log(f() === undefined);                 // true   emit: (0, a_1.f)()
console.log((f)() === undefined);               // false  emit: (a_1.f)()
console.log((f as F)() === undefined);          // false  emit: a_1.f()
console.log((f!)() === undefined);              // false  emit: (a_1.f)()
console.log((f satisfies F)() === undefined);   // false  emit: a_1.f()
console.log((f)`x` === undefined);              // false  emit: (a_1.f) `x`

🙁 Actual behavior

Only the bare call f() is emitted as (0, a_1.f)(). When the callee is parenthesized or wrapped in as, <F>, ! or satisfies, the emit calls a_1.f as a method, so this is the exports object instead of undefined.

🙂 Expected behavior

All six calls print true, as in ESM output: the wrapped forms should also be emitted as (0, a_1.f)(...).


This was found with an AI-assisted differential testing tool; the repros and outputs above were re-run on the listed versions.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions