@vue.ts/complex-types 1.0.0-beta.1 → 1.0.0-beta.10

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![NPM version](https://img.shields.io/npm/v/@vue.ts/complex-types?color=a1b858&label=)](https://www.npmjs.com/package/@vue.ts/complex-types)
4
4
 
5
- Use [`@vue/language-core`](https://github.com/vuejs/language-tools/tree/master/packages/language-core) to support complex types for Vue Macros.
5
+ Use [`@vue/language-core`](https://github.com/vuejs/language-tools/tree/master/packages/language-core) to support complex types for Vue's Macros.
6
6
 
7
7
  For example: fixes https://github.com/vuejs/core/issues/8286.
8
8
 
@@ -14,10 +14,6 @@ $ yarn add -D @vue.ts/complex-types
14
14
  $ pnpm add -D @vue.ts/complex-types
15
15
  ```
16
16
 
17
- ## TODOs
18
-
19
- - [ ] Add more tests
20
-
21
17
  ## 🚀 Usage
22
18
 
23
19
  <details>
package/dist/astro.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
 
3
3
  //#region src/astro.d.ts
4
4
  declare const _default: (options: Options) => {
package/dist/astro.js CHANGED
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createVitePlugin } from "unplugin";
3
3
 
4
4
  //#region src/astro.ts
package/dist/esbuild.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  import * as esbuild0 from "esbuild";
4
4
 
package/dist/esbuild.js CHANGED
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createEsbuildPlugin } from "unplugin";
3
3
 
4
4
  //#region src/esbuild.ts
package/dist/farm.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  import * as _farmfe_core0 from "@farmfe/core";
4
4
 
package/dist/farm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createFarmPlugin } from "unplugin";
3
3
 
4
4
  //#region src/farm.ts
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import * as unplugin0 from "unplugin";
3
3
  import { UnpluginFactory } from "unplugin";
4
4
 
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { src_default, unplugin, unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { n as unplugin, r as unpluginFactory, t as src_default } from "./src-Ci8DJn1t.js";
2
2
 
3
3
  export { src_default as default, unplugin, unpluginFactory };
package/dist/nuxt.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  import * as _nuxt_schema0 from "@nuxt/schema";
4
4
 
package/dist/nuxt.js CHANGED
@@ -1,6 +1,6 @@
1
- import "./src-CI8-WIu4.js";
2
- import { vite_default } from "./vite-Dyi1m2wg.js";
3
- import { webpack_default } from "./webpack-CFfuQfGw.js";
1
+ import "./src-Ci8DJn1t.js";
2
+ import { t as vite_default } from "./vite-D-Xfh8NL.js";
3
+ import { t as webpack_default } from "./webpack-By_BEclB.js";
4
4
  import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";
5
5
 
6
6
  //#region src/nuxt.ts
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  import * as rolldown0 from "rolldown";
4
4
 
package/dist/rolldown.js CHANGED
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createRolldownPlugin } from "unplugin";
3
3
 
4
4
  //#region src/rolldown.ts
package/dist/rollup.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  import * as rollup0 from "rollup";
4
4
 
package/dist/rollup.js CHANGED
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createRollupPlugin } from "unplugin";
3
3
 
4
4
  //#region src/rollup.ts
package/dist/rspack.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
 
4
4
  //#region src/rspack.d.ts
package/dist/rspack.js CHANGED
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createRspackPlugin } from "unplugin";
3
3
 
4
4
  //#region src/rspack.ts
@@ -0,0 +1,228 @@
1
+ import { createUnplugin } from "unplugin";
2
+ import { ensureLanguage, getLanguage } from "@vue.ts/language";
3
+ import { createFilter } from "unplugin-utils";
4
+ import MagicString from "magic-string";
5
+ import ts from "typescript";
6
+ import { join } from "node:path";
7
+ import { defu } from "defu";
8
+ import { normalizePath } from "@vue.ts/shared";
9
+ import { isReservedProp } from "@vue/shared";
10
+
11
+ //#region src/core/utils.ts
12
+ const defaultOptions = {
13
+ include: ["**/*.vue"],
14
+ exclude: ["node_modules/**"],
15
+ tsconfigPath: join(process.cwd(), "tsconfig.json"),
16
+ defineEmits: true,
17
+ defineProps: true
18
+ };
19
+ const resolveOptions = (rawOptions) => defu(rawOptions, defaultOptions);
20
+ const quotesReg = /"/g;
21
+ const escapeQuotes = (s) => s.replace(quotesReg, "\\\"");
22
+
23
+ //#endregion
24
+ //#region src/core/printer.ts
25
+ var Printer = class {
26
+ constructor(checker) {
27
+ this.checker = checker;
28
+ }
29
+ typeToString(type) {
30
+ return this.checker.typeToString(type, void 0, ts.TypeFormatFlags.NoTruncation);
31
+ }
32
+ printUnionOrIntersection(type, separator, isPropertyBlacklisted, inner) {
33
+ return [...new Set(type.types.map((t) => this.printType(t, isPropertyBlacklisted, inner)).filter(Boolean))].join(separator);
34
+ }
35
+ isSymbolOptional(symbol) {
36
+ return !!(symbol.flags & ts.SymbolFlags.Optional);
37
+ }
38
+ printConditionType(type, isPropertyBlacklisted, inner) {
39
+ const { trueType, falseType } = type.root.node;
40
+ const trueTypeNode = this.checker.getTypeAtLocation(trueType);
41
+ const falseTypeNode = this.checker.getTypeAtLocation(falseType);
42
+ return `${this.printType(trueTypeNode, isPropertyBlacklisted, inner)} | ${this.printType(falseTypeNode, isPropertyBlacklisted, inner)}`;
43
+ }
44
+ printPrimitiveType(type) {
45
+ if (type.flags & ts.TypeFlags.BooleanLiteral) return "boolean";
46
+ else if (type.flags & ts.TypeFlags.String || type.isStringLiteral()) return "string";
47
+ else if (type.flags & ts.TypeFlags.Number || type.isNumberLiteral()) return "number";
48
+ else if (type.flags & ts.TypeFlags.BigInt) return "bigint";
49
+ else if (type.flags & ts.TypeFlags.Any) return "any";
50
+ else if (type.flags & ts.TypeFlags.Unknown) return "unknown";
51
+ else if (type.flags & ts.TypeFlags.Null) return "null";
52
+ return "";
53
+ }
54
+ printType(type, isPropertyBlacklisted, inner = false) {
55
+ if (type.isUnion()) return this.printUnionOrIntersection(type, " | ", isPropertyBlacklisted, inner);
56
+ else if (type.isIntersection()) return this.printUnionOrIntersection(type, " & ", isPropertyBlacklisted, inner);
57
+ if (this.checker.isArrayType(type)) return "Array";
58
+ else if (type.flags & ts.TypeFlags.Object) {
59
+ const decl = type.getSymbol()?.declarations?.[0];
60
+ if (decl && ts.isFunctionTypeNode(decl)) return "Function";
61
+ if (inner) return "object";
62
+ const properties = type.getProperties();
63
+ const props = {};
64
+ for (const prop of properties) {
65
+ const propType = this.checker.getTypeOfSymbol(prop);
66
+ const name = prop.getName();
67
+ if (isPropertyBlacklisted(name)) continue;
68
+ props[name] = {
69
+ value: this.printType(propType, isPropertyBlacklisted, true),
70
+ isOptional: this.isSymbolOptional(prop)
71
+ };
72
+ }
73
+ const parts = [];
74
+ for (const [propName, { isOptional, value }] of Object.entries(props)) {
75
+ const questionMark = isOptional ? "?" : "";
76
+ parts.push(`"${escapeQuotes(propName)}"${questionMark}: ${value},`);
77
+ }
78
+ return Object.keys(props).length > 0 ? `{\n${parts.join("\n")}\n}` : "";
79
+ } 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);
80
+ else if (type.flags & ts.TypeFlags.Undefined) return "";
81
+ else if (type.flags & ts.TypeFlags.Conditional) return this.printConditionType(type, isPropertyBlacklisted, inner);
82
+ else if (type.isTypeParameter()) {
83
+ const decl = type.getSymbol()?.declarations?.[0];
84
+ if (!decl || !ts.isTypeParameterDeclaration(decl)) return "";
85
+ const ref = ts.getEffectiveConstraintOfTypeParameter(decl);
86
+ if (!ref) return "";
87
+ const refType = this.checker.getTypeAtLocation(ref);
88
+ return this.printType(refType, isPropertyBlacklisted, inner);
89
+ }
90
+ return this.typeToString(type);
91
+ }
92
+ printPropsTypeArg(node, isPropertyBlacklisted) {
93
+ const type = this.checker.getTypeAtLocation(node);
94
+ return this.printType(type, isPropertyBlacklisted);
95
+ }
96
+ printEventsByCallSignatures(callSignatures) {
97
+ return callSignatures.map((c) => {
98
+ const event = c.getParameters()[0];
99
+ return this.typeToString(this.checker.getTypeOfSymbol(event));
100
+ });
101
+ }
102
+ printEventsByMembers(members) {
103
+ return members.map((m) => `"${escapeQuotes(m.getName())}"`);
104
+ }
105
+ printEventsRuntimeArg(node) {
106
+ const parts = [];
107
+ const type = this.checker.getTypeAtLocation(node);
108
+ const callSignatures = type.getCallSignatures();
109
+ const members = type.getProperties();
110
+ 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.");
111
+ if (members.length > 0) parts.push(...this.printEventsByMembers(members));
112
+ else parts.push(...this.printEventsByCallSignatures(callSignatures));
113
+ return `[${parts.join(", ")}]`;
114
+ }
115
+ };
116
+
117
+ //#endregion
118
+ //#region src/core/transformers/defineEmits.ts
119
+ const transformDefineEmits = (printer, s, id) => {
120
+ const normalizedFilepath = normalizePath(id);
121
+ const language = getLanguage();
122
+ const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
123
+ const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
124
+ const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
125
+ if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
126
+ const scriptSetupDefineEmitsNode = language.findNodeByRange(scriptSetupAst, (scriptSetupRanges) => scriptSetupRanges.defineEmits?.callExp);
127
+ const virtualFileDefineEmitsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Emit");
128
+ if (!scriptSetupDefineEmitsNode || !virtualFileDefineEmitsTypeNode) return;
129
+ const offset = scriptSetupBlock.startTagEnd;
130
+ if (ts.isCallExpression(scriptSetupDefineEmitsNode) && scriptSetupDefineEmitsNode.arguments[0]) throw new Error("[@vue.ts/complex-types] `defineEmits` cannot accept both runtime argument and type argument.");
131
+ const tokens = scriptSetupDefineEmitsNode.getChildren(scriptSetupAst);
132
+ const lessThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.LessThanToken);
133
+ const greaterThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.GreaterThanToken);
134
+ const openParenToken = tokens.find((t) => t.kind === ts.SyntaxKind.OpenParenToken);
135
+ if (!lessThanToken || !greaterThanToken || !openParenToken) return;
136
+ const defineEmitsTypeArgRange = [offset + lessThanToken.pos, offset + greaterThanToken.end];
137
+ const runtimeArgPos = offset + openParenToken.end;
138
+ const printedRuntimeArg = printer.printEventsRuntimeArg(virtualFileDefineEmitsTypeNode);
139
+ s.remove(...defineEmitsTypeArgRange);
140
+ s.appendRight(runtimeArgPos, printedRuntimeArg);
141
+ };
142
+
143
+ //#endregion
144
+ //#region src/core/transformers/defineProps.ts
145
+ const transformDefineProps = (printer, s, id) => {
146
+ const normalizedFilepath = normalizePath(id);
147
+ const language = getLanguage();
148
+ const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
149
+ const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
150
+ const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
151
+ if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
152
+ const scriptSetupDefinePropsTypeRange = language.parseScriptSetupRanges(scriptSetupAst).defineProps?.typeArg;
153
+ const virtualFileDefinePropsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Props");
154
+ if (!scriptSetupDefinePropsTypeRange || !virtualFileDefinePropsTypeNode) return;
155
+ const printedType = printer.printPropsTypeArg(virtualFileDefinePropsTypeNode, isReservedProp);
156
+ const offset = scriptSetupBlock.startTagEnd;
157
+ s.overwrite(offset + scriptSetupDefinePropsTypeRange.start, offset + scriptSetupDefinePropsTypeRange.end, printedType);
158
+ };
159
+
160
+ //#endregion
161
+ //#region src/core/transformers/index.ts
162
+ const transformers = [["defineEmits", transformDefineEmits], ["defineProps", transformDefineProps]];
163
+ const getTransformers = (options) => transformers.filter(([key]) => !!options[key]);
164
+
165
+ //#endregion
166
+ //#region src/core/transform.ts
167
+ function transform(code, id, options) {
168
+ const s = new MagicString(code);
169
+ const printer = new Printer(getLanguage().tsLs.getProgram().getTypeChecker());
170
+ const transformers$1 = getTransformers(options);
171
+ for (const [, transform$1] of transformers$1) transform$1(printer, s, id);
172
+ return {
173
+ code: s.toString(),
174
+ map: s.generateMap({ hires: true })
175
+ };
176
+ }
177
+
178
+ //#endregion
179
+ //#region src/index.ts
180
+ const languageExtRegexp = /\.((?:c|m)?(?:j|t)sx?|d\.ts|vue)$/i;
181
+ const unpluginFactory = (options = {}) => {
182
+ const resolvedOptions = resolveOptions(options);
183
+ const filter = createFilter(resolvedOptions.include, resolvedOptions.exclude);
184
+ return {
185
+ name: "@vue.ts/complex-types",
186
+ enforce: "pre",
187
+ vite: { async handleHotUpdate(ctx) {
188
+ const { file } = ctx;
189
+ if (!languageExtRegexp.test(file)) return;
190
+ const language = getLanguage();
191
+ const { read } = ctx;
192
+ async function readAndUpdateLanguage() {
193
+ const content = await read();
194
+ language.updateFile(file, content);
195
+ return content;
196
+ }
197
+ if (!filter(file)) {
198
+ await readAndUpdateLanguage();
199
+ return;
200
+ }
201
+ let cached;
202
+ ctx.read = async () => {
203
+ cached ??= (async () => {
204
+ const code = await readAndUpdateLanguage();
205
+ return transform(code, file, resolvedOptions)?.code ?? code;
206
+ })();
207
+ return cached;
208
+ };
209
+ for (const mod of ctx.modules) ctx.server.moduleGraph.invalidateModule(mod);
210
+ return ctx.modules;
211
+ } },
212
+ buildStart() {
213
+ ensureLanguage(resolveOptions(options).tsconfigPath);
214
+ },
215
+ transform: {
216
+ filter: { id: {
217
+ include: resolvedOptions.include,
218
+ exclude: resolvedOptions.exclude
219
+ } },
220
+ handler: (code, id) => transform(code, id, resolvedOptions)
221
+ }
222
+ };
223
+ };
224
+ const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
225
+ var src_default = unplugin;
226
+
227
+ //#endregion
228
+ export { unplugin as n, unpluginFactory as r, src_default as t };
@@ -1,8 +1,8 @@
1
+ import { FilterPattern } from "unplugin";
1
2
  import MagicString from "magic-string";
2
3
  import ts from "typescript";
3
- import { FilterPattern } from "@rollup/pluginutils";
4
4
 
5
- //#region ../common/src/types.d.ts
5
+ //#region ../shared/src/types.d.ts
6
6
  interface BaseOptions {
7
7
  include?: FilterPattern;
8
8
  exclude?: FilterPattern;
@@ -12,27 +12,13 @@ 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 printIntersectionTypeNode;
18
- private printUnionTypeNode;
19
- private printTypeLiteralNode;
20
- private printTypeByType;
21
- private printIdentifier;
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 printUnionOrIntersection;
17
+ private isSymbolOptional;
18
+ private printConditionType;
19
+ private printPrimitiveType;
20
+ private printType;
21
+ printPropsTypeArg(node: ts.Node, isPropertyBlacklisted: (prop: string) => boolean): string;
36
22
  private printEventsByCallSignatures;
37
23
  private printEventsByMembers;
38
24
  printEventsRuntimeArg(node: ts.Node): string;
@@ -48,4 +34,4 @@ type TransformOptions = Pick<ResolvedOptions, ValidTransforms>;
48
34
  type Transformer = (printer: Printer, s: MagicString, id: string) => void;
49
35
  type Transformers = [ValidTransforms, Transformer][];
50
36
  //#endregion
51
- export { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms };
37
+ export { Transformers as a, Transformer as i, ResolvedOptions as n, ValidTransforms as o, TransformOptions as r, Options as t };
package/dist/types.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms } from "./types-CFmCIAvs.js";
1
+ import { a as Transformers, i as Transformer, n as ResolvedOptions, o as ValidTransforms, r as TransformOptions, t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  export { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms };
package/dist/types.js CHANGED
@@ -0,0 +1 @@
1
+ export { };
@@ -1,8 +1,8 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createVitePlugin } from "unplugin";
3
3
 
4
4
  //#region src/vite.ts
5
5
  var vite_default = createVitePlugin(unpluginFactory);
6
6
 
7
7
  //#endregion
8
- export { vite_default };
8
+ export { vite_default as t };
package/dist/vite.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  import * as vite0 from "vite";
4
4
 
package/dist/vite.js CHANGED
@@ -1,4 +1,4 @@
1
- import "./src-CI8-WIu4.js";
2
- import { vite_default } from "./vite-Dyi1m2wg.js";
1
+ import "./src-Ci8DJn1t.js";
2
+ import { t as vite_default } from "./vite-D-Xfh8NL.js";
3
3
 
4
4
  export { vite_default as default };
@@ -1,8 +1,8 @@
1
- import { unpluginFactory } from "./src-CI8-WIu4.js";
1
+ import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
2
2
  import { createWebpackPlugin } from "unplugin";
3
3
 
4
4
  //#region src/webpack.ts
5
5
  var webpack_default = createWebpackPlugin(unpluginFactory);
6
6
 
7
7
  //#endregion
8
- export { webpack_default };
8
+ export { webpack_default as t };
package/dist/webpack.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { t as Options } from "./types-B0cfnWIB.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  import * as webpack0 from "webpack";
4
4
 
package/dist/webpack.js CHANGED
@@ -1,4 +1,4 @@
1
- import "./src-CI8-WIu4.js";
2
- import { webpack_default } from "./webpack-CFfuQfGw.js";
1
+ import "./src-Ci8DJn1t.js";
2
+ import { t as webpack_default } from "./webpack-By_BEclB.js";
3
3
 
4
4
  export { webpack_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vue.ts/complex-types",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.10",
4
4
  "author": "Ray <i@mk1.io> (@so1ve)",
5
5
  "type": "module",
6
6
  "description": "Resolve complex types in Vue SFCs.",
@@ -62,31 +62,34 @@
62
62
  "access": "public"
63
63
  },
64
64
  "dependencies": {
65
- "@nuxt/kit": "^3.17.6",
66
- "magic-string": "^0.30.17",
67
- "unplugin": "^2.3.5",
68
- "@vue.ts/language": "1.0.0-beta.1",
69
- "@vue.ts/common": "1.0.0-beta.1"
65
+ "@nuxt/kit": "^4.2.0",
66
+ "@vue/shared": "^3.5.22",
67
+ "defu": "^6.1.4",
68
+ "magic-string": "^0.30.21",
69
+ "unplugin": "2.3.10",
70
+ "unplugin-utils": "^0.3.1",
71
+ "@vue.ts/language": "1.0.0-beta.10",
72
+ "@vue.ts/shared": "1.0.0-beta.10"
70
73
  },
71
74
  "devDependencies": {
72
- "@nuxt/schema": "^3.17.6",
73
- "@vue-macros/better-define": "^1.11.4",
74
- "rolldown": "1.0.0-beta.27",
75
- "rollup": "^4.44.2",
76
- "vite": "^7.0.4",
77
- "webpack": "^5.100.1"
75
+ "@nuxt/schema": "^4.2.0",
76
+ "@vue-macros/better-define": "^3.1.1",
77
+ "rolldown": "1.0.0-beta.45",
78
+ "rollup": "^4.52.5",
79
+ "vite": "7.1.12",
80
+ "webpack": "^5.102.1"
78
81
  },
79
82
  "peerDependencies": {
80
- "@farmfe/core": ">=1",
81
- "@nuxt/kit": "^3 || ^4",
82
- "@nuxt/schema": "^3 || ^4",
83
+ "@farmfe/core": ">=1.7.11",
84
+ "@nuxt/kit": "^4.2.0",
85
+ "@nuxt/schema": "^4.2.0",
83
86
  "esbuild": "*",
84
87
  "rolldown": "*",
85
- "rollup": "^3",
86
- "typescript": "^5.2.2",
87
- "vite": ">=3",
88
- "vue": "^3.3.7",
89
- "webpack": "^4 || ^5"
88
+ "rollup": "^4.52.5",
89
+ "typescript": ">=5.9.3",
90
+ "vite": ">=7.1.12",
91
+ "vue": ">=3.5.22",
92
+ "webpack": "^5.102.1"
90
93
  },
91
94
  "peerDependenciesMeta": {
92
95
  "@farmfe/core": {
@@ -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 };