@vue-macros/volar
Advanced tools
Comparing version 0.18.14 to 0.18.15
@@ -5,3 +5,3 @@ // src/jsx-directive.ts | ||
// src/jsx-directive/v-slot.ts | ||
var import_language_core2 = require("@vue/language-core"); | ||
var import_language_core3 = require("@vue/language-core"); | ||
@@ -65,2 +65,87 @@ // src/common.ts | ||
// src/jsx-directive/v-for.ts | ||
var import_language_core2 = require("@vue/language-core"); | ||
function resolveVFor(attribute, { ts, sfc, source }) { | ||
const result = []; | ||
if (attribute.initializer && ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression && ts.isBinaryExpression(attribute.initializer.expression)) { | ||
let index; | ||
let objectIndex; | ||
let item = attribute.initializer.expression.left; | ||
const list = attribute.initializer.expression.right; | ||
if (ts.isParenthesizedExpression(item)) { | ||
if (ts.isBinaryExpression(item.expression)) { | ||
if (ts.isBinaryExpression(item.expression.left)) { | ||
index = item.expression.left.right; | ||
objectIndex = item.expression.right; | ||
item = item.expression.left.left; | ||
} else { | ||
index = item.expression.right; | ||
item = item.expression.left; | ||
} | ||
} else { | ||
item = item.expression; | ||
} | ||
} | ||
if (item && list) { | ||
result.push( | ||
"__VLS_getVForSourceType(", | ||
[ | ||
sfc[source].content.slice(list.getStart(sfc[source]?.ast), list.end), | ||
source, | ||
list.getStart(sfc[source]?.ast), | ||
import_language_core2.FileRangeCapabilities.full | ||
], | ||
").map(([", | ||
[ | ||
`${sfc[source]?.content.slice(item.getStart(sfc[source]?.ast), item.end)}`, | ||
source, | ||
item.getStart(sfc[source]?.ast), | ||
import_language_core2.FileRangeCapabilities.full | ||
], | ||
", ", | ||
index ? [ | ||
`${sfc[source]?.content.slice(index.getStart(sfc[source]?.ast), index.end)}`, | ||
source, | ||
index.getStart(sfc[source]?.ast), | ||
import_language_core2.FileRangeCapabilities.full | ||
] : objectIndex ? "undefined" : "", | ||
...objectIndex ? [ | ||
", ", | ||
[ | ||
`${sfc[source]?.content.slice(objectIndex.getStart(sfc[source]?.ast), objectIndex.end)}`, | ||
source, | ||
objectIndex.getStart(sfc[source]?.ast), | ||
import_language_core2.FileRangeCapabilities.full | ||
] | ||
] : "", | ||
"]) => " | ||
); | ||
} | ||
} | ||
return result; | ||
} | ||
function transformVFor({ | ||
nodes, | ||
codes, | ||
ts, | ||
sfc, | ||
source | ||
}) { | ||
nodes.forEach(({ attribute, node, parent }) => { | ||
const result = resolveVFor(attribute, { ts, sfc, source }); | ||
if (parent) { | ||
result.unshift("{"); | ||
} | ||
(0, import_language_core2.replaceSourceRange)(codes, source, node.pos, node.pos, ...result); | ||
(0, import_language_core2.replaceSourceRange)( | ||
codes, | ||
source, | ||
node.end - 1, | ||
node.end, | ||
`>)${parent ? "}" : ""}` | ||
); | ||
(0, import_language_core2.replaceSourceRange)(codes, source, attribute.pos, attribute.end); | ||
}); | ||
} | ||
// src/jsx-directive/v-slot.ts | ||
@@ -81,69 +166,86 @@ function transformVSlot({ | ||
const attributes = Array.from(attributeMap); | ||
attributes.forEach(([attribute, { children, vIfAttribute }], index) => { | ||
if (!attribute) | ||
return; | ||
const vIfAttributeName = vIfAttribute?.name.getText(sfc[source]?.ast); | ||
if (vIfAttribute && vIfAttributeName) { | ||
if ("v-if" === vIfAttributeName) { | ||
result.push("..."); | ||
attributes.forEach( | ||
([attribute, { children, vIfAttribute, vForAttribute }], index) => { | ||
if (!attribute) | ||
return; | ||
const vIfAttributeName = vIfAttribute?.name.getText(sfc[source]?.ast); | ||
if (vIfAttribute && vIfAttributeName) { | ||
if ("v-if" === vIfAttributeName) { | ||
result.push("..."); | ||
} | ||
if (["v-if", "v-else-if"].includes(vIfAttributeName) && vIfAttribute.initializer && ts.isJsxExpression(vIfAttribute.initializer) && vIfAttribute.initializer.expression) { | ||
result.push( | ||
"(", | ||
[ | ||
vIfAttribute.initializer.expression.getText(sfc[source]?.ast), | ||
source, | ||
vIfAttribute.initializer.expression.getStart(sfc[source]?.ast), | ||
import_language_core3.FileRangeCapabilities.full | ||
], | ||
") ? {" | ||
); | ||
} else if ("v-else" === vIfAttributeName) { | ||
result.push("{"); | ||
} | ||
} | ||
if (["v-if", "v-else-if"].includes(vIfAttributeName) && vIfAttribute.initializer && ts.isJsxExpression(vIfAttribute.initializer) && vIfAttribute.initializer.expression) { | ||
if (vForAttribute) { | ||
result.push( | ||
"(", | ||
[ | ||
vIfAttribute.initializer.expression.getText(sfc[source]?.ast), | ||
source, | ||
vIfAttribute.initializer.expression.getStart(sfc[source]?.ast), | ||
import_language_core2.FileRangeCapabilities.full | ||
], | ||
") ? {" | ||
"...", | ||
...resolveVFor(vForAttribute, { ts, sfc, source }), | ||
"({" | ||
); | ||
} else if ("v-else" === vIfAttributeName) { | ||
result.push("{"); | ||
} | ||
} | ||
let attributeName = attribute.name?.getText(sfc[source]?.ast).slice(6).split(" ")[0]; | ||
const isNamespace = attributeName.startsWith(":"); | ||
attributeName = attributeName.slice(1); | ||
result.push( | ||
isNamespace ? [ | ||
`'${attributeName}'`, | ||
source, | ||
attribute.name.getStart(sfc[source]?.ast) + 6, | ||
import_language_core2.FileRangeCapabilities.full | ||
] : "default", | ||
`: (`, | ||
attribute.initializer && ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression ? [ | ||
attribute.initializer.expression.getText(sfc[source]?.ast), | ||
source, | ||
attribute.initializer.expression.getStart(sfc[source]?.ast), | ||
import_language_core2.FileRangeCapabilities.full | ||
] : "", | ||
") => <>", | ||
...children.map((child) => { | ||
(0, import_language_core2.replaceSourceRange)(codes, source, child.pos, child.end); | ||
const node2 = ts.isJsxElement(child) && child.openingElement.tagName.getText(sfc[source]?.ast) === "template" ? child.children : child; | ||
return ts.isJsxSelfClosingElement(child) ? "" : [ | ||
sfc[source].content.slice(node2.pos, node2.end), | ||
let isDynamic = false; | ||
let attributeName = attribute.name?.getText(sfc[source]?.ast).slice(6).split(" ")[0].replace(/\$(.*)\$/, (_, $1) => { | ||
isDynamic = true; | ||
return $1; | ||
}); | ||
const isNamespace = attributeName.startsWith(":"); | ||
attributeName = attributeName.slice(1); | ||
result.push( | ||
isNamespace ? [ | ||
isDynamic ? `[${attributeName}]` : `'${attributeName}'`, | ||
source, | ||
node2.pos, | ||
import_language_core2.FileRangeCapabilities.full | ||
]; | ||
}), | ||
"</>," | ||
); | ||
if (vIfAttribute && vIfAttributeName) { | ||
if (["v-if", "v-else-if"].includes(vIfAttributeName)) { | ||
const nextIndex = index + (attributes[index + 1]?.[0] ? 1 : 2); | ||
result.push( | ||
"}", | ||
`${attributes[nextIndex]?.[1].vIfAttribute?.name.getText( | ||
sfc[source]?.ast | ||
)}`.startsWith("v-else") ? " : " : " : null," | ||
); | ||
} else if ("v-else" === vIfAttributeName) { | ||
result.push("},"); | ||
attribute.name.getStart(sfc[source]?.ast) + (isDynamic ? 7 : 6), | ||
import_language_core3.FileRangeCapabilities.full | ||
] : "default", | ||
`: (`, | ||
attribute.initializer && ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression ? [ | ||
attribute.initializer.expression.getText(sfc[source]?.ast), | ||
source, | ||
attribute.initializer.expression.getStart(sfc[source]?.ast), | ||
import_language_core3.FileRangeCapabilities.full | ||
] : "", | ||
isDynamic ? ": any" : "", | ||
") => <>", | ||
...children.map((child) => { | ||
(0, import_language_core3.replaceSourceRange)(codes, source, child.pos, child.end); | ||
const node2 = ts.isJsxElement(child) && child.openingElement.tagName.getText(sfc[source]?.ast) === "template" ? child.children : child; | ||
return ts.isJsxSelfClosingElement(child) ? "" : [ | ||
sfc[source].content.slice(node2.pos, node2.end), | ||
source, | ||
node2.pos, | ||
import_language_core3.FileRangeCapabilities.full | ||
]; | ||
}), | ||
"</>," | ||
); | ||
if (vIfAttribute && vIfAttributeName) { | ||
if (["v-if", "v-else-if"].includes(vIfAttributeName)) { | ||
const nextIndex = index + (attributes[index + 1]?.[0] ? 1 : 2); | ||
result.push( | ||
"}", | ||
`${attributes[nextIndex]?.[1].vIfAttribute?.name.getText( | ||
sfc[source]?.ast | ||
)}`.startsWith("v-else") ? " : " : " : null," | ||
); | ||
} else if ("v-else" === vIfAttributeName) { | ||
result.push("},"); | ||
} | ||
} | ||
if (vForAttribute) { | ||
result.push("}))"); | ||
} | ||
} | ||
}); | ||
); | ||
const tagName = ts.isJsxSelfClosingElement(node) ? node.tagName.getText(sfc[source]?.ast) : ts.isJsxElement(node) ? node.openingElement.tagName.getText(sfc[source]?.ast) : null; | ||
@@ -157,3 +259,3 @@ const slotType = `} satisfies __VLS_getSlots<typeof ${tagName}>}`; | ||
if (vSlotAttribute) { | ||
(0, import_language_core2.replaceSourceRange)( | ||
(0, import_language_core3.replaceSourceRange)( | ||
codes, | ||
@@ -166,3 +268,3 @@ source, | ||
} else if (ts.isJsxElement(node)) { | ||
(0, import_language_core2.replaceSourceRange)( | ||
(0, import_language_core3.replaceSourceRange)( | ||
codes, | ||
@@ -174,3 +276,3 @@ source, | ||
); | ||
(0, import_language_core2.replaceSourceRange)( | ||
(0, import_language_core3.replaceSourceRange)( | ||
codes, | ||
@@ -186,83 +288,2 @@ source, | ||
// src/jsx-directive/v-for.ts | ||
var import_language_core3 = require("@vue/language-core"); | ||
function transformVFor({ | ||
nodes, | ||
codes, | ||
ts, | ||
sfc, | ||
source | ||
}) { | ||
nodes.forEach(({ attribute, node, parent }) => { | ||
if (!(attribute.initializer && ts.isJsxExpression(attribute.initializer) && attribute.initializer.expression && ts.isBinaryExpression(attribute.initializer.expression))) | ||
return; | ||
let index; | ||
let objectIndex; | ||
let item = attribute.initializer.expression.left; | ||
const list = attribute.initializer.expression.right; | ||
if (ts.isParenthesizedExpression(item)) { | ||
if (ts.isBinaryExpression(item.expression)) { | ||
if (ts.isBinaryExpression(item.expression.left)) { | ||
index = item.expression.left.right; | ||
objectIndex = item.expression.right; | ||
item = item.expression.left.left; | ||
} else { | ||
index = item.expression.right; | ||
item = item.expression.left; | ||
} | ||
} else { | ||
item = item.expression; | ||
} | ||
} | ||
if (!item || !list) | ||
return; | ||
(0, import_language_core3.replaceSourceRange)( | ||
codes, | ||
source, | ||
node.pos, | ||
node.pos, | ||
`${parent ? "{" : " "}`, | ||
"__VLS_getVForSourceType(", | ||
[ | ||
sfc[source].content.slice(list.getStart(sfc[source]?.ast), list.end), | ||
source, | ||
list.getStart(sfc[source]?.ast), | ||
import_language_core3.FileRangeCapabilities.full | ||
], | ||
").map(([", | ||
[ | ||
`${sfc[source]?.content.slice(item.getStart(sfc[source]?.ast), item.end)}`, | ||
source, | ||
item.getStart(sfc[source]?.ast), | ||
import_language_core3.FileRangeCapabilities.full | ||
], | ||
", ", | ||
index ? [ | ||
`${sfc[source]?.content.slice(index.getStart(sfc[source]?.ast), index.end)}`, | ||
source, | ||
index.getStart(sfc[source]?.ast), | ||
import_language_core3.FileRangeCapabilities.full | ||
] : objectIndex ? "undefined" : "", | ||
...objectIndex ? [ | ||
", ", | ||
[ | ||
`${sfc[source]?.content.slice(objectIndex.getStart(sfc[source]?.ast), objectIndex.end)}`, | ||
source, | ||
objectIndex.getStart(sfc[source]?.ast), | ||
import_language_core3.FileRangeCapabilities.full | ||
] | ||
] : "", | ||
"]) => " | ||
); | ||
(0, import_language_core3.replaceSourceRange)( | ||
codes, | ||
source, | ||
node.end - 1, | ||
node.end, | ||
`>)${parent ? "}" : ""}` | ||
); | ||
(0, import_language_core3.replaceSourceRange)(codes, source, attribute.pos, attribute.end); | ||
}); | ||
} | ||
// src/jsx-directive/v-if.ts | ||
@@ -284,5 +305,2 @@ var import_language_core4 = require("@vue/language-core"); | ||
const hasScope = parent && attribute.name.escapedText === "v-if"; | ||
const expressionText = attribute.initializer.expression.getText( | ||
sfc[source]?.ast | ||
); | ||
(0, import_language_core4.replaceSourceRange)( | ||
@@ -295,3 +313,3 @@ codes, | ||
[ | ||
expressionText, | ||
attribute.initializer.expression.getText(sfc[source]?.ast), | ||
source, | ||
@@ -579,17 +597,19 @@ attribute.initializer.expression.getStart(sfc[source]?.ast), | ||
} | ||
if (vIfAttribute && !(vSlotAttribute && tagName === "template")) { | ||
vIfMap.has(parent) || vIfMap.set(parent, []); | ||
vIfMap.get(parent).push({ | ||
node, | ||
attribute: vIfAttribute, | ||
parent | ||
}); | ||
if (!(vSlotAttribute && tagName === "template")) { | ||
if (vIfAttribute) { | ||
vIfMap.has(parent) || vIfMap.set(parent, []); | ||
vIfMap.get(parent).push({ | ||
node, | ||
attribute: vIfAttribute, | ||
parent | ||
}); | ||
} | ||
if (vForAttribute) { | ||
vForNodes.push({ | ||
node, | ||
attribute: vForAttribute, | ||
parent: vIfAttribute ? void 0 : parent | ||
}); | ||
} | ||
} | ||
if (vForAttribute) { | ||
vForNodes.push({ | ||
node, | ||
attribute: vForAttribute, | ||
parent: vIfAttribute ? void 0 : parent | ||
}); | ||
} | ||
if (vSlotAttribute) { | ||
@@ -605,3 +625,6 @@ const slotNode = tagName === "template" ? parent : node; | ||
children: [], | ||
vIfAttribute: tagName === "template" && vIfAttribute ? vIfAttribute : void 0 | ||
...tagName === "template" ? { | ||
vIfAttribute, | ||
vForAttribute | ||
} : {} | ||
}).get(vSlotAttribute).children; | ||
@@ -608,0 +631,0 @@ if (slotNode === parent && ts.isJsxElement(parent)) { |
{ | ||
"name": "@vue-macros/volar", | ||
"version": "0.18.14", | ||
"version": "0.18.15", | ||
"packageManager": "pnpm@8.15.5", | ||
@@ -51,4 +51,4 @@ "description": "Volar plugin for Vue Macros.", | ||
"@vue-macros/common": "1.10.1", | ||
"@vue-macros/define-props": "2.0.3", | ||
"@vue-macros/short-bind": "0.2.2", | ||
"@vue-macros/define-props": "2.0.3", | ||
"@vue-macros/short-vmodel": "1.4.2" | ||
@@ -55,0 +55,0 @@ }, |
46484
1391