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

Package breaks when using import assertions #270

Open
Csszabi98 opened this issue Nov 21, 2023 · 7 comments · May be fixed by #273 or #293
Open

Package breaks when using import assertions #270

Csszabi98 opened this issue Nov 21, 2023 · 7 comments · May be fixed by #273 or #293

Comments

@Csszabi98
Copy link

Your Environment

  • Prettier version: 3.1.0
  • node version 18.17.1
  • package manager: npm@9.6.7
  • IDE: VSCode, CLI

Describe the bug

When running prettier against the import assertion syntax,
(import X from 'y' assert { type: 'json' })
both using the new with and the deprecated assert keywords,
the passing breaks for the given file.

This is especially problematic because newer node versions require the assertion to be present when running under esm otherwise
the application will throw runtime exceptions, if not running through a bundler first ERR_IMPORT_ASSERTION_TYPE_MISSING.

To Reproduce

  1. Open the reproduciton
    prettier-plugin-import-attributes-issue.zip
  2. npm install && npm run prettier

Notes:

  • Babel is properly set up in the example:
    .prettierrc.cjs
importOrderParserPlugins: [
    'typescript',
    'jsx',
    '["importAttributes", { "deprecatedAssertSyntax": true }]',
]

Expected behavior

Import assertions should not break the parsing.

Screenshots, code sample, etc

image

Configuration File (.prettier.cjs)

module.exports = {
  plugins: [require.resolve('@trivago/prettier-plugin-sort-imports')],
  arrowParens: 'always',
  bracketSameLine: false,
  bracketSpacing: true,
  importOrder: ['<THIRD_PARTY_MODULES>', '^@/(.*)$', '^[./]'],
  importOrderParserPlugins: [
    'typescript',
    'jsx',
    '["importAttributes", { "deprecatedAssertSyntax": true }]',
  ],
  importOrderGroupNamespaceSpecifiers: true,
  importOrderSeparation: true,
  importOrderSortSpecifiers: true,
  quoteProps: 'as-needed',
  semi: true,
  singleAttributePerLine: true,
  singleQuote: true,
  trailingComma: 'all',
  vueIndentScriptAndStyle: false,
};

Error log

[error] src/main.ts: SyntaxError: ';' expected. (3:36)
[error]   1 | import { setupCounter } from './counter.ts';
[error]   2 | import './style.css';
[error] > 3 | import { test } from './test.json' assert { type: 'json' };
[error]     |                                    ^
[error]   4 | import typescriptLogo from './typescript.svg';
[error]   5 | import viteLogo from '/vite.svg';
[error]   6 |
@chentsulin
Copy link

After upgraded to prettier 3.1.1, the error message is changed from SyntaxError: ';' expected. to SyntaxError: '{' expected.

@chentsulin
Copy link

We need to allow passing the importAttributesKeyword option to babel-generator:

const { code } = generate(newAST);

If the importAttributesKeyword option is undefined, babel unexpectedly transforms with { type: 'json' } to with type: 'json' (with-legacy): https://github.com/babel/babel/blob/8fa2a916e0bb601e728c3d368c1e271763880f40/packages/babel-generator/src/generators/modules.ts#L102-L106

Ref: https://babeljs.io/docs/babel-generator

@chentsulin
Copy link

Another requirement is to bump @babel/generator to >=7.22.0:

"@babel/generator": "7.17.7",

Ref: https://github.com/babel/babel/releases/tag/v7.22.0

@chentsulin chentsulin linked a pull request Dec 12, 2023 that will close this issue
1 task
raineorshine added a commit to raineorshine/npm-check-updates that referenced this issue Dec 18, 2023
@trivago/prettier-plugin-sort-imports does not yet support import assertions.

See: trivago/prettier-plugin-sort-imports#270
@JulianCataldo
Copy link

JulianCataldo commented Feb 4, 2024

I have the same problem. I tried to fiddle but the Babel parser importAttributes isn't working, while importAssertions does work. That is a blocker, so I had to disable the plugin altogether, so I can still continue to format the document as needed.

@fisker
Copy link

fisker commented Feb 6, 2024

You can use ["importAttributes", { deprecatedAssertSyntax: true }] to support both new and legacy syntax.

@vincerubinetti
Copy link

Is there any solution to this, even just a way to make a prettier lint check or in-IDE check not fail? I tried // prettier-ignore, // sort-imports-ignore, // @eslint-disable-next-line, using importAttributes vs importAssertions, with vs assert, nothing works.

I need to have this because I'm importing some JSON into a Playwright test and it can't handle it without the assert for some reason.

@MillerSvt
Copy link

You can use ["importAttributes", { deprecatedAssertSyntax: true }] to support both new and legacy syntax.

it is not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants