本文整理汇总了TypeScript中babel-types.isJSXExpressionContainer函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isJSXExpressionContainer函数的具体用法?TypeScript isJSXExpressionContainer怎么用?TypeScript isJSXExpressionContainer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isJSXExpressionContainer函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: setAncestorCondition
export function setAncestorCondition (jsx: NodePath<t.Node>, expr: t.Expression): t.Expression {
const ifAttrSet = new Set<string>([
Adapter.if,
Adapter.else
])
const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => ifAttrSet.has(a.name.name as string))) as NodePath<t.JSXElement>
if (logicalJSX) {
const attr = logicalJSX.node.openingElement.attributes.find(a => ifAttrSet.has(a.name.name as string))
if (attr) {
if (attr.name.name === Adapter.else) {
const prevElement: NodePath<t.JSXElement | null> = (logicalJSX as any).getPrevSibling()
if (prevElement && prevElement.isJSXElement()) {
const attr = prevElement.node.openingElement.attributes.find(a => a.name.name === Adapter.if)
if (attr && t.isJSXExpressionContainer(attr.value)) {
const condition = reverseBoolean(cloneDeep(attr.value.expression))
expr = t.logicalExpression('&&', setAncestorCondition(logicalJSX, condition), expr)
}
}
} else if (t.isJSXExpressionContainer(attr.value)) {
const condition = cloneDeep(attr.value.expression)
expr = t.logicalExpression('&&', setAncestorCondition(logicalJSX, condition), expr)
}
}
}
return expr
}
示例2: parseJSXElement
.reduce((str, child) => {
if (t.isJSXText(child)) {
const strings: string[] = []
child.value.split(/(\r?\n\s*)/).forEach((val) => {
const value = val.replace(/\u00a0/g, ' ')
if (!value) {
return
}
if (value.startsWith('\n')) {
return
}
strings.push(value)
})
return str + strings.join('')
}
if (t.isJSXElement(child)) {
return str + parseJSXElement(child)
}
if (t.isJSXExpressionContainer(child)) {
if (t.isJSXElement(child.expression)) {
return str + parseJSXElement(child.expression)
}
return str + `{${generateJSXAttr(child)}}`
}
return str
}, '')
示例3: setParentCondition
export function setParentCondition (jsx: NodePath<t.Node>, expr: t.Expression, array = false) {
const conditionExpr = jsx.findParent(p => p.isConditionalExpression())
const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }))
if (array) {
const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => a.name.name === Adapter.if)) as NodePath<t.JSXElement>
if (logicalJSX) {
const attr = logicalJSX.node.openingElement.attributes.find(a => a.name.name === Adapter.if)
if (attr && t.isJSXExpressionContainer(attr.value)) {
expr = t.conditionalExpression(attr.value.expression, expr, t.arrayExpression())
return expr
}
}
}
if (conditionExpr && conditionExpr.isConditionalExpression()) {
const consequent = conditionExpr.get('consequent')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(conditionExpr.get('test').node as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
}
}
if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
const consequent = logicExpr.get('right')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(logicExpr.get('left').node as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
}
}
return expr
}
示例4: parseJSXElement
.reduce((str, child) => {
if (t.isJSXText(child)) {
return str + child.value
}
if (t.isJSXElement(child)) {
return str + parseJSXElement(child)
}
if (t.isJSXExpressionContainer(child)) {
if (t.isJSXElement(child.expression)) {
return str + parseJSXElement(child.expression)
}
return str + `{${
decodeUnicode(
generate(child, {
quotes: 'single',
jsonCompatibleStrings: true
})
.code
)
.replace(/(this\.props\.)|(this\.state\.)/g, '')
.replace(/(props\.)|(state\.)/g, '')
.replace(/this\./, '')
}}`
}
return str
}, '')
示例5: convertAstExpressionToVariable
export function convertAstExpressionToVariable (node) {
if (t.isObjectExpression(node)) {
const obj = {}
const properties = node.properties
properties.forEach(property => {
if (property.type === 'ObjectProperty' || property.type === 'ObjectMethod') {
const key = convertAstExpressionToVariable(property.key)
const value = convertAstExpressionToVariable(property.value)
obj[key] = value
}
})
return obj
} else if (t.isArrayExpression(node)) {
return node.elements.map(convertAstExpressionToVariable)
} else if (t.isLiteral(node)) {
return node['value']
} else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
const name = node.name
return name === 'undefined'
? undefined
: name
} else if (t.isJSXExpressionContainer(node)) {
return convertAstExpressionToVariable(node.expression)
}
}
示例6: codeFrameError
attributes: attributes.reduce((obj, attr) => {
if (t.isJSXSpreadAttribute(attr)) {
throw codeFrameError(attr.loc, 'JSX 参数暂不支持 ...spread 表达式')
}
const name = attr.name.name === 'className' ? 'class' : attr.name.name
let value: string | boolean = true
let attrValue = attr.value
if (typeof name === 'string') {
if (t.isStringLiteral(attrValue)) {
value = attrValue.value
} else if (t.isJSXExpressionContainer(attrValue)) {
const isBindEvent =
name.startsWith('bind') || name.startsWith('catch')
let { code } = generate(attrValue.expression)
code = code
.replace(/(this\.props\.)|(this\.state\.)/, '')
.replace(/this\./, '')
value = isBindEvent ? code : `{{${code}}}`
if (t.isStringLiteral(attrValue.expression)) {
value = attrValue.expression.value
}
}
if (
componentSpecialProps &&
componentSpecialProps.has(name)
) {
obj[name] = value
} else {
obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
}
}
return obj
}, {}),
示例7: 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)
}
示例8: setParentCondition
export function setParentCondition (jsx: NodePath<t.Node>, expr: t.Expression, array = false) {
const conditionExpr = jsx.findParent(p => p.isConditionalExpression())
const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }))
if (array) {
const ifAttrSet = new Set<string>([
Adapter.if,
Adapter.else
])
const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => ifAttrSet.has(a.name.name as string))) as NodePath<t.JSXElement>
if (logicalJSX) {
const attr = logicalJSX.node.openingElement.attributes.find(a => ifAttrSet.has(a.name.name as string))
if (attr) {
if (attr.name.name === Adapter.else) {
const prevElement: NodePath<t.JSXElement | null> = (logicalJSX as any).getPrevSibling()
if (prevElement && prevElement.isJSXElement()) {
const attr = prevElement.node.openingElement.attributes.find(a => a.name.name === Adapter.if)
if (attr && t.isJSXExpressionContainer(attr.value)) {
expr = t.conditionalExpression(reverseBoolean(cloneDeep(attr.value.expression)), expr, t.arrayExpression())
return expr
}
}
} else if (t.isJSXExpressionContainer(attr.value)) {
expr = t.conditionalExpression(cloneDeep(attr.value.expression), expr, t.arrayExpression())
return expr
}
}
}
}
if (conditionExpr && conditionExpr.isConditionalExpression()) {
const consequent = conditionExpr.get('consequent')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(cloneDeep(conditionExpr.get('test').node) as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
}
}
if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
const consequent = logicExpr.get('right')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(cloneDeep(logicExpr.get('left').node) as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
}
}
return expr
}
示例9: codeFrameError
attributesTrans = attributes.reduce((obj, attr) => {
if (t.isJSXSpreadAttribute(attr)) {
throw codeFrameError(attr.loc, 'JSX 参数暂不支持 ...spread 表达式')
}
let name = attr.name.name
if (DEFAULT_Component_SET.has(componentName)) {
if (name === 'className') {
name = 'class'
}
}
let value: string | boolean = true
let attrValue = attr.value
if (typeof name === 'string') {
const isAlipayEvent = Adapter.type === Adapters.alipay && /(^on[A-Z_])|(^catch[A-Z_])/.test(name)
if (t.isStringLiteral(attrValue)) {
value = attrValue.value
} else if (t.isJSXExpressionContainer(attrValue)) {
let isBindEvent =
(name.startsWith('bind') && name !== 'bind') || (name.startsWith('catch') && name !== 'catch')
const code = decodeUnicode(generate(attrValue.expression, {
quotes: 'single',
concise: true
}).code)
.replace(/"/g, "'")
.replace(/(this\.props\.)|(this\.state\.)/g, '')
.replace(/this\./g, '')
value = isBindEvent || isAlipayEvent ? code : `{{${code}}}`
if (Adapter.type === Adapters.swan && name === Adapter.for) {
value = code
}
if (t.isStringLiteral(attrValue.expression)) {
value = attrValue.expression.value
}
} else if (attrValue === null && name !== Adapter.else) {
value = `{{true}}`
}
if ((componentName === 'Input' || componentName === 'input') && name === 'maxLength') {
obj['maxlength'] = value
} else if (
componentSpecialProps && componentSpecialProps.has(name) ||
name.startsWith('__fn_') ||
isAlipayEvent
) {
obj[name] = value
} else {
obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
}
}
if (!isDefaultComponent && !specialComponentName.includes(componentName)) {
obj[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
}
return obj
}, {})
示例10: getWXS
function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>, imports: Imports[]): WXS {
let moduleName: string | null = null
let src: string | null = null
for (const attr of attrs) {
if (t.isJSXIdentifier(attr.name)) {
const attrName = attr.name.name
const attrValue = attr.value
let value: string | null = null
if (attrValue === null) {
throw new Error('WXS 标签的属性值不得为空')
}
if (t.isStringLiteral(attrValue)) {
value = attrValue.value
} else if (
t.isJSXExpressionContainer(attrValue) &&
t.isStringLiteral(attrValue.expression)
) {
value = attrValue.expression.value
}
if (attrName === 'module') {
moduleName = value
}
if (attrName === 'src') {
src = value
}
}
}
if (!src) {
const { children: [ script ] } = path.node
if (!t.isJSXText(script)) {
throw new Error('wxs 如果没有 src 属性,标签内部必须有 wxs 代码。')
}
src = './wxs__' + moduleName
imports.push({
ast: parseCode(script.value),
name: moduleName as string,
wxs: true
})
}
if (!moduleName || !src) {
throw new Error('一个 WXS 需要同时存在两个属性:`wxs`, `src`')
}
path.remove()
return {
module: moduleName,
src
}
}