From 4fd9c29a77a2bce334cd32fcd6d92763a8aaa7c1 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Tue, 7 Jul 2020 11:29:01 -0400 Subject: [PATCH] fix: Do not serialize `?` on optional parameters with initializer in ASTBuilder (#1377) --- src/extra/ast.ts | 2 +- tests/parser/constructor.ts.fixture.ts | 2 +- tests/parser/function.ts.fixture.ts | 2 +- tests/parser/parameter-optional.ts | 1 + tests/parser/parameter-optional.ts.fixture.ts | 1 + tests/parser/parameter-order.ts.fixture.ts | 2 +- tests/parser/string-binding.ts.fixture.ts | 2 +- 7 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 tests/parser/parameter-optional.ts create mode 100644 tests/parser/parameter-optional.ts.fixture.ts diff --git a/src/extra/ast.ts b/src/extra/ast.ts index 9044ac7aae..9c0dfa26c1 100644 --- a/src/extra/ast.ts +++ b/src/extra/ast.ts @@ -1613,7 +1613,7 @@ export class ASTBuilder { var type = node.type; var initializer = node.initializer; if (type) { - if (kind == ParameterKind.OPTIONAL) sb.push("?"); + if (kind == ParameterKind.OPTIONAL && !initializer) sb.push("?"); if (!isTypeOmitted(type)) { sb.push(": "); this.visitTypeNode(type); diff --git a/tests/parser/constructor.ts.fixture.ts b/tests/parser/constructor.ts.fixture.ts index 8b80d9e70f..481863215c 100644 --- a/tests/parser/constructor.ts.fixture.ts +++ b/tests/parser/constructor.ts.fixture.ts @@ -4,5 +4,5 @@ class MyClass { constructor(a: i32, b: i32) {} } class MyClassImplicit { - constructor(public a: i32, private readonly b?: i32 = 2, c?: i32 = 3) {} + constructor(public a: i32, private readonly b: i32 = 2, c: i32 = 3) {} } diff --git a/tests/parser/function.ts.fixture.ts b/tests/parser/function.ts.fixture.ts index 277b48a93f..be705e4334 100644 --- a/tests/parser/function.ts.fixture.ts +++ b/tests/parser/function.ts.fixture.ts @@ -1,5 +1,5 @@ function simple(): void {} -function typeparams(a?: V | null = null): void {} +function typeparams(a: V | null = null): void {} @decorator() function withdecorator(): void {} function withthis(this: i32): i32 { diff --git a/tests/parser/parameter-optional.ts b/tests/parser/parameter-optional.ts new file mode 100644 index 0000000000..6bcd8c3b06 --- /dev/null +++ b/tests/parser/parameter-optional.ts @@ -0,0 +1 @@ +function optionalParam(a: string = ""): void {} diff --git a/tests/parser/parameter-optional.ts.fixture.ts b/tests/parser/parameter-optional.ts.fixture.ts new file mode 100644 index 0000000000..6bcd8c3b06 --- /dev/null +++ b/tests/parser/parameter-optional.ts.fixture.ts @@ -0,0 +1 @@ +function optionalParam(a: string = ""): void {} diff --git a/tests/parser/parameter-order.ts.fixture.ts b/tests/parser/parameter-order.ts.fixture.ts index 5398bfffe2..e972dfe4d7 100644 --- a/tests/parser/parameter-order.ts.fixture.ts +++ b/tests/parser/parameter-order.ts.fixture.ts @@ -2,7 +2,7 @@ function restValid(a: i32, ...b: Array): void {} function optionalValid(a: i32, b?: i32): void {} function restParameterMustBeLast(...a: Array, b: i32): void {} function optionalCannotPrecedeRequired(a?: i32, b: i32): void {} -function optionalWithInitializerCannotPrecedeRequired(a?: i32 = 1, b: i32): void {} +function optionalWithInitializerCannotPrecedeRequired(a: i32 = 1, b: i32): void {} // ERROR 1014: "A rest parameter must be last in a parameter list." in parameter-order.ts(5,37+1) // ERROR 1016: "A required parameter cannot follow an optional parameter." in parameter-order.ts(8,49+1) // ERROR 1016: "A required parameter cannot follow an optional parameter." in parameter-order.ts(11,67+1) diff --git a/tests/parser/string-binding.ts.fixture.ts b/tests/parser/string-binding.ts.fixture.ts index 92f9e21a5c..373498726c 100644 --- a/tests/parser/string-binding.ts.fixture.ts +++ b/tests/parser/string-binding.ts.fixture.ts @@ -1,7 +1,7 @@ @binding(BindingCall.NEW, [BindingType.STRING], BindingType.OBJECT_HANDLE) export class ExternalString { @binding(BindingCall.FUNCTION, [BindingType.U32, BindingType.U32], BindingType.OBJECT_HANDLE) - static fromCharCode(char: u16, schar?: u16 = -1): String { + static fromCharCode(char: u16, schar: u16 = -1): String { return unreachable(); } @binding(BindingCall.FUNCTION, [BindingType.U32], BindingType.OBJECT_HANDLE)