benjamn / benjamn/recast

Incorrect print of default export of template literal

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

Description

When printing a default export whose value is or includes a template literal, there are extra backticks in the output:

```js
const recast = require("recast");
const types = require("ast-types");

const ast = recast.parse("export default `${3}`");

types.visit(ast, {
visitTemplateLiteral(path) {
console.log(recast.print(path.node).code);
return false;
}
});
```

## Actual behavior

The above example code logs '``${3}``', which isn't valid JS

## Expected behavior

The above example code should log '`${3}`'

## Details

Tested with Recast 0.18.1 (latest as of this writing)

Thanks for your help in figuring this out!

Deleting the two calls to 'parts.push("\`")' in the `case "TemplateLiteral"` statement in printer.ts seems like it might fix the issue.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the default-export template-literal example from the issue with Recast, then inspect the `case "TemplateLiteral"` statement in `printer.ts`, especially the two `parts.push("\`")` calls. Done means `recast.print(path.node).code` produces ```${3}``` rather than extra backticks and the example output is valid JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.