Additional semicolon after "strict mode" directive when @babel/parser used
- Dominant language
- TypeScript
- Stars
- 5.3k
- Forks
- 363
- Avg merge
- 3d 8h
- Merged PRs (30d)
- 3
Description
When I use `recast` with `@babel/parser` I got a strange behavior with `use strict` directive.
After removing a `node` near `use strict`, additional semicolon occurs, and new line is removed:
This code:
```js
'use strict';
const {a} = obj;
const t = 'hi';
```
Becames this:
```js
'use strict';;
const t = 'hi';
```
Would be great if new line not removed and semicolon not appear 🙂. Thank you for a great library 🙂.
```js
const babel = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;
const recast = require('recast');
const source = `
'use strict';
const {a} = obj;
const t = 'hi';
`;
const babelAST = recast.parse(source, {
parser: {
parse: babel.parse,
}
});
let once = false;
traverse(babelAST, {
enter(path) {
if (once)
return;
if (path.type !== 'VariableDeclaration')
return;
path.remove();
once = true;
}
});
console.log(recast.print(babelAST));
// outputs
// PrintResult { code: "\n 'use strict';;\n const t = 'hi';\n" }
console.log(generate(babelAST));
// outputs
// { code: "'use strict';\n\nconst t = 'hi';",
// map: null,
// rawMappings: null }
```
This can relate to https://github.com/benjamn/recast/issues/344
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the supplied reproduction using recast.parse with @babel/parser, then follow recast.print after removing the VariableDeclaration node. Compare the printed result with @babel/generator's output and inspect how the 'use strict' directive and surrounding newline are handled. Done means the removal no longer produces an extra semicolon or removes the blank line.
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
- Clearly specified
- Newbie friendliness
- 35/100