goatslacker / get-parameter-names

Retrieves the argument names of a function
46 stars 13 forks source link

Fails when a default is on last parameter of function #13

Open mattdawson opened 7 years ago

mattdawson commented 7 years ago

function (a,b,c=1) { .. code here ... }

returns a,b, ...first line of code...

This is because the following regexp doesn't end on a bracket.... const DEFAULT_PARAMS = /=[^,]+/mg;

Changing it to: /=[^,)]+/ fixes this. (Note, this won't capture funky defaults which contain commas, or brackets)

Also maybe consider changing: var code = fn.toString() to const code = fn.prototype.constructor.toString() This will prevent the whole function being converted to string.

Unless this is for parsing functions that are already strings?

CaptEmulation commented 7 years ago

This is fixed in @captemulation/get-parameter-names which is a re-write of this library. See https://github.com/CaptEmulation/get-parameter-names