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

Babel plugin importAttributes can't be enabled and formatting fails #15911

Closed
hdodov opened this issue Jan 12, 2024 · 5 comments
Closed

Babel plugin importAttributes can't be enabled and formatting fails #15911

hdodov opened this issue Jan 12, 2024 · 5 comments
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues)

Comments

@hdodov
Copy link

hdodov commented Jan 12, 2024

I'm trying to format a TypeScript file called index.ts with the following line inside:

import content from "./content.json" assert { type: "json" }; 

…but I get the following error:

[error] src/index.ts: SyntaxError: This experimental syntax requires enabling one of the following parser plugin(s): "importAttributes", "importAssertions". (3:37)

I tried adding a babel.config.json file containing the following:

{
	"plugins": ["importAttributes"],
	"parserOpts": {
		"plugins": ["importAttributes"]
	}
}

…but it didn't work.

I also tried installing @babel/plugin-syntax-import-assertions and putting it in babel.config.json, .babelrc, .prettierrc.json as well, etc, but nothing seems to enable the plugin.

Environments:

  • Prettier Version: 3.1.1, @babel/parser 7.23.6 (from package-lock.json)
  • Running Prettier via: npx prettier --write .
  • Runtime: Node v20.9.0, npm 10.1.0
  • Operating System: macOS Ventura 13.6.3
  • Prettier plugins (if any): @trivago/prettier-plugin-sort-imports (don't think it's relevant)

Steps to reproduce:

Try to format the aforementioned line with Prettier 3.1.1 and you'll see an error.

@sosukesuzuki
Copy link
Member

The version of @babel/parser and plugins you have installed has no effect on Prettier's output. Also import attributes syntax has been supported by Prettier 3.1.1. Can you reproduce on playground?

Prettier 3.1.1
Playground link

--parser typescript

Input:

import content from "./content.json" assert { type: "json" }; 

Output:

import content from "./content.json" assert { type: "json" };

@sosukesuzuki sosukesuzuki added the lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) label Jan 14, 2024
@fisker fisker added the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Jan 15, 2024
@hdodov
Copy link
Author

hdodov commented Jan 16, 2024

@sosukesuzuki I've made a reproduction repo: https://github.com/hdodov/mcve-prettier-import-attributes

It appears that somehow @trivago/prettier-plugin-sort-imports is causing the issue. If I remove it from .prettierrc.json in the reproduction repo, everything works. Maybe the error message generated by Prettier is misleading? I can see that @fisker's PR is related to something like that.

@github-actions github-actions bot removed the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Jan 16, 2024
@fisker
Copy link
Member

fisker commented Jan 16, 2024

#15928 is hiding this kind of error message, since we don't read the babel config.

@fisker
Copy link
Member

fisker commented Jan 18, 2024

Maybe caused by this line, can you open an issue there?

@fisker fisker closed this as completed Jan 19, 2024
@lukeapage
Copy link

FYI I found this issue first and fixed it for the plugin with this:

importOrderParserPlugins: ['importAssertions'],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues)
Projects
None yet
Development

No branches or pull requests

4 participants