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

Mixup in tagged template literals in exported function #598

Closed
cauthmann opened this issue Jan 21, 2020 · 3 comments · Fixed by #599
Closed

Mixup in tagged template literals in exported function #598

cauthmann opened this issue Jan 21, 2020 · 3 comments · Fixed by #599
Assignees
Milestone

Comments

@cauthmann
Copy link

Hello,

swc+webpack got our compilation times for debug builds down to ~3s from ~7s, which makes hitting F5 after a change much less frustrating! Thanks!

However, it breaks our use of tagged template literals for i18n. Setting target: "es2019" avoids the broken transformation, but there's still a bug somewhere.

cat test.js:

export function foo() {
	console.log(i18n`Hello World`);
	console.log(i18n`Nobody will ever see this.`);
}

npx swc --no-swcrc test.js

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([
        'Hello World'
    ]);
    _templateObject = function _templateObject() {
        return data;
    };
    return data;
}
function _templateObject1() {
    var data = _taggedTemplateLiteral([
        'Nobody will ever see this.'
    ]);
    _templateObject1 = function _templateObject1() {
        return data;
    };
    return data;
}
export function foo() {
    console.log(i18n(_templateObject()));
    console.log(i18n(_templateObject()));
}

Note the last line, where it should use _templateObject1(), but uses _templateObject() again. Consequently, if we insert an implementation of i18n() and call foo(), running the transformed function says Hello World twice.

Note that the bug only appears in exported functions.

@kdy1 kdy1 self-assigned this Jan 21, 2020
@kdy1 kdy1 added this to the v1.1.14 milestone Jan 21, 2020
@kdy1
Copy link
Member

kdy1 commented Jan 21, 2020

I think the problem is export function. Can you try removing export from it?

Btw, I need to find a better test suite.

@cauthmann
Copy link
Author

Yes, the bug only appears in exported functions. When mixing exported functions with non-exported ones, only the exported ones break. Removing the export fixes it.

kdy1 added a commit to kdy1/swc that referenced this issue Jan 21, 2020
bors bot pushed a commit that referenced this issue Jan 21, 2020
Kind indicates type of identifier. hygiene pass tracks it to preserve ident renaming rule.

It means,
```js
export function foo() {
    console.log(i18n(_templateObject()));
    console.log(i18n(_templateObject()));
    console.log(i18n(_templateObject()));
}
```
should be
```js
export function foo() {
    console.log(i18n(_templateObject()));
    console.log(i18n(_templateObject1()));
    console.log(i18n(_templateObject2()));
}
```

Closes #598
@kdy1 kdy1 closed this as completed in #599 Jan 22, 2020
kdy1 added a commit to kdy1/swc that referenced this issue Aug 14, 2020
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 28, 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 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants