@vue-jsx-vapor/macros 3.2.0 → 3.2.1

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/dist/volar.cjs CHANGED
@@ -23,7 +23,7 @@ const __setup = `);
23
23
  type __Slots = Parameters<__Setup>[1] extends { slots?: infer Slots } | undefined ? Slots : {}
24
24
  type __Exposed = Parameters<__Setup>[1] extends { expose?: (exposed: infer Exposed) => any } | undefined ? Exposed : {}`, "\n return ", [node.expression.getText(ast), node.expression.getStart(ast)], `({\n ...{} as {
25
25
  setup: (props: __Props) => __Exposed,
26
- render: () => ReturnType<__Setup>
26
+ render: () => ReturnType<__Setup>,
27
27
  slots: ${isVaporComponent ? "__Slots" : `import('vue').SlotsType<__Slots>`}
28
28
  },`, ...compOptions ? ["...", [compOptions.getText(ast), compOptions.getStart(ast)]] : [], `})`, `\n})()`);
29
29
  }
@@ -242,8 +242,7 @@ function getMacro(node, ts, options) {
242
242
  function getRootMap(options) {
243
243
  const { ts, ast, codes } = options;
244
244
  const rootMap = /* @__PURE__ */ new Map();
245
- let prevRoot;
246
- function walk(node, parents) {
245
+ function walk(node, parents, scopes) {
247
246
  const root = parents[1] && (ts.isArrowFunction(parents[1]) || ts.isFunctionExpression(parents[1]) || ts.isFunctionDeclaration(parents[1])) ? parents[1] : void 0;
248
247
  function getDefineComponentCaller(node) {
249
248
  if (!node) return;
@@ -260,8 +259,6 @@ function getRootMap(options) {
260
259
  }
261
260
  }
262
261
  }
263
- const fnRoot = ts.isArrowFunction(node) || ts.isFunctionExpression(node) || ts.isFunctionDeclaration(node) ? node : void 0;
264
- if (fnRoot && !getDefineComponentCaller(parents[3])) prevRoot = fnRoot;
265
262
  const macro = getMacro(node, ts, options);
266
263
  if (macro) {
267
264
  const { expression, initializer, variableDeclaration } = macro;
@@ -303,17 +300,21 @@ function getRootMap(options) {
303
300
  }
304
301
  }
305
302
  const slotNode = ts.isJsxElement(node) ? node.openingElement : ts.isJsxSelfClosingElement(node) ? node : null;
306
- if (prevRoot && slotNode?.tagName.getText(ast) === "slot") {
307
- if (!rootMap.has(prevRoot)) rootMap.set(prevRoot, {});
308
- (rootMap.get(prevRoot).slots ??= []).push(slotNode);
303
+ const scope = scopes[0];
304
+ if (scope && slotNode?.tagName.getText(ast) === "slot") {
305
+ if (!rootMap.has(scope)) rootMap.set(scope, {});
306
+ (rootMap.get(scope).slots ??= []).push(slotNode);
309
307
  }
310
308
  node.forEachChild((child) => {
311
309
  parents.unshift(node);
312
- walk(child, parents);
310
+ const scopeNode = (ts.isArrowFunction(child) || ts.isFunctionExpression(child) || ts.isFunctionDeclaration(child)) && !ts.isReturnStatement(node);
311
+ if (scopeNode) scopes.unshift(child);
312
+ walk(child, parents, scopes);
313
313
  parents.shift();
314
+ if (scopeNode) scopes.shift();
314
315
  });
315
316
  }
316
- ast.forEachChild((node) => walk(node, []));
317
+ ast.forEachChild((node) => walk(node, [], []));
317
318
  return rootMap;
318
319
  }
319
320
  function toValidAssetId(name, type) {
@@ -341,6 +342,15 @@ const plugin = (0, ts_macro.createPlugin)(({ ts }, userOptions = {}) => {
341
342
  };
342
343
  const rootMap = getRootMap(options);
343
344
  if (rootMap.size) transformJsxMacros(rootMap, options);
345
+ /**
346
+ * JSX and inline import types co-usage will break TS.
347
+ * So use `import ''` to fixed it
348
+ * ```tsx
349
+ * const foo = <div></div>
350
+ * const bar = {} as import('vue').ShallowUnwrapRef<any>
351
+ * ```
352
+ */
353
+ codes.unshift("import '';\n");
344
354
  codes.push(getGlobalTypes(rootMap, options));
345
355
  }
346
356
  };
package/dist/volar.js CHANGED
@@ -21,7 +21,7 @@ const __setup = `);
21
21
  type __Slots = Parameters<__Setup>[1] extends { slots?: infer Slots } | undefined ? Slots : {}
22
22
  type __Exposed = Parameters<__Setup>[1] extends { expose?: (exposed: infer Exposed) => any } | undefined ? Exposed : {}`, "\n return ", [node.expression.getText(ast), node.expression.getStart(ast)], `({\n ...{} as {
23
23
  setup: (props: __Props) => __Exposed,
24
- render: () => ReturnType<__Setup>
24
+ render: () => ReturnType<__Setup>,
25
25
  slots: ${isVaporComponent ? "__Slots" : `import('vue').SlotsType<__Slots>`}
26
26
  },`, ...compOptions ? ["...", [compOptions.getText(ast), compOptions.getStart(ast)]] : [], `})`, `\n})()`);
27
27
  }
@@ -240,8 +240,7 @@ function getMacro(node, ts, options) {
240
240
  function getRootMap(options) {
241
241
  const { ts, ast, codes } = options;
242
242
  const rootMap = /* @__PURE__ */ new Map();
243
- let prevRoot;
244
- function walk(node, parents) {
243
+ function walk(node, parents, scopes) {
245
244
  const root = parents[1] && (ts.isArrowFunction(parents[1]) || ts.isFunctionExpression(parents[1]) || ts.isFunctionDeclaration(parents[1])) ? parents[1] : void 0;
246
245
  function getDefineComponentCaller(node) {
247
246
  if (!node) return;
@@ -258,8 +257,6 @@ function getRootMap(options) {
258
257
  }
259
258
  }
260
259
  }
261
- const fnRoot = ts.isArrowFunction(node) || ts.isFunctionExpression(node) || ts.isFunctionDeclaration(node) ? node : void 0;
262
- if (fnRoot && !getDefineComponentCaller(parents[3])) prevRoot = fnRoot;
263
260
  const macro = getMacro(node, ts, options);
264
261
  if (macro) {
265
262
  const { expression, initializer, variableDeclaration } = macro;
@@ -301,17 +298,21 @@ function getRootMap(options) {
301
298
  }
302
299
  }
303
300
  const slotNode = ts.isJsxElement(node) ? node.openingElement : ts.isJsxSelfClosingElement(node) ? node : null;
304
- if (prevRoot && slotNode?.tagName.getText(ast) === "slot") {
305
- if (!rootMap.has(prevRoot)) rootMap.set(prevRoot, {});
306
- (rootMap.get(prevRoot).slots ??= []).push(slotNode);
301
+ const scope = scopes[0];
302
+ if (scope && slotNode?.tagName.getText(ast) === "slot") {
303
+ if (!rootMap.has(scope)) rootMap.set(scope, {});
304
+ (rootMap.get(scope).slots ??= []).push(slotNode);
307
305
  }
308
306
  node.forEachChild((child) => {
309
307
  parents.unshift(node);
310
- walk(child, parents);
308
+ const scopeNode = (ts.isArrowFunction(child) || ts.isFunctionExpression(child) || ts.isFunctionDeclaration(child)) && !ts.isReturnStatement(node);
309
+ if (scopeNode) scopes.unshift(child);
310
+ walk(child, parents, scopes);
311
311
  parents.shift();
312
+ if (scopeNode) scopes.shift();
312
313
  });
313
314
  }
314
- ast.forEachChild((node) => walk(node, []));
315
+ ast.forEachChild((node) => walk(node, [], []));
315
316
  return rootMap;
316
317
  }
317
318
  function toValidAssetId(name, type) {
@@ -339,6 +340,15 @@ const plugin = createPlugin(({ ts }, userOptions = {}) => {
339
340
  };
340
341
  const rootMap = getRootMap(options);
341
342
  if (rootMap.size) transformJsxMacros(rootMap, options);
343
+ /**
344
+ * JSX and inline import types co-usage will break TS.
345
+ * So use `import ''` to fixed it
346
+ * ```tsx
347
+ * const foo = <div></div>
348
+ * const bar = {} as import('vue').ShallowUnwrapRef<any>
349
+ * ```
350
+ */
351
+ codes.unshift("import '';\n");
342
352
  codes.push(getGlobalTypes(rootMap, options));
343
353
  }
344
354
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vue-jsx-vapor/macros",
3
3
  "type": "module",
4
- "version": "3.2.0",
4
+ "version": "3.2.1",
5
5
  "description": "Macros for Vue JSX Vapor",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/vuejs/vue-jsx-vapor#readme",