-2

I'm writing a simple nodejs function to loop over a code base, read all files, find javascript functions and report if that function has documentation written for it.

I've written a function that detects all the functions (and their context), I've also written a separate function to detect the documentation blocks:

(\/\*\*([\S\s]*?)(?:\s)?\*\/)

This should match any doc format in the below format:

/**
 * @description - some description
 */

Basically I can't figure out how to join them both, i want to report functions that do and don't have docs attached to them:

Been testing in regex101: https://regex101.com/r/WGqfm8/9/

Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95

1 Answers1

-1

First thing, your comment matching regex can be reduced to this:

/\/\*\*(\S|\s)*?\*\//g

but this may be better if you're search for more than one:

/\/\*\*(?:(\S|\s)(?!\*\/))*?(\S|\s)\*\//g

Second, you may have to do this in two parts:

Step 1: Use {string}.match() to create an array of all your matched functions.

Step 2: Use {string}.split() to remove all matched functions, leaving the rest of the code. You can then also use .map() to break it down.

Something like this:

var functions = code.match(funcRegex);
var restOfCode = code.split(funcRegex).map(c=>c.match(/\/\*\*(?:(\S|\s)(?!\*\/))*?(\S|\s)\*\//g));

Note: Each item of restOfCode will be an array of comments found, and the last one should be the one just before the function. For example, for function body functions[0], you would check if the last item in the array restOfCode[0][restOfCode[0].length-1] starts and ends with '/**' and '*/'.

Warning: Most regexes cannot protected against some cases, like this, or similar:

var b=`
function(){ }
`;

No regex will be foolproof for what you want to do, but can help break code up into parts to parse properly. For simple cases though it may be fine.

BTW: You did ask to "join them both" but did not clearly give BOTH regexes here in your question. Yes, I know the link as them already combined, but it should be clear in your question.

Since parsing function bodies with a regex is a fool's errand, so to speak, here is one that at least matches comments with the start of what appears to be a function start:

/^(\/\*\*(?:(?:\S|\s)(?!\*\/))*?(?:\S|\s)?\*\/\s*)?((?:(?:const|var|let).*{.*})?[^}\n]*(?:\(.*\).*{))/gm

https://regex101.com/r/WGqfm8/15

Group 1 returns the doc comment, if exists, and group 2 the function start line.

James Wilkins
  • 6,836
  • 3
  • 48
  • 73
  • Thanks for this, should get me most of the way there, but your expression doesn't match all the functions in my demo mate – Shannon Hochkins Oct 09 '19 at 21:12
  • That should be in your question. You should not be linking to extra requirements outside your question. I’ll take another look shortly. – James Wilkins Oct 16 '19 at 04:24
  • "I've written a function that detects all the functions " - that's in my question, I would assume if someone else is working on something, that they should retain the functionality they already have said they have, but are stuck on one specific piece, no need to down vote me i didn't down vote you, someone else did. – Shannon Hochkins Oct 16 '19 at 04:42
  • What I meant was that I'm sure I could think of many ways to lay out a function (what about `new Function(...)`?) . I don't know, since the requirements are not that clear. You should have clear requirements in your question, instead of linking to a snippet of code with a list of possible function formats where, at any point in the future, the regex will fail because you didn't foresee some scenario. All scenarios should be in your question, rather that linking to external code (which may not exist in the future if the site ever went down). That's most likely why someone else voted it down. – James Wilkins Oct 16 '19 at 22:14
  • BTW: A regex should never be used for this in the first place, but I'm trying my best to see how far one can get. – James Wilkins Oct 16 '19 at 22:17
  • Why shouldn't a regex be used for this? Elegance aside... I was just stuck on matching the comment part, I've written out most of the ways we use functions here, it's what it's built for and doing what it was supposed to do, but yes i am 100% certain theres many cases I've not covered, but for now it will do! – Shannon Hochkins Oct 16 '19 at 22:22
  • It all depends on your goal. If the goal is to handle all situations, you need to properly parse the JavaScript. Any regex you send me I can break, because a Regex cannot properly extract every possible scenario. Same goes for HTML (https://stackoverflow.com/a/1732454/1236397). If you have a *limited* scenario, then it should be ok. I've updated the regex to catch the destructured assignments as well. – James Wilkins Oct 16 '19 at 22:29