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

Runtime issue with named export overrides #2101

Closed
lexanth opened this issue Aug 18, 2021 · 7 comments · Fixed by #2883
Closed

Runtime issue with named export overrides #2101

lexanth opened this issue Aug 18, 2021 · 7 comments · Fixed by #2883
Labels
Milestone

Comments

@lexanth
Copy link

lexanth commented Aug 18, 2021

Describe the bug
Based on #1714

Valid JS is generated, but at runtime if the override is not the same (identity-wise) as the export from * the code won't load.

Input code

import { customRender } from './customRender';

// re-export everything
export * from '@testing-library/react';

// override render method
export { customRender as render };

Config

{
  "sourceMaps": true,
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "dynamicImport": true
    },
    "transform": {
      "react": {
        "runtime": "automatic",
        "refresh": true
      },
      "hidden": {
        "jest": true
      }
    }
  },
  "module": {
    "type": "commonjs"
  },
  "env": {
    "coreJs": 3,
    "mode": "usage"
  }
}

Expected behavior
The transpilation should respect the behaviour that the named exports can override the wildcard exports.
e.g. Babel REPL generates the _exportNames object for overrides and then excludes those when defining the wildcard export.

var _exportNames = {
  render: true
};

Object.keys(_react).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; // <-- This check is missing in the swc output
  if (key in exports && exports[key] === _react[key]) return; // <-- previous fix added this check, but only works when the override is the same as the * export
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function () {
      return _react[key];
    }
  });
});

Version
The version of @swc/core: 1.2.77

Additional context
This code should have this behaviour too:

export { customRender as render } from './customRender';

// re-export everything, but keep named 'render' exported above
export * from '@testing-library/react';
@lexanth lexanth added the C-bug label Aug 18, 2021
@kdy1 kdy1 modified the milestones: v1.2.82, v1.2.83, v1.2.84 Aug 19, 2021
@kdy1 kdy1 modified the milestones: v1.2.84, v1.2.85, v1.2.86 Aug 26, 2021
@kdy1 kdy1 modified the milestones: v1.2.86, v1.2.87, v1.2.88, v1.2.89 Sep 8, 2021
@kdy1 kdy1 modified the milestones: v1.2.89, v1.2.90, v1.2.91 Sep 16, 2021
@kdy1 kdy1 modified the milestones: v1.2.91, v1.2.92, v1.2.93, v1.2.94 Sep 23, 2021
@kdy1 kdy1 modified the milestones: v1.2.94, v1.2.95 Oct 3, 2021
@kdy1 kdy1 modified the milestones: v1.2.105, v1.2.106, v1.2.107 Oct 25, 2021
@th3fallen
Copy link

i'd like to second this im running into this issue too, is there any workaround?

@kdy1 kdy1 modified the milestones: v1.2.107, v1.2.108, v1.2.109 Nov 7, 2021
@kdy1 kdy1 modified the milestones: v1.2.109, v1.2.110, v1.2.111 Nov 17, 2021
@joshuacc
Copy link

Just wanted to add that I have also run into this issue. I have looked into fixing it, but haven't yet been able to spend enough time to figure out exactly where the fix needs to be made. The fact that I've barely touched Rust before certainly doesn't help. :)

@kdy1 kdy1 modified the milestones: v1.2.111, v1.2.112, v1.2.113 Nov 19, 2021
@JeremyGrieshop
Copy link
Contributor

Is this still an issue with the latest release? I merged a fix in a few weeks ago to address something similar to this issue, but it's not 100% fixed yet in all cases, so I was planning a new PR for it.

@kdy1 kdy1 modified the milestones: v1.2.113, v1.2.114 Nov 27, 2021
kdy1 pushed a commit that referenced this issue Nov 28, 2021
swc_ecma_transforms_module:
 - Emit correct value for `_exportNames`. (Closes #2101)
@th3fallen
Copy link

So im not sure if this is relevant but I still get this issue when doing...


auth.getUserId = jest.fn().mockReturnValue(1);
auth.getTokenInfo = jest.fn().mockReturnValue({ limited: false });

import { render } from '@testing-library/react';
//re-export everything
export * from '@testing-library/react';

//override render method
export { default as render } from 'tests/renderers/baseRenderer';
export { default as renderDialog } from 'tests/renderers/dialogRenderer';

export { default as selectEvent } from 'react-select-event';
export * from 'tests/helpers/reactSelect';
export { default as userEvent } from '@testing-library/user-event';
export { screen } from 'query-extensions';

the screen export is not available for anything importing from that file.




      at Object.get [as screen] (app/tests/testUtils.js:37:33)
      at app/tests/testUtils.js:46:34````

@JeremyGrieshop
Copy link
Contributor

How did you verify this? The PR was merged, but will not be released until 1.2.114 milestone.

So im not sure if this is relevant but I still get this issue when doing...


auth.getUserId = jest.fn().mockReturnValue(1);
auth.getTokenInfo = jest.fn().mockReturnValue({ limited: false });

import { render } from '@testing-library/react';
//re-export everything
export * from '@testing-library/react';

//override render method
export { default as render } from 'tests/renderers/baseRenderer';
export { default as renderDialog } from 'tests/renderers/dialogRenderer';

export { default as selectEvent } from 'react-select-event';
export * from 'tests/helpers/reactSelect';
export { default as userEvent } from '@testing-library/user-event';
export { screen } from 'query-extensions';

the screen export is not available for anything importing from that file.




      at Object.get [as screen] (app/tests/testUtils.js:37:33)
      at app/tests/testUtils.js:46:34````

@th3fallen
Copy link

ah derp i saw the release this morning and assumed. great catch

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 20, 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 20, 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.

6 participants