Indentation inside template string is not preserved in some cases
- Dominant language
- TypeScript
- Stars
- 5.3k
- Forks
- 363
- Avg merge
- 3d 8h
- Merged PRs (30d)
- 3
Description
In some cases when printing a function with template string in arguments leading spaces are removed…
Here is a failing test to reproduce the issue:
```ts
it("preserves indentation in template expression argument in function call converted from flow to ts", function () {
const source = [
"// @flow",
"test(",
" \`1",
" 2",
" 3",
"4\`);",
].join(eol);
const expected = [
"// @flow",
"test(\`1",
" 2",
" 3",
"4\`);",
].join(eol);
const t = require('@babel/types')
const ast = recast.parse(source, {
parser: require("../parsers/flow"),
});
const traverse = require('@babel/traverse').default;
traverse(ast, {
CallExpression(path: any) {
path.node.typeArguments = null;
path.node.typeParameters = t.tsTypeParameterInstantiation([
t.tsAnyKeyword(),
]);
},
});
const printer = new Printer();
const result = printer.print(ast).code;
assert.strictEqual(result, expected);
});
```
```diff
+ '// @flow\ntest(`1\n2\n3\n4`);'
- '// @flow\ntest(`1\n 2\n 3\n4`);'
```
Another example on which will fail in a similar way:
```
// @flow
export const StyledTableHeadCell = styled(
'th',
() => {
return {
'::after': {
left: '0',
backgroundImage: `
linear-gradient()
`,
},
};
},
);
```
---
Here is a workaround, which happens to work for me:
```ts
const visitor = {
TemplateElement(path: any) {
path.node.original = null;
},
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the supplied failing test and inspect how Printer handles TemplateElement nodes during printing. Compare the output with the expected indentation and verify that template-string indentation remains preserved without relying on the shown visitor workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100