Whitelist recovered babel parse errors (#10495)
This commit is contained in:
1 parent
1ce6b8dc3d
commit
3995c2a1a1
53 files changed
+1051
-844
No files matched your search
@@ -144,31 +144,79 @@ const parseTypeScript = createParse(
|
||||
);
|
||||
const parseExpression = createParse("parseExpression", appendPlugins(["jsx"]));
|
||||
|
||||
const messagesShouldThrow = new Set([
|
||||
// TSErrors.UnexpectedTypeAnnotation
|
||||
// https://github.com/babel/babel/blob/008fe25ae22e78288fbc637d41069bb4a1040987/packages/babel-parser/src/plugins/typescript/index.js#L95
|
||||
"Did not expect a type annotation here.",
|
||||
// ErrorMessages.ModuleAttributeDifferentFromType
|
||||
// https://github.com/babel/babel/blob/a023b6456cac4505096028f91c5b78829955bfc2/packages/babel-parser/src/parser/error-message.js#L92
|
||||
"The only accepted module attribute is `type`",
|
||||
// FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction
|
||||
// https://github.com/babel/babel/blob/a023b6456cac4505096028f91c5b78829955bfc2/packages/babel-parser/src/plugins/flow.js#L118
|
||||
"Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`",
|
||||
// Rethrow on omitted call arguments: foo("a", , "b");
|
||||
// ErrorMessages.UnexpectedToken
|
||||
"Unexpected token ','",
|
||||
// ErrorMessages.EscapedCharNotAnIdentifier
|
||||
"Invalid Unicode escape",
|
||||
// ErrorMessages.MissingUnicodeEscape
|
||||
"Expecting Unicode escape sequence \\uXXXX",
|
||||
// ErrorMessages.MissingSemicolon
|
||||
"Missing semicolon",
|
||||
]);
|
||||
const allowedMessages = new Set([
|
||||
"The only valid numeric escape in strict mode is '\\0'",
|
||||
"'with' in strict mode",
|
||||
"Legacy octal literals are not allowed in strict mode",
|
||||
|
||||
"Invalid left-hand side in parenthesized expression",
|
||||
"Invalid left-hand side in assignment expression",
|
||||
"Invalid left-hand side in postfix operation",
|
||||
"Invalid left-hand side in prefix operation",
|
||||
|
||||
"Type argument list cannot be empty.",
|
||||
"Type parameter list cannot be empty.",
|
||||
"Type parameters cannot appear on a constructor declaration.",
|
||||
|
||||
"A parameter property may not be declared using a binding pattern.",
|
||||
"A parameter property is only allowed in a constructor implementation.",
|
||||
|
||||
"Tuple members must all have names or all not have names.",
|
||||
"Tuple members must be labeled with a simple identifier.",
|
||||
|
||||
"'abstract' modifier can only appear on a class, method, or property declaration.",
|
||||
"'readonly' modifier can only appear on a property declaration or index signature.",
|
||||
"Class methods cannot have the 'declare' modifier",
|
||||
"Class methods cannot have the 'readonly' modifier",
|
||||
"'public' modifier cannot appear on a type member.",
|
||||
"'private' modifier cannot appear on a type member.",
|
||||
"'protected' modifier cannot appear on a type member.",
|
||||
"'static' modifier cannot appear on a type member.",
|
||||
"'declare' modifier cannot appear on a type member.",
|
||||
"'abstract' modifier cannot appear on a type member.",
|
||||
"'readonly' modifier cannot appear on a type member.",
|
||||
"Accessibility modifier already seen.",
|
||||
"Index signatures cannot have the 'declare' modifier",
|
||||
|
||||
"Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
|
||||
"Argument name clash",
|
||||
"import() requires exactly one or two arguments",
|
||||
"Invalid decimal",
|
||||
"Unexpected trailing comma after rest element",
|
||||
"Decorators cannot be used to decorate parameters",
|
||||
"Unterminated JSX contents",
|
||||
"Invalid parenthesized assignment pattern",
|
||||
'Unexpected token, expected "}"',
|
||||
"Unexpected token :",
|
||||
"Unexpected reserved word 'package'",
|
||||
'Duplicate key "type" is not allowed in module attributes',
|
||||
"Cannot use new with import(...)",
|
||||
"No line break is allowed before '=>'",
|
||||
"Invalid escape sequence in template",
|
||||
"Abstract methods can only appear within an abstract class.",
|
||||
"Decorators cannot be used to decorate object literal properties",
|
||||
"A required element cannot follow an optional element.",
|
||||
"A binding pattern parameter cannot be optional in an implementation signature.",
|
||||
"Initializers are not allowed in ambient contexts.",
|
||||
"A type-only import can specify a default import or named bindings, but not both.",
|
||||
"An implementation cannot be declared in ambient contexts.",
|
||||
"Classes may not have a field named 'constructor'",
|
||||
]);
|
||||
function shouldRethrowRecoveredError(error) {
|
||||
const [, message] = error.message.match(/(.*?)\s*\(\d+:\d+\)/);
|
||||
// Only works for literal message
|
||||
return messagesShouldThrow.has(message);
|
||||
|
||||
if (
|
||||
allowedMessages.has(message) ||
|
||||
/^Identifier '.*?' has already been declared$/.test(message) ||
|
||||
/^Private name #.*? is not defined$/.test(message) ||
|
||||
/^`.*?` has already been exported\. Exported identifiers must be unique\.$/.test(
|
||||
message
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const babel = createParser(parse);
|
||||
|
||||
Reference in new issue
Block a user