@vue.ts/complex-types 0.4.0
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 +21 -0
- package/README.md +225 -0
- package/dist/astro.cjs +22 -0
- package/dist/astro.d.cts +13 -0
- package/dist/astro.d.mts +13 -0
- package/dist/astro.d.ts +13 -0
- package/dist/astro.mjs +20 -0
- package/dist/esbuild.cjs +13 -0
- package/dist/esbuild.d.cts +9 -0
- package/dist/esbuild.d.mts +9 -0
- package/dist/esbuild.d.ts +9 -0
- package/dist/esbuild.mjs +11 -0
- package/dist/index.cjs +228 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.mjs +221 -0
- package/dist/nuxt.cjs +35 -0
- package/dist/nuxt.d.cts +9 -0
- package/dist/nuxt.d.mts +9 -0
- package/dist/nuxt.d.ts +9 -0
- package/dist/nuxt.mjs +33 -0
- package/dist/rollup.cjs +13 -0
- package/dist/rollup.d.cts +9 -0
- package/dist/rollup.d.mts +9 -0
- package/dist/rollup.d.ts +9 -0
- package/dist/rollup.mjs +11 -0
- package/dist/rspack.cjs +13 -0
- package/dist/rspack.d.cts +8 -0
- package/dist/rspack.d.mts +8 -0
- package/dist/rspack.d.ts +8 -0
- package/dist/rspack.mjs +11 -0
- package/dist/types.cjs +2 -0
- package/dist/types.d.cts +30 -0
- package/dist/types.d.mts +30 -0
- package/dist/types.d.ts +30 -0
- package/dist/types.mjs +1 -0
- package/dist/vite.cjs +13 -0
- package/dist/vite.d.cts +9 -0
- package/dist/vite.d.mts +9 -0
- package/dist/vite.d.ts +9 -0
- package/dist/vite.mjs +11 -0
- package/dist/webpack.cjs +13 -0
- package/dist/webpack.d.cts +9 -0
- package/dist/webpack.d.mts +9 -0
- package/dist/webpack.d.ts +9 -0
- package/dist/webpack.mjs +11 -0
- package/package.json +107 -0
package/LICENSE
ADDED
|
@@ -0,0 +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.
|
package/README.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# unplugin-vue-complex-types
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/unplugin-vue-complex-types)
|
|
4
|
+
|
|
5
|
+
Use [`@vue/language-core`](https://github.com/vuejs/language-tools/tree/master/packages/language-core) to support complex types for Vue Macros.
|
|
6
|
+
|
|
7
|
+
For example: fixes https://github.com/vuejs/core/issues/8286.
|
|
8
|
+
|
|
9
|
+
## 📦 Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
$ npm install -D unplugin-vue-complex-types
|
|
13
|
+
$ yarn add -D unplugin-vue-complex-types
|
|
14
|
+
$ pnpm add -D unplugin-vue-complex-types
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## TODOs
|
|
18
|
+
|
|
19
|
+
- [ ] Add more tests
|
|
20
|
+
|
|
21
|
+
## 🚀 Usage
|
|
22
|
+
|
|
23
|
+
<details>
|
|
24
|
+
<summary>Vite</summary><br>
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// vite.config.ts
|
|
28
|
+
import VueComplexTypes from "unplugin-vue-complex-types/vite";
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
plugins: [
|
|
32
|
+
VueComplexTypes({
|
|
33
|
+
/* options */
|
|
34
|
+
}),
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
<br></details>
|
|
40
|
+
|
|
41
|
+
<details>
|
|
42
|
+
<summary>Rollup</summary><br>
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
// rollup.config.js
|
|
46
|
+
import VueComplexTypes from "unplugin-vue-complex-types/rollup";
|
|
47
|
+
|
|
48
|
+
export default {
|
|
49
|
+
plugins: [
|
|
50
|
+
VueComplexTypes({
|
|
51
|
+
/* options */
|
|
52
|
+
}),
|
|
53
|
+
// other plugins
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
<br></details>
|
|
59
|
+
|
|
60
|
+
<details>
|
|
61
|
+
<summary>Webpack</summary><br>
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
// webpack.config.js
|
|
65
|
+
module.exports = {
|
|
66
|
+
/* ... */
|
|
67
|
+
plugins: [
|
|
68
|
+
require("unplugin-vue-complex-types/webpack")({
|
|
69
|
+
/* options */
|
|
70
|
+
}),
|
|
71
|
+
],
|
|
72
|
+
};
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
<br></details>
|
|
76
|
+
|
|
77
|
+
<details>
|
|
78
|
+
<summary>Rspack</summary><br>
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
// rspack.config.js
|
|
82
|
+
module.exports = {
|
|
83
|
+
/* ... */
|
|
84
|
+
plugins: [
|
|
85
|
+
require("unplugin-vue-complex-types/rspack")({
|
|
86
|
+
/* options */
|
|
87
|
+
}),
|
|
88
|
+
],
|
|
89
|
+
};
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
<br></details>
|
|
93
|
+
|
|
94
|
+
<details>
|
|
95
|
+
<summary>Nuxt</summary><br>
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
// nuxt.config.ts
|
|
99
|
+
export default defineNuxtConfig({
|
|
100
|
+
modules: ["unplugin-vue-complex-types/nuxt"],
|
|
101
|
+
complexTypes: {
|
|
102
|
+
/* options */
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
<br></details>
|
|
108
|
+
|
|
109
|
+
<details>
|
|
110
|
+
<summary>Vue CLI</summary><br>
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// vue.config.js
|
|
114
|
+
module.exports = {
|
|
115
|
+
configureWebpack: {
|
|
116
|
+
plugins: [
|
|
117
|
+
require("unplugin-vue-complex-types/webpack")({
|
|
118
|
+
/* options */
|
|
119
|
+
}),
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
<br></details>
|
|
126
|
+
|
|
127
|
+
<details>
|
|
128
|
+
<summary>Quasar</summary><br>
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
// quasar.conf.js [Vite]
|
|
132
|
+
module.exports = {
|
|
133
|
+
vitePlugins: [
|
|
134
|
+
[
|
|
135
|
+
"unplugin-vue-complex-types/vite",
|
|
136
|
+
{
|
|
137
|
+
/* options */
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
// quasar.conf.js [Webpack]
|
|
146
|
+
const VueComplexTypesPlugin = require("unplugin-vue-complex-types/webpack");
|
|
147
|
+
|
|
148
|
+
module.exports = {
|
|
149
|
+
build: {
|
|
150
|
+
chainWebpack(chain) {
|
|
151
|
+
chain.plugin("unplugin-vue-complex-types").use(
|
|
152
|
+
VueComplexTypesPlugin({
|
|
153
|
+
/* options */
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
<br></details>
|
|
162
|
+
|
|
163
|
+
<details>
|
|
164
|
+
<summary>esbuild</summary><br>
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
// esbuild.config.js
|
|
168
|
+
import { build } from "esbuild";
|
|
169
|
+
|
|
170
|
+
build({
|
|
171
|
+
/* ... */
|
|
172
|
+
plugins: [
|
|
173
|
+
require("unplugin-vue-complex-types/esbuild")({
|
|
174
|
+
/* options */
|
|
175
|
+
}),
|
|
176
|
+
],
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
<br></details>
|
|
181
|
+
|
|
182
|
+
<details>
|
|
183
|
+
<summary>Astro</summary><br>
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
// astro.config.mjs
|
|
187
|
+
import VueComplexTypes from "unplugin-vue-complex-types/astro";
|
|
188
|
+
|
|
189
|
+
export default defineConfig({
|
|
190
|
+
integrations: [
|
|
191
|
+
VueComplexTypes({
|
|
192
|
+
/* options */
|
|
193
|
+
}),
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
<br></details>
|
|
199
|
+
|
|
200
|
+
## 📚 Options
|
|
201
|
+
|
|
202
|
+
### `tsconfigPath`
|
|
203
|
+
|
|
204
|
+
Path to your `tsconfig.json`.
|
|
205
|
+
|
|
206
|
+
- Type: `string`
|
|
207
|
+
- Default: `path.join(process.cwd(), "tsconfig.json")`
|
|
208
|
+
|
|
209
|
+
### `defineEmits`
|
|
210
|
+
|
|
211
|
+
Transform `defineEmits` or not.
|
|
212
|
+
|
|
213
|
+
- Type: `boolean`
|
|
214
|
+
- Default: `true`
|
|
215
|
+
|
|
216
|
+
### `defineProps`
|
|
217
|
+
|
|
218
|
+
Transform `defineProps` or not.
|
|
219
|
+
|
|
220
|
+
- Type: `boolean`
|
|
221
|
+
- Default: `true`
|
|
222
|
+
|
|
223
|
+
## 📝 License
|
|
224
|
+
|
|
225
|
+
[MIT](./LICENSE). Made with ❤️ by [Ray](https://github.com/so1ve)
|
package/dist/astro.cjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const index = require('./index.cjs');
|
|
4
|
+
require('@vue.ts/common');
|
|
5
|
+
require('@vue.ts/language');
|
|
6
|
+
require('unplugin');
|
|
7
|
+
require('magic-string');
|
|
8
|
+
require('typescript');
|
|
9
|
+
require('node:path');
|
|
10
|
+
|
|
11
|
+
const astro = (options) => ({
|
|
12
|
+
name: "unplugin-vue-complex-types",
|
|
13
|
+
hooks: {
|
|
14
|
+
"astro:config:setup": async (astro) => {
|
|
15
|
+
var _a;
|
|
16
|
+
(_a = astro.config.vite).plugins || (_a.plugins = []);
|
|
17
|
+
astro.config.vite.plugins.push(index.vite(options));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
module.exports = astro;
|
package/dist/astro.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Options } from './types.cjs';
|
|
2
|
+
import '@vue.ts/common';
|
|
3
|
+
import 'magic-string';
|
|
4
|
+
import 'typescript';
|
|
5
|
+
|
|
6
|
+
declare const _default: (options: Options) => {
|
|
7
|
+
name: string;
|
|
8
|
+
hooks: {
|
|
9
|
+
"astro:config:setup": (astro: any) => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { _default as default };
|
package/dist/astro.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Options } from './types.mjs';
|
|
2
|
+
import '@vue.ts/common';
|
|
3
|
+
import 'magic-string';
|
|
4
|
+
import 'typescript';
|
|
5
|
+
|
|
6
|
+
declare const _default: (options: Options) => {
|
|
7
|
+
name: string;
|
|
8
|
+
hooks: {
|
|
9
|
+
"astro:config:setup": (astro: any) => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { _default as default };
|
package/dist/astro.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Options } from './types.js';
|
|
2
|
+
import '@vue.ts/common';
|
|
3
|
+
import 'magic-string';
|
|
4
|
+
import 'typescript';
|
|
5
|
+
|
|
6
|
+
declare const _default: (options: Options) => {
|
|
7
|
+
name: string;
|
|
8
|
+
hooks: {
|
|
9
|
+
"astro:config:setup": (astro: any) => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { _default as default };
|
package/dist/astro.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import unplugin from './index.mjs';
|
|
2
|
+
import '@vue.ts/common';
|
|
3
|
+
import '@vue.ts/language';
|
|
4
|
+
import 'unplugin';
|
|
5
|
+
import 'magic-string';
|
|
6
|
+
import 'typescript';
|
|
7
|
+
import 'node:path';
|
|
8
|
+
|
|
9
|
+
const astro = (options) => ({
|
|
10
|
+
name: "unplugin-vue-complex-types",
|
|
11
|
+
hooks: {
|
|
12
|
+
"astro:config:setup": async (astro) => {
|
|
13
|
+
var _a;
|
|
14
|
+
(_a = astro.config.vite).plugins || (_a.plugins = []);
|
|
15
|
+
astro.config.vite.plugins.push(unplugin.vite(options));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export { astro as default };
|
package/dist/esbuild.cjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const index = require('./index.cjs');
|
|
4
|
+
require('@vue.ts/common');
|
|
5
|
+
require('@vue.ts/language');
|
|
6
|
+
require('unplugin');
|
|
7
|
+
require('magic-string');
|
|
8
|
+
require('typescript');
|
|
9
|
+
require('node:path');
|
|
10
|
+
|
|
11
|
+
const esbuild = index.esbuild;
|
|
12
|
+
|
|
13
|
+
module.exports = esbuild;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { Options } from './types.cjs';
|
|
3
|
+
import '@vue.ts/common';
|
|
4
|
+
import 'magic-string';
|
|
5
|
+
import 'typescript';
|
|
6
|
+
|
|
7
|
+
declare const _default: (options?: Options | undefined) => unplugin.EsbuildPlugin;
|
|
8
|
+
|
|
9
|
+
export { _default as default };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { Options } from './types.mjs';
|
|
3
|
+
import '@vue.ts/common';
|
|
4
|
+
import 'magic-string';
|
|
5
|
+
import 'typescript';
|
|
6
|
+
|
|
7
|
+
declare const _default: (options?: Options | undefined) => unplugin.EsbuildPlugin;
|
|
8
|
+
|
|
9
|
+
export { _default as default };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { Options } from './types.js';
|
|
3
|
+
import '@vue.ts/common';
|
|
4
|
+
import 'magic-string';
|
|
5
|
+
import 'typescript';
|
|
6
|
+
|
|
7
|
+
declare const _default: (options?: Options | undefined) => unplugin.EsbuildPlugin;
|
|
8
|
+
|
|
9
|
+
export { _default as default };
|
package/dist/esbuild.mjs
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const common = require('@vue.ts/common');
|
|
4
|
+
const language = require('@vue.ts/language');
|
|
5
|
+
const unplugin$1 = require('unplugin');
|
|
6
|
+
const MagicString = require('magic-string');
|
|
7
|
+
const ts = require('typescript');
|
|
8
|
+
const node_path = require('node:path');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
11
|
+
|
|
12
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
13
|
+
const ts__default = /*#__PURE__*/_interopDefaultCompat(ts);
|
|
14
|
+
|
|
15
|
+
const resolveOptions = (rawOptions) => ({
|
|
16
|
+
include: rawOptions.include ?? ["**/*.vue"],
|
|
17
|
+
exclude: rawOptions.exclude ?? ["node_modules/**"],
|
|
18
|
+
tsconfigPath: rawOptions.tsconfigPath ?? node_path.join(process.cwd(), "tsconfig.json"),
|
|
19
|
+
defineEmits: rawOptions.defineEmits ?? true,
|
|
20
|
+
defineProps: rawOptions.defineProps ?? true
|
|
21
|
+
});
|
|
22
|
+
const quotesReg = /"/g;
|
|
23
|
+
const escapeQuotes = (s) => s.replace(quotesReg, '\\"');
|
|
24
|
+
|
|
25
|
+
class Printer {
|
|
26
|
+
constructor(checker) {
|
|
27
|
+
this.checker = checker;
|
|
28
|
+
}
|
|
29
|
+
getBaseType(nodeOrType) {
|
|
30
|
+
if ("kind" in nodeOrType) {
|
|
31
|
+
nodeOrType = this.checker.getTypeAtLocation(nodeOrType);
|
|
32
|
+
}
|
|
33
|
+
return this.checker.getBaseConstraintOfType(nodeOrType) ?? this.checker.getBaseTypeOfLiteralType(nodeOrType);
|
|
34
|
+
}
|
|
35
|
+
printIntersectionTypeNode(node) {
|
|
36
|
+
return node.types.map((t) => this.printTypeArg(t)).join(" & ");
|
|
37
|
+
}
|
|
38
|
+
printUnionTypeNode(node) {
|
|
39
|
+
return node.types.map((t) => this.printTypeArg(t)).join(" | ");
|
|
40
|
+
}
|
|
41
|
+
printTypeLiteralNode(node) {
|
|
42
|
+
const parts = ["{"];
|
|
43
|
+
for (const member of node.members) {
|
|
44
|
+
if (ts__default.isPropertySignature(member)) {
|
|
45
|
+
const stringBaseType = member.type ? this.checker.typeToString(this.getBaseType(member.type)) : "any";
|
|
46
|
+
parts.push(
|
|
47
|
+
[
|
|
48
|
+
member.name.getText(),
|
|
49
|
+
member.questionToken?.getText(),
|
|
50
|
+
": ",
|
|
51
|
+
stringBaseType
|
|
52
|
+
].filter(Boolean).join("")
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
parts.push("}");
|
|
57
|
+
return parts.join("\n");
|
|
58
|
+
}
|
|
59
|
+
printByType(node) {
|
|
60
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
61
|
+
const properties = type.getProperties();
|
|
62
|
+
const parts = ["{"];
|
|
63
|
+
let questionToken = "";
|
|
64
|
+
if (ts__default.isMappedTypeNode(node)) {
|
|
65
|
+
questionToken = node.questionToken?.getText() ?? "";
|
|
66
|
+
}
|
|
67
|
+
for (const property of properties) {
|
|
68
|
+
const valueType = this.checker.getTypeOfSymbol(property);
|
|
69
|
+
const stringValueType = this.checker.typeToString(
|
|
70
|
+
this.getBaseType(valueType)
|
|
71
|
+
);
|
|
72
|
+
parts.push(
|
|
73
|
+
`${this.checker.symbolToString(
|
|
74
|
+
property
|
|
75
|
+
)}${questionToken}: ${stringValueType}`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
parts.push("}");
|
|
79
|
+
return parts.join("\n");
|
|
80
|
+
}
|
|
81
|
+
printTypeArg(node) {
|
|
82
|
+
if (ts__default.isIntersectionTypeNode(node)) {
|
|
83
|
+
return this.printIntersectionTypeNode(node);
|
|
84
|
+
} else if (ts__default.isUnionTypeNode(node)) {
|
|
85
|
+
return this.printUnionTypeNode(node);
|
|
86
|
+
} else if (ts__default.isTypeLiteralNode(node)) {
|
|
87
|
+
return this.printTypeLiteralNode(node);
|
|
88
|
+
} else if (ts__default.isMappedTypeNode(node) || ts__default.isTypeReferenceNode(node)) {
|
|
89
|
+
return this.printByType(node);
|
|
90
|
+
} else {
|
|
91
|
+
console.error(
|
|
92
|
+
`[unplugin-vue-complex-types] \`${ts__default.SyntaxKind[node.kind]}\` is not supported.`
|
|
93
|
+
);
|
|
94
|
+
return node.getText();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
printEventsByCallSignatures(callSignatures) {
|
|
98
|
+
return callSignatures.map((c) => {
|
|
99
|
+
const parameters = c.getParameters();
|
|
100
|
+
const event = parameters[0];
|
|
101
|
+
return this.checker.typeToString(this.checker.getTypeOfSymbol(event));
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
printEventsByMembers(members) {
|
|
105
|
+
return members.map((m) => `"${escapeQuotes(m.getName())}"`);
|
|
106
|
+
}
|
|
107
|
+
printEventsRuntimeArg(node) {
|
|
108
|
+
const parts = [];
|
|
109
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
110
|
+
const callSignatures = type.getCallSignatures();
|
|
111
|
+
const members = type.getProperties();
|
|
112
|
+
if (callSignatures.length > 0 && members.length > 0) {
|
|
113
|
+
throw new Error(
|
|
114
|
+
"[unplugin-vue-complex-types] You may not use old style `defineEmits` and `defineEmits` shorthand together."
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
if (members.length > 0) {
|
|
118
|
+
parts.push(...this.printEventsByMembers(members));
|
|
119
|
+
} else {
|
|
120
|
+
parts.push(...this.printEventsByCallSignatures(callSignatures));
|
|
121
|
+
}
|
|
122
|
+
return `[${parts.join(", ")}]`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const transformDefineEmits = (printer, s, id) => {
|
|
127
|
+
const language$1 = language.getLanguage();
|
|
128
|
+
const defineEmits = language$1.findNodeByRange(
|
|
129
|
+
id,
|
|
130
|
+
(scriptSetupRanges) => scriptSetupRanges.emits.define
|
|
131
|
+
);
|
|
132
|
+
if (!defineEmits) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const scriptSetupAst = language$1.getScriptSetupAst(id);
|
|
136
|
+
const {
|
|
137
|
+
scriptNode: defineEmitsNode,
|
|
138
|
+
virtualFileNode: virtualFileDefineEmitsNode,
|
|
139
|
+
offset
|
|
140
|
+
} = defineEmits;
|
|
141
|
+
const defineEmitsTypeArg = ts__default.isCallExpression(virtualFileDefineEmitsNode) && virtualFileDefineEmitsNode.typeArguments?.[0];
|
|
142
|
+
const defineEmitsRuntimeArg = ts__default.isCallExpression(virtualFileDefineEmitsNode) && virtualFileDefineEmitsNode.arguments[0];
|
|
143
|
+
if (!defineEmitsTypeArg) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (defineEmitsRuntimeArg) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
"[unplugin-vue-complex-types] `defineEmits` cannot accept both runtime argument and type argument."
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
const tokens = defineEmitsNode.getChildren(scriptSetupAst);
|
|
152
|
+
const lessThanToken = tokens.find(
|
|
153
|
+
(t) => t.kind === ts__default.SyntaxKind.LessThanToken
|
|
154
|
+
);
|
|
155
|
+
const greaterThanToken = tokens.find(
|
|
156
|
+
(t) => t.kind === ts__default.SyntaxKind.GreaterThanToken
|
|
157
|
+
);
|
|
158
|
+
const openParenToken = tokens.find(
|
|
159
|
+
(t) => t.kind === ts__default.SyntaxKind.OpenParenToken
|
|
160
|
+
);
|
|
161
|
+
if (!lessThanToken || !greaterThanToken || !openParenToken) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const defineEmitsTypeArgRange = [
|
|
165
|
+
offset + lessThanToken.pos,
|
|
166
|
+
offset + greaterThanToken.end
|
|
167
|
+
];
|
|
168
|
+
const runtimeArgPos = offset + openParenToken.end;
|
|
169
|
+
const printedRuntimeArg = printer.printEventsRuntimeArg(defineEmitsTypeArg);
|
|
170
|
+
s.remove(...defineEmitsTypeArgRange);
|
|
171
|
+
s.appendRight(runtimeArgPos, printedRuntimeArg);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const transformDefineProps = (printer, s, id) => {
|
|
175
|
+
const language$1 = language.getLanguage();
|
|
176
|
+
const definePropsTypeArg = language$1.findNodeByRange(
|
|
177
|
+
id,
|
|
178
|
+
(scriptSetupRanges) => scriptSetupRanges.props.define?.typeArg
|
|
179
|
+
);
|
|
180
|
+
if (!definePropsTypeArg) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const { virtualFileNode: typeArgNode, setupRange: typeArgRange } = definePropsTypeArg;
|
|
184
|
+
const printedType = printer.printTypeArg(typeArgNode);
|
|
185
|
+
s.overwrite(typeArgRange.start, typeArgRange.end, printedType);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const transformers = [
|
|
189
|
+
["defineEmits", transformDefineEmits],
|
|
190
|
+
["defineProps", transformDefineProps]
|
|
191
|
+
];
|
|
192
|
+
const getTransformers = (options) => transformers.filter(([key]) => !!options[key]);
|
|
193
|
+
|
|
194
|
+
function transform(code, id, options) {
|
|
195
|
+
const s = new MagicString__default(code);
|
|
196
|
+
const language$1 = language.getLanguage();
|
|
197
|
+
const typeChecker = language$1.__internal__.typeChecker;
|
|
198
|
+
const printer = new Printer(typeChecker);
|
|
199
|
+
const transformers = getTransformers(options);
|
|
200
|
+
for (const [, transform2] of transformers) {
|
|
201
|
+
transform2(printer, s, id);
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
code: s.toString(),
|
|
205
|
+
map: s.generateMap({ hires: true })
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const unplugin = unplugin$1.createUnplugin((options = {}) => ({
|
|
210
|
+
name: "@vue.ts/complex-types",
|
|
211
|
+
buildStart() {
|
|
212
|
+
const resolvedOptions = resolveOptions(options);
|
|
213
|
+
language.ensureLanguage(resolvedOptions.tsconfigPath);
|
|
214
|
+
},
|
|
215
|
+
transform(code, id) {
|
|
216
|
+
const resolvedOptions = resolveOptions(options);
|
|
217
|
+
const filter = common.createFilter(
|
|
218
|
+
resolvedOptions.include,
|
|
219
|
+
resolvedOptions.exclude
|
|
220
|
+
);
|
|
221
|
+
if (!filter(id)) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
return transform(code, id, resolvedOptions);
|
|
225
|
+
}
|
|
226
|
+
}));
|
|
227
|
+
|
|
228
|
+
module.exports = unplugin;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { Options } from './types.cjs';
|
|
3
|
+
import '@vue.ts/common';
|
|
4
|
+
import 'magic-string';
|
|
5
|
+
import 'typescript';
|
|
6
|
+
|
|
7
|
+
declare const _default: unplugin.UnpluginInstance<Options | undefined, boolean>;
|
|
8
|
+
|
|
9
|
+
export { _default as default };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { Options } from './types.mjs';
|
|
3
|
+
import '@vue.ts/common';
|
|
4
|
+
import 'magic-string';
|
|
5
|
+
import 'typescript';
|
|
6
|
+
|
|
7
|
+
declare const _default: unplugin.UnpluginInstance<Options | undefined, boolean>;
|
|
8
|
+
|
|
9
|
+
export { _default as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { Options } from './types.js';
|
|
3
|
+
import '@vue.ts/common';
|
|
4
|
+
import 'magic-string';
|
|
5
|
+
import 'typescript';
|
|
6
|
+
|
|
7
|
+
declare const _default: unplugin.UnpluginInstance<Options | undefined, boolean>;
|
|
8
|
+
|
|
9
|
+
export { _default as default };
|