benjamn / benjamn/recast

Original indentation gets lost when printing non-file node

Open
#1,209 0 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

I've attempted to only print a subset of the original file because I only want to replace that part through VS Code's WorkspaceEdits (I'm developing a VS Code extension). Replacing the whole file puts more pressure on the VS Code as it has to recompute syntax highlighting, code lenses, and more. And I know which part of the file I'd like to touch - so it's best to focus on that part.

However, I've discovered that in such a situation Recast dedents my result and removes the original indentation of this node.

**Repro case**

https://codesandbox.io/s/affectionate-curie-dvwykb?file=/src/index.js

```ts
import * as recast from "recast";
import * as babelParser from "recast/parsers/babel";

const ast = recast.parse(
`
createMachine(
{
initial: 'foo'
},
{
actions: {}
}
)
`,
{
parser: babelParser
}
);

const config = ast.program.body[0].expression.arguments[0];

config.properties[0].value = recast.types.builders.stringLiteral("REPLACED");

console.log(recast.print(config).code);
```

**Actual**
```
{
initial: "REPLACED"
}
```

**Expected**
```
{
initial: "REPLACED"
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the src/index.js reproduction from the linked CodeSandbox and trace recast.parse with the Babel parser through recast.print(config). Verify how printing the non-file config node handles its original indentation. Done means the output preserves the two-space indentation shown in the expected result while still replacing the property value.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.