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

[Bug]: @babel/preset-typescript@7.22.15 fails when exporting namespace #15940

Closed
1 task done
snitin315 opened this issue Sep 7, 2023 · 11 comments · Fixed by #16071
Closed
1 task done

[Bug]: @babel/preset-typescript@7.22.15 fails when exporting namespace #15940

snitin315 opened this issue Sep 7, 2023 · 11 comments · Fixed by #16071
Assignees
Labels
area: typescript good first issue i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@snitin315
Copy link
Contributor

snitin315 commented Sep 7, 2023

💻

  • Would you like to work on a fix?

How are you using Babel?

@rollup/plugin-babel

Input code

Building the following code with @babel/preset-typescript fails:

import type { Brand, NativeOrWebBrand } from './types';

namespace Platform {
  export type Name = 'web';
}

export { Platform };

with the following error:

Screenshot 2023-09-07 at 8 36 52 AM

However if I move the export to declaration itself, it builds successful:

import type { Brand, NativeOrWebBrand } from './types';

export namespace Platform {
  export type Name = 'web';
}

Configuration file name

.babelrc.js

Configuration

{
      presets: [
        ['@babel/preset-env', { modules: false }],
        '@babel/preset-typescript',
        ['@babel/preset-react', { runtime: 'automatic' }],
      ],
      plugins: [
        [
          '@babel/plugin-transform-runtime',
          {
            helpers: true,
            regenerator: true,
          },
        ],
        [
          'babel-plugin-styled-components',
          {
            displayName: true,
            pure: true,
            ssr: true,
          },
        ],
      ],
    },

Current and expected behavior

Currently it throws error with v7.22

It should not throw error like in v7.16

Environment

System:
    OS: macOS 13.4.1
  Binaries:
    Node: 20.2.0 - ~/.nvm/versions/node/v20.2.0/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.6.6 - ~/.nvm/versions/node/v20.2.0/bin/npm
  npmPackages:
    @babel/core: 7.22.15 => 7.22.15 
    @babel/plugin-transform-react-jsx: 7.22.15 => 7.22.15 
    @babel/plugin-transform-runtime: 7.22.15 => 7.22.15 
    @babel/preset-env: 7.22.15 => 7.22.15 
    @babel/preset-react: 7.22.15 => 7.22.15 
    @babel/preset-typescript: 7.22.15 => 7.22.15 
    @babel/runtime: 7.22.15 => 7.22.15 
    babel-jest: 29.6.1 => 29.6.1 
    babel-loader: 8.2.3 => 8.2.3 
    babel-plugin-module-resolver: 4.1.0 => 4.1.0 
    babel-plugin-styled-components: 1.12.0 => 1.12.0 
    jest: 29.6.1 => 29.6.1 
    metro-react-native-babel-preset: 0.76.7 => 0.76.7 
    react-native: 0.72.3 => 0.72.3

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @snitin315! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@snitin315 snitin315 changed the title [Bug]: [Bug]: @babel/preset-typescript@7.22.15 fails when exporting namespace Sep 7, 2023
@nicolo-ribaudo
Copy link
Member

This has been probably caused by #15799, but the old output was wrong too.

We might have to add a "is namespace && will be stripped because it only contains types" check in

, or call registerGlobalType in .

I see that you marked "Would you like to work on a fix?", let me know if you need further help :)

@snitin315
Copy link
Contributor Author

We might have to add a "is namespace && will be stripped because it only contains types" check in

@nicolo-ribaudo How do I check it's a namespace, currently stmt doesn't have access to the kind property

@nicolo-ribaudo
Copy link
Member

You can use stmt.isTSModuleDeclaration() for that

@nicolo-ribaudo
Copy link
Member

@snitin315 Are you still working on this?

@snitin315
Copy link
Contributor Author

@nicolo-ribaudo not as of now. Feel free to pick this up.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Oct 13, 2023

For anyone that wants to help :)

If it is the first time that you contribute to Babel, follow these steps: (you need to have make and yarn available on your machine)

  1. Write a comment there to let other possible contributors know that you are working on this bug.
  2. Fork the repo
  3. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  4. Run yarn && make bootstrap
  5. Wait ⏳
  6. Run make watch (or make build whenever you change a file)
  7. Add a test in https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-typescript/test/fixtures/namespace (only input.ts; output.js will be automatically generated)
  8. Update the code!
  9. yarn jest transform-typescript to run the tests
    • If some test outputs don't match but the new results are correct, you can delete the bad output.js files and run the tests again
    • If you prefer, you can run OVERWRITE=true yarn jest transform-typescript and they will be automatically updated.
  10. If it is working, run make test to run all the tests
  11. Run git push and open a PR!

@niftytyro
Copy link

hey there can I pick this up?

@nicolo-ribaudo
Copy link
Member

Sure! Feel free to write here if you need more help :)

@CheadleCheadle
Copy link

I could try taking a stab at it if niftytyro isn't working on it anymore!

@niftytyro
Copy link

Yes sure please! I'm not working on it right now. Feel free to take it

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Feb 6, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript good first issue i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants