@vue.ts/complex-types 1.0.0-beta.2 → 1.0.0-beta.4

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Ray <https://github.com/so1ve>
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Ray <https://github.com/so1ve>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
1
+ import { src_default, unplugin, unpluginFactory } from "./src-koVD0EYI.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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
2
- import { vite_default } from "./vite-D_E5rjeG.js";
3
- import { webpack_default } from "./webpack-BQtMUXaQ.js";
1
+ import "./src-koVD0EYI.js";
2
+ import { vite_default } from "./vite-DFtk2L0c.js";
3
+ import { webpack_default } from "./webpack-BWJ3ulvy.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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.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 { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.js";
2
2
  import { createRspackPlugin } from "unplugin";
3
3
 
4
4
  //#region src/rspack.ts
@@ -0,0 +1,201 @@
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
+ import { defu } from "defu";
8
+
9
+ //#region src/core/utils.ts
10
+ const defaultOptions = {
11
+ include: ["**/*.vue"],
12
+ exclude: ["node_modules/**"],
13
+ tsconfigPath: join(process.cwd(), "tsconfig.json"),
14
+ defineEmits: true,
15
+ defineProps: true
16
+ };
17
+ const resolveOptions = (rawOptions) => defu(rawOptions, defaultOptions);
18
+ const quotesReg = /"/g;
19
+ const escapeQuotes = (s) => s.replace(quotesReg, "\\\"");
20
+
21
+ //#endregion
22
+ //#region src/core/printer.ts
23
+ var Printer = class {
24
+ constructor(checker) {
25
+ this.checker = checker;
26
+ }
27
+ typeToString(type) {
28
+ return this.checker.typeToString(type, void 0, ts.TypeFormatFlags.NoTruncation);
29
+ }
30
+ printUnionOrIntersection(type, separator, inner) {
31
+ return [...new Set(type.types.map((t) => this.printType(t, inner)).filter(Boolean))].join(separator);
32
+ }
33
+ containsUndefined(type) {
34
+ return type.isUnion() ? type.types.some((t) => t.flags & ts.TypeFlags.Undefined) : !!(type.flags & ts.TypeFlags.Undefined);
35
+ }
36
+ printConditionTypeNode(type) {
37
+ const decl = type.root.node;
38
+ const { trueType, falseType } = decl;
39
+ const trueTypeNode = this.checker.getTypeAtLocation(trueType);
40
+ const falseTypeNode = this.checker.getTypeAtLocation(falseType);
41
+ return `${this.printType(trueTypeNode)} | ${this.printType(falseTypeNode)}`;
42
+ }
43
+ printPrimitiveType(type) {
44
+ if (type.flags & ts.TypeFlags.BooleanLiteral) return "boolean";
45
+ else if (type.flags & ts.TypeFlags.String || type.isStringLiteral()) return "string";
46
+ else if (type.flags & ts.TypeFlags.Number || type.isNumberLiteral()) return "number";
47
+ else if (type.flags & ts.TypeFlags.BigInt) return "bigint";
48
+ else if (type.flags & ts.TypeFlags.Any) return "any";
49
+ else if (type.flags & ts.TypeFlags.Unknown) return "unknown";
50
+ else if (type.flags & ts.TypeFlags.Null) return "null";
51
+ return "";
52
+ }
53
+ printType(type, inner = false) {
54
+ if (type.isUnion()) return this.printUnionOrIntersection(type, " | ", inner);
55
+ else if (type.isIntersection()) return this.printUnionOrIntersection(type, " & ", inner);
56
+ if (this.checker.isArrayType(type)) return "Array";
57
+ else if (type.flags & ts.TypeFlags.Object) {
58
+ const decl = type.getSymbol()?.declarations?.[0];
59
+ if (decl && ts.isFunctionTypeNode(decl)) return "Function";
60
+ if (inner) return "object";
61
+ const properties = type.getProperties();
62
+ const props = {};
63
+ for (const prop of properties) {
64
+ const propType = this.checker.getTypeOfSymbol(prop);
65
+ props[prop.getName()] = {
66
+ value: this.printType(propType, true),
67
+ isOptional: this.containsUndefined(propType)
68
+ };
69
+ }
70
+ const parts = [];
71
+ for (const [propName, { isOptional, value }] of Object.entries(props)) {
72
+ const questionMark = isOptional ? "?" : "";
73
+ parts.push(`"${escapeQuotes(propName)}"${questionMark}: ${value},`);
74
+ }
75
+ return Object.keys(props).length > 0 ? `{\n${parts.join("\n")}\n}` : "";
76
+ } 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);
77
+ else if (type.flags & ts.TypeFlags.Undefined) return "";
78
+ else if (type.flags & ts.TypeFlags.Conditional) return this.printConditionTypeNode(type);
79
+ else if (type.isTypeParameter()) {
80
+ const symbol = type.getSymbol();
81
+ const decl = symbol?.declarations?.[0];
82
+ if (!decl || !ts.isTypeParameterDeclaration(decl)) return "";
83
+ const ref = ts.getEffectiveConstraintOfTypeParameter(decl);
84
+ if (!ref) return "";
85
+ const refType = this.checker.getTypeAtLocation(ref);
86
+ return this.printType(refType);
87
+ }
88
+ return this.typeToString(type);
89
+ }
90
+ printPropsTypeArg(node) {
91
+ const type = this.checker.getTypeAtLocation(node);
92
+ return this.printType(type);
93
+ }
94
+ printEventsByCallSignatures(callSignatures) {
95
+ return callSignatures.map((c) => {
96
+ const parameters = c.getParameters();
97
+ const event = parameters[0];
98
+ return this.typeToString(this.checker.getTypeOfSymbol(event));
99
+ });
100
+ }
101
+ printEventsByMembers(members) {
102
+ return members.map((m) => `"${escapeQuotes(m.getName())}"`);
103
+ }
104
+ printEventsRuntimeArg(node) {
105
+ const parts = [];
106
+ const type = this.checker.getTypeAtLocation(node);
107
+ const callSignatures = type.getCallSignatures();
108
+ const members = type.getProperties();
109
+ 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.");
110
+ if (members.length > 0) parts.push(...this.printEventsByMembers(members));
111
+ else parts.push(...this.printEventsByCallSignatures(callSignatures));
112
+ return `[${parts.join(", ")}]`;
113
+ }
114
+ };
115
+
116
+ //#endregion
117
+ //#region src/core/transformers/defineEmits.ts
118
+ const transformDefineEmits = (printer, s, id) => {
119
+ const normalizedFilepath = normalizePath(id);
120
+ const language = getLanguage();
121
+ const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
122
+ const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
123
+ const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
124
+ if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
125
+ const scriptSetupDefineEmitsNode = language.findNodeByRange(scriptSetupAst, (scriptSetupRanges) => scriptSetupRanges.defineEmits?.callExp);
126
+ const virtualFileDefineEmitsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Emit");
127
+ if (!scriptSetupDefineEmitsNode || !virtualFileDefineEmitsTypeNode) return;
128
+ const offset = scriptSetupBlock.startTagEnd;
129
+ const defineEmitsRuntimeArg = ts.isCallExpression(scriptSetupDefineEmitsNode) && scriptSetupDefineEmitsNode.arguments[0];
130
+ if (defineEmitsRuntimeArg) 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);
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 language = getLanguage();
170
+ const typeChecker = language.__internal__.typeChecker;
171
+ const printer = new Printer(typeChecker);
172
+ const transformers$1 = getTransformers(options);
173
+ for (const [, transform$1] of transformers$1) transform$1(printer, s, id);
174
+ return {
175
+ code: s.toString(),
176
+ map: s.generateMap({ hires: true })
177
+ };
178
+ }
179
+
180
+ //#endregion
181
+ //#region src/index.ts
182
+ const unpluginFactory = (options = {}) => {
183
+ const resolvedOptions = resolveOptions(options);
184
+ const filter = createFilter(resolvedOptions.include, resolvedOptions.exclude);
185
+ return {
186
+ name: "@vue.ts/complex-types",
187
+ buildStart() {
188
+ const resolvedOptions$1 = resolveOptions(options);
189
+ ensureLanguage(resolvedOptions$1.tsconfigPath);
190
+ },
191
+ transform(code, id) {
192
+ if (!filter(id)) return;
193
+ return transform(code, id, resolvedOptions);
194
+ }
195
+ };
196
+ };
197
+ const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
198
+ var src_default = unplugin;
199
+
200
+ //#endregion
201
+ 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 ../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 containsUndefined;
18
+ private printConditionTypeNode;
19
+ private printPrimitiveType;
20
+ private printType;
21
+ printPropsTypeArg(node: ts.Node): string;
36
22
  private printEventsByCallSignatures;
37
23
  private printEventsByMembers;
38
24
  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-CFmCIAvs.js";
1
+ import { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms } from "./types-WYBg4JEW.js";
2
2
  import "./types-u_Hr5uXA.js";
3
3
  export { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms };
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.js";
2
2
  import { createVitePlugin } from "unplugin";
3
3
 
4
4
  //#region src/vite.ts
package/dist/vite.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
2
- import { vite_default } from "./vite-D_E5rjeG.js";
1
+ import "./src-koVD0EYI.js";
2
+ import { vite_default } from "./vite-DFtk2L0c.js";
3
3
 
4
4
  export { vite_default as default };
@@ -1,4 +1,4 @@
1
- import { unpluginFactory } from "./src-k-9RVLWt.js";
1
+ import { unpluginFactory } from "./src-koVD0EYI.js";
2
2
  import { createWebpackPlugin } from "unplugin";
3
3
 
4
4
  //#region src/webpack.ts
package/dist/webpack.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options } from "./types-CFmCIAvs.js";
1
+ import { Options } from "./types-WYBg4JEW.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-k-9RVLWt.js";
2
- import { webpack_default } from "./webpack-BQtMUXaQ.js";
1
+ import "./src-koVD0EYI.js";
2
+ import { webpack_default } from "./webpack-BWJ3ulvy.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.2",
3
+ "version": "1.0.0-beta.4",
4
4
  "author": "Ray <i@mk1.io> (@so1ve)",
5
5
  "type": "module",
6
6
  "description": "Resolve complex types in Vue SFCs.",
@@ -62,31 +62,32 @@
62
62
  "access": "public"
63
63
  },
64
64
  "dependencies": {
65
- "@nuxt/kit": "^3.17.6",
65
+ "@nuxt/kit": "^4.0.2",
66
+ "defu": "^6.1.4",
66
67
  "magic-string": "^0.30.17",
67
68
  "unplugin": "^2.3.5",
68
- "@vue.ts/common": "1.0.0-beta.2",
69
- "@vue.ts/language": "1.0.0-beta.2"
69
+ "@vue.ts/language": "1.0.0-beta.4",
70
+ "@vue.ts/shared": "1.0.0-beta.4"
70
71
  },
71
72
  "devDependencies": {
72
- "@nuxt/schema": "^3.17.6",
73
+ "@nuxt/schema": "^4.0.2",
73
74
  "@vue-macros/better-define": "^1.11.4",
74
75
  "rolldown": "1.0.0-beta.27",
75
- "rollup": "^4.44.2",
76
+ "rollup": "^4.46.2",
76
77
  "vite": "^7.0.4",
77
- "webpack": "^5.100.1"
78
+ "webpack": "^5.101.0"
78
79
  },
79
80
  "peerDependencies": {
80
- "@farmfe/core": ">=1",
81
- "@nuxt/kit": "^3 || ^4",
82
- "@nuxt/schema": "^3 || ^4",
81
+ "@farmfe/core": ">=1.7.10",
82
+ "@nuxt/kit": "^4.0.2",
83
+ "@nuxt/schema": "^4.0.2",
83
84
  "esbuild": "*",
84
85
  "rolldown": "*",
85
- "rollup": "^3",
86
- "typescript": "^5.2.2",
87
- "vite": ">=3",
88
- "vue": "^3.3.7",
89
- "webpack": "^4 || ^5"
86
+ "rollup": "^4.46.2",
87
+ "typescript": ">=5.9.2",
88
+ "vite": ">=7.0.6",
89
+ "vue": ">=3.5.18",
90
+ "webpack": "^5.101.0"
90
91
  },
91
92
  "peerDependenciesMeta": {
92
93
  "@farmfe/core": {
@@ -1,360 +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 isOptional = property.flags & ts.SymbolFlags.Optional;
59
- const questionToken = isMapped ? node.questionToken?.getText() ?? "" : isOptional ? "?" : "";
60
- const valueType = this.checker.getTypeOfSymbol(property);
61
- const stringValueType = this.typeToString(this.getBaseType(valueType));
62
- parts.push(`${this.checker.symbolToString(property)}${questionToken}: ${stringValueType}`);
63
- }
64
- parts.push("}");
65
- return parts.join("\n");
66
- }
67
- printIdentifier(node) {
68
- const symbol = this.checker.getSymbolAtLocation(node);
69
- if (!symbol) return node.getText();
70
- const declarations = symbol.getDeclarations();
71
- if (!declarations || declarations.length === 0) return node.getText();
72
- return this.print(declarations[0]);
73
- }
74
- printInterfaceDeclaration(node) {
75
- const parts = ["{"];
76
- for (const member of node.members) if (ts.isPropertySignature(member)) {
77
- const stringBaseType = member.type ? this.print(member.type) : "any";
78
- parts.push([
79
- member.name?.getText(),
80
- member.questionToken?.getText(),
81
- ": ",
82
- stringBaseType
83
- ].filter(Boolean).join(""));
84
- } else if (ts.isMethodSignature(member)) {
85
- const signature = this.checker.getSignatureFromDeclaration(member);
86
- const returnType = signature ? this.typeToString(signature.getReturnType()) : "any";
87
- const parameters = member.parameters.map((param) => {
88
- const paramType = param.type ? this.typeToString(this.getBaseType(param.type)) : "any";
89
- return `${param.name.getText()}: ${paramType}`;
90
- }).join(", ");
91
- parts.push(`${member.name?.getText()}(${parameters}): ${returnType}`);
92
- }
93
- parts.push("}");
94
- let result = parts.join("\n");
95
- if (node.heritageClauses && node.heritageClauses.length > 0) result = node.heritageClauses.map((clause) => this.print(clause)).join(" & ");
96
- return result;
97
- }
98
- printEnumDeclaration(node) {
99
- const parts = ["{"];
100
- for (const member of node.members) {
101
- const name = member.name.getText();
102
- if (member.initializer) {
103
- const value = member.initializer.getText();
104
- parts.push(`${name}: ${value}`);
105
- } else parts.push(`${name}: number`);
106
- }
107
- parts.push("}");
108
- return parts.join("\n");
109
- }
110
- printClassDeclaration(node) {
111
- const parts = ["{"];
112
- for (const member of node.members) if (ts.isPropertyDeclaration(member)) {
113
- const name = member.name?.getText();
114
- const typeAnnotation = member.type ? this.typeToString(this.getBaseType(member.type)) : "any";
115
- const optional = member.questionToken ? "?" : "";
116
- parts.push(`${name}${optional}: ${typeAnnotation}`);
117
- } else if (ts.isMethodDeclaration(member)) {
118
- const name = member.name?.getText();
119
- const signature = this.checker.getSignatureFromDeclaration(member);
120
- const returnType = signature ? this.typeToString(signature.getReturnType()) : "any";
121
- const parameters = member.parameters.map((param) => {
122
- const paramType = param.type ? this.typeToString(this.getBaseType(param.type)) : "any";
123
- return `${param.name.getText()}: ${paramType}`;
124
- }).join(", ");
125
- parts.push(`${name}(${parameters}): ${returnType}`);
126
- } else if (ts.isConstructorDeclaration(member)) {
127
- const parameters = member.parameters.map((param) => {
128
- const paramType = param.type ? this.typeToString(this.getBaseType(param.type)) : "any";
129
- return `${param.name.getText()}: ${paramType}`;
130
- }).join(", ");
131
- parts.push(`constructor(${parameters}): void`);
132
- }
133
- parts.push("}");
134
- return parts.join("\n");
135
- }
136
- printFunctionTypeNode(node) {
137
- const parameters = node.parameters.map((param) => {
138
- const paramType = param.type ? this.print(param.type) : "any";
139
- const optional = param.questionToken ? "?" : "";
140
- const rest = param.dotDotDotToken ? "..." : "";
141
- return `${rest}${param.name.getText()}${optional}: ${paramType}`;
142
- }).join(", ");
143
- const returnType = node.type ? this.print(node.type) : "any";
144
- return `(${parameters}) => ${returnType}`;
145
- }
146
- printConstructorTypeNode(node) {
147
- const parameters = node.parameters.map((param) => {
148
- const paramType = param.type ? this.print(param.type) : "any";
149
- const optional = param.questionToken ? "?" : "";
150
- const rest = param.dotDotDotToken ? "..." : "";
151
- return `${rest}${param.name.getText()}${optional}: ${paramType}`;
152
- }).join(", ");
153
- const returnType = node.type ? this.print(node.type) : "any";
154
- return `new (${parameters}) => ${returnType}`;
155
- }
156
- printTypeQueryNode(node) {
157
- const exprName = node.exprName.getText();
158
- return `typeof ${exprName}`;
159
- }
160
- printArrayTypeNode(node) {
161
- return `${this.print(node.elementType)}[]`;
162
- }
163
- printTupleTypeNode(node) {
164
- const elements = node.elements.map((element) => {
165
- if (ts.isRestTypeNode(element)) return `...${this.print(element.type)}`;
166
- if (ts.isOptionalTypeNode(element)) return `${this.print(element.type)}?`;
167
- if (ts.isNamedTupleMember(element)) {
168
- const type = this.print(element.type);
169
- const optional = element.questionToken ? "?" : "";
170
- const rest = element.dotDotDotToken ? "..." : "";
171
- return `${rest}${element.name.getText()}${optional}: ${type}`;
172
- }
173
- return this.print(element);
174
- });
175
- return `[${elements.join(", ")}]`;
176
- }
177
- printConditionalTypeNode(node) {
178
- const conditionalType = this.checker.getTypeAtLocation(node);
179
- if (conditionalType && !(conditionalType.flags & ts.TypeFlags.Conditional)) {
180
- const resolvedTypeString = this.typeToString(conditionalType);
181
- if (resolvedTypeString !== node.getText()) return resolvedTypeString;
182
- }
183
- const checkType = this.print(node.checkType);
184
- const extendsType = this.print(node.extendsType);
185
- const trueType = this.print(node.trueType);
186
- const falseType = this.print(node.falseType);
187
- return `${checkType} extends ${extendsType} ? ${trueType} : ${falseType}`;
188
- }
189
- printIndexedAccessTypeNode(node) {
190
- const objectType = this.print(node.objectType);
191
- const indexType = this.print(node.indexType);
192
- return `${objectType}[${indexType}]`;
193
- }
194
- printHeritageClause(node) {
195
- if (node.token === ts.SyntaxKind.ExtendsKeyword) {
196
- const parts = [];
197
- for (const type of node.types) if (ts.isExpressionWithTypeArguments(type)) if (type.expression && type.typeArguments) parts.push(this.print(type));
198
- else parts.push(this.print(type.expression));
199
- return parts.join(" & ");
200
- }
201
- return "";
202
- }
203
- printImportSpecifier(node) {
204
- const name = node.propertyName ? node.propertyName.getText() : node.name.getText();
205
- const symbol = this.checker.getSymbolAtLocation(node.name);
206
- if (!symbol) return name;
207
- const aliasedSymbol = this.checker.getAliasedSymbol(symbol);
208
- const targetSymbol = aliasedSymbol || symbol;
209
- const declarations = targetSymbol.getDeclarations();
210
- if (!declarations || declarations.length === 0) return name;
211
- const declaration = declarations.find((decl) => ts.isTypeAliasDeclaration(decl) || ts.isInterfaceDeclaration(decl) || ts.isClassDeclaration(decl) || ts.isEnumDeclaration(decl) || ts.isVariableDeclaration(decl));
212
- if (declaration) return this.print(declaration);
213
- return name;
214
- }
215
- isKeywordTypeNode(node) {
216
- 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;
217
- }
218
- print(node) {
219
- if (ts.isIdentifier(node)) return this.printIdentifier(node);
220
- else if (ts.isIntersectionTypeNode(node)) return this.printIntersectionTypeNode(node);
221
- else if (ts.isUnionTypeNode(node)) return this.printUnionTypeNode(node);
222
- else if (ts.isTypeLiteralNode(node)) return this.printTypeLiteralNode(node);
223
- else if (ts.isTypeReferenceNode(node)) return this.print(node.typeName);
224
- else if (ts.isInterfaceDeclaration(node)) return this.printInterfaceDeclaration(node);
225
- else if (ts.isTypeAliasDeclaration(node)) return node.type ? this.print(node.type) : "any";
226
- else if (ts.isEnumDeclaration(node)) return this.printEnumDeclaration(node);
227
- else if (ts.isClassDeclaration(node)) return this.printClassDeclaration(node);
228
- else if (ts.isFunctionTypeNode(node)) return this.printFunctionTypeNode(node);
229
- else if (ts.isConstructorTypeNode(node)) return this.printConstructorTypeNode(node);
230
- else if (ts.isTypeQueryNode(node)) return this.printTypeQueryNode(node);
231
- else if (ts.isArrayTypeNode(node)) return this.printArrayTypeNode(node);
232
- else if (ts.isTupleTypeNode(node)) return this.printTupleTypeNode(node);
233
- else if (ts.isConditionalTypeNode(node)) return this.printConditionalTypeNode(node);
234
- else if (ts.isIndexedAccessTypeNode(node)) return this.printIndexedAccessTypeNode(node);
235
- else if (ts.isParenthesizedTypeNode(node)) return this.print(node.type);
236
- else if (ts.isHeritageClause(node)) return this.printHeritageClause(node);
237
- else if (ts.isTypeAliasDeclaration(node) && node.type) return this.print(node.type);
238
- else if (ts.isInterfaceDeclaration(node)) return this.printInterfaceDeclaration(node);
239
- else if (ts.isVariableDeclaration(node) && node.type) return this.print(node.type);
240
- else if (ts.isImportSpecifier(node)) return this.printImportSpecifier(node);
241
- else if (ts.isTypeParameterDeclaration(node)) {
242
- if (node.constraint) return this.print(node.constraint);
243
- else if (node.default) return this.print(node.default);
244
- return "{}";
245
- } else if (ts.isParameter(node) && node.type) return this.print(node.type);
246
- else if (ts.isLiteralTypeNode(node) || ts.isThisTypeNode(node) || ts.isPropertyAccessExpression(node) || this.isKeywordTypeNode(node)) return node.getText();
247
- else if (ts.isMappedTypeNode(node) || ts.isExpressionWithTypeArguments(node)) return this.printTypeByType(node);
248
- else {
249
- console.error(`[@vue.ts/complex-types] \`${ts.SyntaxKind[node.kind]}\` is not supported.`);
250
- return node.getText();
251
- }
252
- }
253
- printEventsByCallSignatures(callSignatures) {
254
- return callSignatures.map((c) => {
255
- const parameters = c.getParameters();
256
- const event = parameters[0];
257
- return this.typeToString(this.checker.getTypeOfSymbol(event));
258
- });
259
- }
260
- printEventsByMembers(members) {
261
- return members.map((m) => `"${escapeQuotes(m.getName())}"`);
262
- }
263
- printEventsRuntimeArg(node) {
264
- const parts = [];
265
- const type = this.checker.getTypeAtLocation(node);
266
- const callSignatures = type.getCallSignatures();
267
- const members = type.getProperties();
268
- 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.");
269
- if (members.length > 0) parts.push(...this.printEventsByMembers(members));
270
- else parts.push(...this.printEventsByCallSignatures(callSignatures));
271
- return `[${parts.join(", ")}]`;
272
- }
273
- };
274
-
275
- //#endregion
276
- //#region src/core/transformers/defineEmits.ts
277
- const transformDefineEmits = (printer, s, id) => {
278
- const normalizedFilepath = normalizePath(id);
279
- const language = getLanguage();
280
- const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
281
- const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
282
- const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
283
- if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
284
- const scriptSetupDefineEmitsNode = language.findNodeByRange(scriptSetupAst, (scriptSetupRanges) => scriptSetupRanges.defineEmits?.callExp);
285
- const virtualFileDefineEmitsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Emit");
286
- if (!scriptSetupDefineEmitsNode || !virtualFileDefineEmitsTypeNode) return;
287
- const offset = scriptSetupBlock.startTagEnd;
288
- const defineEmitsRuntimeArg = ts.isCallExpression(scriptSetupDefineEmitsNode) && scriptSetupDefineEmitsNode.arguments[0];
289
- if (defineEmitsRuntimeArg) throw new Error("[@vue.ts/complex-types] `defineEmits` cannot accept both runtime argument and type argument.");
290
- const tokens = scriptSetupDefineEmitsNode.getChildren(scriptSetupAst);
291
- const lessThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.LessThanToken);
292
- const greaterThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.GreaterThanToken);
293
- const openParenToken = tokens.find((t) => t.kind === ts.SyntaxKind.OpenParenToken);
294
- if (!lessThanToken || !greaterThanToken || !openParenToken) return;
295
- const defineEmitsTypeArgRange = [offset + lessThanToken.pos, offset + greaterThanToken.end];
296
- const runtimeArgPos = offset + openParenToken.end;
297
- const printedRuntimeArg = printer.printEventsRuntimeArg(virtualFileDefineEmitsTypeNode);
298
- s.remove(...defineEmitsTypeArgRange);
299
- s.appendRight(runtimeArgPos, printedRuntimeArg);
300
- };
301
-
302
- //#endregion
303
- //#region src/core/transformers/defineProps.ts
304
- const transformDefineProps = (printer, s, id) => {
305
- const normalizedFilepath = normalizePath(id);
306
- const language = getLanguage();
307
- const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
308
- const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
309
- const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
310
- if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
311
- const scriptSetupDefinePropsTypeRange = language.parseScriptSetupRanges(scriptSetupAst).defineProps?.typeArg;
312
- const virtualFileDefinePropsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Props");
313
- if (!scriptSetupDefinePropsTypeRange || !virtualFileDefinePropsTypeNode) return;
314
- const printedType = printer.print(virtualFileDefinePropsTypeNode);
315
- const offset = scriptSetupBlock.startTagEnd;
316
- s.overwrite(offset + scriptSetupDefinePropsTypeRange.start, offset + scriptSetupDefinePropsTypeRange.end, printedType);
317
- };
318
-
319
- //#endregion
320
- //#region src/core/transformers/index.ts
321
- const transformers = [["defineEmits", transformDefineEmits], ["defineProps", transformDefineProps]];
322
- const getTransformers = (options) => transformers.filter(([key]) => !!options[key]);
323
-
324
- //#endregion
325
- //#region src/core/transform.ts
326
- function transform(code, id, options) {
327
- const s = new MagicString(code);
328
- const language = getLanguage();
329
- const typeChecker = language.__internal__.typeChecker;
330
- const printer = new Printer(typeChecker);
331
- const transformers$1 = getTransformers(options);
332
- for (const [, transform$1] of transformers$1) transform$1(printer, s, id);
333
- return {
334
- code: s.toString(),
335
- map: s.generateMap({ hires: true })
336
- };
337
- }
338
-
339
- //#endregion
340
- //#region src/index.ts
341
- const unpluginFactory = (options = {}) => {
342
- const resolvedOptions = resolveOptions(options);
343
- const filter = createFilter(resolvedOptions.include, resolvedOptions.exclude);
344
- return {
345
- name: "@vue.ts/complex-types",
346
- buildStart() {
347
- const resolvedOptions$1 = resolveOptions(options);
348
- ensureLanguage(resolvedOptions$1.tsconfigPath);
349
- },
350
- transform(code, id) {
351
- if (!filter(id)) return;
352
- return transform(code, id, resolvedOptions);
353
- }
354
- };
355
- };
356
- const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
357
- var src_default = unplugin;
358
-
359
- //#endregion
360
- export { src_default, unplugin, unpluginFactory };