-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue-script-setup-converter): Convert page meta into definePageMe…
…ta (#50) * feat: Convert to definePageMeta * feat: Convert page meta in convertSrt
- Loading branch information
1 parent
6a92d55
commit e65a483
Showing
7 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./emitsConverter"; | ||
export * from "./pageMetaConverter"; | ||
export * from "./propsConverter"; | ||
export * from "./setupConverter"; |
85 changes: 85 additions & 0 deletions
85
packages/vue-script-setup-converter/src/lib/converter/pageMetaConverter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { expect, describe, it } from "vitest"; | ||
import { CallExpression, ScriptTarget, SyntaxKind, Project } from "ts-morph"; | ||
import { parse } from "@vue/compiler-sfc"; | ||
import prettier from "prettier"; | ||
import parserTypeScript from "prettier/parser-typescript"; | ||
import { getNodeByKind } from "../helper"; | ||
import { convertPageMeta } from "./pageMetaConverter"; | ||
|
||
const parseScript = (input: string, lang: "js" | "ts" = "js") => { | ||
const { | ||
descriptor: { script }, | ||
} = parse(input); | ||
|
||
const project = new Project({ | ||
tsConfigFilePath: "tsconfig.json", | ||
compilerOptions: { | ||
target: ScriptTarget.Latest, | ||
}, | ||
}); | ||
|
||
const sourceFile = project.createSourceFile("s.tsx", script?.content ?? ""); | ||
|
||
const callExpression = getNodeByKind(sourceFile, SyntaxKind.CallExpression); | ||
|
||
const pageMeta = convertPageMeta(callExpression as CallExpression, lang); | ||
|
||
const formatedText = prettier.format(pageMeta, { | ||
parser: "typescript", | ||
plugins: [parserTypeScript], | ||
}); | ||
|
||
return formatedText; | ||
}; | ||
|
||
describe("convertPageMeta", () => { | ||
describe("basic", () => { | ||
const source = `<script> | ||
import { defineComponent } from 'vue'; | ||
export default defineComponent({ | ||
name: 'HelloWorld', | ||
layout: 'test-layout', | ||
middleware: 'test-middleware', | ||
}) | ||
</script>`; | ||
|
||
it("returns text including definePageMeta", () => { | ||
const output = parseScript(source); | ||
|
||
const expected = `definePageMeta({ | ||
name: "HelloWorld", | ||
layout: "test-layout", | ||
middleware: "test-middleware", | ||
}); | ||
`; | ||
|
||
expect(output).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe("when middleware is array", () => { | ||
const source = `<script> | ||
import { defineComponent } from 'vue'; | ||
export default defineComponent({ | ||
name: 'HelloWorld', | ||
layout: 'test-layout', | ||
middleware: ['test-middleware-1', 'test-middleware-2'], | ||
}) | ||
</script>`; | ||
|
||
it("returns text including definePageMeta", () => { | ||
const output = parseScript(source); | ||
|
||
const expected = `definePageMeta({ | ||
name: "HelloWorld", | ||
layout: "test-layout", | ||
middleware: ["test-middleware-1", "test-middleware-2"], | ||
}); | ||
`; | ||
|
||
expect(output).toBe(expected); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
packages/vue-script-setup-converter/src/lib/converter/pageMetaConverter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { CallExpression, PropertyAssignment } from "ts-morph"; | ||
import { getOptionsNode } from "../helper"; | ||
|
||
export const convertPageMeta = (node: CallExpression, lang: string = "js") => { | ||
const nameNode = getOptionsNode(node, "name"); | ||
const layoutNode = getOptionsNode(node, "layout"); | ||
const middlewareNode = getOptionsNode(node, "middleware"); | ||
|
||
if (!nameNode && !layoutNode && !middlewareNode) return ""; | ||
|
||
return convertToDefinePageMeta({ nameNode, layoutNode, middlewareNode }); | ||
}; | ||
|
||
const convertToDefinePageMeta = ({ | ||
nameNode, | ||
layoutNode, | ||
middlewareNode, | ||
}: { | ||
nameNode: PropertyAssignment | undefined; | ||
layoutNode: PropertyAssignment | undefined; | ||
middlewareNode: PropertyAssignment | undefined; | ||
}) => { | ||
const nameProperty = nameNode ? nameNode.getText() : ""; | ||
const layoutProperty = layoutNode ? layoutNode.getText() : ""; | ||
const middlewareProperty = middlewareNode ? middlewareNode.getText() : ""; | ||
|
||
return `definePageMeta({ | ||
${[nameProperty, layoutProperty, middlewareProperty] | ||
.filter(Boolean) | ||
.join(",")} | ||
});`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters