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

ES2015::block_scoping renames exports #8148

Closed
fatso83 opened this issue Oct 19, 2023 · 2 comments · Fixed by #8175
Closed

ES2015::block_scoping renames exports #8148

fatso83 opened this issue Oct 19, 2023 · 2 comments · Fixed by #8175
Assignees
Labels
Milestone

Comments

@fatso83
Copy link

fatso83 commented Oct 19, 2023

Describe the bug

I was writing a how-to on using a new Sinon feature for auto-cleanup of stubs injected using accessors when I discovered it did not work when I exposed accessors on an exported object called 'mock'.

Basically, if I write something like this:

export const mock = {
    get toBeMocked(){ return toBeMocked; },
    set toBeMocked(mock){ toBeMocked = mock; }
}

it will be transpiled to this by SWC, which is very different from tsc:

export const _$mock = {
    get toBeMocked(){ return toBeMocked; },
    set toBeMocked(mock){ toBeMocked = mock; }
}

Somehow 'mock' gets turned into _$mock.

To run this example, just do

gh repo clone fatso83/sinon-swc-bug
cd sinon-swc-bug
git checkout swc-special-handling-mock
npm install
npm test

This is not a typescript issue, btw, as the playground shows the issue with a minimal example

Input code

// The entire runnable example is here: 
// https://github.com/fatso83/sinon-swc-bug/tree/swc-special-handling-mock

function _toBeMocked() {
  return "I am the original function";
}

export let toBeMocked = _toBeMocked

export const mock = {
    get toBeMocked(){ return toBeMocked; },
    set toBeMocked(mock){ toBeMocked = mock; }
}

Config

{
    "test": ".*.ts$",
    "jsc": {
      "parser": {
        "syntax": "typescript",
        "decorators": true
      },
      "transform": {
        "legacyDecorator": true,
        "decoratorMetadata": true
      }
    },
    "module": {
      "type": "commonjs"
    }
  }

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.94-nightly-20231016.1&code=H4sIAAAAAAAAA1WNQQrCMBBF93OKT1cteIPixp0LzyAlTmuwnZF0CkLJ3ZuIWrP9vP9ev4gzr4Kr6Ykv6h58qxusBAS2JQiqM7oJdmdo8IOXbkT%2FOVUtRSJ%2BPTUYRjbsDhz%2FjT%2FIqcyGKY0JyBFgKH51s37D%2B9giHt7sXLLZk%2FiimrfEU9wALRrz29oAAAA%3D&config=H4sIAAAAAAAAA1WPSw7DIAwF9zkF8rrbdtE79BAWdSIifrKJVBTl7iUE0maH3xsz8jooBbNoeKq1PMsQkYX4nEsi2Sf8lARIOxTNJia49XaWvRrRCtVoOxpIyBOluiX3hoMNQajjLXPGmzH%2FC3VwkUnkCu4o%2BsnSVTc0JbjwXmrZDkk50qF%2FwA%2FqsvNjMPLqm4kXGrYvhlQioBQBAAA%3D

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:43:05 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6020
    CPU: (12 cores)
        Models: Apple M2 Max

Binaries:
    Node: 20.5.0
    npm: 9.8.0
    Yarn: 1.22.19
    pnpm: 7.13.0

Relevant Packages:
    @swc/core: 1.3.76
    @swc/helpers: N/A
    @swc/types: N/A
    typescript: 5.1.6

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

This code:

export const mock = {
    get toBeMocked(){ return toBeMocked; },
    set toBeMocked(mock){ toBeMocked = mock; }
}

should be pretty much unaltered when compiled to ES5 with ESM exports. Probably something like this

function _toBeMocked() {
    return "I am the original function";
}
export var toBeMocked = _toBeMocked;
export var mock = {
    get toBeMocked () {
        return toBeMocked;
    },
    set toBeMocked (mock){
        toBeMocked = mock;
    }
};

Actual behavior

The export is called _$mock instead of mock

function _toBeMocked() {
    return "I am the original function";
}
export var toBeMocked = _toBeMocked;
export var _$mock = {
    get toBeMocked () {
        return toBeMocked;
    },
    set toBeMocked (mock){
        toBeMocked = mock;
    }
};

Version

1.3.94

Additional context

Originally posted as question on the Q&A.

Just play with the playground. It will show in any combination using ES5 as the target

@fatso83 fatso83 added the C-bug label Oct 19, 2023
@kdy1 kdy1 changed the title An export named mock is transpiled to _$mock when transpiling to ES5 (_not_ running Jest) ES2015::block_scoping renames exports Oct 19, 2023
fatso83 added a commit to fatso83/sinon-swc-bug that referenced this issue Oct 19, 2023
@kdy1 kdy1 self-assigned this Oct 19, 2023
@kdy1 kdy1 added this to the Planned milestone Oct 19, 2023
kdy1 added a commit that referenced this issue Oct 30, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.96 Nov 3, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Dec 4, 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 Dec 4, 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.

3 participants