@tsrx/vue 0.3.5 → 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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Vue compiler built on @tsrx/core",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.3.5",
6
+ "version": "0.4.0",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -35,21 +35,22 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "esrap": "^2.3.2",
38
+ "esrap": "^2.4.0",
39
39
  "is-reference": "^3.0.3",
40
40
  "zimmerframe": "^1.1.2",
41
- "@tsrx/core": "0.5.5",
42
- "@tsrx/vue-runtime": "0.1.13"
41
+ "@tsrx/core": "0.6.0",
42
+ "@tsrx/vue-runtime": "0.2.0"
43
43
  },
44
44
  "peerDependencies": {
45
- "vue": ">=3.5",
46
- "vue-jsx-vapor": ">=3.2.14"
45
+ "vue": ">=3.6.0-0",
46
+ "vue-jsx-vapor": ">=3.2.27"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/estree": "^1.0.8",
50
50
  "@types/estree-jsx": "^1.0.5",
51
51
  "typescript": "^5.9.3",
52
- "vue": "3.6.0-beta.12"
52
+ "vue": "3.6.0-rc.14",
53
+ "vue-jsx-vapor": "^3.2.29"
53
54
  },
54
55
  "files": [
55
56
  "src",
package/src/index.js CHANGED
@@ -117,5 +117,7 @@ export function compile_to_volar_mappings(source, filename, options) {
117
117
  ...result,
118
118
  mappings: dedupeMappings(result.mappings),
119
119
  };
120
- return uses_platform_flags ? withPlatformTypes(deduped, options?.platform) : deduped;
120
+ return uses_platform_flags
121
+ ? withPlatformTypes(deduped, options?.platform, transformed.ast)
122
+ : deduped;
121
123
  }
package/src/interop.js CHANGED
@@ -1,3 +1,7 @@
1
+ /** @import * as AST from 'estree' */
2
+
3
+ import { parseModule } from '@tsrx/core';
4
+
1
5
  const vue_import_pattern = /import(?!\s+type)\s*\{([\s\S]*?)\}\s*from\s*(['"])vue\2\s*;?/g;
2
6
 
3
7
  /**
@@ -15,6 +19,139 @@ export function addVaporInteropToCreateVaporApp(source) {
15
19
  return source;
16
20
  }
17
21
 
22
+ /** @type {AST.Program} */
23
+ let program;
24
+ try {
25
+ program = parseModule(source, 'module.tsx');
26
+ } catch {
27
+ // The TSRX parser rejects some valid modules, such as a `<number>value`
28
+ // type assertion. Match the import text instead, as before.
29
+ return add_interop_by_pattern(source);
30
+ }
31
+ return add_interop_to_imports(source, program);
32
+ }
33
+
34
+ /**
35
+ * Rewrite the real `vue` import declarations of a parsed module, so import-like
36
+ * text in strings, template literals, comments and regular expressions is
37
+ * never touched.
38
+ *
39
+ * @param {string} source
40
+ * @param {AST.Program} program
41
+ * @returns {string}
42
+ */
43
+ function add_interop_to_imports(source, program) {
44
+ const vue_imports = program.body.filter(
45
+ /** @returns {node is AST.ImportDeclaration} */
46
+ (node) =>
47
+ node.type === 'ImportDeclaration' &&
48
+ node.source.value === 'vue' &&
49
+ /** @type {{ importKind?: string }} */ (node).importKind !== 'type',
50
+ );
51
+
52
+ /** @type {string | null} */
53
+ let existing_interop_local = null;
54
+ for (const declaration of vue_imports) {
55
+ for (const specifier of declaration.specifiers) {
56
+ if (is_value_import(specifier, 'vaporInteropPlugin')) {
57
+ existing_interop_local = specifier.local.name;
58
+ }
59
+ }
60
+ }
61
+
62
+ if (existing_interop_local && source.includes(`.use(${existing_interop_local})`)) {
63
+ return source;
64
+ }
65
+
66
+ const interop_local = existing_interop_local ?? 'vaporInteropPlugin';
67
+ let added_interop_import = existing_interop_local !== null;
68
+ /** @type {Array<{ start: number, end: number, text: string }>} */
69
+ const replacements = [];
70
+
71
+ for (const declaration of vue_imports) {
72
+ /** @type {string[]} */
73
+ const wrappers = [];
74
+ /** @type {string | null} */
75
+ let default_import = null;
76
+ /** @type {string[]} */
77
+ const named_imports = [];
78
+
79
+ for (const specifier of declaration.specifiers) {
80
+ if (specifier.type !== 'ImportSpecifier') {
81
+ default_import = slice(source, specifier);
82
+ } else if (is_value_import(specifier, 'createVaporApp')) {
83
+ const wrapped_local = `__tsrx_${specifier.local.name}`;
84
+ wrappers.push(
85
+ `const ${specifier.local.name} = (...args) => ${wrapped_local}(...args).use(${interop_local});`,
86
+ );
87
+ named_imports.push(`createVaporApp as ${wrapped_local}`);
88
+ } else {
89
+ named_imports.push(slice(source, specifier));
90
+ }
91
+ }
92
+
93
+ if (wrappers.length === 0) continue;
94
+ if (!added_interop_import) {
95
+ named_imports.push(interop_local);
96
+ added_interop_import = true;
97
+ }
98
+
99
+ // The source literal through the end keeps its quotes and any import
100
+ // attributes.
101
+ let tail = source.slice(start_of(declaration.source), end_of(declaration));
102
+ if (!tail.endsWith(';')) tail += ';';
103
+ const bindings = `${default_import === null ? '' : `${default_import}, `}{ ${named_imports.join(', ')} }`;
104
+ replacements.push({
105
+ start: start_of(declaration),
106
+ end: end_of(declaration),
107
+ text: `import ${bindings} from ${tail}\n${wrappers.join('\n')}`,
108
+ });
109
+ }
110
+
111
+ let result = source;
112
+ for (const { start, end, text } of replacements.reverse()) {
113
+ result = result.slice(0, start) + text + result.slice(end);
114
+ }
115
+ return result;
116
+ }
117
+
118
+ /**
119
+ * @param {AST.ImportDeclaration['specifiers'][number]} specifier
120
+ * @param {string} name
121
+ * @returns {specifier is AST.ImportSpecifier}
122
+ */
123
+ function is_value_import(specifier, name) {
124
+ return (
125
+ specifier.type === 'ImportSpecifier' &&
126
+ /** @type {{ importKind?: string }} */ (specifier).importKind !== 'type' &&
127
+ specifier.imported.type === 'Identifier' &&
128
+ specifier.imported.name === name
129
+ );
130
+ }
131
+
132
+ /** @param {AST.Node} node */
133
+ function start_of(node) {
134
+ return /** @type {AST.Node & { start: number }} */ (node).start;
135
+ }
136
+
137
+ /** @param {AST.Node} node */
138
+ function end_of(node) {
139
+ return /** @type {AST.Node & { end: number }} */ (node).end;
140
+ }
141
+
142
+ /** @param {string} source @param {AST.Node} node */
143
+ function slice(source, node) {
144
+ return source.slice(start_of(node), end_of(node));
145
+ }
146
+
147
+ /**
148
+ * The rewrite for a module the parser rejects: the same edit, applied to the
149
+ * matches of an import pattern over the raw source.
150
+ *
151
+ * @param {string} source
152
+ * @returns {string}
153
+ */
154
+ function add_interop_by_pattern(source) {
18
155
  /** @type {string | null} */
19
156
  let existing_interop_local = null;
20
157
 
@@ -0,0 +1,60 @@
1
+ /** @import * as AST from 'estree' */
2
+
3
+ import { isFunctionNode } from '@tsrx/core';
4
+
5
+ const AST_METADATA_KEYS = new Set(['loc', 'start', 'end', 'metadata']);
6
+
7
+ /**
8
+ * The `yield`s that vue-jsx-vapor moves out of their generator. Vapor compiles
9
+ * each element into an immediately called arrow that creates it, and its
10
+ * dynamic parts into render effects and prop getters, so a `yield` anywhere in
11
+ * an element or fragment ends up in a function that isn't its generator.
12
+ *
13
+ * This follows vue-jsx-vapor 3.2.14 (@vue-jsx-vapor/compiler-rs 3.2.14).
14
+ * `packages/vite-plugin-vue/tests/template-yield-downstream.test.js` runs
15
+ * probes through the installed compiler, so a release that compiles them
16
+ * differently fails there.
17
+ *
18
+ * @param {AST.Program} program
19
+ * @returns {AST.YieldExpression[]}
20
+ */
21
+ export function find_deferred_template_yields(program) {
22
+ /** @type {AST.YieldExpression[]} */
23
+ const found = [];
24
+
25
+ /**
26
+ * @param {AST.Node} node
27
+ * @param {boolean} in_generator whether the nearest function is a generator
28
+ * @param {boolean} in_template whether the node is inside an element or fragment
29
+ */
30
+ const visit = (node, in_generator, in_template) => {
31
+ if (isFunctionNode(node)) {
32
+ in_generator = !!node.generator;
33
+ in_template = false;
34
+ } else if (node.type === 'YieldExpression' && in_generator && in_template) {
35
+ found.push(node);
36
+ } else if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
37
+ in_template = true;
38
+ }
39
+ for_each_child(node, (child) => visit(child, in_generator, in_template));
40
+ };
41
+
42
+ visit(program, false, false);
43
+ return found;
44
+ }
45
+
46
+ /**
47
+ * @param {AST.Node} node
48
+ * @param {(child: AST.Node) => void} visit
49
+ */
50
+ function for_each_child(node, visit) {
51
+ for (const key of Object.keys(node)) {
52
+ if (AST_METADATA_KEYS.has(key)) continue;
53
+ const value = /** @type {unknown} */ (/** @type {Record<string, unknown>} */ (node)[key]);
54
+ for (const child of Array.isArray(value) ? value : [value]) {
55
+ if (child && typeof child === 'object' && 'type' in child) {
56
+ visit(/** @type {AST.Node} */ (child));
57
+ }
58
+ }
59
+ }
60
+ }