1

I have built an angular1 application, which has thousands of methods in many many controllers, etc. During the development, I wasn't too careful when creating docs comments. I have spent the night fixing the format to be able to generate docs, again.

Now I get this error message: Don't know how to format @ngdoc: method. I have learned from other, similar questions, that this happens (or might happen), when the @methodOf attribute is missing. The Error message does not clarify where this happens.

So I'd like to find all those ngdoc comments, which are not properly formatted, using the IDEs (PhPStorm) search function and a regular expression.

This the current Expression, that I have come up with:

\/\*\*\s*\n\s\*\s@ngdoc method\n(\s\*.*(?<!methodOf)\n)*\s\*\/

From these comments, I'd like it to find the second one, only:

/** 
 * @ngdoc method
 * @name app.admin:AuditLogController#auditsWatch
 * @methodOf app.admin:AuditLogController
 */


/**
 * @ngdoc method
 * @name app.admin:AuditLogController#auditsWatch
 */


/**
 * @name app.admin:AuditLogController#auditsWatch
 */

Test it here: https://regex101.com/r/wfIiZJ/1

So basically I'd like it to find any comment, that starts off with /** and @ngdoc method, ends with */and does not have methodOf somewhere in between.

The closest I have come, is this one: \/\*\*\s\n*.*(?!methodOf)\n*.*\n\s\*\/ https://regex101.com/r/qpt8G7/1

I'll continue trying, but maybe someone has the solution and is willing to share. Any help here is greatly appreciated!

devastato
  • 71
  • 7

2 Answers2

1

Okay, so right after I posted the question and thought it through from the beginning, I got the solution.

The regex looks like this: (\/\*\*\s*\n\s*\*\s@ngdoc method\n)(((\s\*\s)(?!@methodOf).*\s*\n)*)\s*\*\/ https://regex101.com/r/Ep50XQ/1/

I divided it into several groups.

(\/\*\*\s*\n\s*\*\s@ngdoc method\n) finds

/**
 * @ngdoc method

So any comment which documents a method.

(\s\*\s)

Takes care of a new comment line, consisting of possible white spaces, one *, followed by more whitespaces.

Here lies the culprit I fell into before. I need to be aware of the current cursors position.

(?!@methodOf)

Makes sure the line does not start with @methodOf In combination (((\s\*\s)(?!@methodOf).*\s*\n)*) ensures a line within the comment, which does not start with @methodOf, can have any characters besides (.*), possibly some white spaces (\s) and ends with a line break. This can be repeated any number of times (*).

The last part

\s*\*\/

is this: */which ends the comment block and the regex.

Thank you, StackOverflow, for helping me think. Maybe it can help somebody else, though.

devastato
  • 71
  • 7
0

You can make use of a tempered greedy token here:

(?s)\/\*\*\s*\*\s*@ngdoc method\n(?:(?!\*\/|methodOf).)*\s\*\/
                                 ^^^^^^^^^^^^^^^^^^^^^^^   

See the regex demo

The pattern matches:

  • (?s) - enables . to match line breaks
  • \/\*\* - a literal /** substring
  • \s*\*\s* - a * enclosed with 0+ whitespaces
  • @ngdoc method\n - literal @ngdoc method substring followed with a newline
  • (?:(?!\*\/|methodOf).)* - the tempered greedy token matching any char, 0 or more times, that is not the starting point of a */ literal char sequence or methodOf substring
  • \s - a whitespace
  • \*\/ - a */ literal char sequence.

Note that you may consider using \R or \r?\n to match line breaks if there may be a CR before LF.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563