try to parse JS files without using the supported extensions

This commit is contained in:
erik-krogh
2022-09-19 12:20:20 +02:00
parent a16233aa7d
commit ccae0933c7
6 changed files with 85 additions and 72 deletions

View File

@@ -40,7 +40,7 @@ public class Options {
}
private boolean allowHashBang, allowReturnOutsideFunction, allowImportExportEverywhere, allowGeneratedCodeExprs;
private boolean preserveParens, mozExtensions, jscript, esnext, v8Extensions, e4x;
private boolean preserveParens, mozExtensions, jscript, esnext, v8Extensions, e4x, allowFlowTypes;
private int ecmaVersion;
private AllowReserved allowReserved;
private String sourceType;
@@ -70,6 +70,7 @@ public class Options {
this.v8Extensions = false;
this.e4x = false;
this.onRecoverableError = null;
this.allowFlowTypes = false;
}
public Options(Options that) {
@@ -92,6 +93,7 @@ public class Options {
this.onComment = that.onComment;
this.program = that.program;
this.onRecoverableError = that.onRecoverableError;
this.allowFlowTypes = that.allowFlowTypes;
}
public boolean allowHashBang() {
@@ -130,6 +132,10 @@ public class Options {
return v8Extensions;
}
public boolean allowFlowTypes() {
return allowFlowTypes;
}
public boolean e4x() {
return e4x;
}
@@ -202,6 +208,10 @@ public class Options {
this.v8Extensions = v8Extensions;
}
public void allowFlowTypes(boolean allowFlowTypes) {
this.allowFlowTypes = allowFlowTypes;
}
public void e4x(boolean e4x) {
this.e4x = e4x;
}

View File

@@ -830,7 +830,7 @@ public class FlowParser extends ESNextParser {
/** Should Flow syntax be allowed? */
private boolean flow() {
return options.esnext();
return options.allowFlowTypes();
}
@Override

View File

@@ -41,7 +41,7 @@ public class Main {
* A version identifier that should be updated every time the extractor changes in such a way that
* it may produce different tuples for the same file under the same {@link ExtractorConfig}.
*/
public static final String EXTRACTOR_VERSION = "2022-08-25";
public static final String EXTRACTOR_VERSION = "2022-09-19";
public static final Pattern NEWLINE = Pattern.compile("\n");

View File

@@ -28,20 +28,29 @@ public class JcornWrapper {
.onToken(tokens)
.preserveParens(true)
.allowReturnOutsideFunction(true);
if (config.isMozExtensions()) options.mozExtensions(true);
if (config.isJscript()) options.jscript(true);
if (config.isJsx()) options = new JSXOptions(options);
if (config.isEsnext()) options.esnext(true);
if (config.isV8Extensions()) options.v8Extensions(true);
if (config.isE4X()) options.e4x(true);
Program program = null;
List<ParseError> errors = new ArrayList<>();
try {
if (config.isTolerateParseErrors())
options.onRecoverableError((err) -> errors.add(mkParseError(err)));
program = sourceType.createParser(options, source, 0).parse();
try {
// First try to parse as a regular JavaScript program.
program = sourceType.createParser(options, source, 0).parse();
} catch (SyntaxError e) {
// If that fails, try to enable all the extensions that we support.
if (config.isTolerateParseErrors())
options.onRecoverableError((err) -> errors.add(mkParseError(err)));
comments.clear();
tokens.clear();
if (config.isMozExtensions()) options.mozExtensions(true);
if (config.isJscript()) options.jscript(true);
if (config.isJsx()) options = new JSXOptions(options);
if (config.isV8Extensions()) options.v8Extensions(true);
if (config.isE4X()) options.e4x(true);
if (config.isEsnext()) options.allowFlowTypes(true); // allow the flow-parser to parse types.
program = sourceType.createParser(options, source, 0).parse();
}
} catch (SyntaxError e) {
errors.add(mkParseError(e));
}