本文整理汇总了TypeScript中babel-types.jSXExpressionContainer函数的典型用法代码示例。如果您正苦于以下问题:TypeScript jSXExpressionContainer函数的具体用法?TypeScript jSXExpressionContainer怎么用?TypeScript jSXExpressionContainer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了jSXExpressionContainer函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: handleConditions
function handleConditions (conditions: Condition[]) {
if (conditions.length === 1) {
const ct = conditions[0]
ct.path.replaceWith(
t.jSXExpressionContainer(
t.logicalExpression('&&', ct.tester.expression, cloneDeep(ct.path.node))
)
)
}
if (conditions.length > 1) {
const lastLength = conditions.length - 1
const lastCon = conditions[lastLength]
let lastAlternate: t.Expression = cloneDeep(lastCon.path.node)
if (lastCon.condition === WX_ELSE_IF) {
lastAlternate = t.logicalExpression(
'&&',
lastCon.tester.expression,
lastAlternate
)
}
const node = conditions
.slice(0, lastLength)
.reduceRight((acc: t.Expression, condition) => {
return t.conditionalExpression(
condition.tester.expression,
cloneDeep(condition.path.node),
acc
)
}, lastAlternate)
conditions[0].path.replaceWith(t.jSXExpressionContainer(node))
conditions.slice(1).forEach(c => c.path.remove())
}
}
示例2: parseText
function parseText (node: Text) {
const { type, content } = parseContent(node.content)
if (type === 'raw') {
return t.jSXText(content)
}
return t.jSXExpressionContainer(buildTemplate(content))
}
示例3: transformLoop
function transformLoop (
name: string,
attr: NodePath<t.JSXAttribute>,
jsx: NodePath<t.JSXElement>,
value: AttrValue
) {
if (name !== WX_FOR) {
return
}
if (!value || !t.isJSXExpressionContainer(value)) {
throw new Error('wx:for 的值必须使用 "{{}}" 包裹')
}
attr.remove()
let item = t.stringLiteral('item')
let index = t.stringLiteral('index')
jsx
.get('openingElement')
.get('attributes')
.forEach(p => {
const node = p.node
if (node.name.name === WX_FOR_ITEM) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_ITEM + ' 的值必须是一个字符串')
}
item = node.value
p.remove()
}
if (node.name.name === WX_FOR_INDEX) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_INDEX + ' 的值必须是一个字符串')
}
index = node.value
p.remove()
}
if (node.name.name === WX_KEY) {
p.get('name').replaceWith(t.jSXIdentifier('key'))
}
})
const replacement = t.jSXExpressionContainer(
t.callExpression(
t.memberExpression(value.expression, t.identifier('map')),
[
t.arrowFunctionExpression(
[t.identifier(item.value), t.identifier(index.value)],
t.blockStatement([t.returnStatement(jsx.node)])
)
]
)
)
const block = buildBlockElement()
block.children = [replacement]
jsx.replaceWith(block)
}
示例4: parseText
function parseText (node: Text, tagName?: string) {
if (tagName === 'wxs') {
return t.jSXText(node.content)
}
const { type, content } = parseContent(node.content)
if (type === 'raw') {
const text = content.replace(/([{}]+)/g,"{'$1'}")
return t.jSXText(text)
}
return t.jSXExpressionContainer(buildTemplate(content))
}
示例5: parseAttribute
function parseAttribute (attr: Attribute) {
const { key, value } = attr
let jsxValue: null | t.JSXExpressionContainer | t.StringLiteral = null
if (value) {
const { type, content } = parseContent(value)
jsxValue =
type === 'raw'
? t.stringLiteral(content)
: t.jSXExpressionContainer(buildTemplate(content))
}
const jsxKey = handleAttrKey(key)
if (/^on[A-Z]/.test(jsxKey) && jsxValue && t.isStringLiteral(jsxValue)) {
jsxValue = t.jSXExpressionContainer(
t.memberExpression(t.thisExpression(), t.identifier(jsxValue.value))
)
}
return t.jSXAttribute(t.jSXIdentifier(jsxKey), jsxValue)
}
示例6: parseNode
function parseNode (node: AllKindNode) {
if (node.type === NodeType.Text) {
return parseText(node)
} else if (node.type === NodeType.Comment) {
const emptyStatement = t.jSXEmptyExpression()
emptyStatement.innerComments = [{
type: 'CommentBlock',
value: ' ' + node.content + ' '
}] as any[]
return t.jSXExpressionContainer(emptyStatement)
}
return parseElement(node)
}
示例7: transformIf
function transformIf (
name: string,
attr: NodePath<t.JSXAttribute>,
jsx: NodePath<t.JSXElement>,
value: AttrValue
) {
if (name !== WX_IF) {
return
}
const conditions: Condition[] = []
let siblings: NodePath<t.Node>[] = []
try {
siblings = jsx.getAllNextSiblings().filter(s => !(s.isJSXExpressionContainer() && s.get('expression').isJSXEmptyExpression()))
} catch (error) {
return
}
if (value === null || !t.isJSXExpressionContainer(value)) {
// tslint:disable-next-line
console.error('wx:if 的值需要用双括号 `{{}}` 包裹它的值')
if (value && t.isStringLiteral(value)) {
value = t.jSXExpressionContainer(buildTemplate(value.value))
}
}
conditions.push({
condition: WX_IF,
path: jsx,
tester: value as t.JSXExpressionContainer
})
attr.remove()
for (let index = 0; index < siblings.length; index++) {
const sibling = siblings[index]
const next = cloneDeep(siblings[index + 1])
const currMatches = findWXIfProps(sibling)
const nextMatches = findWXIfProps(next)
if (currMatches === null) {
break
}
conditions.push({
condition: currMatches.reg.input as string,
path: sibling as any,
tester: currMatches.tester as t.JSXExpressionContainer
})
if (nextMatches === null) {
break
}
}
handleConditions(conditions)
}
示例8: parseModule
export function parseModule (jsx: NodePath<t.JSXElement>, dirPath: string, type: 'include' | 'import') {
const openingElement = jsx.get('openingElement')
const attrs = openingElement.get('attributes')
const src = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'src' }))
if (!src) {
throw new Error(`${type} 标签必须包含 \`src\` 属性`)
}
const value = src.get('value')
if (!value.isStringLiteral()) {
throw new Error(`${type} 标签的 src 属性值必须是一个字符串`)
}
const srcValue = value.node.value
if (type === 'import') {
const wxml = getWXMLsource(dirPath, srcValue, type)
const { imports } = parseWXML(resolve(dirPath, srcValue), wxml, true)
try {
jsx.remove()
} catch (error) {
//
}
return imports
} else {
const { wxml } = parseWXML(dirPath, getWXMLsource(dirPath, srcValue, type), true)
const block = buildBlockElement()
try {
if (wxml) {
block.children = [wxml as any]
jsx.replaceWith(wxml)
} else {
block.children = [t.jSXExpressionContainer(t.jSXEmptyExpression())]
jsx.replaceWith(block)
}
} catch (error) {
//
}
}
}
示例9: parseTemplate
export function parseTemplate (path: NodePath<t.JSXElement>, dirPath: string) {
const openingElement = path.get('openingElement')
const attrs = openingElement.get('attributes')
const is = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'is' }))
const data = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'data' }))
// const spread = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'spread' }))
const name = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'name' }))
const refIds = new Set<string>()
if (name) {
const value = name.node.value
if (value === null || !t.isStringLiteral(value)) {
throw new Error('template 的 `name` 属性只能是字符串')
}
const className = pascalName(value.value) + pascalName(basename(dirPath))
path.traverse({
Identifier (p) {
if (!p.isReferencedIdentifier()) {
return
}
const jsxExprContainer = p.findParent(p => p.isJSXExpressionContainer())
if (!jsxExprContainer || !jsxExprContainer.isJSXExpressionContainer()) {
return
}
refIds.add(p.node.name)
}
})
const block = buildBlockElement()
block.children = path.node.children
let render: t.ClassMethod
if (refIds.size === 0) {
// 无状态组件
render = buildRender(block, [], [])
} else if (refIds.size === 1) {
// 只有一个数据源
render = buildRender(block, [], Array.from(refIds), Array.from(refIds)[0])
} else {
// 使用 ...spread
render = buildRender(block, [], Array.from(refIds), [])
}
const classDecl = t.classDeclaration(
t.identifier(className),
t.memberExpression(t.identifier('Taro'), t.identifier('Component')),
t.classBody([render!]),
[]
)
path.remove()
return {
name: className,
ast: classDecl
}
} else if (is) {
const value = is.node.value
if (!value) {
throw new Error('template 的 `is` 属性不能为空')
}
if (t.isStringLiteral(value)) {
const className = pascalName(value.value)
let attributes: t.JSXAttribute[] = []
if (data) {
attributes.push(data.node)
}
path.replaceWith(t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier(className), attributes),
t.jSXClosingElement(t.jSXIdentifier(className)),
[],
true
))
} else if (t.isJSXExpressionContainer(value)) {
if (t.isStringLiteral(value.expression)) {
const className = pascalName(value.expression.value)
let attributes: t.JSXAttribute[] = []
if (data) {
attributes.push(data.node)
}
path.replaceWith(t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier(className), attributes),
t.jSXClosingElement(t.jSXIdentifier(className)),
[],
true
))
} else if (t.isConditional(value.expression)) {
const { test, consequent, alternate } = value.expression
if (!t.isStringLiteral(consequent) || !t.isStringLiteral(alternate)) {
throw new Error('当 template is 标签是三元表达式时,他的两个值都必须为字符串')
}
let attributes: t.JSXAttribute[] = []
if (data) {
attributes.push(data.node)
}
const block = buildBlockElement()
block.children = [t.jSXExpressionContainer(t.conditionalExpression(
test,
t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier('Template'), attributes.concat(
[t.jSXAttribute(t.jSXIdentifier('is'), consequent)]
)),
t.jSXClosingElement(t.jSXIdentifier('Template')),
[],
//.........这里部分代码省略.........
示例10: parseLoopBody
//.........这里部分代码省略.........
} else if (t.isReturnStatement(parentNode)) {
if (!isFinalReturn) {
const caller = parentPath.findParent(p => p.isCallExpression())
if (caller.isCallExpression()) {
const callee = caller.node.callee
if (
t.isMemberExpression(callee) &&
t.isIdentifier(callee.property) &&
callee.property.name === 'map'
) {
let ary = callee.object
const blockStatementPath = parentPath.findParent(p => p.isBlockStatement()) as NodePath<t.BlockStatement>
const body = blockStatementPath.node.body
let stateToBeAssign = new Set<string>()
for (const statement of body) {
if (t.isVariableDeclaration(statement)) {
for (const dcl of statement.declarations) {
if (t.isIdentifier(dcl.id)) {
const scope = blockStatementPath.scope
const stateName = scope.generateUid(LOOP_STATE)
stateToBeAssign.add(stateName)
blockStatementPath.scope.rename(dcl.id.name, stateName)
}
}
}
}
if (t.isCallExpression(ary) || isContainFunction(caller.get('callee').get('object'))) {
const variableName = `anonymousState_${bodyScope.generateUid()}`
caller.getStatementParent().insertBefore(
buildConstVariableDeclaration(variableName, ary)
)
ary = t.identifier(variableName)
}
setJSXAttr(jsxElementPath.node, Adapter.for, t.jSXExpressionContainer(ary))
const [func] = caller.node.arguments
if (
t.isFunctionExpression(func) ||
t.isArrowFunctionExpression(func)
) {
const [item, index] = func.params
if (t.isIdentifier(item)) {
setJSXAttr(
jsxElementPath.node,
Adapter.forItem,
t.stringLiteral(item.name)
)
loopScopes.add(item.name)
} else {
setJSXAttr(
jsxElementPath.node,
Adapter.forItem,
t.stringLiteral('__item')
)
}
if (t.isIdentifier(index)) {
setJSXAttr(
jsxElementPath.node,
Adapter.forIndex,
t.stringLiteral(index.name)
)
loopScopes.add(index.name)
}
caller.replaceWith(jsxElementPath.node)
if (statementParent) {
const name = findIdentifierFromStatement(
statementParent.node as t.VariableDeclaration