1919 */
2020package org .sonar .javascript .checks ;
2121
22+ import com .google .common .collect .ImmutableList ;
2223import org .sonar .api .server .rule .RulesDefinition ;
2324import org .sonar .check .Priority ;
2425import org .sonar .check .Rule ;
2526import org .sonar .check .RuleProperty ;
27+ import org .sonar .javascript .checks .utils .SubscriptionBaseVisitor ;
28+ import org .sonar .javascript .model .internal .lexical .InternalSyntaxToken ;
29+ import org .sonar .plugins .javascript .api .tree .Tree ;
30+ import org .sonar .plugins .javascript .api .tree .lexical .SyntaxToken ;
2631import org .sonar .squidbridge .annotations .ActivatedByDefault ;
2732import org .sonar .squidbridge .annotations .SqaleConstantRemediation ;
2833import org .sonar .squidbridge .annotations .SqaleSubCharacteristic ;
29- import org .sonar .squidbridge .checks .SquidCheck ;
30- import org .sonar .sslr .parser .LexerlessGrammar ;
3134
32- import com .sonar .sslr .api .AstNode ;
33- import com .sonar .sslr .api .GenericTokenType ;
35+ import java .util .List ;
3436
3537@ Rule (
3638 key = "S104" ,
4042@ ActivatedByDefault
4143@ SqaleSubCharacteristic (RulesDefinition .SubCharacteristics .READABILITY )
4244@ SqaleConstantRemediation ("1h" )
43- public class TooManyLinesInFileCheck extends SquidCheck < LexerlessGrammar > {
45+ public class TooManyLinesInFileCheck extends SubscriptionBaseVisitor {
4446
4547 private static final int DEFAULT = 1000 ;
4648
@@ -51,18 +53,24 @@ public class TooManyLinesInFileCheck extends SquidCheck<LexerlessGrammar> {
5153 public int maximum = DEFAULT ;
5254
5355 @ Override
54- public void init () {
55- subscribeTo (GenericTokenType .EOF );
56- }
56+ public void visitNode (Tree tree ) {
57+ if (!((InternalSyntaxToken ) tree ).isEOF ()) {
58+ return ;
59+ }
5760
58- @ Override
59- public void visitNode (AstNode astNode ) {
60- int lines = astNode .getTokenLine ();
61+ SyntaxToken token = (SyntaxToken ) tree ;
62+ int lines = token .line ();
6163
6264 if (lines > maximum ) {
63- getContext ().createFileViolation (this , "File \" {0} \" has {1} lines, which is greater than {2} authorized. Split it into smaller files." ,
64- getContext ().getFile ().getName (), lines , maximum );
65+ getContext ().addFileIssue (this , String . format ( "File \" %s \" has %d lines, which is greater than %d authorized. Split it into smaller files." ,
66+ getContext ().getFile ().getName (), lines , maximum )) ;
6567 }
6668 }
69+
70+ @ Override
71+ public List <Tree .Kind > nodesToVisit () {
72+ return ImmutableList .of (Tree .Kind .TOKEN );
73+ }
74+
6775}
6876
0 commit comments