Merge branch 'main' into js/hoist-in-block

This commit is contained in:
Asger F
2025-02-04 14:01:57 +01:00
committed by GitHub
41 changed files with 1228 additions and 318 deletions

View File

@@ -56,8 +56,10 @@ public class JSExtractor {
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').
@@ -74,7 +76,7 @@ public class JSExtractor {
if (wrongGuess) {
sourceType = SourceType.SCRIPT;
parserRes =
JSParser.parse(config, sourceType, source, textualExtractor.getMetrics());
JSParser.parse(config, sourceType, extension, source, textualExtractor.getMetrics());
}
}

View File

@@ -68,9 +68,9 @@ public class JSParser {
}
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;
}

View File

@@ -14,9 +14,14 @@ import com.semmle.js.extractor.ExtractorConfig.ECMAVersion;
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<>();
@@ -32,7 +37,11 @@ public class JcornWrapper {
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.