benjamn / benjamn/recast

recast + babel parser generate wrong start property

Open
#717 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
5.3k
Forks
364
Avg merge
3d 8h
Merged PRs (30d)
3

Description

When I parse the js code:

```

function test() {

}
test()
```

Th e `start` property of the first function declaration is wrong of `recast + babel parser`:

```javascript
const { resolve } = require("path");
const fs = require("fs");
const recast = require("recast");

const jsCode = fs.readFileSync(resolve(__dirname, "./test.js"), {
encoding: "UTF-8"
});
const ast = recast.parse(jsCode, { parser: require("@babel/parser") });
console.log(ast)
```

![screenshot_2020-06-14_19-00-31](https://user-images.githubusercontent.com/41773861/84592126-d2d80300-ae75-11ea-982e-9dde1e9319dd.png)

If use the `babel parser` only is right:

```javascript
const { resolve } = require("path");
const fs = require("fs");
const { parse: babelParse } = require("@babel/parser");

const jsCode = fs.readFileSync(resolve(__dirname, "./test.js"), {
encoding: "UTF-8"
});
const ast = babelParse(jsCode);
console.log(ast)
```

![screenshot_2020-06-14_19-02-27](https://user-images.githubusercontent.com/41773861/84592090-87255980-ae75-11ea-8588-80187e4d00db.png)

One thing that must be pointed out is that there are two empty lines before the function declaration.

Contributor guide

No contributing guide indexed for this repository

Research direction

No project files or tests are named. Start by reproducing the supplied recast.parse example with @babel/parser and the JavaScript input containing two blank lines before the function; compare its start property with direct Babel parsing. Done means the function declaration receives the expected start position in the recast AST.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.