[Bug]: Nodes without `loc` included in sourcemap
- Dominant language
- TypeScript
- Stars
- 44k
- Forks
- 6k
- Avg merge
- 5d 15h
- Merged PRs (30d)
- 23
Description
### 💻
- [X] Would you like to work on a fix?
### How are you using Babel?
Programmatic API (`babel.transform`, `babel.parse`)
### Input code
Babel seems to be assigning source locations to nodes which do not have a source location at all.
The below example code adds some ast without a sourcemap around an existing node.
In this case the source map for the output ` || 1` points to the `b` identifier in the source code.
This is especially problematic for code coverage which will cause this output code to count as a potentially uncovered branch.
```js
import * as t from "@babel/types";
import * as babel from "@babel/core";
const result = await babel.transformAsync(`a = b`, {
babelrc: false,
configFile: false,
sourceMaps: "inline",
plugins: [
{
visitor: {
AssignmentExpression(assignmentExpression) {
assignmentExpression
.get("right")
.replaceWith(
t.logicalExpression(
"||",
assignmentExpression.get("right").node,
t.numericLiteral(1)
)
);
},
},
},
],
});
```
Outputs the following code:
```js
a = b || 1;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhIiwiYiJdLCJzb3VyY2VzIjpbInVua25vd24iXSwic291cmNlc0NvbnRlbnQiOlsiYSA9IGIiXSwibWFwcGluZ3MiOiJBQUFBQSxDQUFDLEdBQUdDLENBQUMifQ==
```
You can view that the sourcemap is incorrect here https://evanw.github.io/source-map-visualization/#MjMyAGEgPSBiIHx8IDE7Ci8vIyBzb3VyY2VNYXBwaW5nVVJMPWRhdGE6YXBwbGljYXRpb24vanNvbjtjaGFyc2V0PXV0Zi04O2Jhc2U2NCxleUoyWlhKemFXOXVJam96TENKdVlXMWxjeUk2V3lKaElpd2lZaUpkTENKemIzVnlZMlZ6SWpwYkluVnVhMjV2ZDI0aVhTd2ljMjkxY21ObGMwTnZiblJsYm5RaU9sc2lZU0E5SUdJaVhTd2liV0Z3Y0dsdVozTWlPaUpCUVVGQlFTeERRVUZETEVkQlFVZERMRU5CUVVNaWZRPT0xMTUAeyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhIiwiYiJdLCJzb3VyY2VzIjpbInVua25vd24iXSwic291cmNlc0NvbnRlbnQiOlsiYSA9IGIiXSwibWFwcGluZ3MiOiJBQUFBQSxDQUFDLEdBQUdDLENBQUMifQ==
### Configuration file name
_No response_
### Configuration
_No response_
### Current and expected behavior
Currently the output sourcemap contains unnecessary and invalid additional mappings.
I would expect that only mappings related to nodes with a `loc` get output in the sourcemap.
### Environment
System:
OS: macOS 13.5
Binaries:
Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
pnpm: 8.8.0 - ~/pnpm/pnpm
npmPackages:
@babel/core: ^7.23.2 => 7.23.2
### Possible solution
The issue appears to be related to [this code](https://github.com/babel/babel/blob/419644f27c5c59deb19e71aaabd417a3bc5483ca/packages/babel-generator/src/buffer.ts#L471-L481) which is not cleared and applies it's position to future nodes.
### Additional context
_No response_
Contributor guide
Research direction
Start with packages/babel-generator/src/buffer.ts around lines 471-481, then run the provided babel.transformAsync reproduction using the AssignmentExpression visitor. Confirm that the generated sourcemap does not add mappings for AST nodes without loc; the existing-node mappings should remain correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100