generated from so1ve/starter-unplugin
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
199 additions
and
8 deletions.
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as ts from "typescript"; | ||
|
||
export const normalizePath = (id: string) => id.replace(/\\/g, "/"); | ||
|
||
export const vueFilepathToVirtualFilepath = (filepath: string) => | ||
filepath.replace(/\.vue$/, ".vue-complex-types.vue.ts"); | ||
|
||
export function getProgramAndSourceFile(code: string, id: string) { | ||
const sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest); | ||
const defaultCompilerHost = ts.createCompilerHost( | ||
// TODO: receive a tsconfig | ||
{}, | ||
); | ||
const customCompilerHost: ts.CompilerHost = { | ||
...defaultCompilerHost, | ||
getSourceFile: (name, languageVersion) => { | ||
if (vueFilepathToVirtualFilepath(name) === id) { | ||
return sourceFile; | ||
} | ||
|
||
return defaultCompilerHost.getSourceFile(name, languageVersion); | ||
}, | ||
}; | ||
const program = ts.createProgram([id], {}, customCompilerHost); | ||
|
||
return { program, sourceFile }; | ||
} | ||
|
||
const vueAcceptableTypes = ["string", "number", "boolean", "bigint", "symbol"]; | ||
function toVueAcceptableType(typeString: string) { | ||
if (vueAcceptableTypes.includes(typeString)) { | ||
return typeString; | ||
} | ||
if (typeString.endsWith("[]")) { | ||
return "any[]"; | ||
} | ||
|
||
return "object"; | ||
} | ||
|
||
export function printVueAcceptableTypeLiteralFromSymbols( | ||
symbols: ts.Symbol[], | ||
typeChecker: ts.TypeChecker, | ||
) { | ||
const codes = ["{"]; | ||
function push(code: string) { | ||
codes.push(` ${code}`); | ||
} | ||
for (const symbol of symbols) { | ||
for (const decl of symbol.getDeclarations() ?? []) { | ||
if (ts.isPropertySignature(decl)) { | ||
const type = typeChecker.getTypeAtLocation(decl); | ||
const typeString = typeChecker.typeToString(type); | ||
push(`${decl.name.getText()}: ${toVueAcceptableType(typeString)};`); | ||
} | ||
} | ||
} | ||
codes.push("}"); | ||
|
||
return codes.join("\n"); | ||
} | ||
|
||
export function findDefinePropsCall(node: ts.Node) { | ||
let definePropsCall: ts.CallExpression | undefined; | ||
function traverse(node: ts.Node) { | ||
if ( | ||
ts.isCallExpression(node) && | ||
node.expression.getText() === "defineProps" && | ||
node.typeArguments | ||
) { | ||
definePropsCall = node; | ||
} | ||
|
||
node.forEachChild(traverse); | ||
} | ||
traverse(node); | ||
|
||
return definePropsCall; | ||
} |
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,9 @@ | ||
<script setup lang="ts"> | ||
import type { A } from "./foo"; | ||
defineProps< | ||
{ | ||
msg: string; | ||
} & A | ||
>(); | ||
</script> |
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,11 @@ | ||
export interface A { | ||
foo: B; | ||
bar: C; | ||
baz: D; | ||
} | ||
|
||
interface B {} | ||
type C = number extends boolean ? string : number; | ||
interface D { | ||
aaa: 1; | ||
} |
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,29 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`should > exported 1`] = ` | ||
{ | ||
"code": "<script setup lang=\\"ts\\"> | ||
import type { A } from \\"./foo\\"; | ||
defineProps< | ||
{ | ||
msg: string; | ||
foo: object; | ||
bar: number; | ||
baz: object; | ||
} | ||
>(); | ||
</script> | ||
", | ||
"map": SourceMap { | ||
"file": undefined, | ||
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACxB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACZ,CAAC;;;;;CAEK;AACN,CAAC,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;", | ||
"names": [], | ||
"sources": [ | ||
"", | ||
], | ||
"sourcesContent": undefined, | ||
"version": 3, | ||
}, | ||
} | ||
`; |
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,7 +1,14 @@ | ||
import { join } from "node:path"; | ||
|
||
import { describe, expect, it } from "vitest"; | ||
|
||
import { transform } from "../src/core/transform"; | ||
import AVue from "./__fixtures__/a.vue?raw"; | ||
|
||
describe("should", () => { | ||
it("exported", () => { | ||
expect(1).toBe(1); | ||
expect( | ||
transform(AVue, join(__dirname, "__fixtures__", "a.vue")), | ||
).toMatchSnapshot(); | ||
}); | ||
}); |
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