|
1 | 1 | import hash from 'hash-sum' |
2 | 2 | import { type MagicStringBase } from 'magic-string-ast' |
| 3 | +import { ts } from '@ast-grep/napi' |
| 4 | +import MagicString from "magic-string"; |
3 | 5 | import type { IParseSFCRes, TMatchVariable } from '../parser' |
4 | 6 |
|
5 | 7 | const importer = 'import { useCssVars as _useCssVars } from "vue"\n' |
| 8 | + |
| 9 | +function findIdentifierFromExp(cssContent: string) { |
| 10 | + return ts.parse(cssContent).root().findAll({ |
| 11 | + rule: { |
| 12 | + matches: 'cssComplexExpIdentifier', |
| 13 | + }, |
| 14 | + utils:{ |
| 15 | + cssComplexExpIdentifier: { |
| 16 | + any: [ |
| 17 | + { |
| 18 | + kind: 'identifier', |
| 19 | + }, |
| 20 | + ], |
| 21 | + } |
| 22 | + } |
| 23 | + }) |
| 24 | +} |
| 25 | + |
6 | 26 | export const injectCSSVars = ( |
7 | 27 | vbindVariableList: TMatchVariable | undefined, |
8 | 28 | isScriptSetup: boolean, |
@@ -123,13 +143,28 @@ export function createCSSVarsObjCode( |
123 | 143 | vbVar.hash = hashVal |
124 | 144 | let varStr = '' |
125 | 145 | // composition api 和 option api 一直帶 _ctx |
126 | | - if (!isScriptSetup) { |
127 | | - varStr = `_ctx.${vbVar.value}` |
| 146 | + if (!isScriptSetup) { // non-inline |
| 147 | + varStr = vbVar.value ? `(_ctx.${vbVar.value})` : '()' |
128 | 148 | } else { |
129 | | - // vbVar.has === false, 要带上 _ctx. |
130 | | - varStr = vbVar.has ? vbVar.value : `_ctx.${vbVar.value}` |
131 | | - // ref 用.value |
132 | | - varStr = vbVar.isRef ? `${vbVar.value}.value` : varStr |
| 149 | + if(!vbVar.has){ |
| 150 | + varStr = `_ctx.${vbVar.value}` |
| 151 | + }else { |
| 152 | + // TODO use BindingsType |
| 153 | + debugger |
| 154 | + const ms = new MagicString(vbVar.value) |
| 155 | + // get Identifier sgNode |
| 156 | + const cssBindKeySgNodes = findIdentifierFromExp(vbVar.value) |
| 157 | + cssBindKeySgNodes.forEach((node) => { |
| 158 | + const range = node.range() |
| 159 | + ms.overwrite( |
| 160 | + range.start.index, |
| 161 | + range.end.index, |
| 162 | + `(_ctx.${node.text()})` |
| 163 | + // genCSSVarsValue(node, bindings, propsAlias), |
| 164 | + ) |
| 165 | + }) |
| 166 | + varStr = ms.toString() |
| 167 | + } |
133 | 168 | } |
134 | 169 | resCode = `\n "${hashVal}": ${varStr},${resCode}` |
135 | 170 | }) |
|
0 commit comments