benjamn / benjamn/recast

after insertAfter,always have a empty line

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

Description

i am trying to edit ast, then `print` it into js file,but, there is something confuse me。
after `insertAfter` something, there is a empty line,that i do not want this。
```
let hasImport = false;
traverse(parsedCode, {
ImportDeclaration(element) {
if (
element.getSibling(element.key + 1).node.type !== 'ImportDeclaration'
&& element.node.type === 'ImportDeclaration'
&& !hasImport
) {
element.insertAfter([
t.importDeclaration([
t.ImportDefaultSpecifier(t.identifier(moduleName)),
],
t.stringLiteral(modulePath)),
]);
hasImport = true;
}
},
JSXElement(element) {
const { node: { openingElement } } = element;
if (openingElement.name.name === 'Redirect') {
element.insertAfter(
[t.jSXText('\n'),
t.jSXElement(
t.jSXOpeningElement(
t.jSXIdentifier('Route'),
[
t.jSXAttribute(t.jSXIdentifier('path'), t.stringLiteral(routePath)),
t.jSXAttribute(t.jSXIdentifier('component'), t.jSXExpressionContainer(t.jSXIdentifier(moduleName))),
],
true,
),
null,
[],
true,
)],
);
}
},
});
```
![image](https://user-images.githubusercontent.com/22486446/68541715-1816ec80-03de-11ea-9bc3-689356498078.png)
Thanks! for anyone who can help
@benjamn

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the reported AST edit with insertAfter and print, using the JavaScript example in the issue. Compare the generated file with the expected formatting and trace the relevant insertAfter or printer entry point; done means the inserted import or JSX element no longer produces an unwanted empty line.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.