detective icon indicating copy to clipboard operation
detective copied to clipboard

speed up by using acorn tokenizer when possible

Open goto-bus-stop opened this issue 8 years ago • 3 comments

Add a findFast function that's exactly like detective.find, but doesn't parse the source. detective.find automatically uses findFast if possible.

we cannot do this if a custom isRequire function is provided, because that can check an arbitrarily complex callee. If a custom isRequire function is not provided we only have to check for a single token with name opts.word, so then we can look at only the token stream.

acorn's tokenizer ignores comments so things like require /* xyz */ ('abc') already work.

when opts.nodes is set, detected require calls are parsed using acorn.parseExpressionAt.

opts.fullParse can be set in order to always do a full parse. this can be useful if folks want to make sure that all syntax is correct.

I've so far found only one case where this doesn't work:

detective("(require)('abc')", { fullParse: true }) // → ['abc']
detective("(require)('abc')") // → []

may add a special case for that if there's nothing else.


e;

Before:

➜  node bench/detect.js
147

After:

➜  node bench/detect.js
66

goto-bus-stop avatar Nov 26 '17 14:11 goto-bus-stop

Fixed the merge conflict

bcomnes avatar Dec 25 '17 00:12 bcomnes

yeah not sure about side effects either, might try using it in some more projects first

goto-bus-stop avatar Jan 07 '18 22:01 goto-bus-stop

Well that got a lot more complex to support the scope stuff introduced by #79 :laughing: it looks like it works tho, will do a major bump for this to be safe (+ the scope stuff may technically be breaking as well)

goto-bus-stop avatar May 11 '18 09:05 goto-bus-stop