'use strict'; const core = require('@unocss/core'); const napi = require('@ast-grep/napi'); function createFilter(include, exclude) { const includePattern = core.toArray(include || []); const excludePattern = core.toArray(exclude || []); return (id) => { if (excludePattern.some((p) => id.match(p))) return false; return includePattern.some((p) => id.match(p)); }; } function transformerAttributifyJsxSg(options = {}) { const { blocklist = [] } = options; const isBlocked = (matchedRule) => { for (const blockedRule of blocklist) { if (blockedRule instanceof RegExp) { if (blockedRule.test(matchedRule)) return true; } else if (matchedRule === blockedRule) { return true; } } return false; }; const idFilter = createFilter( options.include || [/\.[jt]sx$/, /\.mdx$/], options.exclude || [] ); return { name: "@unocss/transformer-attributify-jsx-sg", enforce: "pre", idFilter, async transform(code, _, { uno }) { const tasks = []; const ast = await napi.js.parseAsync(code.original); const root = ast.root(); const nodes = root.findAll({ rule: { kind: "jsx_attribute", not: { has: { any: [{ kind: "jsx_expression" }, { kind: "string" }] } } } }); for (const node of nodes) { const range = node.range(); const matchedRule = node.text().replace(/:/i, "-"); if (isBlocked(matchedRule)) continue; tasks.push( uno.parseToken(matchedRule).then((matched) => { if (matched) { code.overwrite( range.start.index, range.end.index, `${matchedRule}=""` ); } }) ); } await Promise.all(tasks); } }; } module.exports = transformerAttributifyJsxSg;