Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public Pair<Label, ParseResultInfo> extract(

SourceType sourceType = establishSourceType(source, true);

String extension = textualExtractor.getLocationManager().getSourceFileExtension();

JSParser.Result parserRes =
JSParser.parse(config, sourceType, source, textualExtractor.getMetrics());
JSParser.parse(config, sourceType, extension, source, textualExtractor.getMetrics());

// Check if we guessed wrong with the regex in `establishSourceType`, (which could
// happen due to a block-comment line starting with ' import').
Expand All @@ -74,7 +76,7 @@ public Pair<Label, ParseResultInfo> extract(
if (wrongGuess) {
sourceType = SourceType.SCRIPT;
parserRes =
JSParser.parse(config, sourceType, source, textualExtractor.getMetrics());
JSParser.parse(config, sourceType, extension, source, textualExtractor.getMetrics());
}
}

Expand Down
4 changes: 2 additions & 2 deletions javascript/extractor/src/com/semmle/js/parser/JSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public List<ParseError> getErrors() {
}

public static Result parse(
ExtractorConfig config, SourceType sourceType, String source, ExtractionMetrics metrics) {
ExtractorConfig config, SourceType sourceType, String extension, String source, ExtractionMetrics metrics) {
metrics.startPhase(ExtractionPhase.JSParser_parse);
Result result = JcornWrapper.parse(config, sourceType, source);
Result result = JcornWrapper.parse(config, sourceType, extension, source);
metrics.stopPhase(ExtractionPhase.JSParser_parse);
return result;
}
Expand Down
13 changes: 11 additions & 2 deletions javascript/extractor/src/com/semmle/js/parser/JcornWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
import com.semmle.js.extractor.ExtractorConfig.SourceType;

public class JcornWrapper {
public static boolean alwaysParseWithJsx(String extension) {
// Note that .tsx is not relevant here since this is specifically for the JS parser.
return extension.equals(".jsx");
}

/** Parse source code as a program. */
public static JSParser.Result parse(
ExtractorConfig config, SourceType sourceType, String source) {
ExtractorConfig config, SourceType sourceType, String extension, String source) {
ECMAVersion ecmaVersion = config.getEcmaVersion();
List<Comment> comments = new ArrayList<>();
List<Token> tokens = new ArrayList<>();
Expand All @@ -32,7 +37,11 @@ public static JSParser.Result parse(

Program program = null;
List<ParseError> errors = new ArrayList<>();


// If the file extension implies JSX syntax, use that in the first parsing attempt.
// This enables us to parse JSX files that the Flow parser cannot handle due to ambiguous syntax.
if (alwaysParseWithJsx(extension)) options = new JSXOptions(options);

try {
try {
// First try to parse as a regular JavaScript program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ES2015DetectorTests {
private static final ExtractorConfig CONFIG = new ExtractorConfig(true);

private void isES2015(String src, boolean expected) {
Result res = JSParser.parse(CONFIG, SourceType.MODULE, src, new ExtractionMetrics());
Result res = JSParser.parse(CONFIG, SourceType.MODULE, ".jsx", src, new ExtractionMetrics());
Node ast = res.getAST();
Assert.assertNotNull(ast);
Assert.assertTrue(ES2015Detector.looksLikeES2015(ast) == expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class NodeJSDetectorTests {
private static final ExtractorConfig CONFIG = new ExtractorConfig(true);

private void isNodeJS(String src, boolean expected) {
Result res = JSParser.parse(CONFIG, SourceType.SCRIPT, src, new ExtractionMetrics());
Result res = JSParser.parse(CONFIG, SourceType.SCRIPT, ".jsx", src, new ExtractionMetrics());
Node ast = res.getAST();
Assert.assertNotNull(ast);
Assert.assertTrue(NodeJSDetector.looksLikeNodeJS(ast) == expected);
Expand Down
4 changes: 4 additions & 0 deletions javascript/extractor/tests/jsx/input/repro1.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function foo() {
let x = <div></div>;
return true ? (null, null) : e => { };
}
Loading