diff --git a/crates/oxc_semantic/src/checker/mod.rs b/crates/oxc_semantic/src/checker/mod.rs index 59284e196..fe0d444c8 100644 --- a/crates/oxc_semantic/src/checker/mod.rs +++ b/crates/oxc_semantic/src/checker/mod.rs @@ -100,6 +100,7 @@ pub fn check<'a>(node: &AstNode<'a>, ctx: &SemanticBuilder<'a>) { AstKind::ObjectExpression(expr) => js::check_object_expression(expr, ctx), AstKind::UnaryExpression(expr) => js::check_unary_expression(expr, node, ctx), AstKind::YieldExpression(expr) => js::check_yield_expression(expr, node, ctx), + AstKind::VariableDeclaration(decl) => ts::check_variable_declaration(decl, ctx), AstKind::VariableDeclarator(decl) => ts::check_variable_declarator(decl, ctx), AstKind::SimpleAssignmentTarget(target) => ts::check_simple_assignment_target(target, ctx), AstKind::TSInterfaceDeclaration(decl) => ts::check_ts_interface_declaration(decl, ctx), diff --git a/crates/oxc_semantic/src/checker/typescript.rs b/crates/oxc_semantic/src/checker/typescript.rs index da7dee513..51ac2d48a 100644 --- a/crates/oxc_semantic/src/checker/typescript.rs +++ b/crates/oxc_semantic/src/checker/typescript.rs @@ -26,9 +26,25 @@ pub fn check_ts_type_parameter_declaration( } } +/// Initializers are not allowed in ambient contexts. ts(1039) +fn initializer_in_ambient_context(init_span: Span) -> OxcDiagnostic { + ts_error("1039", "Initializers are not allowed in ambient contexts.").with_label(init_span) +} + +pub fn check_variable_declaration(decl: &VariableDeclaration, ctx: &SemanticBuilder<'_>) { + if decl.declare { + for var in &decl.declarations { + if let Some(init) = &var.init { + ctx.error(initializer_in_ambient_context(init.span())); + } + } + } +} + fn unexpected_optional(span0: Span) -> OxcDiagnostic { OxcDiagnostic::error("Unexpected `?` operator").with_label(span0) } + #[allow(clippy::cast_possible_truncation)] pub fn check_variable_declarator(decl: &VariableDeclarator, ctx: &SemanticBuilder<'_>) { if decl.id.optional { diff --git a/tasks/coverage/parser_typescript.snap b/tasks/coverage/parser_typescript.snap index 6337e3571..8afe0a3b7 100644 --- a/tasks/coverage/parser_typescript.snap +++ b/tasks/coverage/parser_typescript.snap @@ -3,7 +3,7 @@ commit: a709f989 parser_typescript Summary: AST Parsed : 6470/6479 (99.86%) Positive Passed: 6445/6479 (99.48%) -Negative Passed: 1203/5715 (21.05%) +Negative Passed: 1208/5715 (21.14%) 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/ClassDeclaration13.ts @@ -55,7 +55,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/alwaysStrict Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/alwaysStrictModule.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/alwaysStrictNoImplicitUseStrict.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientEnum1.ts -Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientErrors1.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientExportDefaultErrors.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientExternalModuleInAnotherExternalModule.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ambientExternalModuleWithRelativeExternalImportDeclaration.ts @@ -415,7 +414,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constDeclara Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constDeclarations-access3.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constDeclarations-access4.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constDeclarations-access5.ts -Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constDeclarations-ambient-errors.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constDeclarations-useBeforeDefinition.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constDeclarations-useBeforeDefinition2.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/constEnumBadPropertyNames.ts @@ -850,7 +848,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/extendPrivat Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/extendedInterfacesWithDuplicateTypeParameters.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/extendsClauseAlreadySeen.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/extendsClauseAlreadySeen2.ts -Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externSemantics.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externSyntax.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externalModuleExportingGenericClass.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/externalModuleImmutableBindings.ts @@ -2301,7 +2298,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/ambient/a Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/ambient/ambientDeclarationsPatterns_merging2.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/ambient/ambientDeclarationsPatterns_merging3.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/ambient/ambientDeclarationsPatterns_tooManyAsterisks.ts -Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/ambient/ambientErrors.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/ambient/ambientExternalModuleInsideNonAmbient.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/ambient/ambientExternalModuleInsideNonAmbientExternalModule.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/async/asyncFunctionDeclarationParameterEvaluation.ts @@ -3765,7 +3761,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserCommaInTypeMemberList2.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnPropertySignature1.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock1.ts -Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock3.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock4.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ExportAssignments/parserExportAssignment1.ts @@ -5103,6 +5098,12 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro 4 │ } ╰──── + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/ambientErrors1.ts:1:17] + 1 │ declare var x = 4; + · ─ + ╰──── + × Expected a semicolon or an implicit semicolon after a statement, but found none ╭─[typescript/tests/cases/compiler/ambientPropertyDeclarationInJs.ts:6:17] 5 │ @@ -6485,6 +6486,46 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro 22 │ } ╰──── + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/constDeclarations-ambient-errors.ts:2:29] + 1 │ // error: no intialization expected in ambient declarations + 2 │ declare const c1: boolean = true; + · ──── + 3 │ declare const c2: number = 0; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/constDeclarations-ambient-errors.ts:3:28] + 2 │ declare const c1: boolean = true; + 3 │ declare const c2: number = 0; + · ─ + 4 │ declare const c3 = null, c4 :string = "", c5: any = 0; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/constDeclarations-ambient-errors.ts:4:20] + 3 │ declare const c2: number = 0; + 4 │ declare const c3 = null, c4 :string = "", c5: any = 0; + · ──── + 5 │ + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/constDeclarations-ambient-errors.ts:4:39] + 3 │ declare const c2: number = 0; + 4 │ declare const c3 = null, c4 :string = "", c5: any = 0; + · ── + 5 │ + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/constDeclarations-ambient-errors.ts:4:53] + 3 │ declare const c2: number = 0; + 4 │ declare const c3 = null, c4 :string = "", c5: any = 0; + · ─ + 5 │ + ╰──── + × Missing initializer in const declaration ╭─[typescript/tests/cases/compiler/constDeclarations-errors.ts:2:7] 1 │ // error, missing intialicer @@ -7826,6 +7867,20 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro ╰──── help: Try insert a semicolon here + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/externSemantics.ts:1:15] + 1 │ declare var x=10; + · ── + 2 │ declare var v; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/externSemantics.ts:3:22] + 2 │ declare var v; + 3 │ declare var y:number=3; + · ─ + ╰──── + × Expected `,` but found `=>` ╭─[typescript/tests/cases/compiler/fatarrowfunctionsErrors.ts:2:8] 1 │ foo((...Far:any[])=>{return 0;}) @@ -12672,6 +12727,85 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro 12 │ } ╰──── + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:15:17] + 14 │ + 15 │ declare var b = 10; + · ── + 16 │ + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:21:18] + 20 │ + 21 │ declare var da = 10; + · ── + 22 │ declare var d3, d4 = 10; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:22:22] + 21 │ declare var da = 10; + 22 │ declare var d3, d4 = 10; + · ── + 23 │ + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:25:21] + 24 │ module m3 { + 25 │ declare var d = 10; + · ── + 26 │ declare var d2, d3 = 10, d4 = 10; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:26:26] + 25 │ declare var d = 10; + 26 │ declare var d2, d3 = 10, d4 = 10; + · ── + 27 │ export declare var dE = 10; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:26:35] + 25 │ declare var d = 10; + 26 │ declare var d2, d3 = 10, d4 = 10; + · ── + 27 │ export declare var dE = 10; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:27:29] + 26 │ declare var d2, d3 = 10, d4 = 10; + 27 │ export declare var dE = 10; + · ── + 28 │ export declare var d2E, d3E = 10, d4E = 10; + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:28:35] + 27 │ export declare var dE = 10; + 28 │ export declare var d2E, d3E = 10, d4E = 10; + · ── + 29 │ } + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:28:45] + 27 │ export declare var dE = 10; + 28 │ export declare var d2E, d3E = 10, d4E = 10; + · ── + 29 │ } + ╰──── + + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/compiler/varBlock.ts:39:17] + 38 │ declare var c; + 39 │ declare var c = 10; + · ── + ╰──── + × Cannot assign to 'eval' in strict mode ╭─[typescript/tests/cases/compiler/variableDeclarationInStrictMode1.ts:2:5] 1 │ "use strict"; @@ -12727,6 +12861,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro 3 │ } ╰──── + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/conformance/ambient/ambientErrors.ts:2:17] + 1 │ // Ambient variable with an initializer + 2 │ declare var x = 4; + · ─ + 3 │ + ╰──── + × Expected a semicolon or an implicit semicolon after a statement, but found none ╭─[typescript/tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts:11:8] 10 │ @@ -19968,6 +20110,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro 5 │ return this.doWhile((item, i) => filter(item, i) ? test(item, index++) : true); ╰──── + × TS(1039): Initializers are not allowed in ambient contexts. + ╭─[typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserModifierOnStatementInBlock2.ts:2:20] + 1 │ { + 2 │ declare var x = this; + · ──── + 3 │ } + ╰──── + × Expected a semicolon or an implicit semicolon after a statement, but found none ╭─[typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserPublicBreak1.ts:1:7] 1 │ public break;