Skip to content

Commit

Permalink
bugfix: keep default value in method addition
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Aug 15, 2020
1 parent eafb0f8 commit e464eea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/code-object/method.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export class Foo {
/**
* remove param "this":
*/
const params = raw_params.filter(param => isIdentifier(param) && param.name !== "this");
const params = raw_params.filter(param => {
if (isIdentifier(param) && param.name === "this") {
return false;
}
return true;
});
const kind = id.name === "constructor" ? id.name : "method";

const class_method = classMethod(kind, id, params, body);
Expand Down
6 changes: 6 additions & 0 deletions test/code-object/__snapshots__/method.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`method default value 1`] = `
"Test(a: number, b: string = \\"\\") {
return a.toString() + b;
}"
`;

exports[`method to class method 1`] = `
"Test(a: number, b: string) {
return a.toString() + b;
Expand Down
12 changes: 12 additions & 0 deletions test/code-object/method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ describe("method", () => {
expect(ToString(method.ToClassMethod())).toMatchSnapshot();
expect(method.m_ClassName).toEqual("Foo");
});

test("default value", () => {
const method = new MethodCode(
ToNodePath<ExpressionStatement>(`
<Foo/> + function Test(this: Foo, a: number, b: string = ""){
return a.toString()+b;
}
`)
);

expect(ToString(method.ToClassMethod())).toMatchSnapshot();
});
});

0 comments on commit e464eea

Please sign in to comment.