Skip to content

Commit 8a4ad9a

Browse files
committed
JS: Skip minified file if avg line length > 200
1 parent 6ddb9c7 commit 8a4ad9a

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,15 @@ private ParseResultInfo extractContents(
549549
new TextualExtractor(
550550
trapwriter, locationManager, source, config.getExtractLines(), metrics, extractedFile);
551551
ParseResultInfo loc = extractor.extract(textualExtractor);
552-
int numLines = textualExtractor.isSnippet() ? 0 : textualExtractor.getNumLines();
553-
int linesOfCode = loc.getLinesOfCode(), linesOfComments = loc.getLinesOfComments();
554-
trapwriter.addTuple("numlines", fileLabel, numLines, linesOfCode, linesOfComments);
555-
trapwriter.addTuple("filetype", fileLabel, fileType.toString());
552+
if (loc.getSkipReason() != null) {
553+
System.err.println("Skipping file " + extractedFile + ": " + loc.getSkipReason());
554+
System.err.flush();
555+
} else{
556+
int numLines = textualExtractor.isSnippet() ? 0 : textualExtractor.getNumLines();
557+
int linesOfCode = loc.getLinesOfCode(), linesOfComments = loc.getLinesOfComments();
558+
trapwriter.addTuple("numlines", fileLabel, numLines, linesOfCode, linesOfComments);
559+
trapwriter.addTuple("filetype", fileLabel, fileType.toString());
560+
}
556561
metrics.stopPhase(ExtractionPhase.FileExtractor_extractContents);
557562
metrics.writeTimingsToTrap(trapwriter);
558563
successful = true;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@
1010
public class ParseResultInfo {
1111
private int linesOfCode, linesOfComments;
1212
private List<ParseError> parseErrors;
13+
private String skipReason;
1314

1415
public ParseResultInfo(int linesOfCode, int linesOfComments, List<ParseError> parseErrors) {
1516
this.linesOfCode = linesOfCode;
1617
this.linesOfComments = linesOfComments;
1718
this.parseErrors = new ArrayList<>(parseErrors);
1819
}
1920

21+
private ParseResultInfo() {
22+
this.linesOfCode = 0;
23+
this.linesOfComments = 0;
24+
this.parseErrors = new ArrayList<>();
25+
this.skipReason = null;
26+
}
27+
28+
public static final ParseResultInfo skipped(String reason) {
29+
ParseResultInfo info = new ParseResultInfo();
30+
info.skipReason = reason;
31+
return info;
32+
}
33+
2034
public void add(ParseResultInfo that) {
2135
this.linesOfCode += that.linesOfCode;
2236
this.linesOfComments += that.linesOfComments;
@@ -41,4 +55,11 @@ public int getLinesOfComments() {
4155
public List<ParseError> getParseErrors() {
4256
return parseErrors;
4357
}
58+
59+
/**
60+
* If extraction of this file was skipped, gets the reason for skipping it.
61+
*/
62+
public String getSkipReason() {
63+
return skipReason;
64+
}
4465
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,34 @@ private boolean isAlwaysCommonJSModule(String extension, String packageType) {
3838
return extension.equals(".cjs") || (extension.equals(".js") && "commonjs".equals(packageType));
3939
}
4040

41+
private boolean isMinified(String source) {
42+
// If the average line length is over 200 characters, consider the file minified.
43+
int numberOfLineBreaks = 0;
44+
for (int i = 0; i < source.length(); i++) {
45+
char c = source.charAt(i);
46+
if (c == '\n') {
47+
numberOfLineBreaks++;
48+
} else if (c == '\r') {
49+
numberOfLineBreaks++;
50+
if (i + 1 < source.length() && source.charAt(i + 1) == '\n') {
51+
i++; // skip the next \n in case of \r\n
52+
}
53+
}
54+
}
55+
int averageLineLength =
56+
numberOfLineBreaks == 0 ? source.length() : source.length() / numberOfLineBreaks;
57+
return averageLineLength > 200;
58+
}
59+
4160
@Override
4261
public ParseResultInfo extract(TextualExtractor textualExtractor) {
4362
LocationManager locationManager = textualExtractor.getLocationManager();
4463
String source = textualExtractor.getSource();
64+
65+
if (isMinified(source)) {
66+
return ParseResultInfo.skipped("File appears to be minified.");
67+
}
68+
4569
String shebangLine = null, shebangLineTerm = null;
4670

4771
if (source.startsWith("#!")) {

0 commit comments

Comments
 (0)