Skip to content

Commit

Permalink
Merge pull request #39 from wattanx/fix/support-other-default-pattern
Browse files Browse the repository at this point in the history
fix(vue-script-setup-converter): support return statement
  • Loading branch information
wattanx authored Sep 11, 2023
2 parents 63f7c12 + 698582b commit 5403f16
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@ const props = withDefaults(defineProps<Props>(), {
expect(output).toBe(expected);
});

it("default arrow function and return", () => {
const source = `<script lang="ts">
import { defineComponent, toRefs, computed, ref } from 'vue';
export default defineComponent({
name: 'HelloWorld',
props: {
foo: {
type: Object,
default: () => {
return { msg: "Hello World" }
}
},
bar: {
type: Array,
default: () => {
return ["foo", "bar"]
}
}
}
})
</script>`;
const output = parseScript(source, "ts");

const expected = `type Props = { foo?: { msg: string }; bar?: string[] };
const props = withDefaults(defineProps<Props>(), {
foo: () => ({ msg: "Hello World" }),
bar: () => ["foo", "bar"],
});
`;

expect(output).toBe(expected);
});

it("non primitive", () => {
const source = `<script lang="ts">
import { defineComponent, toRefs, computed, ref, PropType } from 'vue';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
AsExpression,
ArrowFunction,
} from "ts-morph";
import { getOptionsNode } from "../helper";
import { getNodeByKind, getOptionsNode } from "../helper";

export const convertProps = (node: CallExpression, lang: string = "js") => {
const propsNode = getOptionsNode(node, "props");
Expand Down Expand Up @@ -257,6 +257,21 @@ const getPropTypeByDefault = (propsNode: MethodDeclaration) => {
const getPropTypeByArrowFunction = (node: ArrowFunction) => {
const body = node.getBody();

if (Node.isBlock(body)) {
const statement = body.getStatements()[0];

if (Node.isReturnStatement(statement)) {
const expression = statement.getExpression();

if (
Node.isObjectLiteralExpression(expression) ||
Node.isArrayLiteralExpression(expression)
) {
return expression.getType().getText();
}
}
}

if (Node.isArrayLiteralExpression(body)) {
return body.getType().getText();
}
Expand Down Expand Up @@ -313,6 +328,26 @@ const getPropsOption = (
if (!initializer) {
throw new Error("props property not found.");
}

if (Node.isArrowFunction(initializer)) {
const returnStatement = getNodeByKind(
initializer,
SyntaxKind.ReturnStatement
) as ReturnStatement;

if (!returnStatement) {
return initializer.getText();
}

const expression = returnStatement.getExpression();
if (
Node.isObjectLiteralExpression(expression) ||
Node.isArrayLiteralExpression(expression)
) {
return `() => (${expression.getText()})`;
}
}

if (
isFalseKeyword(initializer.getKind()) ||
isTrueKeyword(initializer.getKind())
Expand Down

1 comment on commit 5403f16

@vercel
Copy link

@vercel vercel bot commented on 5403f16 Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wattanx-converter – ./

wattanx-converter.vercel.app
wattanx-converter-git-main-wattanx.vercel.app
wattanx-converter-wattanx.vercel.app

Please sign in to comment.