本文整理汇总了TypeScript中babel-types.isNullLiteral函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isNullLiteral函数的具体用法?TypeScript isNullLiteral怎么用?TypeScript isNullLiteral使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isNullLiteral函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: isEmptyDeclarator
export function isEmptyDeclarator (node: t.Node) {
if (
t.isVariableDeclarator(node) &&
(node.init === null ||
t.isNullLiteral(node.init))
) {
return true
}
return false
}
示例2:
// Remove null args at the end of method or constructor calls.
// This may conflict in some situations when comparing null to undefined, see #231,
// but if disabled can cause problems with some APIs that use options to represent optional
// arguments like CanvasRenderingContext2D.fill: ?fillRule: string -> unit (fails if passed null).
function removeNullTailArgs<T>(path: traverse.NodePath<types.NewExpression | types.CallExpression>) {
if (Array.isArray(path.node.arguments)) {
for (var i = path.node.arguments.length - 1; i >= 0; i--) {
if (types.isNullLiteral(path.node.arguments[i]))
path.node.arguments.splice(i, 1);
else
break;
}
}
}
示例3: getArgumentName
export function getArgumentName (arg) {
if (t.isThisExpression(arg)) {
return 'this'
} else if (t.isNullLiteral(arg)) {
return 'null'
} else if (t.isStringLiteral(arg) || t.isNumericLiteral(arg)) {
return arg.value
} else if (t.isIdentifier(arg)) {
return arg.name
} else {
return generate(arg).code
}
throw new Error(`bind 不支持传入该参数: ${arg}`)
}
示例4: toConstant
//.........这里部分代码省略.........
) {
return options.constants[expression.name];
}
}
if (b.isLogicalExpression(expression)) {
const left = toConstant(expression.left);
const right = toConstant(expression.right);
if (constant && expression.operator === '&&') {
return left && right;
}
if (constant && expression.operator === '||') {
return left || right;
}
}
if (b.isMemberExpression(expression)) {
const object = toConstant(expression.object);
if (!object || !constant) {
constant = false;
return;
}
const member = expression.computed
? toConstant(expression.property)
: b.isIdentifier(expression.property)
? expression.property.name
: undefined;
if (member === undefined && !expression.computed) {
constant = false;
}
if (!constant) return;
if ({}.hasOwnProperty.call(object, '' + member) && member[0] !== '_') {
return object[member];
}
}
if (b.isNullLiteral(expression)) {
return null;
}
if (b.isNumericLiteral(expression)) {
return expression.value;
}
if (b.isObjectExpression(expression)) {
const result: any = {};
for (let i = 0; constant && i < expression.properties.length; i++) {
const property = expression.properties[i];
if (b.isObjectProperty(property)) {
if (property.shorthand) {
constant = false;
return;
}
const key = property.computed
? toConstant(property.key)
: b.isIdentifier(property.key)
? property.key.name
: b.isStringLiteral(property.key)
? property.key.value
: undefined;
if (!key || key[0] === '_') {
constant = false;
}
if (!constant) return;
const value = toConstant(property.value);
if (!constant) return;
result[key] = value;
} else if (b.isObjectMethod(property)) {
constant = false;
} else if (b.isSpreadProperty(property)) {
const argument = toConstant(property.argument);
示例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: function
ExpressionStatement: function(path: traverse.NodePath<types.ExpressionStatement>) {
if (types.isNullLiteral(path.node.expression))
path.remove();
}