Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all function signatures with more than 2 arguments in javascript

I need to find all function signatures accepting more than X arguments (say 2). I tried something like function\s*\((\w*,){3,10} (which would catch all signature with 3-10 args, but it did not work. Variations on it are yielding unexpected results. I guess I'm just not that good at regex, but any help is appreciated.

update: I should point out that I am writing a sort of code inspection tool. Among the many things, I want to spot functions that accept more than 2 arguments (as I promote the usage of functions with few arguments, and 1 argument in case of constructors). So I cannot call arguments.length etc.

like image 547
Joe Minichino Avatar asked Apr 17 '26 06:04

Joe Minichino


2 Answers

Just think "easy":

  • A method typically has (...): \(\)
  • A method with 3 parameters has 2 , inside the brackets: \(,{2,2}\)
  • each , NEEDS to be preceeded AND followed by strings: \((?:\w+,\w+){2,2}\)
  • no double matches occur, so does not work - let's make the leading string mandatory, the following optional, but finally it needs to stop with a string: \((?:\w+,\w*){2,2}\w+\)
  • usually a method declaration starts with function name: function\s+\w+\s*\((?:\w*,\w*){2,2}\)
  • finally, there could be whitespaces arround the paremeters: function\s+\w+\s*\((?:\s*\w+\s*,\s*\w*\s*){2,2}\w+\s*\)

There you go. This should cover all "common" method declarations, except nameless lambda-expressions:

function\s+\w+\s*\((?:\s*\w+\s*,\s*\w*\s*){2,2}\w+\s*\)

Regular expression visualization

Debuggex Demo

Matching two to two commas will find signatures with 3 parameters. Matchint two to five commas will find signatures with 3 upto 6 parameters.

like image 96
dognose Avatar answered Apr 18 '26 20:04

dognose


First of all, JavaScript is not a regular language, as a result, one cannot use a regex to fully grasp the language, and thus there is a possibility that you will either accept false positives, or false negatives.

A regex that probably comes close is:

function(?:\s+\w+)*\s*\(([^),]*)(\s*,\s*[^),]*){2,}\)

The regex works as follows:

  • function searches for the function keyword.
  • next there is an optional group \s+\w+ this group is used to identify with the name of the function: it is possible to define an anonymous function with no name, so the group must be optional.
  • Next \s*\( there is an arbitrary number of space and a bracket to open the parameter list;
  • Now between the brackets, we start looking for the parameters. To cover (most) cases, we will define a parameter as [^,)]* (a sequence of characters not containing a comma nor the closed bracket).
  • Now for the next parameters, we need to skip a comma, this is enforced by the \s*,\s* pattern (\s* is actually unnecessary). Next again a group for a parameter name and of course we need to skip at least two commas.
  • Finally, an (optional) closing bracket.
like image 28
Willem Van Onsem Avatar answered Apr 18 '26 19:04

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!