Skip to content

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Dec 12, 2022

Q                       A
Fixed Issues? Deprecate the ModuleDeclaration alias and rename to ImportOrExportDeclaration
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In this PR we deprecate the ModuleDeclaration alias for ImportOrExportDeclaration, due to the name conflict with the stage 2 Module Declarations proposal. The ImportOrExportDeclaration alias now covers the following AST types:

  • ImportDeclaration
  • ExportDefaultDeclaration
  • ExportNamedDeclaration
  • ExportAllDeclaration

We plan to deprecate the ModuleDeclaration alias usage starting from Babel 7.21. In the next major version, ModuleDeclaration will longer function as an alias, instead it will refer to the ModuleDeclaration node module foo {}, followed by the is* and assert* helpers.

Migration guide (for app users)

If one of your Babel plugins is using deprecated methods, you will see errors as such:

Trace: `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration`.
    at isModuleDeclaration (/path/to/example/node_modules/@babel/types/lib/validators/generated/index.js:3939:11)
    at PluginPass.Program (/path/to/example/node_modules/babel-plugin-example/lib/index.js:102:44)
    at newFn (/path/to/example/node_modules/@babel/traverse/lib/visitors.js:148:21)
    at NodePath._call (/path/to/example/node_modules/@babel/traverse/lib/path/context.js:45:20)
    at NodePath.call (/path/to/example/node_modules/@babel/traverse/lib/path/context.js:35:17)
    at NodePath.visit (/path/to/example/node_modules/@babel/traverse/lib/path/context.js:80:31)
    at TraversalContext.visitQueue (/path/to/example/node_modules/@babel/traverse/lib/context.js:86:16)
    at TraversalContext.visitSingle (/path/to/example/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/path/to/example/node_modules/@babel/traverse/lib/context.js:109:19)
    at traverseNode (/path/to/example/node_modules/@babel/traverse/lib/traverse-node.js:18:17)

Reading the error stack trace from top to bottom, we can find the first path that is not related to @babel/ internals, in this case, it is babel-plugin-example on the third line:

at PluginPass.Program (/path/to/example/node_modules/babel-plugin-example/lib/index.js:102:44)

Now, please open a new issue to the affected Babel plugin (babel-plugin-example) and attach this issue to plugin authors as references (The next section is migration guide for plugin authors). Before the plugin author migrates the usage, you can pin @babel/* to v7.20.

Migration guide (for plugin authors)

@babel/types usage

If @babel/types is in dependencies, bump it to ^7.21.0 and update the usage:

import * as t from "@babel/types";
--- t.isModuleDeclaration(node)
+++ t.isImportOrExportDeclaration(node)

--- t.assertModuleDeclaration(node)
+++ t.assertImportOrExportDeclaration(node)

@babel/core usage

If @babel/core is in peerDependencies or you are using Plugin API injected as the first parameter of your plugin.
In this case your plugin should work with both Babel < 7.21 and 7.21+

export default function (api) {
  const t = api.types;
--- t.isModuleDeclaration(node)
+++ t.isImportDeclaration(node) || t.isExportDeclaration(node)

--- t.assertModuleDeclaration(node)
+++ if (!t.isImportDeclaration(node) && !t.isExportDeclaration(node)) {
+++   throw new Error(`Expected type "ImportOrExportDeclaration", but instead got "${node.type}".`)
+++ }
}

Plugin visitor or traverse usage

{
  visitor: {
---    ModuleDeclaration(path, state) {
+++   "ImportDeclaration|ExportDeclaration"(path, state) {
    }
  }
}

Rename ModuleDeclaration to ImportOrExportDeclaration in favor of the module declarations proposal.
@JLHwung JLHwung added the PR: Deprecation 📢 A type of pull request used for our changelog categories label Dec 12, 2022
@babel-bot
Copy link
Collaborator

babel-bot commented Dec 12, 2022

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/53651/

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this PR removes ModuleDeclaration from t.FLIPPED_ALIAS_KEYS. Can we keep it until Babel 8?

@JLHwung JLHwung force-pushed the deprecate-module-declaration-alias branch from 2472c0d to 6911d1b Compare December 14, 2022 19:55
@nicolo-ribaudo nicolo-ribaudo added this to the v7.21.0 milestone Dec 15, 2022
@nicolo-ribaudo nicolo-ribaudo added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Dec 15, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit bca362a into babel:main Feb 18, 2023
@nicolo-ribaudo nicolo-ribaudo deleted the deprecate-module-declaration-alias branch February 18, 2023 11:52
@amingRo
Copy link

amingRo commented Feb 21, 2023

in version 7.21.0,i get the mistake
Trace: isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration.
what can i do to fix this?

@mannymu
Copy link

mannymu commented Feb 21, 2023

Trace: isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration.
what can i do to fix this?

@eleven-net-cn
Copy link

Many third-party packages will automatically use the v7.21.0 version, causing too many warnings in the terminal, and can only wait for the third-party package to change.

So sad!

@amingRo
Copy link

amingRo commented Feb 21, 2023

in version 7.21.0,i get the mistake Trace: isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration. what can i do to fix this?

好了兄弟们,估计作者这会儿还在睡觉?他们那可能是晚上,我先给大家提供一个临时的解决方案,可以试试对你们是否有帮助。
ok,all brothers,there some problems need the author to answer,if somebodys work are blocked,there are some temporary resolutions for you.
1.change your packages manager to Yarn
2.edit your packages.json,add this
resolutions:{ "@babel/core":"7.20.12", "@babel/types":"7.20.7", "@babel/traverse":"7.20.13" }
3. run yarn in your terminal
4. start or build your programs
it's useful for me,maybe can help you

@eleven-net-cn
Copy link

in version 7.21.0,i get the mistake Trace: isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration. what can i do to fix this?

好了兄弟们,估计作者这会儿还在睡觉?他们那可能是晚上,我先给大家提供一个临时的解决方案,可以试试对你们是否有帮助。 ok,all brothers,there some problems need the author to answer,if somebodys work are blocked,there are some temporary resolutions for you. 1.change your packages manager to Yarn 2.edit your packages.json,add this resolutions:{ "@babel/core":"7.20.12", "@babel/types":"7.20.7", "@babel/traverse":"7.20.13" } 3. run yarn in your terminal 4. start or build your programs it's useful for me,maybe can help you

only for yarn, can not with npm, so sad ...

@lpbottle
Copy link

in version 7.21.0,i get the mistake Trace: isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration. what can i do to fix this?

好了兄弟们,估计作者这会儿还在睡觉?他们那可能是晚上,我先给大家提供一个临时的解决方案,可以试试对你们是否有帮助。 ok,all brothers,there some problems need the author to answer,if somebodys work are blocked,there are some temporary resolutions for you. 1.change your packages manager to Yarn 2.edit your packages.json,add this resolutions:{ "@babel/core":"7.20.12", "@babel/types":"7.20.7", "@babel/traverse":"7.20.13" } 3. run yarn in your terminal 4. start or build your programs it's useful for me,maybe can help you

666,这个临时方案可用,强迫症表示实在无法忍受编译时出现这么一大堆编译错误,坐等插件作者更新

@nicolo-ribaudo
Copy link
Member

I'm releasing #15448 now, which should greatly reduce the number of warnings (to 1 in most cases).

@dqm07
Copy link

dqm07 commented Mar 15, 2023

in version 7.21.0,i get the mistake Trace: isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration. what can i do to fix this?

好了兄弟们,估计作者这会儿还在睡觉?他们那可能是晚上,我先给大家提供一个临时的解决方案,可以试试对你们是否有帮助。 ok,all brothers,there some problems need the author to answer,if somebodys work are blocked,there are some temporary resolutions for you. 1.change your packages manager to Yarn 2.edit your packages.json,add this resolutions:{ "@babel/core":"7.20.12", "@babel/types":"7.20.7", "@babel/traverse":"7.20.13" } 3. run yarn in your terminal 4. start or build your programs it's useful for me,maybe can help you

Soooo Nice, It works for me. Thanks again!

jinhodotchoi added a commit to jinhodotchoi/my-company that referenced this pull request May 8, 2023
@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 Jun 14, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: types PR: Deprecation 📢 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants