benjamn / benjamn/recast

recast.print: modifying a trailing CommentBlock when returning an identifier leads to assertion error

Open
#391 1 comment 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 think I've got it down to a fairly minimal test case.

Running the following code...

```javascript
'use strict';

const recast = require('recast');

const ast = recast.parse(`function a() {
return x/*comment*/;
}`);

recast.types.visit(ast, {
visitComment: function (path) {
path.value.value = path.value.value.replace('comment', 'COMMENT');
this.traverse(path);
}
});

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

leads to this assertion error:

```
AssertionError: false == true
at pushSlice (./node_modules/recast/lib/patcher.js:47:14)
at Patcher.self.get (./node_modules/recast/lib/patcher.js:63:5)
at ./node_modules/recast/lib/patcher.js:218:20
at maybeReprint (./node_modules/recast/lib/printer.js:95:41)
at print (./node_modules/recast/lib/printer.js:75:29)
at exports.printComments (./node_modules/recast/lib/comments.js:325:22)
at printWithComments (./node_modules/recast/lib/printer.js:61:16)
at FastPath.call (./node_modules/recast/lib/fast-path.js:119:16)
at genericPrintNoParens (./node_modules/recast/lib/printer.js:573:33)
at genericPrint (./node_modules/recast/lib/printer.js:164:9)
```

If I don't modify the comment, things are fine since it doesn't try to reprint it. If the semi-colon is placed before the comment, it will also work.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the supplied JavaScript reproduction and trace the assertion from lib/patcher.js through lib/printer.js and lib/comments.js. Confirm the trailing comment case where the semicolon follows the identifier, then verify that printing the modified comment completes without an assertion while the existing semicolon-before-comment case still works.

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
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.