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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong scope's variable is used after SWC compilation #1140

Closed
patrick-entinux opened this issue Oct 5, 2020 · 1 comment 路 Fixed by #1143
Closed

Wrong scope's variable is used after SWC compilation #1140

patrick-entinux opened this issue Oct 5, 2020 · 1 comment 路 Fixed by #1143
Assignees
Labels
Milestone

Comments

@patrick-entinux
Copy link

Describe the bug

Hi, I submitted a bug a few days ago, thank you for looking into that bug. 馃檱 I am running the same project through SWC today too, I just came across this now, got a runtime error because a totally different variable was used than expected.

Input code

Not sure if it has any effect in this case, but this is in a TypeScript project, by the way.

const categories = [{ key: "apple" }, { key: "banana" }, { key: "strawberry" }];

const item = "some item";

const catNames = categories.reduce((a, item) => {
  return { ...a, [item.key.toString()]: item };
}, {});

Here was the SWC output:

"use strict";
const categories = [
    {
        key: "apple"
    },
    {
        key: "banana"
    },
    {
        key: "strawberry"
    }
];
const item = "some item";
const catNames = categories.reduce((a, item1)=>{
    return {
        ...a,
        [item.key.toString()]: item1
    };
}, {
});

Config

{
  "env": {
    "targets": {
      "node": 10
    }
  },
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": false,
      "dynamicImport": false
    }
  },
  "module": {
    "type": "commonjs"
  }
}

Expected behavior
A clear and concise description of what you expected to happen.

I expected the item.key.toString() line to get its item value from the parameter of the reduce() callback, not the other root level item.

"use strict";
const categories = [
    {
        key: "apple"
    },
    {
        key: "banana"
    },
    {
        key: "strawberry"
    }
];
const item = "some item";
const catNames = categories.reduce((a, item1)=>{
    return {
        ...a,
-         [item.key.toString()]: item1
+         [item1.key.toString()]: item1
    };
}, {
});

Hope this is clear? If any questions/clarifications, happy to add more info, thanks.

Version
The version of @swc/core: 1.2.35

Additional context
Add any other context about the problem here.

@kdy1 kdy1 added this to the v1.2.36 milestone Oct 5, 2020
@kdy1 kdy1 mentioned this issue Oct 6, 2020
3 tasks
kdy1 added a commit to kdy1/swc that referenced this issue Oct 6, 2020
@kdy1 kdy1 self-assigned this Oct 6, 2020
@kdy1 kdy1 closed this as completed in #1143 Oct 6, 2020
kdy1 added a commit that referenced this issue Oct 6, 2020
swc_ecma_transforms:
 - fixer: Fix conditional expression in await expressions. (#1133)
 - optional_chaining: Fix call expression. (#1136)
 - hygiene: Fix codegen of computed keys. (#1140)
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 26, 2022

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, 2022
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.

3 participants