unassert-js / unassert

Encourages programming with assertions by providing tools to compile them away.
MIT License
192 stars 13 forks source link

Comments are being stripped. #13

Closed RebeccaStevens closed 5 years ago

RebeccaStevens commented 5 years ago

This tool should not strip out comments from the input; it should only strip out asserts.

Not sure where exactly the problem lays but it seems to exist in both babel-plugin-unassert and rollup-plugin-unassert.

twada commented 5 years ago

@RebeccaStevens Thank you for your suggestion.

Basically code manipulation such as babel consists of 3 phases.

  1. code -(parser)-> ast
  2. ast -(transformer)-> ast
  3. ast -(generator)-> code

The phase 2 is where unassert core lives, so unassert does not touch any comments since it manipulates AST where comments are already removed by the parser in phase 1.

To preserve comments, configuration of phase 1 and 3, in other words configration of parser and generator, should be configured to preserve comments. In Babel7, there are some options for comments.

Example unassert but preserve comments

But there are also tools that parses, transforms and generates to unassert. Parsers remove code comments by default and unassert tools does not change the behavior. These tools would be better to provide configuration option to preserve comments. Thank you for making me realize it.

Libraries that only transforms AST to AST

Tools that parses, transforms and generates

To recap,

RebeccaStevens commented 5 years ago

@twada Thanks for your detailed response. I was able to get this working with babel-plugin-unassert and rollup-plugin-babel. :smile: