本文整理汇总了TypeScript中babel-types.callExpression函数的典型用法代码示例。如果您正苦于以下问题:TypeScript callExpression函数的具体用法?TypeScript callExpression怎么用?TypeScript callExpression使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了callExpression函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: evalClass
export function evalClass (ast: t.File, props = '', isRequire = false) {
let mainClass!: t.ClassDeclaration
const statements = new Set<t.ExpressionStatement>([
template('Current.inst = this;')() as any
])
traverse(ast, {
ClassDeclaration (path) {
mainClass = path.node
},
/**
* 目前 node 的版本支持不了 class-properties
* 但 babel 又有 bug,某些情况竟然把转换后的 class-properties 编译到 super 之前
* 不然用 babel.transformFromAst 就完事了
* 现在只能自己实现这个 feature 的部分功能了,真 tm 麻烦
* @TODO 有空再给他们提 PR 吧
*/
ClassProperty (path) {
const { key, value } = path.node
statements.add(t.expressionStatement(t.assignmentExpression(
'=',
t.memberExpression(
t.thisExpression(),
key
),
value
)))
path.remove()
}
})
for (const method of mainClass.body.body) {
// constructor 即便没有被定义也会被加上
if (t.isClassMethod(method) && method.kind === 'constructor') {
const index = method.body.body.findIndex(node => t.isSuper(node))
method.body.body.push(
t.expressionStatement(t.assignmentExpression(
'=',
t.memberExpression(
t.thisExpression(),
t.identifier('state')
),
t.callExpression(t.memberExpression(t.thisExpression(), t.identifier('_createData')), [])
))
)
method.body.body.splice(index, 0, ...statements)
}
}
let code = `function f() {};` +
generate(t.classDeclaration(t.identifier('Test'), t.identifier('f'), mainClass.body, [])).code +
';' + `var classInst = new Test(${props});classInst`
code = internalFunction + code
// tslint:disable-next-line
return eval(code)
}
示例2:
ast.program.body.forEach((node, index, body) => {
if (node === statementPath.node) {
body.splice(index + 1, 0, t.expressionStatement(
t.callExpression(t.identifier('setStore'), [
t.identifier(storeName)
])
))
}
})
示例3: 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)
}
示例4: templateLiterals
export function templateLiterals (path, state) {
let nodes: Array<Object> = []
const expressions = path.get('expressions')
for (const elem of (path.node.quasis)) {
nodes.push(t.stringLiteral(elem.value.cooked))
const expr = expressions.shift()
if (expr) {
// tslint:disable-next-line:no-multi-spaces
if (state.opts.spec && !expr.isBaseType('string') && !expr.isBaseType('number')) {
nodes.push(t.callExpression(t.identifier('String'), [expr.node]))
} else {
nodes.push(expr.node)
}
}
}
// filter out empty string literals
nodes = nodes.filter((n) => !t.isLiteral(n, { value: '' }))
// since `+` is left-to-right associative
// ensure the first node is a string if first/second isn't
if (!isString(nodes[0]) && !isString(nodes[1])) {
nodes.unshift(t.stringLiteral(''))
}
if (nodes.length > 1) {
let root = buildBinaryExpression(nodes.shift(), nodes.shift())
for (const node of nodes) {
root = buildBinaryExpression(root, node)
}
path.replaceWith(root)
} else {
path.replaceWith(nodes[0])
}
}
示例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: transform
//.........这里部分代码省略.........
}
}
},
ClassExpression (path) {
mainClass = path as any
},
ClassMethod (path) {
if (t.isIdentifier(path.node.key) && path.node.key.name === 'render') {
renderMethod = path
}
},
IfStatement (path) {
const consequent = path.get('consequent')
if (!consequent.isBlockStatement()) {
consequent.replaceWith(
t.blockStatement([
consequent.node as any
])
)
}
},
CallExpression (path) {
const callee = path.get('callee')
if (isContainJSXElement(path)) {
return
}
if (callee.isReferencedMemberExpression()) {
const id = findFirstIdentifierFromMemberExpression(callee.node)
const calleeIds = getIdsFromMemberProps(callee.node)
if (t.isIdentifier(id) && id.name.startsWith('on') && Adapters.alipay !== Adapter.type) {
const fullPath = buildFullPathThisPropsRef(id, calleeIds, path)
if (fullPath) {
path.replaceWith(
t.callExpression(
fullPath,
path.node.arguments
)
)
}
}
}
if (callee.isReferencedIdentifier()) {
const id = callee.node
const ids = [id.name]
if (t.isIdentifier(id) && id.name.startsWith('on')) {
const fullPath = buildFullPathThisPropsRef(id, ids, path)
if (fullPath) {
path.replaceWith(
t.callExpression(
fullPath,
path.node.arguments
)
)
}
}
}
},
// JSXIdentifier (path) {
// const parentPath = path.parentPath
// if (!parentPath.isJSXAttribute()) {
// return
// }
// const element = parentPath.parentPath
// if (!element.isJSXOpeningElement()) {
// return
示例7: parseAst
//.........这里部分代码省略.........
const superClass = declaration.superClass
if (superClass) {
let hasCreateData = false
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: '_createData' })) {
hasCreateData = true
}
}
})
if (hasCreateData) {
needExportDefault = true
if (declaration.id === null) {
componentClassName = '_TaroComponentClass'
} else if (declaration.id.name === 'App') {
componentClassName = '_App'
} else {
componentClassName = declaration.id.name
}
const isClassDcl = declaration.type === 'ClassDeclaration'
const classDclProps = [t.identifier(componentClassName), superClass, declaration.body, declaration.decorators || []]
astPath.replaceWith(isClassDcl ? t.classDeclaration.apply(null, classDclProps) : t.classExpression.apply(null, classDclProps))
}
}
} else if (declaration.type === 'CallExpression') {
const callee = declaration.callee
if (callee && callee.type === 'CallExpression') {
const subCallee = callee.callee
if (subCallee.type === 'Identifier' && subCallee.name === taroJsReduxConnect) {
const args = declaration.arguments as t.Identifier[]
if (args.length === 1 && args[0].name === componentClassName) {
needExportDefault = true
exportTaroReduxConnected = `${componentClassName}__Connected`
astPath.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(`${componentClassName}__Connected`), t.callExpression(declaration.callee as t.Expression, declaration.arguments as Array<t.Expression | t.SpreadElement>))]))
}
}
}
} else if (declaration.type === 'Identifier') {
const name = declaration.name
if (name === componentClassName || name === exportTaroReduxConnected) {
needExportDefault = true
astPath.remove()
}
}
},
ExportNamedDeclaration (astPath) {
const node = astPath.node
const source = node.source
if (source && source.type === 'StringLiteral') {
const value = source.value
analyzeImportUrl({ astPath, value, sourceFilePath, filePath, styleFiles, scriptFiles, jsonFiles, mediaFiles })
}
},
ExportAllDeclaration (astPath) {
const node = astPath.node
const source = node.source
if (source && source.type === 'StringLiteral') {
const value = source.value
analyzeImportUrl({ astPath, value, sourceFilePath, filePath, styleFiles, scriptFiles, jsonFiles, mediaFiles })
}
},
Program: {
exit (astPath) {
示例8: transformLoop
function transformLoop (
name: string,
attr: NodePath<t.JSXAttribute>,
jsx: NodePath<t.JSXElement>,
value: AttrValue
) {
const jsxElement = jsx.get('openingElement')
if (!jsxElement.node) {
return
}
const attrs = jsxElement.get('attributes').map(a => a.node)
const wxForItem = attrs.find(a => a.name.name === WX_FOR_ITEM)
const hasSinglewxForItem = wxForItem && wxForItem.value && t.isJSXExpressionContainer(wxForItem.value)
if (hasSinglewxForItem || name === WX_FOR || name === 'wx:for-items') {
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()
}
})
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]
try {
jsx.replaceWith(block)
} catch (error) {
//
}
return {
item: item.value,
index: index.value
}
}
}
示例9: parsePage
//.........这里部分代码省略.........
const body = prop.get('body')
if (hasArguments) {
return t.classMethod('method', t.identifier(name), params, body.node)
}
return t.classProperty(
t.identifier(name),
t.arrowFunctionExpression(params, body.node, isAsync)
)
}
if (hasArguments && (value.isFunctionExpression() || value.isArrowFunctionExpression())) {
const method = t.classMethod('method', t.identifier(name), params, value.node.body as any)
method.async = isAsync
return method
}
const classProp = t.classProperty(
t.identifier(name),
value.isFunctionExpression() || value.isArrowFunctionExpression()
? t.arrowFunctionExpression(value.node.params, value.node.body, isAsync)
: value.node
) as any
if (staticProps.includes(name)) {
classProp.static = true
}
return classProp
})
if (globals.hasCatchTrue) {
classBody.push(t.classMethod('method', t.identifier('privateStopNoop'), [t.identifier('e')], t.blockStatement([
t.expressionStatement(
t.callExpression(
t.memberExpression(t.identifier('e'), t.identifier('stopPropagation')),
[]
)
)
])))
}
if (defaultProps.length) {
let classProp = t.classProperty(t.identifier('defaultProps'), t.objectExpression(
defaultProps.map(p => t.objectProperty(t.identifier(p.name), p.value))
)) as any
classProp.static = true
classBody.unshift(classProp)
}
} else if (arg.isIdentifier()) {
weappConf = arg.node.name
} else {
throw codeFrameError(arg.node, `${componentType || '组件'} 的第一个参数必须是一个对象或变量才能转换。`)
}
if (json && t.isObjectExpression(json)) {
classBody.push(t.classProperty(t.identifier('config'), json))
}
if (componentType === 'App') {
let hasWillMount = false
const globalData = template(`this.$app.globalData = this.globalData`)()
for (const method of classBody) {
if (!method) {
continue
}
if (!t.isClassMethod(method)) {
示例10:
const buildDecorator = (type: string, id?: string) => id ? t.decorator(
t.callExpression(t.identifier('withWeapp'), [t.stringLiteral(type), t.identifier(id)])
) : t.decorator(
t.callExpression(t.identifier('withWeapp'), [t.stringLiteral(type)])
)