Skip to content

SWC parser returns unevenly escaped windows path in ast #11214

@kwonoj

Description

@kwonoj

Describe the bug

Having a windows path in a variable returns part of path segment have duplicated backslash, output ast is

{
  "type": "Module",
  "span": {
    "start": 0,
    "end": 108
  },
  "body": [
    {
      "type": "VariableDeclaration",
      "span": {
        "start": 0,
        "end": 108
      },
      "ctxt": 0,
      "kind": "var",
      "declare": false,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "span": {
            "start": 4,
            "end": 108
          },
          "id": {
            "type": "Identifier",
            "span": {
              "start": 4,
              "end": 16
            },
            "ctxt": 2,
            "value": "coverageData",
            "optional": false,
            "typeAnnotation": null
          },
          "init": {
            "type": "ObjectExpression",
            "span": {
              "start": 19,
              "end": 108
            },
            "properties": [
              {
                "type": "KeyValueProperty",
                "key": {
                  "type": "Identifier",
                  "span": {
                    "start": 26,
                    "end": 30
                  },
                  "value": "path"
                },
                "value": {
                  "type": "StringLiteral",
                  "span": {
                    "start": 32,
                    "end": 101
                  },
                  "value": "C:\\github\\swc-plugin-coverage-instrument\\spec\\\\util\\verifier.ts",
                  "raw": "\"C:\\\\github\\\\swc-plugin-coverage-instrument\\\\spec\\\\util\\\\verifier.ts\""
                }
              }
            ]
          },
          "definite": false
        }
      ]
    }
  ],
  "interpreter": null
}

the relavant node is

 "value": {
                  "type": "StringLiteral",
                  "span": {
                    "start": 32,
                    "end": 101
                  },
                  "value": "C:\\github\\swc-plugin-coverage-instrument\\spec\\\\util\\verifier.ts",
                  "raw": "\"C:\\\\github\\\\swc-plugin-coverage-instrument\\\\spec\\\\util\\\\verifier.ts\""
                }

in value, there's spec\\\\util while all others are \\.

Input code

var coverageData = {
    path: "C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts",   
}

Config

Default ast parser config in playground

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.14.0&code=H4sIAAAAAAAAAzWMMQqAMAwAd8E%2FhM7WBwhO%2BowsscQa0FraVAfx73bx1uPuogTuvDiR55mUYISnbaASSbcBzDQgetGtLIj5djbuxUuwf2MlZE3l4KDVR3aIRWVHrFpW4dRrNl3dtc37AcmeTbtsAAAA&config=H4sIAAAAAAAAA1WPSw6DMAxE95wCed1tu%2Bgd2PQGVmpQUH6yjQRCvXsDJLTs4pnnmXht2hZGMfBs1%2FzMQ0IW4nPOiixBcc4KkPEohm1SuFWX5uSssfoiiRMb6jDgQJ6C5g3liU5ylC2kRye0S5%2FDAUUeSPd8uZdgcDEKVbxo3gbbL%2F9fM9EnJpEruKEYBkfXuqZUgo%2FvaTfLybokOuof8INq2RkMVrq6uR3WfL4TPVvTPgEAAA%3D%3D

SWC Info output

    Operating System:
        Platform: win32
        Arch: x64
        Machine Type: x86_64
        Version: Windows 11 Pro
        CPU: (8 cores)
            Models: Snapdragon (TM) 8cx Gen 3 @ 3.0 GHz

    Binaries:
        Node: 25.1.0
        npm: 11.6.2
        Yarn: N/A
        pnpm: N/A

    Relevant Packages:
        @swc/core: 1.14.0
        @swc/helpers: N/A
        @swc/types: 0.1.25
        typescript: 5.5.3

    SWC Config:
        output: N/A
        .swcrc path: N/A

    Next.js info:
        output: N/A

Expected behavior

expect "value": "C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts",

Actual behavior

value output have inconsistent backslash

"value": "C:\\github\\swc-plugin-coverage-instrument\\spec\\\\util\\verifier.ts",

Version

1.14.0

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions