Skip to content
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

References to destructured exports in namespaces aren't resolvable at runtime #7453

Closed
yseymour opened this issue May 26, 2023 · 3 comments · Fixed by #7202
Closed

References to destructured exports in namespaces aren't resolvable at runtime #7453

yseymour opened this issue May 26, 2023 · 3 comments · Fixed by #7202
Labels
Milestone

Comments

@yseymour
Copy link

yseymour commented May 26, 2023

Describe the bug

Inside a namespace, referring to an exported property in the namespace generally works because it gets emitted as a var binding. However, when the export is defined using destructuring syntax, there are no such bindings and so any references to exports declared that way fail at runtime.

Input code

export namespace cli {
  export const { options, args } = parseCommandLine();
  export function run() {
    const command = args.shift();
  }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "target": "es2022",
    "loose": false,
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.60&code=H4sIAAAAAAAAA0XMQQqDMBCF4b2neEuF0guUrtx6iWE62oCZhEwEQXL3xkZw9%2BDxf7LHkDKUvFgkFvDqcHSAtIODWsaBELOr8wFKi6HgjUjJZAzek34mp9IPrzubN%2BUzQNq0H%2F4gLotbUoWTetrXzbm1pSs%2FQPhzc48AAAA%3D&config=H4sIAAAAAAAAA22Quw6DMAxF934F8tyhYuzclY%2BwgkFB5CHbSEWIf294hFLRLfZ1zr32dCsK6MTAs5jSMxURWYiPOnVk9Irv1AEdI4lhGxXuWVVZpAZ7oaNXkwmMGliSpDzQKsybDorcki48kvJRljsL%2BhCEflmgjF6awO4cqKcWzfjKJrvH1b0ixRoV%2F4Vw1ttmPFNNcJFJ5LKNQ9%2F2R7CNcttJ4EI9rOJ%2BvuVEy2qJ5oLvBL6T2fGgg5Uqf18Dzh87elJPjwEAAA%3D%3D

Expected behavior

Either emit a var binding for each destructured property, or rewrite the reference to the exported property from args to cli.args (tsc does the latter).

Actual behavior

var cli;
(function(cli) {
    ({ options: cli.options , args: cli.args  } = parseCommandLine());
    function run() {
        const command = args.shift();
    }
    cli.run = run;
})(cli || (cli = {}));

At runtime, ReferenceError: args is not defined

Version

1.3.60

Additional context

Here's the tsc output, for comparison.

var cli;
(function (cli) {
    var _a;
    _a = parseCommandLine(), cli.options = _a.options, cli.args = _a.args;
    function run() {
        const command = cli.args.shift();
    }
    cli.run = run;
})(cli = exports.cli || (exports.cli = {}));

I had a quick look but only found #3073 as a possible related issue, which was quite a while ago.

@yseymour yseymour added the C-bug label May 26, 2023
@yseymour yseymour changed the title References to destructured exports in namespaces don't get rewritten References to destructured exports in namespaces aren't resolvable at runtime May 26, 2023
@yseymour
Copy link
Author

Here's the emit for a non-destructured export, for reference:

Input:

export namespace cli {
  export const x = 42;
  export function run() {
    const zzz = x;
  }
}

Output:

var cli;
(function(cli) {
    var x = cli.x = 42;
    function run() {
        const zzz = x;
    }
    cli.run = run;
})(cli || (cli = {}));

@magic-akari
Copy link
Member

I will fix this issue in #7202 . But it will take a long time. feel free if anyone can provide a quick fix.

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 26, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants