danielstjules / jsinspect

Detect copy-pasted and structurally similar code
MIT License
3.56k stars 128 forks source link

Alternative parser #43

Closed gcazaciuc closed 7 years ago

gcazaciuc commented 7 years ago

I plan in creating a Vscode extension that provides in editor feedback for copy pasted code. The only problem i have is that i need to process code with Flowtype type annotations in it. I have been using flowparser package with great success previously since it produces output compatible with most of the other parsers and also works fine with tools such as Recast, Jscodeshift etc. Wondering if there is an easy way to make the parser configurable via options ? thanks.

danielstjules commented 7 years ago

Is the AST produced by the parser identical to acorn?

gcazaciuc commented 7 years ago

Both produce output conforming to estree spec so i assume yes. Here is a link to the parser i was mentioning: https://www.npmjs.com/package/flow-parser

danielstjules commented 7 years ago

The Inspector's constructor could perhaps be updated to accepted an optional parse fn, to be used instead of require('acorn/dist/acorn_loose').parse_dammit;

danielstjules commented 7 years ago

I think the option should accept a plain parsing function to simplify the interface/implementation. E.g.

var parser = require('flow-parser');

var parse = function(str) {
  return parser.parse(str, {
    esproposal_decorators: true,
    types: false,
    // etc
  });
};

new Inspector({parse, otherOptions});
danielstjules commented 7 years ago

Flow support was added in https://github.com/danielstjules/jsinspect/commit/f04c1429ecf8b240ef7825032148f42482ae3453 :) Thanks!