Skip to content

Import assertions are blanket converted to import attributes #7908

@PaulSinghDev

Description

@PaulSinghDev

Describe the bug

When using the jsc.experiment.keepImportAssertions flag the SWC compiler simply takes all assertions and outputs the new with { type: "some-type" } syntax regardless of what the input was.

This causes issues with Node being able to run the code and generates a syntax error when trying to run the generated code. As it has been accepted that the assert syntax will not be getting removed there is no need to blanket change the syntax as this causes more issues than it solves.

Input code

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

Config

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2022",
    "experimental": { "keepImportAssertions": true }
  },
  "module": {
    "type": "es6"
  },
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.82&code=H4sIAAAAAAAAA8vMLcgvKlEoSEzOTkxP9Qr291NIK8rPVVDS04eK6WUV5%2BcpKSQWF6cCFVaXVBakWikogQVrrbmS8%2FOK83NS9XLy0zWQDNG0BgDW5p%2FHWQAAAA%3D%3D&config=H4sIAAAAAAAAA0VOOw7CMAzdc4rIMwPKwMDGyMAhomKhQNNYtiu1qnL3OrSUze%2FvxXkPb%2Bng6hc7DVBkQT6wMTIPGidjQGdC6TiRwlespy2jkV%2BozYESziHAzuNEyCmj5fvW6OGDSPdMhfUmNqOpDGKK8oi%2Bur0RcnmOPf5%2Fartb%2BwUOU5LHz9birroVVF6is80AAAA%3D

SWC Info output

No response

Expected behavior

The type of assertion is preserved.

Actual behavior

The assertion is transformed into the newer with syntax.

Version

1.3.82

Additional context

I have solved this issue for me by downgrading to swc version 1.3.80. However, I think a more flexible solution is required.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions