本文整理匯總了TypeScript中babel-types.isBlockStatement函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isBlockStatement函數的具體用法?TypeScript isBlockStatement怎麽用?TypeScript isBlockStatement使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了isBlockStatement函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: resetTSClassProperty
/**
* TS 編譯器會把 class property 移到構造器,
* 而小程序要求 `config` 和所有函數在初始化(after new Class)之後就收集到所有的函數和 config 信息,
* 所以當如構造器裏有 this.func = () => {...} 的形式,就給他轉換成普通的 classProperty function
* 如果有 config 就給他還原
*/
function resetTSClassProperty (body: (t.ClassMethod | t.ClassProperty)[]) {
for (const method of body) {
if (t.isClassMethod(method) && method.kind === 'constructor') {
if (t.isBlockStatement(method.body)) {
method.body.body = method.body.body.filter(statement => {
if (t.isExpressionStatement(statement) && t.isAssignmentExpression(statement.expression)) {
const expr = statement.expression
const { left, right } = expr
if (
t.isMemberExpression(left) &&
t.isThisExpression(left.object) &&
t.isIdentifier(left.property)
) {
if (
(t.isArrowFunctionExpression(right) || t.isFunctionExpression(right))
||
(left.property.name === 'config' && t.isObjectExpression(right))
) {
const classProp = t.classProperty(left.property, right)
body.push(classProp)
handleThirdPartyComponent(classProp)
return false
}
}
}
return true
})
}
}
}
}
示例2: 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: (p) => {
const { id, init } = p.node
if (t.isIdentifier(id)) {
const newId = scope.generateDeclaredUidIdentifier('$' + id.name)
refIds.forEach((refId) => {
if (refId.name === variableName && !variableName.startsWith('_$')) {
refIds.delete(refId)
}
})
variableName = newId.name
refIds.add(t.identifier(variableName))
blockStatement.scope.rename(id.name, newId.name)
p.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 {
func.body.body.splice(func.body.body.length - 1, 0, buildConstVariableDeclaration(variableName, expr))
}
}
}
const id = t.identifier(variableName)
expression.replaceWith(id)
return id
}
示例3: 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 conditionExpr = jsx.findParent(p => p.isConditionalExpression())
const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }))
let expr = cloneDeep(expression.node)
if (conditionExpr && conditionExpr.isConditionalExpression()) {
const consequent = conditionExpr.get('consequent')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(conditionExpr.get('test').node as any, expr, t.nullLiteral())
}
}
if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
const consequent = logicExpr.get('right')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(logicExpr.get('left').node as any, expr, t.nullLiteral())
}
}
if (!callExpr) {
refIds.add(t.identifier(variableName))
statementParent.insertBefore(
buildConstVariableDeclaration(variableName, expr)
)
} 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 {
statementParent.insertBefore(
buildConstVariableDeclaration(variableName, expr)
)
}
}
}
expression.replaceWith(
t.identifier(variableName)
)
}
示例4: 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
}
示例5: JSXElement
} = () => {
return {
visitor: {
JSXElement (path) {
const arrowFuncExpr = path.findParent(p => p.isArrowFunctionExpression())
if (arrowFuncExpr && arrowFuncExpr.isArrowFunctionExpression() && arrowFuncExpr.parentPath.isVariableDeclarator()) {
const valDecl = arrowFuncExpr.parentPath.parentPath
if (!valDecl.isVariableDeclaration()) {
throw codeFrameError(valDecl.node, '函數式組件不能同時定義多個值')
}
const id = arrowFuncExpr.parentPath.node.id
if (!t.isIdentifier(id)) {
throw codeFrameError(id, '函數式組件隻能使用普通標識符定義')
}
if (!initialIsCapital(id.name)) {
return
}
const hasClassDecl = arrowFuncExpr.findParent(p => p.isClassDeclaration())
if (hasClassDecl) {
// @TODO: 加上鏈接
return
}
const { body } = arrowFuncExpr.node
if (t.isBlockStatement(body)) {
valDecl.replaceWith(t.functionDeclaration(id, arrowFuncExpr.node.params, body))
} else {
valDecl.replaceWith(t.functionDeclaration(id, arrowFuncExpr.node.params, t.blockStatement([
t.returnStatement(body)
])))
}
return
}
const functionDecl = path.findParent(p => p.isFunctionDeclaration())
if (functionDecl && functionDecl.isFunctionDeclaration()) {
const hasClassDecl = functionDecl.findParent(p => p.isClassDeclaration())
if (hasClassDecl) {
// @TODO: 加上鏈接
return
}
const { id, body, params } = functionDecl.node
let arg: null | t.LVal = null
if (params.length > 1) {
throw codeFrameError(id, '函數式組件的參數最多隻能傳入一個')
} else if (params.length === 1) {
arg = params[0]
}
const cloneBody = cloneDeep(body)
if (!initialIsCapital(id.name)) {
throw codeFrameError(id, `普通函數式組件命名規則請遵守帕斯卡命名法(Pascal Case), 如果是在函數內聲明閉包組件,則需要使用函數表達式的寫法。
形如:
const ${id.name} = ${generate(t.arrowFunctionExpression(params, body)).code}
`)
}
if (arg) {
if (t.isIdentifier(arg)) {
cloneBody.body.unshift(buildConstVariableDeclaration(arg.name, t.memberExpression(t.thisExpression(), t.identifier('props'))))
} else if (t.isObjectPattern(arg)) {
cloneBody.body.unshift(
t.variableDeclaration('const', [
t.variableDeclarator(arg, t.memberExpression(t.thisExpression(), t.identifier('props')))
])
)
} else {
throw codeFrameError(arg, '函數式組件隻支持傳入一個簡單標識符或使用對象結構')
}
}
const classDecl = t.classDeclaration(id, t.memberExpression(t.identifier('Taro'), t.identifier('Component')), t.classBody([
t.classMethod('method', t.identifier('render'), [], cloneBody)
]), [])
functionDecl.replaceWith(classDecl)
}
}
}
}
}