|
| 1 | +import type { Code, Context, TsmLanguagePlugin } from "ts-macro"; |
| 2 | + |
| 3 | +export const narrowedShow = ({ ts }: Context): TsmLanguagePlugin => ({ |
| 4 | + name: "@solid-macros/volar/narrowed-show", |
| 5 | + resolveVirtualCode({ ast, codes, lang }) { |
| 6 | + if (!lang.endsWith("x")) return; |
| 7 | + const showCompNames = new Set<string>(); |
| 8 | + ast.forEachChild(function walk(node) { |
| 9 | + if ( |
| 10 | + ts.isImportSpecifier(node) && |
| 11 | + (node.propertyName ?? node.name).text === "Show" |
| 12 | + ) { |
| 13 | + showCompNames.add(node.name.text); |
| 14 | + } |
| 15 | + if ( |
| 16 | + ts.isJsxElement(node) && |
| 17 | + showCompNames.has(node.openingElement.tagName.getText(ast)) |
| 18 | + ) { |
| 19 | + const condition = node.openingElement.attributes.properties.find( |
| 20 | + (node): node is import("typescript").JsxAttribute => |
| 21 | + ts.isJsxAttribute(node) && node.name.getText(ast) === "when", |
| 22 | + ); |
| 23 | + const fallback = node.openingElement.attributes.properties.find( |
| 24 | + (node): node is import("typescript").JsxAttribute => |
| 25 | + ts.isJsxAttribute(node) && node.name.getText(ast) === "fallback", |
| 26 | + ); |
| 27 | + if (condition?.initializer) { |
| 28 | + const conditionExpr = ts.isJsxExpression(condition.initializer) |
| 29 | + ? condition.initializer.expression |
| 30 | + : condition.initializer; |
| 31 | + const fallbackExpr = |
| 32 | + fallback?.initializer && |
| 33 | + (ts.isJsxExpression(fallback?.initializer) |
| 34 | + ? fallback.initializer.expression |
| 35 | + : fallback?.initializer); |
| 36 | + codes.replaceRange( |
| 37 | + node.pos, |
| 38 | + node.end, |
| 39 | + // convert to a JsxExpression with a ternary |
| 40 | + "{(", |
| 41 | + // insert dummy <Show> for... |
| 42 | + // - making TS to not incorrectly flag Show as unused |
| 43 | + // - displaying correct semantic highlighting |
| 44 | + // - providing typechecks and go-to-defs for each props |
| 45 | + // - providing autocompletes |
| 46 | + ["<", node.openingElement.getStart(ast)], |
| 47 | + [ |
| 48 | + node.openingElement.tagName.getText(ast), |
| 49 | + node.openingElement.tagName.getStart(ast), |
| 50 | + ], |
| 51 | + ...node.openingElement.attributes.properties.flatMap( |
| 52 | + (node): Code[] => { |
| 53 | + if (ts.isJsxSpreadAttribute(node)) { |
| 54 | + return [[node.getFullText(ast), node.pos]]; |
| 55 | + } |
| 56 | + |
| 57 | + return [ |
| 58 | + [node.name.getFullText(ast), node.name.pos], |
| 59 | + ...(node.initializer |
| 60 | + ? [ |
| 61 | + "=", |
| 62 | + ["when", "fallback", "children"].includes( |
| 63 | + node.name.getText(ast), |
| 64 | + ) |
| 65 | + ? // omit spans as they're used again later |
| 66 | + node.initializer.getFullText(ast) |
| 67 | + : // provide spans so typescript can work |
| 68 | + ([ |
| 69 | + node.initializer.getFullText(ast), |
| 70 | + node.initializer.pos, |
| 71 | + ] satisfies Code), |
| 72 | + ] |
| 73 | + : []), |
| 74 | + ]; |
| 75 | + }, |
| 76 | + ), |
| 77 | + // insert dummy children for fulfilling typechecks |
| 78 | + " children={<></>}", |
| 79 | + ["/>", node.openingElement.end - 1], |
| 80 | + ") && ((", |
| 81 | + conditionExpr |
| 82 | + ? [conditionExpr.getText(ast), conditionExpr.getStart(ast)] |
| 83 | + : "undefined", |
| 84 | + ") ? <>", |
| 85 | + ...node.children.map( |
| 86 | + (child): Code => [child.getText(ast), child.getStart(ast)], |
| 87 | + ), |
| 88 | + "</> : ", |
| 89 | + fallbackExpr |
| 90 | + ? [fallbackExpr.getText(ast), fallbackExpr.getStart(ast)] |
| 91 | + : "null", |
| 92 | + ")}", |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + node.forEachChild(walk); |
| 97 | + }); |
| 98 | + }, |
| 99 | +}); |
0 commit comments