Skip to content

Commit c9d5296

Browse files
authored
fix: put untyped plugin to first to avoid losing returnType. (#160)
1 parent 01b565a commit c9d5296

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/loader/babel.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ const babelPluginUntyped: PluginItem = function (
1616
api: ConfigAPI,
1717
options: { experimentalFunctions?: boolean },
1818
) {
19+
const name = "untyped";
1920
api.cache.using(() => version);
2021

2122
return <PluginObj>{
23+
name,
24+
manipulateOptions(opts: { plugins: Array<{ key: string }> }) {
25+
// This `untyped` plugin must come before the `transform-typescript` plugin,
26+
// otherwise the `returnType` annotations will be removed.
27+
// So put it at the beginning.
28+
opts.plugins!.sort((a) => (a.key === name ? -1 : 0));
29+
},
2230
visitor: {
2331
VariableDeclaration(p) {
2432
const declaration = p.node.declarations[0];

test/fixtures/functions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function main(a: number): string {
2+
return a.toString();
3+
}

test/loader.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,36 @@ describe("loader", () => {
3636
}
3737
`);
3838
});
39+
40+
it("should load a schema with function return type annotations", async () => {
41+
const schema = await loadSchema("./test/fixtures/functions.ts");
42+
expect(schema).toMatchInlineSnapshot(`
43+
{
44+
"default": {},
45+
"id": "#",
46+
"properties": {
47+
"main": {
48+
"args": [
49+
{
50+
"items": {},
51+
"name": "a",
52+
"tsType": "number",
53+
"type": "number",
54+
},
55+
],
56+
"description": "",
57+
"id": "#main",
58+
"returns": {
59+
"tsType": "string",
60+
"type": "string",
61+
},
62+
"tags": [],
63+
"title": "",
64+
"type": "function",
65+
},
66+
},
67+
"type": "object",
68+
}
69+
`);
70+
});
3971
});

0 commit comments

Comments
 (0)