Skip to content

Commit

Permalink
fix: Do not serialize ? on optional parameters with initializer in …
Browse files Browse the repository at this point in the history
…ASTBuilder (#1377)
  • Loading branch information
Willem Wyndham authored Jul 7, 2020
1 parent 68db679 commit 4fd9c29
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/extra/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/constructor.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
2 changes: 1 addition & 1 deletion tests/parser/function.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function simple(): void {}
function typeparams<T, V extends T>(a?: V | null = null): void {}
function typeparams<T, V extends T>(a: V | null = null): void {}
@decorator()
function withdecorator(): void {}
function withthis(this: i32): i32 {
Expand Down
1 change: 1 addition & 0 deletions tests/parser/parameter-optional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function optionalParam(a: string = ""): void {}
1 change: 1 addition & 0 deletions tests/parser/parameter-optional.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function optionalParam(a: string = ""): void {}
2 changes: 1 addition & 1 deletion tests/parser/parameter-order.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function restValid(a: i32, ...b: Array<i32>): void {}
function optionalValid(a: i32, b?: i32): void {}
function restParameterMustBeLast(...a: Array<i32>, 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)
2 changes: 1 addition & 1 deletion tests/parser/string-binding.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -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 = <u16>-1): String {
static fromCharCode(char: u16, schar: u16 = <u16>-1): String {
return unreachable();
}
@binding(BindingCall.FUNCTION, [BindingType.U32], BindingType.OBJECT_HANDLE)
Expand Down

0 comments on commit 4fd9c29

Please sign in to comment.