Skip to content

Commit

Permalink
Remove this annotation in function declarations
Browse files Browse the repository at this point in the history
flow doesn't have support for [`this` annotation](https://www.typescriptlang.org/docs/handbook/functions.html) in functions: facebook/flow#452

This will result in prettier failing to parse the generated code. This PR removes the this annotation in function declarations.
  • Loading branch information
lgeiger committed Oct 13, 2018
1 parent 4425490 commit 1f99233
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/__tests__/__snapshots__/function_exports.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ declare export function syncHistoryWithStore(
): History & HistoryUnsubscribe;
"
`;
exports[`should remove this annotation from functions 1`] = `
"declare function addClickListener(onclick: (e: Event) => void): void;
"
`;
7 changes: 7 additions & 0 deletions src/__tests__/function_exports.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ export function syncHistoryWithStore(history: History, store: Store<any>, option
const result = compiler.compileDefinitionString(ts);
expect(beautify(result)).toMatchSnapshot();
});

it("should remove this annotation from functions", () => {
const ts =
"function addClickListener(onclick: (this: void, e: Event) => void): void;";
const result = compiler.compileDefinitionString(ts);
expect(beautify(result)).toMatchSnapshot();
});
4 changes: 3 additions & 1 deletion src/printers/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import type { RawNode } from "../nodes/node";
import printers from "./index";

export const functionType = (func: RawNode, dotAsReturn: boolean = false) => {
const params = func.parameters.map(printers.common.parameter);
const params = func.parameters
.filter(param => param.name.text !== "this")
.map(printers.common.parameter);
const generics = printers.common.generics(func.typeParameters);
const returns = func.type ? printers.node.printType(func.type) : "void";

Expand Down

0 comments on commit 1f99233

Please sign in to comment.