@vue.ts/complex-types 1.0.0-beta.1 → 1.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -4
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +1 -1
- package/dist/farm.d.ts +1 -1
- package/dist/farm.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/nuxt.d.ts +1 -1
- package/dist/nuxt.js +3 -3
- package/dist/rolldown.d.ts +1 -1
- package/dist/rolldown.js +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +1 -1
- package/dist/rspack.d.ts +1 -1
- package/dist/rspack.js +1 -1
- package/dist/src-C2cWUkxC.js +192 -0
- package/dist/{types-CFmCIAvs.d.ts → types-afZXKowK.d.ts} +6 -21
- package/dist/types.d.ts +1 -1
- package/dist/{vite-Dyi1m2wg.js → vite-kxApzRU8.js} +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/dist/{webpack-CFfuQfGw.js → webpack-DB3Ln7vl.js} +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +2 -2
- package/package.json +5 -5
- package/dist/src-CI8-WIu4.js +0 -354
package/README.md
CHANGED
package/dist/astro.d.ts
CHANGED
package/dist/astro.js
CHANGED
package/dist/esbuild.d.ts
CHANGED
package/dist/esbuild.js
CHANGED
package/dist/farm.d.ts
CHANGED
package/dist/farm.js
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/nuxt.d.ts
CHANGED
package/dist/nuxt.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./src-
|
|
2
|
-
import { vite_default } from "./vite-
|
|
3
|
-
import { webpack_default } from "./webpack-
|
|
1
|
+
import "./src-C2cWUkxC.js";
|
|
2
|
+
import { vite_default } from "./vite-kxApzRU8.js";
|
|
3
|
+
import { webpack_default } from "./webpack-DB3Ln7vl.js";
|
|
4
4
|
import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";
|
|
5
5
|
|
|
6
6
|
//#region src/nuxt.ts
|
package/dist/rolldown.d.ts
CHANGED
package/dist/rolldown.js
CHANGED
package/dist/rollup.d.ts
CHANGED
package/dist/rollup.js
CHANGED
package/dist/rspack.d.ts
CHANGED
package/dist/rspack.js
CHANGED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { createUnplugin } from "unplugin";
|
|
2
|
+
import { ensureLanguage, getLanguage } from "@vue.ts/language";
|
|
3
|
+
import { createFilter, normalizePath } from "@vue.ts/shared";
|
|
4
|
+
import MagicString from "magic-string";
|
|
5
|
+
import ts from "typescript";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
//#region src/core/utils.ts
|
|
9
|
+
const resolveOptions = (rawOptions) => ({
|
|
10
|
+
include: rawOptions.include ?? ["**/*.vue"],
|
|
11
|
+
exclude: rawOptions.exclude ?? ["node_modules/**"],
|
|
12
|
+
tsconfigPath: rawOptions.tsconfigPath ?? join(process.cwd(), "tsconfig.json"),
|
|
13
|
+
defineEmits: rawOptions.defineEmits ?? true,
|
|
14
|
+
defineProps: rawOptions.defineProps ?? true
|
|
15
|
+
});
|
|
16
|
+
const quotesReg = /"/g;
|
|
17
|
+
const escapeQuotes = (s) => s.replace(quotesReg, "\\\"");
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/core/printer.ts
|
|
21
|
+
var Printer = class {
|
|
22
|
+
constructor(checker) {
|
|
23
|
+
this.checker = checker;
|
|
24
|
+
}
|
|
25
|
+
typeToString(type) {
|
|
26
|
+
return this.checker.typeToString(type, void 0, ts.TypeFormatFlags.NoTruncation);
|
|
27
|
+
}
|
|
28
|
+
printOuterUnionOrIntersection(type, separator, inner) {
|
|
29
|
+
return [...new Set(type.types.map((t) => this.printType(t, inner)).filter(Boolean))].join(separator);
|
|
30
|
+
}
|
|
31
|
+
isUnionUndefined(union) {
|
|
32
|
+
return union.types.some((t) => t.flags & ts.TypeFlags.Undefined);
|
|
33
|
+
}
|
|
34
|
+
printPrimitiveType(type) {
|
|
35
|
+
if (type.flags & ts.TypeFlags.BooleanLiteral) return "boolean";
|
|
36
|
+
else if (type.flags & ts.TypeFlags.String || type.isStringLiteral()) return "string";
|
|
37
|
+
else if (type.flags & ts.TypeFlags.Number || type.isNumberLiteral()) return "number";
|
|
38
|
+
else if (type.flags & ts.TypeFlags.BigInt) return "bigint";
|
|
39
|
+
else if (type.flags & ts.TypeFlags.Any) return "any";
|
|
40
|
+
else if (type.flags & ts.TypeFlags.Unknown) return "unknown";
|
|
41
|
+
else if (type.flags & ts.TypeFlags.Null) return "null";
|
|
42
|
+
return "";
|
|
43
|
+
}
|
|
44
|
+
printType(type, inner = false) {
|
|
45
|
+
if (type.isUnion()) return this.printOuterUnionOrIntersection(type, " | ", inner);
|
|
46
|
+
else if (type.isIntersection()) return this.printOuterUnionOrIntersection(type, " & ", inner);
|
|
47
|
+
if (this.typeToString(type).endsWith("[]")) return this.typeToString(type);
|
|
48
|
+
else if (type.flags & ts.TypeFlags.Object) {
|
|
49
|
+
const decl = type.getSymbol()?.declarations?.[0];
|
|
50
|
+
if (decl && ts.isFunctionTypeNode(decl)) return "Function";
|
|
51
|
+
if (inner) return "object";
|
|
52
|
+
const properties = type.getProperties();
|
|
53
|
+
const props = {};
|
|
54
|
+
for (const prop of properties) {
|
|
55
|
+
const propType = this.checker.getTypeOfSymbol(prop);
|
|
56
|
+
props[prop.getName()] = {
|
|
57
|
+
value: this.printType(this.checker.getTypeOfSymbol(prop), true),
|
|
58
|
+
isOptional: propType.isUnion() ? this.isUnionUndefined(propType) : !!(propType.flags & ts.TypeFlags.Undefined)
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const parts = [];
|
|
62
|
+
for (const [propName, propData] of Object.entries(props)) {
|
|
63
|
+
const questionMark = propData.isOptional ? "?" : "";
|
|
64
|
+
parts.push(`"${propName}"${questionMark}: ${propData.value},`);
|
|
65
|
+
}
|
|
66
|
+
return Object.keys(props).length > 0 ? `{\n${parts.join("\n")}\n}` : "";
|
|
67
|
+
} else if (type.isLiteral() || type.flags & ts.TypeFlags.BooleanLiteral || type.flags & ts.TypeFlags.String || type.flags & ts.TypeFlags.Number || type.flags & ts.TypeFlags.BigInt || type.flags & ts.TypeFlags.Any || type.flags & ts.TypeFlags.Unknown || type.flags & ts.TypeFlags.Null) return this.printPrimitiveType(type);
|
|
68
|
+
else if (type.flags & ts.TypeFlags.Undefined) return "";
|
|
69
|
+
else if (type.flags & ts.TypeFlags.Conditional) return `${this.printType(type.resolvedTrueType)} | ${this.printType(type.resolvedFalseType)}`;
|
|
70
|
+
else if (type.isTypeParameter()) {
|
|
71
|
+
const symbol = type.getSymbol();
|
|
72
|
+
const decl = symbol?.declarations?.[0];
|
|
73
|
+
if (!decl || !ts.isTypeParameterDeclaration(decl)) return "";
|
|
74
|
+
const ref = ts.getEffectiveConstraintOfTypeParameter(decl);
|
|
75
|
+
if (!ref) return "";
|
|
76
|
+
const refType = this.checker.getTypeAtLocation(ref);
|
|
77
|
+
return this.printType(refType);
|
|
78
|
+
}
|
|
79
|
+
return this.typeToString(type);
|
|
80
|
+
}
|
|
81
|
+
printPropsTypeArg(node) {
|
|
82
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
83
|
+
return this.printType(type);
|
|
84
|
+
}
|
|
85
|
+
printEventsByCallSignatures(callSignatures) {
|
|
86
|
+
return callSignatures.map((c) => {
|
|
87
|
+
const parameters = c.getParameters();
|
|
88
|
+
const event = parameters[0];
|
|
89
|
+
return this.typeToString(this.checker.getTypeOfSymbol(event));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
printEventsByMembers(members) {
|
|
93
|
+
return members.map((m) => `"${escapeQuotes(m.getName())}"`);
|
|
94
|
+
}
|
|
95
|
+
printEventsRuntimeArg(node) {
|
|
96
|
+
const parts = [];
|
|
97
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
98
|
+
const callSignatures = type.getCallSignatures();
|
|
99
|
+
const members = type.getProperties();
|
|
100
|
+
if (callSignatures.length > 0 && members.length > 0) throw new Error("[@vue.ts/complex-types] You may not use old style `defineEmits` and `defineEmits` shorthand together.");
|
|
101
|
+
if (members.length > 0) parts.push(...this.printEventsByMembers(members));
|
|
102
|
+
else parts.push(...this.printEventsByCallSignatures(callSignatures));
|
|
103
|
+
return `[${parts.join(", ")}]`;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/core/transformers/defineEmits.ts
|
|
109
|
+
const transformDefineEmits = (printer, s, id) => {
|
|
110
|
+
const normalizedFilepath = normalizePath(id);
|
|
111
|
+
const language = getLanguage();
|
|
112
|
+
const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
|
|
113
|
+
const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
|
|
114
|
+
const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
|
|
115
|
+
if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
|
|
116
|
+
const scriptSetupDefineEmitsNode = language.findNodeByRange(scriptSetupAst, (scriptSetupRanges) => scriptSetupRanges.defineEmits?.callExp);
|
|
117
|
+
const virtualFileDefineEmitsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Emit");
|
|
118
|
+
if (!scriptSetupDefineEmitsNode || !virtualFileDefineEmitsTypeNode) return;
|
|
119
|
+
const offset = scriptSetupBlock.startTagEnd;
|
|
120
|
+
const defineEmitsRuntimeArg = ts.isCallExpression(scriptSetupDefineEmitsNode) && scriptSetupDefineEmitsNode.arguments[0];
|
|
121
|
+
if (defineEmitsRuntimeArg) throw new Error("[@vue.ts/complex-types] `defineEmits` cannot accept both runtime argument and type argument.");
|
|
122
|
+
const tokens = scriptSetupDefineEmitsNode.getChildren(scriptSetupAst);
|
|
123
|
+
const lessThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.LessThanToken);
|
|
124
|
+
const greaterThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.GreaterThanToken);
|
|
125
|
+
const openParenToken = tokens.find((t) => t.kind === ts.SyntaxKind.OpenParenToken);
|
|
126
|
+
if (!lessThanToken || !greaterThanToken || !openParenToken) return;
|
|
127
|
+
const defineEmitsTypeArgRange = [offset + lessThanToken.pos, offset + greaterThanToken.end];
|
|
128
|
+
const runtimeArgPos = offset + openParenToken.end;
|
|
129
|
+
const printedRuntimeArg = printer.printEventsRuntimeArg(virtualFileDefineEmitsTypeNode);
|
|
130
|
+
s.remove(...defineEmitsTypeArgRange);
|
|
131
|
+
s.appendRight(runtimeArgPos, printedRuntimeArg);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/core/transformers/defineProps.ts
|
|
136
|
+
const transformDefineProps = (printer, s, id) => {
|
|
137
|
+
const normalizedFilepath = normalizePath(id);
|
|
138
|
+
const language = getLanguage();
|
|
139
|
+
const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
|
|
140
|
+
const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
|
|
141
|
+
const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
|
|
142
|
+
if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
|
|
143
|
+
const scriptSetupDefinePropsTypeRange = language.parseScriptSetupRanges(scriptSetupAst).defineProps?.typeArg;
|
|
144
|
+
const virtualFileDefinePropsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Props");
|
|
145
|
+
if (!scriptSetupDefinePropsTypeRange || !virtualFileDefinePropsTypeNode) return;
|
|
146
|
+
const printedType = printer.printPropsTypeArg(virtualFileDefinePropsTypeNode);
|
|
147
|
+
const offset = scriptSetupBlock.startTagEnd;
|
|
148
|
+
s.overwrite(offset + scriptSetupDefinePropsTypeRange.start, offset + scriptSetupDefinePropsTypeRange.end, printedType);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/core/transformers/index.ts
|
|
153
|
+
const transformers = [["defineEmits", transformDefineEmits], ["defineProps", transformDefineProps]];
|
|
154
|
+
const getTransformers = (options) => transformers.filter(([key]) => !!options[key]);
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/core/transform.ts
|
|
158
|
+
function transform(code, id, options) {
|
|
159
|
+
const s = new MagicString(code);
|
|
160
|
+
const language = getLanguage();
|
|
161
|
+
const typeChecker = language.__internal__.typeChecker;
|
|
162
|
+
const printer = new Printer(typeChecker);
|
|
163
|
+
const transformers$1 = getTransformers(options);
|
|
164
|
+
for (const [, transform$1] of transformers$1) transform$1(printer, s, id);
|
|
165
|
+
return {
|
|
166
|
+
code: s.toString(),
|
|
167
|
+
map: s.generateMap({ hires: true })
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/index.ts
|
|
173
|
+
const unpluginFactory = (options = {}) => {
|
|
174
|
+
const resolvedOptions = resolveOptions(options);
|
|
175
|
+
const filter = createFilter(resolvedOptions.include, resolvedOptions.exclude);
|
|
176
|
+
return {
|
|
177
|
+
name: "@vue.ts/complex-types",
|
|
178
|
+
buildStart() {
|
|
179
|
+
const resolvedOptions$1 = resolveOptions(options);
|
|
180
|
+
ensureLanguage(resolvedOptions$1.tsconfigPath);
|
|
181
|
+
},
|
|
182
|
+
transform(code, id) {
|
|
183
|
+
if (!filter(id)) return;
|
|
184
|
+
return transform(code, id, resolvedOptions);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
189
|
+
var src_default = unplugin;
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
export { src_default, unplugin, unpluginFactory };
|
|
@@ -2,7 +2,7 @@ import MagicString from "magic-string";
|
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
import { FilterPattern } from "@rollup/pluginutils";
|
|
4
4
|
|
|
5
|
-
//#region ../
|
|
5
|
+
//#region ../shared/src/types.d.ts
|
|
6
6
|
interface BaseOptions {
|
|
7
7
|
include?: FilterPattern;
|
|
8
8
|
exclude?: FilterPattern;
|
|
@@ -12,27 +12,12 @@ interface BaseOptions {
|
|
|
12
12
|
declare class Printer {
|
|
13
13
|
private checker;
|
|
14
14
|
constructor(checker: ts.TypeChecker);
|
|
15
|
-
private getBaseType;
|
|
16
15
|
private typeToString;
|
|
17
|
-
private
|
|
18
|
-
private
|
|
19
|
-
private
|
|
20
|
-
private
|
|
21
|
-
|
|
22
|
-
private printInterfaceDeclaration;
|
|
23
|
-
private printEnumDeclaration;
|
|
24
|
-
private printClassDeclaration;
|
|
25
|
-
private printFunctionTypeNode;
|
|
26
|
-
private printConstructorTypeNode;
|
|
27
|
-
private printTypeQueryNode;
|
|
28
|
-
private printArrayTypeNode;
|
|
29
|
-
private printTupleTypeNode;
|
|
30
|
-
private printConditionalTypeNode;
|
|
31
|
-
private printIndexedAccessTypeNode;
|
|
32
|
-
private printHeritageClause;
|
|
33
|
-
private printImportSpecifier;
|
|
34
|
-
private isKeywordTypeNode;
|
|
35
|
-
print(node: ts.Node): string;
|
|
16
|
+
private printOuterUnionOrIntersection;
|
|
17
|
+
private isUnionUndefined;
|
|
18
|
+
private printPrimitiveType;
|
|
19
|
+
private printType;
|
|
20
|
+
printPropsTypeArg(node: ts.Node): string;
|
|
36
21
|
private printEventsByCallSignatures;
|
|
37
22
|
private printEventsByMembers;
|
|
38
23
|
printEventsRuntimeArg(node: ts.Node): string;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms } from "./types-
|
|
1
|
+
import { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms } from "./types-afZXKowK.js";
|
|
2
2
|
import "./types-u_Hr5uXA.js";
|
|
3
3
|
export { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms };
|
package/dist/vite.d.ts
CHANGED
package/dist/vite.js
CHANGED
package/dist/webpack.d.ts
CHANGED
package/dist/webpack.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vue.ts/complex-types",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.3",
|
|
4
4
|
"author": "Ray <i@mk1.io> (@so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Resolve complex types in Vue SFCs.",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
"@nuxt/kit": "^3.17.6",
|
|
66
66
|
"magic-string": "^0.30.17",
|
|
67
67
|
"unplugin": "^2.3.5",
|
|
68
|
-
"@vue.ts/language": "1.0.0-beta.
|
|
69
|
-
"@vue.ts/
|
|
68
|
+
"@vue.ts/language": "1.0.0-beta.3",
|
|
69
|
+
"@vue.ts/shared": "1.0.0-beta.3"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@nuxt/schema": "^3.17.6",
|
|
@@ -83,9 +83,9 @@
|
|
|
83
83
|
"esbuild": "*",
|
|
84
84
|
"rolldown": "*",
|
|
85
85
|
"rollup": "^3",
|
|
86
|
-
"typescript": "
|
|
86
|
+
"typescript": ">=5.5",
|
|
87
87
|
"vite": ">=3",
|
|
88
|
-
"vue": "
|
|
88
|
+
"vue": ">=3",
|
|
89
89
|
"webpack": "^4 || ^5"
|
|
90
90
|
},
|
|
91
91
|
"peerDependenciesMeta": {
|
package/dist/src-CI8-WIu4.js
DELETED
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
import { createUnplugin } from "unplugin";
|
|
2
|
-
import { createFilter, normalizePath } from "@vue.ts/common";
|
|
3
|
-
import { ensureLanguage, getLanguage } from "@vue.ts/language";
|
|
4
|
-
import MagicString from "magic-string";
|
|
5
|
-
import ts from "typescript";
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
|
|
8
|
-
//#region src/core/utils.ts
|
|
9
|
-
const resolveOptions = (rawOptions) => ({
|
|
10
|
-
include: rawOptions.include ?? ["**/*.vue"],
|
|
11
|
-
exclude: rawOptions.exclude ?? ["node_modules/**"],
|
|
12
|
-
tsconfigPath: rawOptions.tsconfigPath ?? join(process.cwd(), "tsconfig.json"),
|
|
13
|
-
defineEmits: rawOptions.defineEmits ?? true,
|
|
14
|
-
defineProps: rawOptions.defineProps ?? true
|
|
15
|
-
});
|
|
16
|
-
const quotesReg = /"/g;
|
|
17
|
-
const escapeQuotes = (s) => s.replace(quotesReg, "\\\"");
|
|
18
|
-
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/core/printer.ts
|
|
21
|
-
var Printer = class {
|
|
22
|
-
constructor(checker) {
|
|
23
|
-
this.checker = checker;
|
|
24
|
-
}
|
|
25
|
-
getBaseType(nodeOrType) {
|
|
26
|
-
if ("kind" in nodeOrType) nodeOrType = this.checker.getTypeAtLocation(nodeOrType);
|
|
27
|
-
return this.checker.getBaseConstraintOfType(nodeOrType) ?? this.checker.getBaseTypeOfLiteralType(nodeOrType);
|
|
28
|
-
}
|
|
29
|
-
typeToString(type) {
|
|
30
|
-
return this.checker.typeToString(type, void 0, ts.TypeFormatFlags.NoTruncation);
|
|
31
|
-
}
|
|
32
|
-
printIntersectionTypeNode(node) {
|
|
33
|
-
return node.types.map((t) => this.print(t)).join(" & ");
|
|
34
|
-
}
|
|
35
|
-
printUnionTypeNode(node) {
|
|
36
|
-
return node.types.map((t) => this.print(t)).join(" | ");
|
|
37
|
-
}
|
|
38
|
-
printTypeLiteralNode(node) {
|
|
39
|
-
const parts = ["{"];
|
|
40
|
-
for (const member of node.members) if (ts.isPropertySignature(member)) {
|
|
41
|
-
const stringBaseType = member.type ? this.typeToString(this.getBaseType(member.type)) : "any";
|
|
42
|
-
parts.push([
|
|
43
|
-
member.name.getText(),
|
|
44
|
-
member.questionToken?.getText(),
|
|
45
|
-
": ",
|
|
46
|
-
stringBaseType
|
|
47
|
-
].filter(Boolean).join(""));
|
|
48
|
-
}
|
|
49
|
-
parts.push("}");
|
|
50
|
-
return parts.join("\n");
|
|
51
|
-
}
|
|
52
|
-
printTypeByType(node) {
|
|
53
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
54
|
-
const properties = type.getProperties();
|
|
55
|
-
const parts = ["{"];
|
|
56
|
-
const isMapped = ts.isMappedTypeNode(node);
|
|
57
|
-
for (const property of properties) {
|
|
58
|
-
const questionToken = isMapped ? node.questionToken?.getText() ?? "" : property.flags & ts.SymbolFlags.Optional ? "?" : "";
|
|
59
|
-
const valueType = this.checker.getTypeOfSymbol(property);
|
|
60
|
-
const stringValueType = this.typeToString(this.getBaseType(valueType));
|
|
61
|
-
parts.push(`${this.checker.symbolToString(property)}${questionToken}: ${stringValueType}`);
|
|
62
|
-
}
|
|
63
|
-
parts.push("}");
|
|
64
|
-
return parts.join("\n");
|
|
65
|
-
}
|
|
66
|
-
printIdentifier(node) {
|
|
67
|
-
const symbol = this.checker.getSymbolAtLocation(node);
|
|
68
|
-
if (!symbol) return node.getText();
|
|
69
|
-
const declarations = symbol.getDeclarations();
|
|
70
|
-
if (!declarations || declarations.length === 0) return node.getText();
|
|
71
|
-
return this.print(declarations[0]);
|
|
72
|
-
}
|
|
73
|
-
printInterfaceDeclaration(node) {
|
|
74
|
-
const parts = ["{"];
|
|
75
|
-
for (const member of node.members) if (ts.isPropertySignature(member)) {
|
|
76
|
-
const stringBaseType = member.type ? this.print(member.type) : "any";
|
|
77
|
-
parts.push([
|
|
78
|
-
member.name?.getText(),
|
|
79
|
-
member.questionToken?.getText(),
|
|
80
|
-
": ",
|
|
81
|
-
stringBaseType
|
|
82
|
-
].filter(Boolean).join(""));
|
|
83
|
-
} else if (ts.isMethodSignature(member)) {
|
|
84
|
-
const signature = this.checker.getSignatureFromDeclaration(member);
|
|
85
|
-
const returnType = signature ? this.typeToString(signature.getReturnType()) : "any";
|
|
86
|
-
const parameters = member.parameters.map((param) => {
|
|
87
|
-
const paramType = param.type ? this.typeToString(this.getBaseType(param.type)) : "any";
|
|
88
|
-
return `${param.name.getText()}: ${paramType}`;
|
|
89
|
-
}).join(", ");
|
|
90
|
-
parts.push(`${member.name?.getText()}(${parameters}): ${returnType}`);
|
|
91
|
-
}
|
|
92
|
-
parts.push("}");
|
|
93
|
-
let result = parts.join("\n");
|
|
94
|
-
if (node.heritageClauses && node.heritageClauses.length > 0) result = node.heritageClauses.map((clause) => this.print(clause)).join(" & ");
|
|
95
|
-
return result;
|
|
96
|
-
}
|
|
97
|
-
printEnumDeclaration(node) {
|
|
98
|
-
const parts = ["{"];
|
|
99
|
-
for (const member of node.members) {
|
|
100
|
-
const name = member.name.getText();
|
|
101
|
-
if (member.initializer) {
|
|
102
|
-
const value = member.initializer.getText();
|
|
103
|
-
parts.push(`${name}: ${value}`);
|
|
104
|
-
} else parts.push(`${name}: number`);
|
|
105
|
-
}
|
|
106
|
-
parts.push("}");
|
|
107
|
-
return parts.join("\n");
|
|
108
|
-
}
|
|
109
|
-
printClassDeclaration(node) {
|
|
110
|
-
const parts = ["{"];
|
|
111
|
-
for (const member of node.members) if (ts.isPropertyDeclaration(member)) {
|
|
112
|
-
const name = member.name?.getText();
|
|
113
|
-
const typeAnnotation = member.type ? this.typeToString(this.getBaseType(member.type)) : "any";
|
|
114
|
-
const optional = member.questionToken ? "?" : "";
|
|
115
|
-
parts.push(`${name}${optional}: ${typeAnnotation}`);
|
|
116
|
-
} else if (ts.isMethodDeclaration(member)) {
|
|
117
|
-
const name = member.name?.getText();
|
|
118
|
-
const signature = this.checker.getSignatureFromDeclaration(member);
|
|
119
|
-
const returnType = signature ? this.typeToString(signature.getReturnType()) : "any";
|
|
120
|
-
const parameters = member.parameters.map((param) => {
|
|
121
|
-
const paramType = param.type ? this.typeToString(this.getBaseType(param.type)) : "any";
|
|
122
|
-
return `${param.name.getText()}: ${paramType}`;
|
|
123
|
-
}).join(", ");
|
|
124
|
-
parts.push(`${name}(${parameters}): ${returnType}`);
|
|
125
|
-
} else if (ts.isConstructorDeclaration(member)) {
|
|
126
|
-
const parameters = member.parameters.map((param) => {
|
|
127
|
-
const paramType = param.type ? this.typeToString(this.getBaseType(param.type)) : "any";
|
|
128
|
-
return `${param.name.getText()}: ${paramType}`;
|
|
129
|
-
}).join(", ");
|
|
130
|
-
parts.push(`constructor(${parameters}): void`);
|
|
131
|
-
}
|
|
132
|
-
parts.push("}");
|
|
133
|
-
return parts.join("\n");
|
|
134
|
-
}
|
|
135
|
-
printFunctionTypeNode(node) {
|
|
136
|
-
const parameters = node.parameters.map((param) => {
|
|
137
|
-
const paramType = param.type ? this.print(param.type) : "any";
|
|
138
|
-
const optional = param.questionToken ? "?" : "";
|
|
139
|
-
const rest = param.dotDotDotToken ? "..." : "";
|
|
140
|
-
return `${rest}${param.name.getText()}${optional}: ${paramType}`;
|
|
141
|
-
}).join(", ");
|
|
142
|
-
const returnType = node.type ? this.print(node.type) : "any";
|
|
143
|
-
return `(${parameters}) => ${returnType}`;
|
|
144
|
-
}
|
|
145
|
-
printConstructorTypeNode(node) {
|
|
146
|
-
const parameters = node.parameters.map((param) => {
|
|
147
|
-
const paramType = param.type ? this.print(param.type) : "any";
|
|
148
|
-
const optional = param.questionToken ? "?" : "";
|
|
149
|
-
const rest = param.dotDotDotToken ? "..." : "";
|
|
150
|
-
return `${rest}${param.name.getText()}${optional}: ${paramType}`;
|
|
151
|
-
}).join(", ");
|
|
152
|
-
const returnType = node.type ? this.print(node.type) : "any";
|
|
153
|
-
return `new (${parameters}) => ${returnType}`;
|
|
154
|
-
}
|
|
155
|
-
printTypeQueryNode(node) {
|
|
156
|
-
const exprName = node.exprName.getText();
|
|
157
|
-
return `typeof ${exprName}`;
|
|
158
|
-
}
|
|
159
|
-
printArrayTypeNode(node) {
|
|
160
|
-
return `${this.print(node.elementType)}[]`;
|
|
161
|
-
}
|
|
162
|
-
printTupleTypeNode(node) {
|
|
163
|
-
const elements = node.elements.map((element) => {
|
|
164
|
-
if (ts.isRestTypeNode(element)) return `...${this.print(element.type)}`;
|
|
165
|
-
if (ts.isOptionalTypeNode(element)) return `${this.print(element.type)}?`;
|
|
166
|
-
if (ts.isNamedTupleMember(element)) {
|
|
167
|
-
const type = this.print(element.type);
|
|
168
|
-
const optional = element.questionToken ? "?" : "";
|
|
169
|
-
const rest = element.dotDotDotToken ? "..." : "";
|
|
170
|
-
return `${rest}${element.name.getText()}${optional}: ${type}`;
|
|
171
|
-
}
|
|
172
|
-
return this.print(element);
|
|
173
|
-
});
|
|
174
|
-
return `[${elements.join(", ")}]`;
|
|
175
|
-
}
|
|
176
|
-
printConditionalTypeNode(node) {
|
|
177
|
-
const checkType = this.print(node.checkType);
|
|
178
|
-
const extendsType = this.print(node.extendsType);
|
|
179
|
-
const trueType = this.print(node.trueType);
|
|
180
|
-
const falseType = this.print(node.falseType);
|
|
181
|
-
return `${checkType} extends ${extendsType} ? ${trueType} : ${falseType}`;
|
|
182
|
-
}
|
|
183
|
-
printIndexedAccessTypeNode(node) {
|
|
184
|
-
const objectType = this.print(node.objectType);
|
|
185
|
-
const indexType = this.print(node.indexType);
|
|
186
|
-
return `${objectType}[${indexType}]`;
|
|
187
|
-
}
|
|
188
|
-
printHeritageClause(node) {
|
|
189
|
-
if (node.token === ts.SyntaxKind.ExtendsKeyword) {
|
|
190
|
-
const parts = [];
|
|
191
|
-
for (const type of node.types) if (ts.isExpressionWithTypeArguments(type)) if (type.expression && type.typeArguments) parts.push(this.print(type));
|
|
192
|
-
else parts.push(this.print(type.expression));
|
|
193
|
-
return parts.join(" & ");
|
|
194
|
-
}
|
|
195
|
-
return "";
|
|
196
|
-
}
|
|
197
|
-
printImportSpecifier(node) {
|
|
198
|
-
const name = node.propertyName ? node.propertyName.getText() : node.name.getText();
|
|
199
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
200
|
-
if (!symbol) return name;
|
|
201
|
-
const aliasedSymbol = this.checker.getAliasedSymbol(symbol);
|
|
202
|
-
const targetSymbol = aliasedSymbol || symbol;
|
|
203
|
-
const declarations = targetSymbol.getDeclarations();
|
|
204
|
-
if (!declarations || declarations.length === 0) return name;
|
|
205
|
-
const declaration = declarations.find((decl) => ts.isTypeAliasDeclaration(decl) || ts.isInterfaceDeclaration(decl) || ts.isClassDeclaration(decl) || ts.isEnumDeclaration(decl) || ts.isVariableDeclaration(decl));
|
|
206
|
-
if (declaration) return this.print(declaration);
|
|
207
|
-
return name;
|
|
208
|
-
}
|
|
209
|
-
isKeywordTypeNode(node) {
|
|
210
|
-
return node.kind === ts.SyntaxKind.StringKeyword || node.kind === ts.SyntaxKind.NumberKeyword || node.kind === ts.SyntaxKind.BooleanKeyword || node.kind === ts.SyntaxKind.ObjectKeyword || node.kind === ts.SyntaxKind.UndefinedKeyword || node.kind === ts.SyntaxKind.NullKeyword || node.kind === ts.SyntaxKind.VoidKeyword || node.kind === ts.SyntaxKind.AnyKeyword || node.kind === ts.SyntaxKind.UnknownKeyword || node.kind === ts.SyntaxKind.NeverKeyword || node.kind === ts.SyntaxKind.SymbolKeyword || node.kind === ts.SyntaxKind.BigIntKeyword;
|
|
211
|
-
}
|
|
212
|
-
print(node) {
|
|
213
|
-
if (ts.isIdentifier(node)) return this.printIdentifier(node);
|
|
214
|
-
else if (ts.isIntersectionTypeNode(node)) return this.printIntersectionTypeNode(node);
|
|
215
|
-
else if (ts.isUnionTypeNode(node)) return this.printUnionTypeNode(node);
|
|
216
|
-
else if (ts.isTypeLiteralNode(node)) return this.printTypeLiteralNode(node);
|
|
217
|
-
else if (ts.isTypeReferenceNode(node)) return this.print(node.typeName);
|
|
218
|
-
else if (ts.isInterfaceDeclaration(node)) return this.printInterfaceDeclaration(node);
|
|
219
|
-
else if (ts.isTypeAliasDeclaration(node)) return node.type ? this.print(node.type) : "any";
|
|
220
|
-
else if (ts.isEnumDeclaration(node)) return this.printEnumDeclaration(node);
|
|
221
|
-
else if (ts.isClassDeclaration(node)) return this.printClassDeclaration(node);
|
|
222
|
-
else if (ts.isFunctionTypeNode(node)) return this.printFunctionTypeNode(node);
|
|
223
|
-
else if (ts.isConstructorTypeNode(node)) return this.printConstructorTypeNode(node);
|
|
224
|
-
else if (ts.isTypeQueryNode(node)) return this.printTypeQueryNode(node);
|
|
225
|
-
else if (ts.isArrayTypeNode(node)) return this.printArrayTypeNode(node);
|
|
226
|
-
else if (ts.isTupleTypeNode(node)) return this.printTupleTypeNode(node);
|
|
227
|
-
else if (ts.isConditionalTypeNode(node)) return this.printConditionalTypeNode(node);
|
|
228
|
-
else if (ts.isIndexedAccessTypeNode(node)) return this.printIndexedAccessTypeNode(node);
|
|
229
|
-
else if (ts.isParenthesizedTypeNode(node)) return this.print(node.type);
|
|
230
|
-
else if (ts.isHeritageClause(node)) return this.printHeritageClause(node);
|
|
231
|
-
else if (ts.isTypeAliasDeclaration(node) && node.type) return this.print(node.type);
|
|
232
|
-
else if (ts.isInterfaceDeclaration(node)) return this.printInterfaceDeclaration(node);
|
|
233
|
-
else if (ts.isVariableDeclaration(node) && node.type) return this.print(node.type);
|
|
234
|
-
else if (ts.isImportSpecifier(node)) return this.printImportSpecifier(node);
|
|
235
|
-
else if (ts.isTypeParameterDeclaration(node)) {
|
|
236
|
-
if (node.constraint) return this.print(node.constraint);
|
|
237
|
-
else if (node.default) return this.print(node.default);
|
|
238
|
-
return "{}";
|
|
239
|
-
} else if (ts.isParameter(node) && node.type) return this.print(node.type);
|
|
240
|
-
else if (ts.isLiteralTypeNode(node) || ts.isThisTypeNode(node) || ts.isPropertyAccessExpression(node) || this.isKeywordTypeNode(node)) return node.getText();
|
|
241
|
-
else if (ts.isMappedTypeNode(node) || ts.isExpressionWithTypeArguments(node)) return this.printTypeByType(node);
|
|
242
|
-
else {
|
|
243
|
-
console.error(`[@vue.ts/complex-types] \`${ts.SyntaxKind[node.kind]}\` is not supported.`);
|
|
244
|
-
return node.getText();
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
printEventsByCallSignatures(callSignatures) {
|
|
248
|
-
return callSignatures.map((c) => {
|
|
249
|
-
const parameters = c.getParameters();
|
|
250
|
-
const event = parameters[0];
|
|
251
|
-
return this.typeToString(this.checker.getTypeOfSymbol(event));
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
printEventsByMembers(members) {
|
|
255
|
-
return members.map((m) => `"${escapeQuotes(m.getName())}"`);
|
|
256
|
-
}
|
|
257
|
-
printEventsRuntimeArg(node) {
|
|
258
|
-
const parts = [];
|
|
259
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
260
|
-
const callSignatures = type.getCallSignatures();
|
|
261
|
-
const members = type.getProperties();
|
|
262
|
-
if (callSignatures.length > 0 && members.length > 0) throw new Error("[@vue.ts/complex-types] You may not use old style `defineEmits` and `defineEmits` shorthand together.");
|
|
263
|
-
if (members.length > 0) parts.push(...this.printEventsByMembers(members));
|
|
264
|
-
else parts.push(...this.printEventsByCallSignatures(callSignatures));
|
|
265
|
-
return `[${parts.join(", ")}]`;
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
//#endregion
|
|
270
|
-
//#region src/core/transformers/defineEmits.ts
|
|
271
|
-
const transformDefineEmits = (printer, s, id) => {
|
|
272
|
-
const normalizedFilepath = normalizePath(id);
|
|
273
|
-
const language = getLanguage();
|
|
274
|
-
const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
|
|
275
|
-
const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
|
|
276
|
-
const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
|
|
277
|
-
if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
|
|
278
|
-
const scriptSetupDefineEmitsNode = language.findNodeByRange(scriptSetupAst, (scriptSetupRanges) => scriptSetupRanges.defineEmits?.callExp);
|
|
279
|
-
const virtualFileDefineEmitsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Emit");
|
|
280
|
-
if (!scriptSetupDefineEmitsNode || !virtualFileDefineEmitsTypeNode) return;
|
|
281
|
-
const offset = scriptSetupBlock.startTagEnd;
|
|
282
|
-
const defineEmitsRuntimeArg = ts.isCallExpression(scriptSetupDefineEmitsNode) && scriptSetupDefineEmitsNode.arguments[0];
|
|
283
|
-
if (defineEmitsRuntimeArg) throw new Error("[@vue.ts/complex-types] `defineEmits` cannot accept both runtime argument and type argument.");
|
|
284
|
-
const tokens = scriptSetupDefineEmitsNode.getChildren(scriptSetupAst);
|
|
285
|
-
const lessThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.LessThanToken);
|
|
286
|
-
const greaterThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.GreaterThanToken);
|
|
287
|
-
const openParenToken = tokens.find((t) => t.kind === ts.SyntaxKind.OpenParenToken);
|
|
288
|
-
if (!lessThanToken || !greaterThanToken || !openParenToken) return;
|
|
289
|
-
const defineEmitsTypeArgRange = [offset + lessThanToken.pos, offset + greaterThanToken.end];
|
|
290
|
-
const runtimeArgPos = offset + openParenToken.end;
|
|
291
|
-
const printedRuntimeArg = printer.printEventsRuntimeArg(virtualFileDefineEmitsTypeNode);
|
|
292
|
-
s.remove(...defineEmitsTypeArgRange);
|
|
293
|
-
s.appendRight(runtimeArgPos, printedRuntimeArg);
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
//#endregion
|
|
297
|
-
//#region src/core/transformers/defineProps.ts
|
|
298
|
-
const transformDefineProps = (printer, s, id) => {
|
|
299
|
-
const normalizedFilepath = normalizePath(id);
|
|
300
|
-
const language = getLanguage();
|
|
301
|
-
const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
|
|
302
|
-
const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
|
|
303
|
-
const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
|
|
304
|
-
if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
|
|
305
|
-
const scriptSetupDefinePropsTypeRange = language.parseScriptSetupRanges(scriptSetupAst).defineProps?.typeArg;
|
|
306
|
-
const virtualFileDefinePropsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Props");
|
|
307
|
-
if (!scriptSetupDefinePropsTypeRange || !virtualFileDefinePropsTypeNode) return;
|
|
308
|
-
const printedType = printer.print(virtualFileDefinePropsTypeNode);
|
|
309
|
-
const offset = scriptSetupBlock.startTagEnd;
|
|
310
|
-
s.overwrite(offset + scriptSetupDefinePropsTypeRange.start, offset + scriptSetupDefinePropsTypeRange.end, printedType);
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
//#endregion
|
|
314
|
-
//#region src/core/transformers/index.ts
|
|
315
|
-
const transformers = [["defineEmits", transformDefineEmits], ["defineProps", transformDefineProps]];
|
|
316
|
-
const getTransformers = (options) => transformers.filter(([key]) => !!options[key]);
|
|
317
|
-
|
|
318
|
-
//#endregion
|
|
319
|
-
//#region src/core/transform.ts
|
|
320
|
-
function transform(code, id, options) {
|
|
321
|
-
const s = new MagicString(code);
|
|
322
|
-
const language = getLanguage();
|
|
323
|
-
const typeChecker = language.__internal__.typeChecker;
|
|
324
|
-
const printer = new Printer(typeChecker);
|
|
325
|
-
const transformers$1 = getTransformers(options);
|
|
326
|
-
for (const [, transform$1] of transformers$1) transform$1(printer, s, id);
|
|
327
|
-
return {
|
|
328
|
-
code: s.toString(),
|
|
329
|
-
map: s.generateMap({ hires: true })
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
//#endregion
|
|
334
|
-
//#region src/index.ts
|
|
335
|
-
const unpluginFactory = (options = {}) => {
|
|
336
|
-
const resolvedOptions = resolveOptions(options);
|
|
337
|
-
const filter = createFilter(resolvedOptions.include, resolvedOptions.exclude);
|
|
338
|
-
return {
|
|
339
|
-
name: "@vue.ts/complex-types",
|
|
340
|
-
buildStart() {
|
|
341
|
-
const resolvedOptions$1 = resolveOptions(options);
|
|
342
|
-
ensureLanguage(resolvedOptions$1.tsconfigPath);
|
|
343
|
-
},
|
|
344
|
-
transform(code, id) {
|
|
345
|
-
if (!filter(id)) return;
|
|
346
|
-
return transform(code, id, resolvedOptions);
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
};
|
|
350
|
-
const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
351
|
-
var src_default = unplugin;
|
|
352
|
-
|
|
353
|
-
//#endregion
|
|
354
|
-
export { src_default, unplugin, unpluginFactory };
|