forked from Meituan-Dianping/mpvue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: 优化 class、style 的正则转换为 ast 分析
- Loading branch information
1 parent
ae6e49d
commit 896f3c5
Showing
5 changed files
with
171 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// babel-plugin-transform-object-to-ternary-operator.js | ||
|
||
import * as t from 'babel-types' | ||
import generate from 'babel-generator' | ||
import template from 'babel-template' | ||
import { hyphenate } from 'shared/util' | ||
|
||
function getStrByNode (node, onlyStr = false) { | ||
if (onlyStr) { | ||
return node.value || node.name || '' | ||
} | ||
return node.type === 'StringLiteral' ? node : t.stringLiteral(node.name || '') | ||
} | ||
|
||
// 把 { key: value } 转换成 [ value ? 'key' : '' ] | ||
const objectVisitor = { | ||
ObjectExpression: function (path) { | ||
const elements = path.node.properties.map(propertyItem => { | ||
return t.conditionalExpression(propertyItem.value, getStrByNode(propertyItem.key), t.stringLiteral('')) | ||
}) | ||
path.replaceWith(t.arrayExpression(elements)) | ||
} | ||
} | ||
|
||
export function transformObjectToTernaryOperator (babel) { | ||
return { visitor: objectVisitor } | ||
} | ||
|
||
// 把 { key: value } 转换成 'key:' + value + ';' | ||
const objectToStringVisitor = { | ||
ObjectExpression: function (path) { | ||
const expression = path.node.properties.map(propertyItem => { | ||
const keyStr = getStrByNode(propertyItem.key, true) | ||
const key = keyStr ? hyphenate(keyStr) : keyStr | ||
const { code: val } = generate(t.ExpressionStatement(propertyItem.value)) | ||
return `'${key}:' + ${val.slice(0, -1)} + ';'` | ||
}).join('+') | ||
|
||
const p = template(expression)({}) | ||
path.replaceWith(p.expression) | ||
} | ||
} | ||
export function transformObjectToString (babel) { | ||
return { visitor: objectToStringVisitor } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters