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

Invalid sourcemaps even without any AST changes #12312

Closed
1 task done
mischnic opened this issue Nov 5, 2020 · 3 comments
Closed
1 task done

Invalid sourcemaps even without any AST changes #12312

mischnic opened this issue Nov 5, 2020 · 3 comments
Labels
area: sourcemaps i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator

Comments

@mischnic
Copy link
Contributor

mischnic commented Nov 5, 2020

Bug Report

Current behavior
This script parses a file and then stringifies the AST again without changing it:

const fs = require("fs");
const parse = require("@babel/parser").parse;
const generate = require("@babel/generator").default;

let inputCode = fs.readFileSync("index.js", "utf8");

let ast = parse(inputCode, { sourceFilename: "index.js" });

let { code: outputCode, map: outputMap } = generate(
	ast,
	{ sourceMaps: true },
	{ "index.js": inputCode }
);

fs.writeFileSync(
	"babel/index.js",
	outputCode + "\n//# sourceMappingURL=index.js.map"
);
fs.writeFileSync("babel/index.js.map", JSON.stringify(outputMap));

The bracket after the call arguments is mapped to the start of the line (just like console):
Bildschirmfoto 2020-11-05 um 11 59 23

Input Code

console.log("Other!");

Expected behavior

For example Rollup's output:

Bildschirmfoto 2020-11-05 um 12 01 35

Environment

  System:
    OS: macOS 10.15.5
  Binaries:
    Node: 14.14.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.8 - /usr/local/bin/npm
  npmPackages:
    @babel/core: ^7.12.3 => 7.12.3
    @babel/generator: ^7.12.5 => 7.12.5
    @babel/parser: ^7.12.5 => 7.12.5

Possible solution

It looks like characters that don't have their own AST node (e.g. brackets) just use the location of their parent node (CallExpression), without accounting for any child nodes that might be between the start of the parent and the character (the callee or the arguments).

Additional context
This might cause invalid sourcemaps with Parcel (after passing Babel's output to other tools).

Repo containing script & artifacts for Babel and Rollup: https://github.com/mischnic/parcel-invalid-sourcemaps

@babel-bot
Copy link
Collaborator

Hey @mischnic! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@mischnic
Copy link
Contributor Author

mischnic commented Nov 7, 2020

Looks like this is related to this function resetting the position after the argument is printed:

withSource(prop: string, loc: Location, cb: () => void): void {
if (!this._map) return cb();
// Use the call stack to manage a stack of "source location" data because
// the _sourcePosition object is mutated over the course of code generation,
// and constantly copying it would be slower.
const originalLine = this._sourcePosition.line;
const originalColumn = this._sourcePosition.column;
const originalFilename = this._sourcePosition.filename;
const originalIdentifierName = this._sourcePosition.identifierName;
this.source(prop, loc);
cb();
if (
// If the current active position is forced, we only want to reactivate
// the old position if it is different from the newest position.
(!this._sourcePosition.force ||
this._sourcePosition.line !== originalLine ||
this._sourcePosition.column !== originalColumn ||
this._sourcePosition.filename !== originalFilename) &&
// Verify if reactivating this specific position has been disallowed.
(!this._disallowedPop ||
this._disallowedPop.line !== originalLine ||
this._disallowedPop.column !== originalColumn ||
this._disallowedPop.filename !== originalFilename)
) {
this._sourcePosition.line = originalLine;
this._sourcePosition.column = originalColumn;
this._sourcePosition.filename = originalFilename;
this._sourcePosition.identifierName = originalIdentifierName;
this._sourcePosition.force = false;
this._disallowedPop = null;
}
}

@liuxingbaoyu
Copy link
Member

fixed in #14967

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 30, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: sourcemaps i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator
Projects
None yet
Development

No branches or pull requests

3 participants