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]: Incorrect print result of TSModuleDeclaration #16513

Closed
1 task
qbx2 opened this issue May 18, 2024 · 7 comments
Closed
1 task

[Bug]: Incorrect print result of TSModuleDeclaration #16513

qbx2 opened this issue May 18, 2024 · 7 comments

Comments

@qbx2
Copy link

qbx2 commented May 18, 2024

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

import parser from "@babel/parser";
import generator from "@babel/generator";

const generate = generator.default;

const ast = parser.parse(`module MyModule {
  export function myFunction(): void {
    console.log("Hello, world!");
  }
}`, {
  sourceType: "module",
  plugins: [
    "jsx",
    "typescript",
  ],
  ranges: false,
});

console.log(generate(ast).code);

Configuration file name

No response

Configuration

No response

Current and expected behavior

Current behavior:

namespace MyModule {
  export function myFunction(): void {
    console.log("Hello, world!");
  }
}

Expected behavior:

module MyModule {
  export function myFunction(): void {
    console.log("Hello, world!");
  }
}

Environment

System:
OS: macOS 14.1.1
Binaries:
Node: 18.17.0 - ~/Library/Caches/fnm_multishells/19593_1715933137130/bin/node
Yarn: 1.22.17 - /opt/homebrew/bin/yarn
npm: 9.6.7 - ~/Library/Caches/fnm_multishells/19593_1715933137130/bin/npm
pnpm: 8.13.1 - ~/Library/Caches/fnm_multishells/19593_1715933137130/bin/pnpm
npmPackages:
@babel/generator: ^7.24.5 => 7.24.5
@babel/parser: ^7.24.5 => 7.24.5
@babel/types: ^7.24.5 => 7.24.5

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @qbx2! 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.

@nicolo-ribaudo
Copy link
Member

The two syntaxes are equivalent (like choosing to use single vs double quotes in strings), and module is deprecated. I think Babel should keep the current behaviour.

@qbx2
Copy link
Author

qbx2 commented May 19, 2024

@nicolo-ribaudo In old TypeScript, you might be right. However in the latest TypeScript, 'module' is not deprecated and different from namespace.

@qbx2
Copy link
Author

qbx2 commented May 19, 2024

From https://www.typescriptlang.org/docs/handbook/namespaces.html ,

A note about terminology: It’s important to note that in TypeScript 1.5, the nomenclature has changed. “Internal modules” are now “namespaces”. “External modules” are now simply “modules”, as to align with ECMAScript 2015’s terminology, (namely that module X { is equivalent to the now-preferred namespace X {).

This post outlines the various ways to organize your code using namespaces (previously “internal modules”) in TypeScript. As we alluded in our note about terminology, “internal modules” are now referred to as “namespaces”. Additionally, anywhere the module keyword was used when declaring an internal module, the namespace keyword can and should be used instead. This avoids confusing new users by overloading them with similarly named terms.

It seems that you’re talking that ’internal module’ is deprecated but I’m talking that babel rewrites ‘external module’ to ‘namespace (internal module)’

@qbx2
Copy link
Author

qbx2 commented May 19, 2024

The difference of two is: ‘module’ won’t be shared across files. Unlike ‘module’, ‘namespace’ can be shared across files if the name is equivalent. Therefore, it can lead to a name conflict to rewrite module to namespace.

@nicolo-ribaudo
Copy link
Member

"external modules" are when you write import/export at the top level of the file, not the code you are showing (which is an internal module).

From the docs you quoted: "(namely that module X { is equivalent to the now-preferred namespace X {)."

@qbx2
Copy link
Author

qbx2 commented May 19, 2024

@nicolo-ribaudo Oh, there was my misunderstanding. Thank you for the clarification.

@qbx2 qbx2 closed this as not planned Won't fix, can't repro, duplicate, stale May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants