Skip to content

Default importAttributesKeyword to with#16977

Merged
nicolo-ribaudo merged 6 commits intobabel:mainfrom
JLHwung:default-importAttributesKeyword-to-with
Mar 21, 2025
Merged

Default importAttributesKeyword to with#16977
nicolo-ribaudo merged 6 commits intobabel:mainfrom
JLHwung:default-importAttributesKeyword-to-with

Conversation

@JLHwung
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung commented Nov 25, 2024

Q                       A
Fixed Issues? Fixes #16975
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 defaults the Babel 7 generator option importAttributesKeyword (IAK) to with and prints the missing IAK warning only when users are using a non-standard import attributes syntax, e.g. assert { type: "json" } or with type: "json".

Given that the import attributes proposal has reached stage 4, I think such a change should be considered spec compliance: Babel should output valid JS syntax by default and we should not nudge users for the importAttributesKeyword option unless they are actually using the out-dated syntax.

Caveat: it could be a breaking change for users when these conditions are both satisfied:

  1. Users depend on a long deprecated import attributes syntax with type: "json"
  2. Users have pinned @babel/parser but chooses to update @babel/generator to the latest version

@JLHwung JLHwung added the PR: Spec Compliance 👓 A type of pull request used for our changelog categories label Nov 25, 2024
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Nov 25, 2024

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

@JLHwung JLHwung marked this pull request as ready for review November 25, 2024 14:42
@JLHwung JLHwung force-pushed the default-importAttributesKeyword-to-with branch from 65843c4 to 1d2a877 Compare November 25, 2024 14:46
@@ -1,4 +1,3 @@
{
"BABEL_8_BREAKING": true,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The attributes-with-to-default is merged with this case now that we output the standard syntax by default.

@@ -1 +1 @@
import "a" with type: "json"; No newline at end of file
import "a" with { type: "json" }; No newline at end of file
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We output the standard syntax on input import "a" with { type: "json" }; and options

{
  "BABEL_8_BREAKING": false,
  "plugins": ["importAssertions"]
}

@@ -1 +1 @@
import "a" with type: "json"; No newline at end of file
import "a" with { type: "json" }; No newline at end of file
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We output the standard syntax on input import "a" assert { type: "json" }; and options

{
  "BABEL_8_BREAKING": true,
  "plugins": ["deprecatedImportAssert"]
}

@JLHwung JLHwung force-pushed the default-importAttributesKeyword-to-with branch from f57dd7f to e1fed8c Compare November 25, 2024 15:05
{
"BABEL_8_BREAKING": false,
"plugins": ["importAssertions"],
"warns": "You are using import attributes, without specifying the desired output syntax.",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The input is already valid: import "a" with { type: "json" };

@JLHwung JLHwung requested a review from liuxingbaoyu November 25, 2024 15:52
@liuxingbaoyu
Copy link
Copy Markdown
Member

  1. Users depend on a long deprecated import attributes syntax with type: "json"

From what I understand we already avoid this with extra?

2. Users have pinned @babel/parser but chooses to update @babel/generator to the latest version

Perhaps we could add extra to with instead of with-legacy to avoid this?

Copy link
Copy Markdown
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.

I have a light preference for doing the opposite: for Babel 7, we store on .extra that the user is using the new syntax.

However, it's more out of a purity semver point of view, and I agree that it's very likely that this wouldn't break anybody. https://www.npmjs.com/package/@babel/plugin-syntax-module-attributes has been deprecated for ages, and it has basically no downloads.

@JLHwung
Copy link
Copy Markdown
Contributor Author

JLHwung commented Nov 26, 2024

If we add extra to the standard syntax but later remove it in Babel 8, I think it will unnecessarily confuse plugin developers: Although Babel 8 will not support with-legacy syntax, the feature detection such as if (node.extra.standardWithSyntax) will break in Babel 8 if we do the opposite.

Like Nicolò has mentioned, there is basically no downloads for the long deprecated syntax, I would prefer to promote the ES2025 syntax and hide the IAK option: This option has served its purpose well and it's time to move on.

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Wdyt about doing this in a minor release, so we can at least mention it in the blog post?

@nicolo-ribaudo nicolo-ribaudo modified the milestones: v7.26.0, v7.27.0 Jan 10, 2025
@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 Mar 20, 2025
@nicolo-ribaudo nicolo-ribaudo merged commit 1386d3d into babel:main Mar 21, 2025
@nicolo-ribaudo nicolo-ribaudo deleted the default-importAttributesKeyword-to-with branch March 21, 2025 10:28
laine-hallot pushed a commit to laine-hallot/uwu-parser that referenced this pull request Mar 31, 2025
* fix: default importAttributesKeyword to with

* feat: mark module attributes with deprecated syntax meta

* polish: print missing IAK warning when using non-standard syntax

* fix: default to with-legacy if deprecatedWithLegacySyntax

* revert some test changes

* test: update the first thrown fixture
@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 21, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 21, 2025
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 PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release PR: Spec Compliance 👓 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: import attributes is transformed in an unexpected way but without an error

4 participants