mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 11:24:49 -05:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user