Improve error for unclosed imports and exports (#54634)

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
Jack Works
2023-06-30 01:22:05 +08:00
committed by GitHub
parent 18e19492be
commit ed5008da68
9 changed files with 57 additions and 106 deletions

View File

@@ -2869,6 +2869,11 @@ namespace Parser {
case ParsingContext.HeritageClauses:
return isHeritageClause();
case ParsingContext.ImportOrExportSpecifiers:
// bail out if the next token is [FromKeyword StringLiteral].
// That means we're in something like `import { from "mod"`. Stop here can give better error message.
if (token() === SyntaxKind.FromKeyword && lookAhead(nextTokenIsStringLiteral)) {
return false;
}
return tokenIsIdentifierOrKeyword(token());
case ParsingContext.JsxAttributes:
return tokenIsIdentifierOrKeyword(token()) || token() === SyntaxKind.OpenBraceToken;
@@ -3390,7 +3395,11 @@ namespace Parser {
case ParsingContext.TypeArguments: return parseErrorAtCurrentToken(Diagnostics.Type_argument_expected);
case ParsingContext.TupleElementTypes: return parseErrorAtCurrentToken(Diagnostics.Type_expected);
case ParsingContext.HeritageClauses: return parseErrorAtCurrentToken(Diagnostics.Unexpected_token_expected);
case ParsingContext.ImportOrExportSpecifiers: return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
case ParsingContext.ImportOrExportSpecifiers:
if (token() === SyntaxKind.FromKeyword) {
return parseErrorAtCurrentToken(Diagnostics._0_expected, "}");
}
return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
case ParsingContext.JsxAttributes: return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
case ParsingContext.JsxChildren: return parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
case ParsingContext.AssertEntries: return parseErrorAtCurrentToken(Diagnostics.Identifier_or_string_literal_expected); // AssertionKey.
@@ -7413,6 +7422,9 @@ namespace Parser {
}
}
function nextTokenIsStringLiteral() {
return nextToken() === SyntaxKind.StringLiteral;
}
function nextTokenIsIdentifierOrStringLiteralOnSameLine() {
nextToken();
return !scanner.hasPrecedingLineBreak() && (isIdentifier() || token() === SyntaxKind.StringLiteral);