fix(paresr): do not report missing initializer error in ambient context (#6020)

closes #5958
This commit is contained in:
Boshen 2024-09-24 09:04:09 +00:00
parent 0c9dee1eda
commit 0658576718
3 changed files with 8 additions and 27 deletions

View file

@ -119,14 +119,17 @@ impl<'a> ParserImpl<'a> {
let init = let init =
self.eat(Kind::Eq).then(|| self.parse_assignment_expression_or_higher()).transpose()?; self.eat(Kind::Eq).then(|| self.parse_assignment_expression_or_higher()).transpose()?;
if init.is_none() && decl_ctx.parent == VariableDeclarationParent::Statement { if init.is_none()
&& !self.ctx.has_ambient()
&& decl_ctx.parent == VariableDeclarationParent::Statement
{
// LexicalBinding[In, Yield, Await] : // LexicalBinding[In, Yield, Await] :
// BindingIdentifier[?Yield, ?Await] Initializer[?In, ?Yield, ?Await] opt // BindingIdentifier[?Yield, ?Await] Initializer[?In, ?Yield, ?Await] opt
// BindingPattern[?Yield, ?Await] Initializer[?In, ?Yield, ?Await] // BindingPattern[?Yield, ?Await] Initializer[?In, ?Yield, ?Await]
// the grammar forbids `let []`, `let {}` // the grammar forbids `let []`, `let {}`
if !matches!(id.kind, BindingPatternKind::BindingIdentifier(_)) { if !matches!(id.kind, BindingPatternKind::BindingIdentifier(_)) {
self.error(diagnostics::invalid_destrucuring_declaration(id.span())); self.error(diagnostics::invalid_destrucuring_declaration(id.span()));
} else if kind == VariableDeclarationKind::Const && !self.ctx.has_ambient() { } else if kind == VariableDeclarationKind::Const {
// It is a Syntax Error if Initializer is not present and IsConstantDeclaration of the LexicalDeclaration containing this LexicalBinding is true. // It is a Syntax Error if Initializer is not present and IsConstantDeclaration of the LexicalDeclaration containing this LexicalBinding is true.
self.error(diagnostics::missinginitializer_in_const(id.span())); self.error(diagnostics::missinginitializer_in_const(id.span()));
} }

View file

@ -2,7 +2,7 @@ commit: a709f989
parser_typescript Summary: parser_typescript Summary:
AST Parsed : 6469/6479 (99.85%) AST Parsed : 6469/6479 (99.85%)
Positive Passed: 6456/6479 (99.65%) Positive Passed: 6458/6479 (99.68%)
Negative Passed: 1226/5715 (21.45%) Negative Passed: 1226/5715 (21.45%)
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration10.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration10.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration11.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration11.ts
@ -4519,15 +4519,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/elidedEmbeddedSt
· ──── · ────
24 │ const enum H {} 24 │ const enum H {}
╰──── ╰────
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationNoNewNames.ts
× Missing initializer in destructuring declaration
╭─[typescript/tests/cases/compiler/moduleAugmentationNoNewNames.ts:11:9]
10 │ let y: number, z: string;
11 │ let {a: x, b: x1}: {a: number, b: number};
· ─────────────────────────────────────
12 │ module Z {}
╰────
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/moduleResolutionWithExtensions_unexpected2.ts Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/moduleResolutionWithExtensions_unexpected2.ts
× Expected a semicolon or an implicit semicolon after a statement, but found none × Expected a semicolon or an implicit semicolon after a statement, but found none
@ -4554,15 +4545,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/moduleResolution
· ▲ · ▲
╰──── ╰────
help: Try insert a semicolon here help: Try insert a semicolon here
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/newNamesInGlobalAugmentations1.ts
× Missing initializer in destructuring declaration
╭─[typescript/tests/cases/compiler/newNamesInGlobalAugmentations1.ts:11:9]
10 │ class Cls {x}
11 │ let [a, b]: number[];
· ────────────────
12 │ export import X = M.M1.x;
╰────
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationDecorators.ts Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationDecorators.ts
× Unexpected token × Unexpected token

View file

@ -2,7 +2,7 @@ commit: a709f989
semantic_typescript Summary: semantic_typescript Summary:
AST Parsed : 6479/6479 (100.00%) AST Parsed : 6479/6479 (100.00%)
Positive Passed: 2671/6479 (41.23%) Positive Passed: 2672/6479 (41.24%)
tasks/coverage/typescript/tests/cases/compiler/2dArrays.ts tasks/coverage/typescript/tests/cases/compiler/2dArrays.ts
semantic error: Symbol reference IDs mismatch: semantic error: Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(1)] after transform: SymbolId(0): [ReferenceId(1)]
@ -23132,8 +23132,7 @@ after transform: ScopeId(0): ["Observable", "x"]
rebuilt : ScopeId(0): ["x"] rebuilt : ScopeId(0): ["x"]
tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationNoNewNames.ts tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationNoNewNames.ts
semantic error: Missing initializer in destructuring declaration semantic error: Bindings mismatch:
Bindings mismatch:
after transform: ScopeId(0): ["./observable", "Observable"] after transform: ScopeId(0): ["./observable", "Observable"]
rebuilt : ScopeId(0): ["Observable"] rebuilt : ScopeId(0): ["Observable"]
Scope children mismatch: Scope children mismatch:
@ -24951,9 +24950,6 @@ Unresolved references mismatch:
after transform: ["a"] after transform: ["a"]
rebuilt : [] rebuilt : []
tasks/coverage/typescript/tests/cases/compiler/newNamesInGlobalAugmentations1.ts
semantic error: Missing initializer in destructuring declaration
tasks/coverage/typescript/tests/cases/compiler/noAsConstNameLookup.ts tasks/coverage/typescript/tests/cases/compiler/noAsConstNameLookup.ts
semantic error: Bindings mismatch: semantic error: Bindings mismatch:
after transform: ScopeId(0): ["C", "Cleaner", "FeatureRunner", "Store"] after transform: ScopeId(0): ["C", "Cleaner", "FeatureRunner", "Store"]