本文整理匯總了TypeScript中babel-types.isJSXIdentifier函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isJSXIdentifier函數的具體用法?TypeScript isJSXIdentifier怎麽用?TypeScript isJSXIdentifier使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了isJSXIdentifier函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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)
}
}
示例2: replaceJSXTextWithTextComponent
export function replaceJSXTextWithTextComponent (path: NodePath<t.JSXText | t.JSXExpressionContainer>) {
const parent = path.findParent(p => p.isJSXElement())
if (parent && parent.isJSXElement() && t.isJSXIdentifier(parent.node.openingElement.name) && parent.node.openingElement.name.name !== 'Text') {
path.replaceWith(t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier('Text'), []),
t.jSXClosingElement(t.jSXIdentifier('Text')),
[path.isJSXText() ? t.jSXText(path.node.value) : path.node]
))
}
}
示例3: findJSXAttrByName
export function findJSXAttrByName (attrs: t.JSXAttribute[], name: string) {
for (const attr of attrs) {
if (!t.isJSXIdentifier(attr.name)) {
break
}
if (attr.name.name === name) {
return attr
}
}
return null
}
示例4: 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
}
}
示例5: newJSXIfAttr
export function newJSXIfAttr (jsx: t.JSXElement, value: t.Identifier | t.Expression, path?: NodePath<t.JSXElement>) {
const element = jsx.openingElement
if (!t.isJSXIdentifier(element.name)) {
return
}
if (element.name.name === 'Block' || element.name.name === 'block' || !path) {
element.attributes.push(buildJSXAttr(Adapter.if, value))
} else {
const block = buildBlockElement()
newJSXIfAttr(block, value)
block.children.push(jsx)
path.node = block
}
}
示例6:
.some(path => {
const attr = path.node
if (t.isJSXIdentifier(attr.name)) {
const name = attr.name.name
if (name === WX_IF) {
return true
}
const match = name.match(/wx:else|wx:elif/)
if (match) {
path.remove()
matches = {
reg: match,
tester: attr.value
}
return true
}
}
return false
})
示例7: setJSXAttr
export function setJSXAttr (
jsx: t.JSXElement,
name: string,
value?: t.StringLiteral | t.JSXExpressionContainer | t.JSXElement,
path?: NodePath<t.JSXElement>
) {
const element = jsx.openingElement
if (!t.isJSXIdentifier(element.name)) {
return
}
if (element.name.name === 'Block' || element.name.name === 'block' || !path) {
jsx.openingElement.attributes.push(
t.jSXAttribute(t.jSXIdentifier(name), value)
)
} else {
const block = buildBlockElement()
setJSXAttr(block, name, value)
block.children = [jsx]
path.node = block
}
}
示例8: findMethodName
export function findMethodName (expression: t.Expression): string {
let methodName
if (
t.isIdentifier(expression) ||
t.isJSXIdentifier(expression)
) {
methodName = expression.name
} else if (t.isStringLiteral(expression)) {
methodName = expression.value
} else if (
t.isMemberExpression(expression) &&
t.isIdentifier(expression.property)
) {
const { code } = generate(expression)
const ids = code.split('.')
if (ids[0] === 'this' && ids[1] === 'props' && ids[2]) {
methodName = code.replace('this.props.', '')
} else {
methodName = expression.property.name
}
} else if (
t.isCallExpression(expression) &&
t.isMemberExpression(expression.callee) &&
t.isIdentifier(expression.callee.object)
) {
methodName = expression.callee.object.name
} else if (
t.isCallExpression(expression) &&
t.isMemberExpression(expression.callee) &&
t.isMemberExpression(expression.callee.object) &&
t.isIdentifier(expression.callee.property) &&
expression.callee.property.name === 'bind' &&
t.isIdentifier(expression.callee.object.property)
) {
methodName = expression.callee.object.property.name
} else {
throw codeFrameError(expression.loc, '當 props 為事件時(props name 以 `on` 開頭),隻能傳入一個 this 作用域下的函數。')
}
return methodName
}
示例9: traverse
t.assertBinaryExpression(ast, { operator: "*" });
const exp: t.Expression = t.nullLiteral();
// React examples:
// https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-plugin-transform-react-inline-elements/src/index.js#L61
traverse(ast, {
JSXElement(path, file) {
const { node } = path;
const open = node.openingElement;
// init
const type = open.name;
let newType: t.StringLiteral;
if (t.isJSXIdentifier(type) && t.react.isCompatTag(type.name)) {
newType = t.stringLiteral(type.name);
}
const args: any[] = [];
if (node.children.length) {
const children = t.react.buildChildren(node);
args.push(
t.unaryExpression("void", t.numericLiteral(0), true),
...children,
);
}
}
});
// TypeScript Types
示例10: transform
//.........這裏部分代碼省略.........
JSXOpeningElement (path) {
const { name } = path.node.name as t.JSXIdentifier
if (name === 'Provider') {
const modules = path.scope.getAllBindings('module')
const providerBinding = Object.values(modules).some((m: Binding) => m.identifier.name === 'Provider')
if (providerBinding) {
path.node.name = t.jSXIdentifier('View')
const store = path.node.attributes.find(attr => attr.name.name === 'store')
if (store && t.isJSXExpressionContainer(store.value) && t.isIdentifier(store.value.expression)) {
storeName = store.value.expression.name
}
path.node.attributes = []
}
}
if (IMAGE_COMPONENTS.has(name)) {
for (const attr of path.node.attributes) {
if (
attr.name.name === 'src'
) {
if (t.isStringLiteral(attr.value)) {
imageSource.add(attr.value.value)
} else if (t.isJSXExpressionContainer(attr.value)) {
if (t.isStringLiteral(attr.value.expression)) {
imageSource.add(attr.value.expression.value)
}
}
}
}
}
},
JSXAttribute (path) {
const { name, value } = path.node
if (!t.isJSXIdentifier(name) || value === null || t.isStringLiteral(value) || t.isJSXElement(value)) {
return
}
const expr = value.expression as any
const exprPath = path.get('value.expression')
if (!t.isBinaryExpression(expr, { operator: '+' }) && !t.isLiteral(expr) && name.name === 'style') {
const jsxID = path.findParent(p => p.isJSXOpeningElement()).get('name')
if (jsxID && jsxID.isJSXIdentifier() && DEFAULT_Component_SET.has(jsxID.node.name)) {
exprPath.replaceWith(
t.callExpression(t.identifier(INTERNAL_INLINE_STYLE), [expr])
)
}
}
if (name.name.startsWith('on')) {
if (exprPath.isReferencedIdentifier()) {
const ids = [expr.name]
const fullPath = buildFullPathThisPropsRef(expr, ids, path)
if (fullPath) {
exprPath.replaceWith(fullPath)
}
}
if (exprPath.isReferencedMemberExpression()) {
const id = findFirstIdentifierFromMemberExpression(expr)
const ids = getIdsFromMemberProps(expr)
if (t.isIdentifier(id)) {
const fullPath = buildFullPathThisPropsRef(id, ids, path)
if (fullPath) {
exprPath.replaceWith(fullPath)
}
}