mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
fix(39836): allow type declaration/unknown type in catch arguments in JavaScript files (#42392)
This commit is contained in:
1 parent
38fdce9440
commit
ca8d9e4402
15 files changed
+619
-12
No files matched your search
@@ -8847,9 +8847,11 @@ namespace ts {
|
||||
Debug.assertIsDefined(symbol.valueDeclaration);
|
||||
const declaration = symbol.valueDeclaration;
|
||||
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
|
||||
const decl = declaration as VariableDeclaration;
|
||||
if (!decl.type) return anyType;
|
||||
const type = getTypeOfNode(decl.type);
|
||||
const typeNode = getEffectiveTypeAnnotationNode(declaration);
|
||||
if (typeNode === undefined) {
|
||||
return anyType;
|
||||
}
|
||||
const type = getTypeOfNode(typeNode);
|
||||
// an errorType will make `checkTryStatement` issue an error
|
||||
return isTypeAny(type) || type === unknownType ? type : errorType;
|
||||
}
|
||||
@@ -36044,10 +36046,11 @@ namespace ts {
|
||||
// Grammar checking
|
||||
if (catchClause.variableDeclaration) {
|
||||
const declaration = catchClause.variableDeclaration;
|
||||
if (declaration.type) {
|
||||
const typeNode = getEffectiveTypeAnnotationNode(getRootDeclaration(declaration));
|
||||
if (typeNode) {
|
||||
const type = getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ false);
|
||||
if (type && !(type.flags & TypeFlags.AnyOrUnknown)) {
|
||||
grammarErrorOnFirstToken(declaration.type, Diagnostics.Catch_clause_variable_type_annotation_must_be_any_or_unknown_if_specified);
|
||||
grammarErrorOnFirstToken(typeNode, Diagnostics.Catch_clause_variable_type_annotation_must_be_any_or_unknown_if_specified);
|
||||
}
|
||||
}
|
||||
else if (declaration.initializer) {
|
||||
|
||||
@@ -6279,6 +6279,7 @@ namespace ts {
|
||||
|
||||
function parseVariableDeclaration(allowExclamation?: boolean): VariableDeclaration {
|
||||
const pos = getNodePos();
|
||||
const hasJSDoc = hasPrecedingJSDocComment();
|
||||
const name = parseIdentifierOrPattern(Diagnostics.Private_identifiers_are_not_allowed_in_variable_declarations);
|
||||
let exclamationToken: ExclamationToken | undefined;
|
||||
if (allowExclamation && name.kind === SyntaxKind.Identifier &&
|
||||
@@ -6288,7 +6289,7 @@ namespace ts {
|
||||
const type = parseTypeAnnotation();
|
||||
const initializer = isInOrOfKeyword(token()) ? undefined : parseInitializer();
|
||||
const node = factory.createVariableDeclaration(name, exclamationToken, type, initializer);
|
||||
return finishNode(node, pos);
|
||||
return withJSDoc(finishNode(node, pos), hasJSDoc);
|
||||
}
|
||||
|
||||
function parseVariableDeclarationList(inForStatementInitializer: boolean): VariableDeclarationList {
|
||||
|
||||
@@ -874,6 +874,7 @@ namespace ts {
|
||||
| FunctionDeclaration
|
||||
| ConstructorDeclaration
|
||||
| MethodDeclaration
|
||||
| VariableDeclaration
|
||||
| PropertyDeclaration
|
||||
| AccessorDeclaration
|
||||
| ClassLikeDeclaration
|
||||
@@ -1237,7 +1238,7 @@ namespace ts {
|
||||
|
||||
export type BindingName = Identifier | BindingPattern;
|
||||
|
||||
export interface VariableDeclaration extends NamedDeclaration {
|
||||
export interface VariableDeclaration extends NamedDeclaration, JSDocContainer {
|
||||
readonly kind: SyntaxKind.VariableDeclaration;
|
||||
readonly parent: VariableDeclarationList | CatchClause;
|
||||
readonly name: BindingName; // Declared variable name
|
||||
|
||||
@@ -1204,7 +1204,8 @@ namespace ts {
|
||||
node.kind === SyntaxKind.TypeParameter ||
|
||||
node.kind === SyntaxKind.FunctionExpression ||
|
||||
node.kind === SyntaxKind.ArrowFunction ||
|
||||
node.kind === SyntaxKind.ParenthesizedExpression) ?
|
||||
node.kind === SyntaxKind.ParenthesizedExpression ||
|
||||
node.kind === SyntaxKind.VariableDeclaration) ?
|
||||
concatenate(getTrailingCommentRanges(text, node.pos), getLeadingCommentRanges(text, node.pos)) :
|
||||
getLeadingCommentRanges(text, node.pos);
|
||||
// True if the comment starts with '/**' but not if it is '/**/'
|
||||
|
||||
Reference in new issue
Block a user