@@ -6,30 +6,34 @@ import semmle.javascript.Comments
66private string getALineOfCommentedOutCode ( Comment c ) {
77 result = c .getLine ( _) and
88 // line ends with ';', '{', or '}', optionally followed by a comma,
9- ( ( result .regexpMatch ( ".*[;{}],?\\s*" ) and
9+ (
10+ result .regexpMatch ( ".*[;{}],?\\s*" ) and
1011 // but it doesn't look like a JSDoc-like annotation
1112 not result .regexpMatch ( ".*@\\w+\\s*\\{.*\\}\\s*" ) and
1213 // and it does not contain three consecutive words (which is uncommon in code)
13- not result .regexpMatch ( "[^'\\\"]*\\w\\s++\\w++\\s++\\w[^'\\\"]*" ) ) or
14- // line is part of a block comment and ends with something that looks
15- // like a line comment; character before '//' must not be ':' to
16- // avoid matching URLs
17- ( not c instanceof SlashSlashComment and
18- result .regexpMatch ( "(.*[^:]|^)//.*[^/].*" ) ) or
19- // similar, but don't be fooled by '//// this kind of comment' and
20- // '//// this kind of comment ////'
21- ( c instanceof SlashSlashComment and
22- result .regexpMatch ( "/*([^/].*[^:]|[^:/])//.*[^/].*" ) and
23- // exclude externalization comments
24- not result .regexpMatch ( ".*\\$NON-NLS-\\d+\\$.*" ) ) )
14+ not result .regexpMatch ( "[^'\\\"]*\\w\\s++\\w++\\s++\\w[^'\\\"]*" )
15+ or
16+ // line is part of a block comment and ends with something that looks
17+ // like a line comment; character before '//' must not be ':' to
18+ // avoid matching URLs
19+ not c instanceof SlashSlashComment and
20+ result .regexpMatch ( "(.*[^:]|^)//.*[^/].*" )
21+ or
22+ // similar, but don't be fooled by '//// this kind of comment' and
23+ // '//// this kind of comment ////'
24+ c instanceof SlashSlashComment and
25+ result .regexpMatch ( "/*([^/].*[^:]|[^:/])//.*[^/].*" ) and
26+ // exclude externalization comments
27+ not result .regexpMatch ( ".*\\$NON-NLS-\\d+\\$.*" )
28+ )
2529}
2630
2731/**
2832 * Holds if `c` is a comment containing code examples, and hence should be
2933 * disregarded when looking for commented-out code.
3034 */
3135private predicate containsCodeExample ( Comment c ) {
32- exists ( string text | text = c .getText ( ) |
36+ exists ( string text | text = c .getText ( ) |
3337 text .matches ( "%<pre>%</pre>%" ) or
3438 text .matches ( "%<code>%</code>%" ) or
3539 text .matches ( "%@example%" ) or
@@ -43,19 +47,19 @@ private predicate containsCodeExample(Comment c) {
4347 * preceding it, if any, does not.
4448 */
4549private Comment getCommentInRun ( File f , Comment c ) {
46- exists ( int n |
50+ exists ( int n |
4751 c .onLines ( f , n , _) and
4852 countCommentedOutLines ( c ) > 0 and
49- not exists ( Comment d | d .onLines ( f , _, n - 1 ) |
50- countCommentedOutLines ( d ) > 0
51- )
53+ not exists ( Comment d | d .onLines ( f , _, n - 1 ) | countCommentedOutLines ( d ) > 0 )
5254 ) and
53- ( result = c or
54- exists ( Comment prev , int n |
55- prev = getCommentInRun ( f , c ) and
56- prev .onLines ( f , _, n ) and
57- result .onLines ( f , n + 1 , _)
58- )
55+ (
56+ result = c
57+ or
58+ exists ( Comment prev , int n |
59+ prev = getCommentInRun ( f , c ) and
60+ prev .onLines ( f , _, n ) and
61+ result .onLines ( f , n + 1 , _)
62+ )
5963 )
6064}
6165
@@ -68,9 +72,7 @@ private Comment getRelevantCommentInRun(Comment c) {
6872}
6973
7074/** Gets the number of lines in comment `c` that look like commented-out code. */
71- private int countCommentedOutLines ( Comment c ) {
72- result = count ( getALineOfCommentedOutCode ( c ) )
73- }
75+ private int countCommentedOutLines ( Comment c ) { result = count ( getALineOfCommentedOutCode ( c ) ) }
7476
7577/** Gets the number of non-blank lines in comment `c`. */
7678private int countNonBlankLines ( Comment c ) {
@@ -98,31 +100,27 @@ private int countNonBlankLinesInRun(Comment c) {
98100 * `hasLocationInfo` implementation that assigns it the entire run as its location.
99101 */
100102class CommentedOutCode extends Comment {
101- CommentedOutCode ( ) {
103+ CommentedOutCode ( ) {
102104 exists ( int codeLines , int nonBlankLines |
103105 countCommentedOutLines ( this ) > 0 and
104106 not exists ( Comment prev | this = getCommentInRun ( _, prev ) and this != prev ) and
105107 nonBlankLines = countNonBlankLinesInRun ( this ) and
106108 codeLines = countCommentedOutLinesInRun ( this ) and
107109 nonBlankLines > 0 and
108- 2 * codeLines > nonBlankLines
110+ 2 * codeLines > nonBlankLines
109111 )
110112 }
111113
112114 /**
113115 * Gets the number of lines in this run of comments
114116 * that look like they contain commented-out code.
115117 */
116- int getNumCodeLines ( ) {
117- result = countCommentedOutLinesInRun ( this )
118- }
118+ int getNumCodeLines ( ) { result = countCommentedOutLinesInRun ( this ) }
119119
120120 /**
121121 * Gets the number of non-blank lines in this run of comments.
122122 */
123- int getNumNonBlankLines ( ) {
124- result = countNonBlankLinesInRun ( this )
125- }
123+ int getNumNonBlankLines ( ) { result = countNonBlankLinesInRun ( this ) }
126124
127125 /**
128126 * Holds if this element is at the specified location.
@@ -131,18 +129,20 @@ class CommentedOutCode extends Comment {
131129 * For more information, see
132130 * [LGTM locations](https://lgtm.com/help/ql/locations).
133131 */
134- predicate hasLocationInfo ( string filepath , int startline , int startcolumn , int endline , int endcolumn ) {
135- exists ( Location loc , File f | loc = getLocation ( ) and f = loc .getFile ( ) |
132+ predicate hasLocationInfo (
133+ string filepath , int startline , int startcolumn , int endline , int endcolumn
134+ ) {
135+ exists ( Location loc , File f | loc = getLocation ( ) and f = loc .getFile ( ) |
136136 filepath = f .getAbsolutePath ( ) and
137137 startline = loc .getStartLine ( ) and
138138 startcolumn = loc .getStartColumn ( ) and
139139 exists ( Location last |
140140 last = getCommentInRun ( f , this ) .getLocation ( ) and
141- last .getEndLine ( ) = max ( getCommentInRun ( f , this ) .getLocation ( ) .getEndLine ( ) ) |
141+ last .getEndLine ( ) = max ( getCommentInRun ( f , this ) .getLocation ( ) .getEndLine ( ) )
142+ |
142143 endline = last .getEndLine ( ) and
143144 endcolumn = last .getEndColumn ( )
144145 )
145146 )
146147 }
147148}
148-
0 commit comments