本文整理汇总了TypeScript中babel-types.classMethod函数的典型用法代码示例。如果您正苦于以下问题:TypeScript classMethod函数的具体用法?TypeScript classMethod怎么用?TypeScript classMethod使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了classMethod函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: buildRender
export function buildRender (
returned: t.Expression,
stateKeys: string[],
propsKeys: string[],
templateType?: string | never[]
) {
const returnStatement: t.Statement[] = [t.returnStatement(returned)]
if (stateKeys.length) {
const stateDecl = t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern(Array.from(new Set(stateKeys)).filter(s => !propsKeys.includes(s)).map(s =>
t.objectProperty(t.identifier(s), t.identifier(s))
) as any),
t.memberExpression(t.thisExpression(), t.identifier('state'))
)
])
returnStatement.unshift(stateDecl)
}
if (propsKeys.length) {
let patterns = t.objectPattern(Array.from(new Set(propsKeys)).map(s =>
t.objectProperty(t.identifier(s), t.identifier(s))
) as any)
if (typeof templateType === 'string') {
patterns = t.objectPattern([
t.objectProperty(
t.identifier('data'),
templateType === 'wxParseData'
? t.objectPattern([t.objectProperty(t.identifier('wxParseData'), t.identifier('wxParseData')) as any]) as any
: t.identifier(templateType)
) as any
])
} else if (Array.isArray(templateType)) {
patterns = t.objectPattern([
t.objectProperty(t.identifier('data'), patterns as any) as any
])
}
const stateDecl = t.variableDeclaration('const', [
t.variableDeclarator(
patterns,
t.memberExpression(t.thisExpression(), t.identifier('props'))
)
])
returnStatement.unshift(stateDecl)
}
return t.classMethod(
'method',
t.identifier('render'),
[],
t.blockStatement(returnStatement)
)
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:52,代码来源:utils.ts
示例2: transform
//.........这里部分代码省略.........
}
}
}
// @TODO: bind 的处理待定
}
},
ImportDeclaration (path) {
const source = path.node.source.value
if (importSources.has(source)) {
throw codeFrameError(path.node, '无法在同一文件重复 import 相同的包。')
} else {
importSources.add(source)
}
const names: string[] = []
if (source === TARO_PACKAGE_NAME) {
isImportTaro = true
path.node.specifiers.push(
t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
t.importSpecifier(t.identifier(INTERNAL_GET_ORIGNAL), t.identifier(INTERNAL_GET_ORIGNAL)),
t.importSpecifier(t.identifier(INTERNAL_INLINE_STYLE), t.identifier(INTERNAL_INLINE_STYLE))
)
}
if (
source === REDUX_PACKAGE_NAME || source === MOBX_PACKAGE_NAME
) {
path.node.specifiers.forEach((s, index, specs) => {
if (s.local.name === 'Provider') {
specs.splice(index, 1)
specs.push(
t.importSpecifier(t.identifier('setStore'), t.identifier('setStore'))
)
}
})
}
path.traverse({
ImportDefaultSpecifier (path) {
const name = path.node.local.name
DEFAULT_Component_SET.has(name) || names.push(name)
},
ImportSpecifier (path) {
const name = path.node.imported.name
DEFAULT_Component_SET.has(name) || names.push(name)
if (source === TARO_PACKAGE_NAME && name === 'Component') {
path.node.local = t.identifier('__BaseComponent')
}
}
})
componentSourceMap.set(source, names)
}
})
if (!isImportTaro) {
ast.program.body.unshift(
t.importDeclaration([
t.importDefaultSpecifier(t.identifier('Taro')),
t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
t.importSpecifier(t.identifier(INTERNAL_GET_ORIGNAL), t.identifier(INTERNAL_GET_ORIGNAL)),
t.importSpecifier(t.identifier(INTERNAL_INLINE_STYLE), t.identifier(INTERNAL_INLINE_STYLE))
], t.stringLiteral('@tarojs/taro'))
)
}
if (!mainClass) {
throw new Error('未找到 Taro.Component 的类定义')
}
mainClass.node.body.body.forEach(handleThirdPartyComponent)
const storeBinding = mainClass.scope.getBinding(storeName)
mainClass.scope.rename('Component', '__BaseComponent')
if (storeBinding) {
const statementPath = storeBinding.path.getStatementParent()
if (statementPath) {
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)
])
))
}
})
}
}
resetTSClassProperty(mainClass.node.body.body)
if (options.isApp) {
renderMethod.replaceWith(
t.classMethod('method', t.identifier('_createData'), [], t.blockStatement([]))
)
return { ast } as TransformResult
}
result = new Transformer(mainClass, options.sourcePath, componentProperies).result
result.code = generate(ast).code
result.ast = ast
result.template = prettyPrint(result.template, {
max_char: 0
})
result.imageSrcs = Array.from(imageSource)
return result
}
开发者ID:topud,项目名称:taro,代码行数:101,代码来源:index.ts
示例3: parseAst
//.........这里部分代码省略.........
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) {
astPath.traverse({
ClassBody (astPath) {
if (isQuickApp) {
const node = astPath.node
if (!hasComponentWillMount) {
node.body.push(t.classMethod(
'method', t.identifier('hasComponentWillMount'), [],
t.blockStatement([]), false, false))
}
if (!hasComponentDidShow) {
node.body.push(t.classMethod(
'method', t.identifier('componentDidShow'), [],
t.blockStatement([]), false, false))
}
if (!hasComponentDidHide) {
node.body.push(t.classMethod(
'method', t.identifier('componentDidHide'), [],
t.blockStatement([]), false, false))
}
node.body.push(t.classMethod(
'method', t.identifier('__listenToSetNavigationBarEvent'), [],
t.blockStatement([convertSourceStringToAstExpression(
`if (!Taro.eventCenter.callbacks['TaroEvent:setNavigationBar']) {
Taro.eventCenter.on('TaroEvent:setNavigationBar', params => {
if (params.title) {
this.$scope.$page.setTitleBar({ text: params.title })
}
if (params.frontColor) {
this.$scope.$page.setTitleBar({ textColor: params.frontColor })
}
if (params.backgroundColor) {
this.$scope.$page.setTitleBar({ backgroundColor: params.backgroundColor })
}
})
}`
)]), false, false))
node.body.push(t.classMethod(
'method', t.identifier('__offListenToSetNavigationBarEvent'), [],
开发者ID:YangShaoQun,项目名称:taro,代码行数:67,代码来源:astProcess.ts
示例4: codeFrameError
//.........这里部分代码省略.........
})
}
if (!isValidVarName(propKey)) {
throw codeFrameError(prop, `${propKey} 不是一个合法的 JavaScript 变量名`)
}
}
if (t.isObjectMethod(p) && t.isIdentifier(p.key, { name: 'observer' })) {
observeProps.push({
name: propKey,
observer: t.arrowFunctionExpression(p.params, p.body, p.async)
})
}
}
}
if (propKey) {
propsKeys.push(propKey)
}
}
})
}
currentStateKeys.forEach(s => {
if (propsKeys.includes(s)) {
throw new Error(`当前 Component 定义了重复的 data 和 properites: ${s}`)
}
})
stateKeys.push(...currentStateKeys)
return t.classProperty(t.identifier('_observeProps'), t.arrayExpression(
observeProps.map(p => t.objectExpression([
t.objectProperty(
t.identifier('name'),
t.stringLiteral(p.name)
),
t.objectProperty(
t.identifier('observer'),
p.observer
)
]))
))
}
if (PageLifecycle.has(name)) {
const lifecycle = PageLifecycle.get(name)!
if (name === 'onLoad' && t.isIdentifier(params[0])) {
params = [t.assignmentPattern(params[0] as t.Identifier, t.logicalExpression('||', t.memberExpression(
t.memberExpression(
t.thisExpression(),
t.identifier('$router')
),
t.identifier('params')
), t.objectExpression([])))]
}
if (prop.isObjectMethod()) {
const body = prop.get('body')
return t.classMethod('method', t.identifier(lifecycle), params, body.node)
}
const node = value.node
const method = t.isFunctionExpression(node) || t.isArrowFunctionExpression(node)
? t.classProperty(t.identifier(lifecycle), t.arrowFunctionExpression(params, node.body, isAsync))
: t.classProperty(t.identifier(lifecycle), node)
return method
}
let hasArguments = false
prop.traverse({
Identifier (path) {
if (path.node.name === 'arguments') {
hasArguments = true
path.stop()
}
}
})
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
})
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:script.ts
示例5: parsePage
//.........这里部分代码省略.........
}
currentStateKeys.forEach(s => {
if (propsKeys.includes(s)) {
throw new Error(`当前 Component 定义了重复的 data 和 properites: ${s}`)
}
})
stateKeys.push(...currentStateKeys)
return t.classProperty(t.identifier('_observeProps'), t.arrayExpression(
observeProps.map(p => t.objectExpression([
t.objectProperty(
t.identifier('name'),
t.stringLiteral(p.name)
),
t.objectProperty(
t.identifier('observer'),
p.observer
)
]))
))
}
if (PageLifecycle.has(name)) {
const lifecycle = PageLifecycle.get(name)!
if (name === 'onLoad' && t.isIdentifier(params[0])) {
params = [t.assignmentPattern(params[0] as t.Identifier, t.logicalExpression('||', t.memberExpression(
t.memberExpression(
t.thisExpression(),
t.identifier('$router')
),
t.identifier('params')
), t.objectExpression([])))]
}
if (prop.isObjectMethod()) {
const body = prop.get('body')
return t.classMethod('method', t.identifier(lifecycle), params, body.node)
}
const node = value.node
const method = t.isFunctionExpression(node) || t.isArrowFunctionExpression(node)
? t.classProperty(t.identifier(lifecycle), t.arrowFunctionExpression(params, node.body, isAsync))
: t.classProperty(t.identifier(lifecycle), node)
return method
}
let hasArguments = false
prop.traverse({
Identifier (path) {
if (path.node.name === 'arguments') {
hasArguments = true
path.stop()
}
}
})
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
开发者ID:YangShaoQun,项目名称:taro,代码行数:67,代码来源:script.ts
示例6: codeFrameError
let classBody = properties.map(prop => {
const key = prop.get('key')
const value = prop.get('value')
const params = prop.isObjectMethod()
? prop.node.params
: value.isFunctionExpression() || value.isArrowFunctionExpression()
? value.node.params
: []
const isAsync = prop.isObjectMethod()
? prop.node.async
: value.isFunctionExpression() || value.isArrowFunctionExpression()
? value.node.async
: false
if (!key.isIdentifier()) {
throw codeFrameError(key.node, 'Page 对象的键值只能是字符串')
}
const name = key.node.name
if (name === 'data') {
if (value.isObjectExpression()) {
value
.get('properties')
.map(p => p.node)
.forEach(prop => {
if (t.isObjectProperty(prop)) {
if (t.isStringLiteral(prop.key)) {
stateKeys.push(prop.key.value)
}
if (t.isIdentifier(prop.key)) {
stateKeys.push(prop.key.name)
}
}
})
}
return t.classProperty(t.identifier('state'), value.node)
}
if (name === 'properties') {
const observeProps: { name: string, observer: any }[] = []
if (value.isObjectExpression()) {
value
.get('properties')
.map(p => p.node)
.forEach(prop => {
if (t.isObjectProperty(prop)) {
let propKey: string | null = null
if (t.isStringLiteral(prop.key)) {
propKey = prop.key.value
}
if (t.isIdentifier(prop.key)) {
propKey = prop.key.name
// propsKeys.push(prop.key.name)
}
if (t.isObjectExpression(prop.value) && propKey) {
for (const p of prop.value.properties) {
if (t.isObjectProperty(p)) {
let key: string | null = null
if (t.isStringLiteral(p.key)) {
key = p.key.value
}
if (t.isIdentifier(p.key)) {
key = p.key.name
}
if (key === 'value') {
defaultProps.push({
name: propKey,
value: p.value
})
} else if (key === 'observer') {
observeProps.push({
name: propKey,
observer: p.value
})
}
}
}
}
if (propKey) {
propsKeys.push(propKey)
}
}
})
}
return t.classProperty(t.identifier('_observeProps'), t.arrayExpression(
observeProps.map(p => t.objectExpression([
t.objectProperty(
t.identifier('name'),
t.stringLiteral(p.name)
),
t.objectProperty(
t.identifier('observer'),
p.observer
)
]))
))
}
if (PageLifecycle.has(name)) {
const lifecycle = PageLifecycle.get(name)!
const node = value.node as
| t.FunctionExpression
| t.ArrowFunctionExpression
const method = t.classMethod(
//.........这里部分代码省略.........
开发者ID:topud,项目名称:taro,代码行数:101,代码来源:script.ts
示例7: parsePage
//.........这里部分代码省略.........
} else if (key === 'observer') {
observeProps.push({
name: propKey,
observer: p.value
})
}
}
}
}
if (propKey) {
propsKeys.push(propKey)
}
}
})
}
return t.classProperty(t.identifier('_observeProps'), t.arrayExpression(
observeProps.map(p => t.objectExpression([
t.objectProperty(
t.identifier('name'),
t.stringLiteral(p.name)
),
t.objectProperty(
t.identifier('observer'),
p.observer
)
]))
))
}
if (PageLifecycle.has(name)) {
const lifecycle = PageLifecycle.get(name)!
const node = value.node as
| t.FunctionExpression
| t.ArrowFunctionExpression
const method = t.classMethod(
'method',
t.identifier(lifecycle),
params,
node ? node.body as t.BlockStatement : (prop.get('body') as any).node
)
method.async = isAsync
return method
}
if (prop.isObjectMethod()) {
const body = prop.get('body')
return t.classProperty(
t.identifier(name),
t.arrowFunctionExpression(params, body.node, isAsync)
)
}
return t.classProperty(
t.identifier(name),
value.isFunctionExpression() || value.isArrowFunctionExpression()
? t.arrowFunctionExpression(value.node.params, value.node.body, isAsync)
: value.node
)
})
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)
}
if (json && t.isObjectExpression(json)) {
开发者ID:topud,项目名称:taro,代码行数:67,代码来源:script.ts
示例8: parseJSCode
//.........这里部分代码省略.........
const componentDidHideCallNode = toAst(`this.componentDidHide()`)
node.body.body.unshift(componentDidHideCallNode)
}
if (key.name == 'render') {
let funcBody = `
<${taroComponentsRNProviderName}>
${classRenderReturnJSX}
</${taroComponentsRNProviderName}>`
if (pages.length > 0) {
funcBody = `
<${taroComponentsRNProviderName}>
<RootStack/>
</${taroComponentsRNProviderName}>`
}
if (providerComponentName && storeName) {
// 使用redux 或 mobx
funcBody = `
<${providorImportName} store={${storeName}}>
${funcBody}
</${providorImportName}>`
}
node.body = template(`{return (${funcBody});}`, babylonConfig as any)() as any
}
},
ClassBody: {
exit (astPath: NodePath<t.ClassBody>) {
if (!isEntryFile) return
const node = astPath.node
if (hasComponentDidShow && !hasComponentDidMount) {
node.body.push(t.classMethod(
'method', t.identifier('componentDidMount'), [],
t.blockStatement([
toAst('this.componentDidShow && this.componentDidShow()') as t.Statement
]), false, false))
}
if (hasComponentDidHide && !hasComponentWillUnmount) {
node.body.push(t.classMethod(
'method', t.identifier('componentWillUnmount'), [],
t.blockStatement([
toAst('this.componentDidHide && this.componentDidHide()') as t.Statement
]), false, false))
}
if (!hasConstructor) {
node.body.unshift(t.classMethod(
'method', t.identifier('constructor'), [t.identifier('props'), t.identifier('context')],
t.blockStatement([toAst('super(props, context)'), additionalConstructorNode] as t.Statement[]), false, false))
}
}
},
CallExpression (astPath) {
const node = astPath.node
const callee = node.callee as t.Identifier
const calleeName = callee.name
const parentPath = astPath.parentPath
if (t.isMemberExpression(callee)) {
const object = callee.object as t.Identifier
const property = callee.property as t.Identifier
if (object.name === taroImportDefaultName && property.name === 'render') {
astPath.remove()
}
} else {
开发者ID:YangShaoQun,项目名称:taro,代码行数:67,代码来源:transformJS.ts
示例9: 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)
}
}
}
}
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:76,代码来源:functional.ts
注:本文中的babel-types.classMethod函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论