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

top-level eval breaks the variable renamer #5068

Closed
chentsulin opened this issue Jun 29, 2022 · 5 comments · Fixed by #6818
Closed

top-level eval breaks the variable renamer #5068

chentsulin opened this issue Jun 29, 2022 · 5 comments · Fixed by #6818
Assignees
Labels
Milestone

Comments

@chentsulin
Copy link

chentsulin commented Jun 29, 2022

Describe the bug

When using eval() with styled-components, it generates duplicated _templateObject functions.

Input code

import styled from 'styled-components';

const C1 = styled.div`
  position: absolute;
`;

const C2 = styled.div`
  position: absolute;
`;

const debug = eval('');

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.2.207&code=H4sIAAAAAAAAA5XLuwqAMAyF4T1Pka06KOhocfJFvDRKoTZiYsG3V1Bwdjz85%2FPrxrui6BnI4bzziuYZxcR3ixRVjAWYOIpiV2H7nkvnUw%2BIG4tXz7HBYRQOh5KF%2FgP1T%2BBoPJbbUBpCZkxu4QJigxVtowAAAA%3D%3D&config=H4sIAAAAAAAAA0WOSw7DIAxE7%2BJ1tu2CO%2FQQiDoRFT95HKkIcfcCSpXdaGb8PI0%2BcGQaFStgmQo1qf2SIa2F4cQXpY0Uw1I5uQ9t5WAdDcZjRCFnMJndBvBG0Se%2F1wlyORZh4I5sOsK%2F2Qco5vc5jbZ%2BLeCT%2Bs247jxeV3EN%2BAFJDkBftAAAAA%3D%3D

Expected behavior

It should not output duplicated function name: _templateObject.

Actual behavior

It generates duplicated _templateObject:

function _taggedTemplateLiteral(strings, raw) {
    if (!raw) {
        raw = strings.slice(0);
    }
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    var data = _taggedTemplateLiteral([
        "\n  position: absolute;\n"
    ]);
    _templateObject = function _templateObject() {
        return data;
    };
    return data;
}
function _templateObject() {
    var data = _taggedTemplateLiteral([
        "\n  position: absolute;\n"
    ]);
    _templateObject = function _templateObject() {
        return data;
    };
    return data;
}
import styled from "styled-components";
var C1 = styled.div(_templateObject());
var C2 = styled.div(_templateObject());
var debug = eval("");

Version

1.2.207

Additional context

No response

@chentsulin
Copy link
Author

chentsulin commented Jun 29, 2022

This bug was initially introduced by v1.2.204.

@kdy1
Copy link
Member

kdy1 commented Jun 29, 2022

Yeah, I know the cause.

I think we should force renaming if we have duplicate declaration.
It would be much better if we have a way to create a unique identifier from start, but it's not possible with the current pure ast.
So we may need a way to mark identifiers as 'injected '

@kdy1
Copy link
Member

kdy1 commented Jan 10, 2023

This can be fixed by creating a new mark (named specified_mark) to resolver and creating marks from resolver with the specified mark as the parent.

@Thisen
Copy link

Thisen commented Jan 11, 2023

This issue also comes up when using the @swc/plugin-loadable-components as it utilizes eval.

@swc-bot
Copy link
Collaborator

swc-bot commented Feb 16, 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 Feb 16, 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