Skip to content

Commit e46a9da

Browse files
committed
JS: Count lines of code correctly
1 parent 9c65318 commit e46a9da

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

javascript/extractor/src/com/semmle/js/extractor/FileExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ private Integer extractContents(
511511
new TextualExtractor(
512512
trapwriter, locationManager, source, config.getExtractLines(), metrics, extractedFile);
513513
LoCInfo loc = extractor.extract(textualExtractor);
514-
int numLines = textualExtractor.getNumLines();
514+
int numLines = textualExtractor.isSnippet() ? 0 : textualExtractor.getNumLines();
515515
int linesOfCode = loc.getLinesOfCode(), linesOfComments = loc.getLinesOfComments();
516516
trapwriter.addTuple("numlines", fileLabel, numLines, linesOfCode, linesOfComments);
517517
trapwriter.addTuple("filetype", fileLabel, fileType.toString());

javascript/extractor/src/com/semmle/js/extractor/TextualExtractor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public File getExtractedFile() {
5252
return extractedFile;
5353
}
5454

55+
/**
56+
* Returns true if the extracted file and the source location files are different.
57+
*/
58+
public boolean isSnippet() {
59+
return !extractedFile.equals(locationManager.getSourceFile());
60+
}
61+
5562
public TrapWriter getTrapwriter() {
5663
return trapwriter;
5764
}

javascript/ql/src/semmle/javascript/Files.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ class File extends Container, @file {
206206
override string getAbsolutePath() { files(this, result, _, _, _) }
207207

208208
/** Gets the number of lines in this file. */
209-
int getNumberOfLines() { numlines(this, result, _, _) }
209+
int getNumberOfLines() { result = sum(int loc | numlines(this, loc, _, _) | loc) }
210210

211211
/** Gets the number of lines containing code in this file. */
212-
int getNumberOfLinesOfCode() { numlines(this, _, result, _) }
212+
int getNumberOfLinesOfCode() { result = sum(int loc | numlines(this, _, loc, _) | loc) }
213213

214214
/** Gets the number of lines containing comments in this file. */
215-
int getNumberOfLinesOfComments() { numlines(this, _, _, result) }
215+
int getNumberOfLinesOfComments() { result = sum(int loc | numlines(this, _, _, loc) | loc) }
216216

217217
/** Gets a toplevel piece of JavaScript code in this file. */
218218
TopLevel getATopLevel() { result.getFile() = this }

0 commit comments

Comments
 (0)