From f4959f867b9dc8d5ee5c03dc2c9eb425e407b6c2 Mon Sep 17 00:00:00 2001 From: INOUE Takuya Date: Sun, 5 May 2024 19:00:51 +0900 Subject: [PATCH] feat: Convert page meta in convertSrt --- .../src/lib/__snapshots__/convertSrc.test.ts.snap | 3 +++ .../src/lib/convertSrc.test.ts | 2 ++ .../src/lib/convertSrc.ts | 13 +++++++++++++ 3 files changed, 18 insertions(+) diff --git a/packages/vue-script-setup-converter/src/lib/__snapshots__/convertSrc.test.ts.snap b/packages/vue-script-setup-converter/src/lib/__snapshots__/convertSrc.test.ts.snap index 6a82991..9acbf06 100644 --- a/packages/vue-script-setup-converter/src/lib/__snapshots__/convertSrc.test.ts.snap +++ b/packages/vue-script-setup-converter/src/lib/__snapshots__/convertSrc.test.ts.snap @@ -2,6 +2,9 @@ exports[`snapshot > defineNuxtComponent 1`] = ` "import { defineNuxtComponent, useNuxtApp } from '#imports'; +definePageMeta({ + name: 'HelloWorld', layout: 'test-layout', middleware: 'test-middleware' +}); const { $client } = useNuxtApp(); diff --git a/packages/vue-script-setup-converter/src/lib/convertSrc.test.ts b/packages/vue-script-setup-converter/src/lib/convertSrc.test.ts index c0a5d64..31b90b0 100644 --- a/packages/vue-script-setup-converter/src/lib/convertSrc.test.ts +++ b/packages/vue-script-setup-converter/src/lib/convertSrc.test.ts @@ -79,6 +79,8 @@ import { defineNuxtComponent, useNuxtApp } from '#imports'; export default defineNuxtComponent({ name: 'HelloWorld', + layout: 'test-layout', + middleware: 'test-middleware', setup(props, ctx) { const { $client } = useNuxtApp(); diff --git a/packages/vue-script-setup-converter/src/lib/convertSrc.ts b/packages/vue-script-setup-converter/src/lib/convertSrc.ts index e096dca..8b9b735 100644 --- a/packages/vue-script-setup-converter/src/lib/convertSrc.ts +++ b/packages/vue-script-setup-converter/src/lib/convertSrc.ts @@ -8,6 +8,7 @@ import { } from "ts-morph"; import { parse } from "@vue/compiler-sfc"; import { getNodeByKind } from "./helper"; +import { convertPageMeta } from "./converter/pageMetaConverter"; import { convertProps } from "./converter/propsConverter"; import { convertSetup } from "./converter/setupConverter"; import { convertEmits } from "./converter/emitsConverter"; @@ -40,6 +41,7 @@ export const convertSrc = (input: string) => { throw new Error("defineComponent is not found."); } + const pageMeta = convertPageMeta(callexpression, lang) ?? ""; const props = convertProps(callexpression, lang) ?? ""; const emits = convertEmits(callexpression, lang) ?? ""; const statement = convertSetup(callexpression) ?? ""; @@ -53,6 +55,10 @@ export const convertSrc = (input: string) => { .map((x) => x.getText()) ); + if (isDefineNuxtComponent(callexpression)) { + statements.addStatements(pageMeta); + } + statements.addStatements(props); statements.addStatements(emits); statements.addStatements(statement); @@ -75,3 +81,10 @@ const isDefineComponent = (node: CallExpression) => { node.getExpression().getText() === "defineNuxtComponent" ); }; + +const isDefineNuxtComponent = (node: CallExpression) => { + if (!Node.isIdentifier(node.getExpression())) { + return false; + } + return node.getExpression().getText() === "defineNuxtComponent"; +};