Skip to content

Nested arrays cause off-by-one error in generated sourcemaps #8066

@marcins

Description

@marcins

Describe the bug

When SWC transforms a nested array, the resulting sourcemap has an off-by-one error where the mapping for the end of one of the arrays points to an invalid location in the source.

I validated this by taking the input code below and running it through this code:

import { transform } from '@swc/core';
import fs from 'node:fs';

async function main() {
    const result = await transform(fs.readFileSync('./src/index.js', 'utf8'), {
        sourceMaps: true,
    });
    fs.writeFileSync('./dist/index.js', result.code, 'utf8');
    fs.writeFileSync('./dist/index.js.map', result.map + "\n//# sourceMappingURL=index.js.map",'utf8');
}

main().catch(e => {
    process.exitCode = 1;
    console.error(e);
})

.. and then trying to load the resulting output with source-map-explorer which fails with:

  Your source map refers to generated column 7 on line 2, but the source only contains 6 column(s) on that line.
  Check that you are using the correct source map.

See also the attached Source Map Visualiser link in the repro.

Input code

var a = [[]];

Config

n/a

Playground link (or link to the minimal reproduction)

https://evanw.github.io/source-map-visualization/#NTMAdmFyIGEgPSBbCiAgICBbXQpdOwovLyMgc291cmNlTWFwcGluZ1VSTD1pbmRleC5qcy5tYXAxMjcAeyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgYSA9IFtbXV07Il0sIm5hbWVzIjpbImEiXSwibWFwcGluZ3MiOiJBQUFBLElBQUlBLElBQUk7SUFBQyxFQUFFO0NBQUMifQ==

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 22.6.0: Fri Sep 15 13:41:28 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_ARM64_T6000
    CPU: (10 cores)
        Models: Apple M1 Pro

Binaries:
    Node: 18.18.0
    npm: 9.8.1
    Yarn: 1.22.19
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.3.91
    @swc/helpers: 0.5.2
    @swc/types: 0.1.5


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

Next.js info:
    output: N/A

Expected behavior

The sourcemap will be valid.

Actual behavior

The sourcemap is invalid, the second closing paren is mapped past the end of the line:

image

Version

1.3.91

Additional context

https://evanw.github.io/source-map-visualization/#NTMAdmFyIGEgPSBbCiAgICBbXQpdOwovLyMgc291cmNlTWFwcGluZ1VSTD1pbmRleC5qcy5tYXAxMjcAeyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgYSA9IFtbXV07Il0sIm5hbWVzIjpbImEiXSwibWFwcGluZ3MiOiJBQUFBLElBQUlBLElBQUk7SUFBQyxFQUFFO0NBQUMifQ==

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions