Skip to content

Commit 5bbbd26

Browse files
authored
Merge pull request #1372 from xiemaisi/js/fail-if-no-code
Approved by esben-semmle
2 parents b0a7f20 + 38a38ab commit 5bbbd26

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

change-notes/1.21/extractor-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
## Changes to code extraction
66

77
* ECMAScript 2019 support is now enabled by default.
8-
8+
* On LGTM, JavaScript extraction for projects that do not contain any JavaScript or TypeScript code will now fail, even if the project contains other file types (such as HTML or YAML) recognized by the JavaScript extractor.
99
* YAML files are now extracted by default on LGTM. You can specify exclusion filters in your `lgtm.yml` file to override this behavior.

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

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
package com.semmle.js.extractor;
22

3-
import com.semmle.js.extractor.ExtractorConfig.SourceType;
4-
import com.semmle.js.extractor.FileExtractor.FileType;
5-
import com.semmle.js.extractor.trapcache.DefaultTrapCache;
6-
import com.semmle.js.extractor.trapcache.DummyTrapCache;
7-
import com.semmle.js.extractor.trapcache.ITrapCache;
8-
import com.semmle.js.parser.ParsedProject;
9-
import com.semmle.js.parser.TypeScriptParser;
10-
import com.semmle.ts.extractor.TypeExtractor;
11-
import com.semmle.ts.extractor.TypeTable;
12-
import com.semmle.util.data.StringUtil;
13-
import com.semmle.util.exception.CatastrophicError;
14-
import com.semmle.util.exception.Exceptions;
15-
import com.semmle.util.exception.ResourceError;
16-
import com.semmle.util.exception.UserError;
17-
import com.semmle.util.extraction.ExtractorOutputConfig;
18-
import com.semmle.util.files.FileUtil;
19-
import com.semmle.util.io.csv.CSVReader;
20-
import com.semmle.util.language.LegacyLanguage;
21-
import com.semmle.util.process.Env;
22-
import com.semmle.util.projectstructure.ProjectLayout;
23-
import com.semmle.util.trap.TrapWriter;
243
import java.io.File;
254
import java.io.IOException;
265
import java.io.Reader;
@@ -48,6 +27,28 @@
4827
import java.util.concurrent.TimeUnit;
4928
import java.util.stream.Stream;
5029

30+
import com.semmle.js.extractor.ExtractorConfig.SourceType;
31+
import com.semmle.js.extractor.FileExtractor.FileType;
32+
import com.semmle.js.extractor.trapcache.DefaultTrapCache;
33+
import com.semmle.js.extractor.trapcache.DummyTrapCache;
34+
import com.semmle.js.extractor.trapcache.ITrapCache;
35+
import com.semmle.js.parser.ParsedProject;
36+
import com.semmle.js.parser.TypeScriptParser;
37+
import com.semmle.ts.extractor.TypeExtractor;
38+
import com.semmle.ts.extractor.TypeTable;
39+
import com.semmle.util.data.StringUtil;
40+
import com.semmle.util.exception.CatastrophicError;
41+
import com.semmle.util.exception.Exceptions;
42+
import com.semmle.util.exception.ResourceError;
43+
import com.semmle.util.exception.UserError;
44+
import com.semmle.util.extraction.ExtractorOutputConfig;
45+
import com.semmle.util.files.FileUtil;
46+
import com.semmle.util.io.csv.CSVReader;
47+
import com.semmle.util.language.LegacyLanguage;
48+
import com.semmle.util.process.Env;
49+
import com.semmle.util.projectstructure.ProjectLayout;
50+
import com.semmle.util.trap.TrapWriter;
51+
5152
/**
5253
* An alternative entry point to the JavaScript extractor.
5354
*
@@ -194,6 +195,7 @@ public class AutoBuild {
194195
private final TypeScriptMode typeScriptMode;
195196
private final String defaultEncoding;
196197
private ExecutorService threadPool;
198+
private volatile boolean seenCode = false;
197199

198200
public AutoBuild() {
199201
this.LGTM_SRC = toRealPath(getPathFromEnvVar("LGTM_SRC"));
@@ -425,7 +427,7 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) {
425427
}
426428

427429
/** Perform extraction. */
428-
public void run() throws IOException {
430+
public int run() throws IOException {
429431
startThreadPool();
430432
try {
431433
extractSource();
@@ -434,6 +436,11 @@ public void run() throws IOException {
434436
} finally {
435437
shutdownThreadPool();
436438
}
439+
if (!seenCode) {
440+
warn("No JavaScript or TypeScript code found.");
441+
return -1;
442+
}
443+
return 0;
437444
}
438445

439446
private void startThreadPool() {
@@ -736,7 +743,9 @@ private void doExtract(FileExtractor extractor, Path file, ExtractorState state)
736743

737744
try {
738745
long start = logBeginProcess("Extracting " + file);
739-
extractor.extract(f, state);
746+
Integer loc = extractor.extract(f, state);
747+
if (!extractor.getConfig().isExterns() && (loc == null || loc != 0))
748+
seenCode = true;
740749
logEndProcess(start, "Done extracting " + file);
741750
} catch (Throwable t) {
742751
System.err.println("Exception while extracting " + file + ".");
@@ -787,7 +796,7 @@ protected void extractXml() throws IOException {
787796

788797
public static void main(String[] args) {
789798
try {
790-
new AutoBuild().run();
799+
System.exit(new AutoBuild().run());
791800
} catch (IOException | UserError | CatastrophicError e) {
792801
System.err.println(e.toString());
793802
System.exit(1);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ public boolean supports(File f) {
384384
return config.hasFileType() || FileType.forFile(f, config) != null;
385385
}
386386

387-
public void extract(File f, ExtractorState state) throws IOException {
387+
/**
388+
* @return the number of lines of code extracted, or {@code null} if the file was cached
389+
*/
390+
public Integer extract(File f, ExtractorState state) throws IOException {
388391
// populate source archive
389392
String source = new WholeIO(config.getDefaultEncoding()).strictread(f);
390393
outputConfig.getSourceArchive().add(f, source);
@@ -396,7 +399,7 @@ public void extract(File f, ExtractorState state) throws IOException {
396399
locationManager.emitFileLocation(fileLabel, 0, 0, 0, 0);
397400

398401
// now extract the contents
399-
extractContents(f, fileLabel, source, locationManager, state);
402+
return extractContents(f, fileLabel, source, locationManager, state);
400403
}
401404

402405
/**
@@ -420,7 +423,7 @@ public void extract(File f, ExtractorState state) throws IOException {
420423
* <p>Also note that we support extraction with TRAP writer factories that are not file-backed;
421424
* obviously, no caching is done in that scenario.
422425
*/
423-
private void extractContents(
426+
private Integer extractContents(
424427
File f, Label fileLabel, String source, LocationManager locationManager, ExtractorState state)
425428
throws IOException {
426429
TrapWriter trapwriter = locationManager.getTrapWriter();
@@ -440,7 +443,7 @@ private void extractContents(
440443

441444
if (cacheFile.exists()) {
442445
FileUtil.append(cacheFile, resultFile);
443-
return;
446+
return null;
444447
}
445448

446449
// not in the cache yet, so use a caching TRAP writer to
@@ -463,6 +466,7 @@ private void extractContents(
463466
trapwriter.addTuple("numlines", fileLabel, numLines, linesOfCode, linesOfComments);
464467
trapwriter.addTuple("filetype", fileLabel, fileType.toString());
465468
successful = true;
469+
return linesOfCode;
466470
} finally {
467471
if (!successful && trapwriter instanceof CachingTrapWriter)
468472
((CachingTrapWriter) trapwriter).discard();

0 commit comments

Comments
 (0)