本文整理汇总了TypeScript中babel-types.isLiteral函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isLiteral函数的具体用法?TypeScript isLiteral怎么用?TypeScript isLiteral使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isLiteral函数的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: getIsValue
export function getIsValue(node: babel.ClassDeclaration|
babel.ClassExpression): string|undefined {
const getterValue = getStaticGetterValue(node, 'is');
if (!getterValue || !babel.isLiteral(getterValue)) {
// we only support literals
return undefined;
}
const value = astValue.expressionToValue(getterValue);
if (typeof value !== 'string') {
return undefined;
}
return value;
}
示例3: objectKeyToString
export function objectKeyToString(key: babel.Node): string|undefined {
if (babel.isIdentifier(key)) {
return key.name;
}
if (babel.isLiteral(key)) {
return '' + astValue.expressionToValue(key);
}
if (babel.isMemberExpression(key)) {
return objectKeyToString(key.object) + '.' +
objectKeyToString(key.property);
}
return undefined;
}
示例4: if
scannedMethod.params = (value.params || []).map((nodeParam) => {
let name;
let defaultValue;
let rest;
if (babel.isIdentifier(nodeParam)) {
// Basic parameter: method(param)
name = nodeParam.name;
} else if (
babel.isRestElement(nodeParam) &&
babel.isIdentifier(nodeParam.argument)) {
// Rest parameter: method(...param)
name = nodeParam.argument.name;
rest = true;
} else if (
babel.isAssignmentPattern(nodeParam) &&
babel.isIdentifier(nodeParam.left) &&
babel.isLiteral(nodeParam.right)) {
// Parameter with a default: method(param = "default")
name = nodeParam.left.name;
defaultValue = generate(nodeParam.right).code;
} else {
// Some AST pattern we don't recognize. Hope the code generator does
// something reasonable.
name = generate(nodeParam).code;
}
let type;
let description;
const tag = paramTags.get(name);
if (tag) {
if (tag.type) {
type = doctrine.type.stringify(tag.type);
}
if (tag.description) {
description = tag.description;
}
}
const param: MethodParam = {name, type, defaultValue, rest, description};
return param;
});
示例5: parseLoopBody
export function parseLoopBody (
body: NodePath<t.BlockStatement>,
jsxDeclarations: Set<NodePath<t.Node>>,
// @TODO
// 把 templates 换成 Map 可以支持 shalow variables declared
// 现在先用 ESLint 的 no-shalow 顶着
templates: Map<string, t.JSXElement>,
loopScopes: Set<string>,
finalReturnElement: t.JSXElement,
returnedPaths: NodePath<t.Node>[]
) {
const bodyScope = body.scope
body.traverse({
JSXElement (jsxElementPath) {
const parentNode = jsxElementPath.parent
const parentPath = jsxElementPath.parentPath
const isFinalReturn = jsxElementPath.getFunctionParent().isClassMethod()
const isJSXChildren = t.isJSXElement(parentNode)
if (!isJSXChildren) {
let statementParent = jsxElementPath.getStatementParent()
if (
!(
statementParent.isVariableDeclaration() ||
statementParent.isExpressionStatement()
)
) {
statementParent = statementParent.findParent(
s => s.isVariableDeclaration() || s.isExpressionStatement()
) as NodePath<t.Statement>
}
jsxDeclarations.add(statementParent)
if (t.isVariableDeclarator(parentNode)) {
if (statementParent) {
const name = findIdentifierFromStatement(statementParent.node as t.VariableDeclaration)
// setTemplate(name, path, templates)
name && templates.set(name, jsxElementPath.node)
}
} else if (t.isLogicalExpression(parentNode)) {
const { left, operator } = parentNode
if (operator === '&&') {
if (t.isExpression(left)) {
newJSXIfAttr(jsxElementPath.node, left)
parentPath.replaceWith(jsxElementPath.node)
if (statementParent) {
const name = findIdentifierFromStatement(statementParent.node as t.VariableDeclaration)
setTemplate(name, jsxElementPath, templates)
// name && templates.set(name, path.node)
}
}
}
} else if (t.isConditionalExpression(parentNode)) {
const { test, consequent, alternate } = parentNode
const block = buildBlockElement()
if (t.isJSXElement(consequent) && t.isLiteral(alternate)) {
const { value, confident } = parentPath.get('alternate').evaluate()
if (confident && !value) {
newJSXIfAttr(block, test)
block.children = [ jsxElementPath.node ]
// newJSXIfAttr(jsxElementPath.node, test)
parentPath.replaceWith(block)
if (statementParent) {
const name = findIdentifierFromStatement(
statementParent.node as t.VariableDeclaration
)
setTemplate(name, jsxElementPath, templates)
// name && templates.set(name, path.node)
}
}
} else if (t.isLiteral(consequent) && t.isJSXElement(consequent)) {
if (t.isNullLiteral(consequent)) {
newJSXIfAttr(block, reverseBoolean(test))
// newJSXIfAttr(jsxElementPath.node, reverseBoolean(test))
parentPath.replaceWith(block)
if (statementParent) {
const name = findIdentifierFromStatement(
statementParent.node as t.VariableDeclaration
)
setTemplate(name, jsxElementPath, templates)
// name && templates.set(name, path.node)
}
}
} else if (t.isJSXElement(consequent) && t.isJSXElement(alternate)) {
const block2 = buildBlockElement()
block.children = [consequent]
newJSXIfAttr(block, test)
setJSXAttr(block2, Adapter.else)
block2.children = [alternate]
const parentBlock = buildBlockElement()
parentBlock.children = [block, block2]
parentPath.replaceWith(parentBlock)
if (statementParent) {
const name = findIdentifierFromStatement(
statementParent.node as t.VariableDeclaration
)
setTemplate(name, jsxElementPath, templates)
}
} else {
// console.log('todo')
}
} else if (t.isReturnStatement(parentNode)) {
//.........这里部分代码省略.........
示例6: transform
//.........这里部分代码省略.........
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)
}
}
}
// @TODO: bind 的处理待定
}
},
ImportDeclaration (path) {
示例8: transform
//.........这里部分代码省略.........
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 (
t.isIdentifier(attr) &&
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
if (t.isBinaryExpression(expr, { operator: '+' }) || t.isLiteral(expr) || name.name !== 'style') {
return
}
path.get('value.expression').replaceWith(
t.callExpression(t.identifier(INTERNAL_INLINE_STYLE), [expr])
)
},
ImportDeclaration (path) {
const source = path.node.source.value
const names: string[] = []
if (source === TARO_PACKAGE_NAME) {
path.node.specifiers.push(
t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
t.importSpecifier(t.identifier(INTERNAL_DYNAMIC), t.identifier(INTERNAL_DYNAMIC)),
t.importSpecifier(t.identifier(INTERNAL_INLINE_STYLE), t.identifier(INTERNAL_INLINE_STYLE))
)
}
if (
source === REDUX_PACKAGE_NAME
) {
path.node.specifiers.forEach((s, index, specs) => {
if (s.local.name === 'Provider') {
specs.splice(index, 1)
specs.push(
t.importSpecifier(t.identifier('setStore'), t.identifier('setStore'))
)
}
})
}
path.traverse({
ImportDefaultSpecifier (path) {
const name = path.node.local.name
示例9:
nodes = nodes.filter((n) => !t.isLiteral(n, { value: '' }))
示例10: isString
function isString (node) {
return t.isLiteral(node as any) && typeof node.value === 'string'
}