本文整理汇总了TypeScript中babel-types.isCallExpression函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isCallExpression函数的具体用法?TypeScript isCallExpression怎么用?TypeScript isCallExpression使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isCallExpression函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should move static apis under "Taro"', function () {
const code = `
import { noop } from '@tarojs/taro-h5';
noop;
noop();
`
const result = babel.transform(code, { plugins: [pluginOptions] })
expect(result.code).toMatchSnapshot();
const ast = result.ast as t.File
const body = ast.program.body as [t.ImportDeclaration, t.ExpressionStatement]
expect(t.isImportDeclaration(body[0])).toBeTruthy()
expect(t.isExpressionStatement(body[1])).toBeTruthy()
const defaultImport = body[0].specifiers.find(v => t.isImportDefaultSpecifier(v))
expect(defaultImport).toBeTruthy()
const taroName = defaultImport!.local.name
let memberExpression = body[1].expression
if (t.isCallExpression(body[1])) {
memberExpression = (body[1].expression as t.CallExpression).callee
}
expect(memberExpression).toMatchObject(t.memberExpression(
t.identifier(taroName),
t.identifier('noop')
))
})
示例2: 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
}
示例3: toConstant
function toConstant(expression: b.Expression): any {
if (!constant) return;
if (b.isArrayExpression(expression)) {
const result = [];
for (let i = 0; constant && i < expression.elements.length; i++) {
const element = expression.elements[i];
if (b.isSpreadElement(element)) {
const spread = toConstant(element.argument);
if (!(isSpreadable(spread) && constant)) {
constant = false;
} else {
result.push(...spread);
}
} else {
result.push(toConstant(element));
}
}
return result;
}
if (b.isBinaryExpression(expression)) {
const left = toConstant(expression.left);
const right = toConstant(expression.right);
return constant && binaryOperation(expression.operator, left, right);
}
if (b.isBooleanLiteral(expression)) {
return expression.value;
}
if (b.isCallExpression(expression)) {
const args = [];
for (let i = 0; constant && i < expression.arguments.length; i++) {
const arg = expression.arguments[i];
if (b.isSpreadElement(arg)) {
const spread = toConstant(arg.argument);
if (!(isSpreadable(spread) && constant)) {
constant = false;
} else {
args.push(...spread);
}
} else {
args.push(toConstant(arg));
}
}
if (!constant) return;
if (b.isMemberExpression(expression.callee)) {
const object = toConstant(expression.callee.object);
if (!object || !constant) {
constant = false;
return;
}
const member = expression.callee.computed
? toConstant(expression.callee.property)
: b.isIdentifier(expression.callee.property)
? expression.callee.property.name
: undefined;
if (member === undefined && !expression.callee.computed) {
constant = false;
}
if (!constant) return;
if (canCallMethod(object, '' + member)) {
return object[member].apply(object, args);
}
} else {
const callee = toConstant(expression.callee);
if (!constant) return;
return callee.apply(null, args);
}
}
if (b.isConditionalExpression(expression)) {
const test = toConstant(expression.test);
return test
? toConstant(expression.consequent)
: toConstant(expression.alternate);
}
if (b.isIdentifier(expression)) {
if (
options.constants &&
{}.hasOwnProperty.call(options.constants, expression.name)
) {
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)
//.........这里部分代码省略.........
示例4: parseLoopBody
//.........这里部分代码省略.........
} 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)) {
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
示例5: _rewriteDynamicImport
/**
* Extends dynamic import statements to extract the explicitly namespace
* export for the imported module.
*
* Before:
* import('./module-a.js')
* .then((moduleA) => moduleA.doSomething());
*
* After:
* import('./bundle_1.js')
* .then(({$moduleA}) => $moduleA)
* .then((moduleA) => moduleA.doSomething());
*/
private _rewriteDynamicImport(
baseUrl: ResolvedUrl,
root: babel.Node,
importNodePath: NodePath) {
if (!importNodePath) {
return;
}
const importCallExpression = importNodePath.parent;
if (!importCallExpression ||
!babel.isCallExpression(importCallExpression)) {
return;
}
const importCallArgument = importCallExpression.arguments[0];
if (!babel.isStringLiteral(importCallArgument)) {
return;
}
const sourceUrl = importCallArgument.value;
const resolvedSourceUrl = this.bundler.analyzer.urlResolver.resolve(
baseUrl, sourceUrl as FileRelativeUrl);
if (!resolvedSourceUrl) {
return;
}
const sourceBundle = this.manifest.getBundleForFile(resolvedSourceUrl);
// TODO(usergenic): To support *skipping* the rewrite, we need a way to
// identify whether a bundle contains a single top-level module or is a
// merged bundle with multiple top-level modules.
let exportName;
if (sourceBundle) {
exportName =
getOrSetBundleModuleExportName(sourceBundle, resolvedSourceUrl, '*');
}
// If there's no source bundle or the namespace export name of the bundle
// is just '*', then we don't need to append a .then() to transform the
// return value of the import(). Lets just rewrite the URL to be a relative
// path and exit.
if (!sourceBundle || exportName === '*') {
const relativeSourceUrl =
ensureLeadingDot(this.bundler.analyzer.urlResolver.relative(
baseUrl, resolvedSourceUrl));
importCallArgument.value = relativeSourceUrl;
return;
}
// Rewrite the URL to be a relative path to the bundle.
const relativeSourceUrl = ensureLeadingDot(
this.bundler.analyzer.urlResolver.relative(baseUrl, sourceBundle.url));
importCallArgument.value = relativeSourceUrl;
const importCallExpressionParent = importNodePath.parentPath.parent!;
if (!importCallExpressionParent) {
return;
}
const thenifiedCallExpression = babel.callExpression(
babel.memberExpression(
clone(importCallExpression), babel.identifier('then')),
[babel.arrowFunctionExpression(
[
babel.objectPattern(
[babel.objectProperty(
babel.identifier(exportName),
babel.identifier(exportName),
undefined,
true) as any]),
],
babel.identifier(exportName))]);
rewriteObject(importCallExpression, thenifiedCallExpression);
}
示例6: parseWXML
export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean): {
wxses: WXS[]
wxml?: t.Node
imports: Imports[]
} {
if (!parseImport) {
errors.length = 0
}
usedComponents.clear()
let wxses: WXS[] = []
let imports: Imports[] = []
if (!wxml) {
return {
wxses,
imports,
wxml: t.nullLiteral()
}
}
const nodes = removEmptyTextAndComment(parse(wxml.trim()))
const ast = t.file(
t.program(
[
t.expressionStatement(parseNode(
buildElement('block', nodes as Node[])
) as t.Expression)
],
[]
)
)
traverse(ast, {
JSXAttribute (path) {
const name = path.node.name as t.JSXIdentifier
const jsx = path.findParent(p => p.isJSXElement()) as NodePath<
t.JSXElement
>
const valueCopy = cloneDeep(path.get('value').node)
transformIf(name.name, path, jsx, valueCopy)
transformLoop(name.name, path, jsx, valueCopy)
},
JSXElement: {
enter (path: NodePath<t.JSXElement>) {
const openingElement = path.get('openingElement')
const jsxName = openingElement.get('name')
const attrs = openingElement.get('attributes')
if (!jsxName.isJSXIdentifier()) {
return
}
const tagName = jsxName.node.name
if (tagName === 'Wxs') {
wxses.push(getWXS(attrs.map(a => a.node), path, dirPath))
}
if (tagName === 'Template') {
const template = parseTemplate(path, dirPath)
if (template) {
const { ast: classDecl, name } = template
const taroComponentsImport = buildImportStatement('@tarojs/components', [
...usedComponents
])
const taroImport = buildImportStatement('@tarojs/taro', [], 'Taro')
// const withWeappImport = buildImportStatement(
// '@tarojs/with-weapp',
// [],
// 'withWeapp'
// )
const ast = t.file(t.program([]))
ast.program.body.unshift(
taroComponentsImport,
taroImport,
// withWeappImport,
t.exportDefaultDeclaration(classDecl)
)
imports.push({
ast,
name
})
}
}
if (tagName === 'Import') {
const mods = parseModule(path, dirPath, 'import')
if (mods) {
imports = imports.concat(mods)
}
}
if (tagName === 'Include') {
parseModule(path, dirPath, 'include')
}
},
exit (path: NodePath<t.JSXElement>) {
const openingElement = path.get('openingElement')
const jsxName = openingElement.get('name')
if (!jsxName.isJSXIdentifier({ name: 'Block' })) {
return
}
const children = path.node.children
if (children.length === 1) {
const caller = children[0]
if (t.isJSXExpressionContainer(caller) && t.isCallExpression(caller.expression) && !path.parentPath.isExpressionStatement()) {
path.replaceWith(caller)
}
//.........这里部分代码省略.........
示例7: isArrayMapCallExpression
export function isArrayMapCallExpression (callExpression: NodePath<t.Node>): callExpression is NodePath<t.CallExpression> {
return callExpression &&
t.isCallExpression(callExpression.node) &&
t.isMemberExpression(callExpression.node.callee) &&
t.isIdentifier(callExpression.node.callee.property, { name: 'map' })
}
示例8: cloneDeep
//.........这里部分代码省略.........
if (tagName === 'Slot') {
const nameAttr = attrs.find(a => a.node.name.name === 'name')
let slotName = ''
if (nameAttr) {
if (nameAttr.node.value && t.isStringLiteral(nameAttr.node.value)) {
slotName = nameAttr.node.value.value
} else {
throw codeFrameError(jsxName.node, 'slot 的值必须是一个字符串')
}
}
const children = t.memberExpression(
t.memberExpression(t.thisExpression(), t.identifier('props')),
t.identifier(slotName ? buildSlotName(slotName) : 'children')
)
try {
path.replaceWith(path.parentPath.isJSXElement() ? t.jSXExpressionContainer(children) : children)
} catch (error) {
//
}
}
if (tagName === 'Wxs') {
wxses.push(getWXS(attrs.map(a => a.node), path, imports))
}
if (tagName === 'Template') {
// path.traverse({
// JSXAttribute: jsxAttrVisitor
// })
const template = parseTemplate(path, dirPath)
if (template) {
const { ast: classDecl, name } = template
const taroComponentsImport = buildImportStatement('@tarojs/components', [
...usedComponents
])
const taroImport = buildImportStatement('@tarojs/taro', [], 'Taro')
// const withWeappImport = buildImportStatement(
// '@tarojs/with-weapp',
// [],
// 'withWeapp'
// )
const ast = t.file(t.program([]))
ast.program.body.unshift(
taroComponentsImport,
taroImport,
// withWeappImport,
t.exportDefaultDeclaration(classDecl)
)
let usedTemplate = new Set<string>()
traverse(ast, {
JSXIdentifier (p) {
const node = p.node
if (node.name.endsWith('Tmpl') && node.name.length > 4 && p.parentPath.isJSXOpeningElement()) {
usedTemplate.add(node.name)
}
}
})
usedTemplate.forEach(componentName => {
if (componentName !== classDecl.id.name) {
ast.program.body.unshift(
buildImportStatement(`./${componentName}`, [], componentName)
)
}
})
imports.push({
ast,
name
})
}
}
if (tagName === 'Import') {
const mods = parseModule(path, dirPath, 'import')
if (mods) {
imports.push(...mods)
}
}
if (tagName === 'Include') {
parseModule(path, dirPath, 'include')
}
},
exit (path: NodePath<t.JSXElement>) {
const openingElement = path.get('openingElement')
const jsxName = openingElement.get('name')
if (!jsxName.isJSXIdentifier({ name: 'Block' })) {
return
}
const children = path.node.children
if (children.length === 1) {
const caller = children[0]
if (t.isJSXExpressionContainer(caller) && t.isCallExpression(caller.expression) && !path.parentPath.isExpressionStatement()) {
try {
path.replaceWith(caller)
} catch (error) {
//
}
}
}
}
}
} as Visitor
}