本文整理汇总了TypeScript中babel-types.expressionStatement函数的典型用法代码示例。如果您正苦于以下问题:TypeScript expressionStatement函数的具体用法?TypeScript expressionStatement怎么用?TypeScript expressionStatement使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了expressionStatement函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: parseWXML
export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean): {
wxses: WXS[]
wxml?: t.Node
imports: Imports[]
refIds: Set<string>
} {
if (!parseImport) {
errors.length = 0
usedComponents.clear()
}
usedComponents.add('Block')
let wxses: WXS[] = []
let imports: Imports[] = []
const refIds = new Set<string>()
const loopIds = new Set<string>()
if (!wxml) {
return {
wxses,
imports,
refIds,
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, createWxmlVistor(loopIds, refIds, dirPath, wxses, imports))
refIds.forEach(id => {
if (loopIds.has(id) || imports.filter(i => i.wxs).map(i => i.name).includes(id)) {
refIds.delete(id)
}
})
return {
wxses,
imports,
wxml: hydrate(ast),
refIds
}
}
示例4: BinaryExpression
}
}
});
// Examples from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#writing-your-first-babel-plugin
const v1: Visitor = {
BinaryExpression(path) {
if (t.isIdentifier(path.node.left)) {
// ...
}
path.replaceWith(
t.binaryExpression("**", path.node.left, t.numericLiteral(2))
);
path.parentPath.replaceWith(
t.expressionStatement(t.stringLiteral("Anyway the wind blows, doesn't really matter to me, to me."))
);
path.parentPath.remove();
},
Identifier(path) {
if (path.isReferencedIdentifier()) {
// ...
}
if (t.isReferenced(path.node, path.parent)) {
// ...
}
},
ReturnStatement(path) {
path.replaceWithMultiple([
示例5: 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)
}
//.........这里部分代码省略.........
示例6: parsePage
//.........这里部分代码省略.........
if (prop.isObjectMethod()) {
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
}
示例7: generateAnonymousState
export function generateAnonymousState (
scope: Scope,
expression: NodePath<t.Expression>,
refIds: Set<t.Identifier>,
isLogical?: boolean
) {
let variableName = `anonymousState_${scope.generateUid()}`
let statementParent = expression.getStatementParent()
if (!statementParent) {
throw codeFrameError(expression.node.loc, '无法生成匿名 State,尝试先把值赋到一个变量上再把变量调换。')
}
const jsx = isLogical ? expression : expression.findParent(p => p.isJSXElement())
const callExpr = jsx.findParent(p => p.isCallExpression() && isArrayMapCallExpression(p)) as NodePath<t.CallExpression>
const ifExpr = jsx.findParent(p => p.isIfStatement())
const blockStatement = jsx.findParent(p => p.isBlockStatement() && p.parentPath === ifExpr) as NodePath<t.BlockStatement>
const expr = setParentCondition(jsx, cloneDeep(expression.node))
if (!callExpr) {
refIds.add(t.identifier(variableName))
statementParent.insertBefore(
buildConstVariableDeclaration(variableName, expr)
)
if (blockStatement && blockStatement.isBlockStatement()) {
blockStatement.traverse({
VariableDeclarator: (path) => {
const { id, init } = path.node
const isArrowFunctionInJSX = path.findParent(p => p.isJSXAttribute() ||
(
p.isAssignmentExpression() && t.isMemberExpression(p.node.left) && t.isThisExpression(p.node.left.object)
&& t.isIdentifier(p.node.left.property) && p.node.left.property.name.startsWith('')
)
)
if (isArrowFunctionInJSX) {
return
}
if (t.isIdentifier(id) && !id.name.startsWith(LOOP_STATE)) {
const newId = scope.generateDeclaredUidIdentifier('$' + id.name)
refIds.forEach((refId) => {
if (refId.name === variableName && !variableName.startsWith('_$')) {
refIds.delete(refId)
}
})
variableName = newId.name
if (Adapter.type === Adapters.quickapp && variableName.startsWith('_$')) {
const newVarName = variableName.slice(2)
scope.rename(variableName, newVarName)
variableName = newVarName
}
refIds.add(t.identifier(variableName))
blockStatement.scope.rename(id.name, newId.name)
path.parentPath.replaceWith(
template('ID = INIT;')({ ID: newId, INIT: init })
)
}
}
})
}
} else {
variableName = `${LOOP_STATE}_${callExpr.scope.generateUid()}`
const func = callExpr.node.arguments[0]
if (t.isArrowFunctionExpression(func)) {
if (!t.isBlockStatement(func.body)) {
func.body = t.blockStatement([
buildConstVariableDeclaration(variableName, expr),
t.returnStatement(func.body)
])
} else {
if (ifExpr && ifExpr.isIfStatement() && ifExpr.findParent(p => p === callExpr)) {
const consequent = ifExpr.get('consequent')
const test = ifExpr.get('test')
if (consequent.isBlockStatement()) {
if (jsx === test || jsx.findParent(p => p === test)) {
func.body.body.unshift(buildConstVariableDeclaration(variableName, expr))
} else {
func.body.body.unshift(t.variableDeclaration('let', [t.variableDeclarator(t.identifier(variableName), t.nullLiteral())]))
consequent.node.body.push(t.expressionStatement(t.assignmentExpression(
'=',
t.identifier(variableName),
expr
)))
}
} else {
throw codeFrameError(consequent.node, 'if 表达式的结果必须由一个花括号包裹')
}
} else {
func.body.body.splice(func.body.body.length - 1, 0, buildConstVariableDeclaration(variableName, expr))
}
}
}
}
const id = t.identifier(variableName)
expression.replaceWith(id)
return id
}