本文整理汇总了TypeScript中babel-types.classProperty函数的典型用法代码示例。如果您正苦于以下问题:TypeScript classProperty函数的具体用法?TypeScript classProperty怎么用?TypeScript classProperty使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了classProperty函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: resetTSClassProperty
export function resetTSClassProperty (body) {
for (const method of body) {
if (t.isClassMethod(method) && method.kind === 'constructor') {
for (const statement of cloneDeep(method.body.body)) {
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))
) {
body.push(
t.classProperty(left.property, right)
)
remove(method.body.body, statement)
}
}
}
}
}
}
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:27,代码来源:helper.ts
示例2: handleClosureJSXFunc
function handleClosureJSXFunc (jsx: NodePath<t.JSXElement>, mainClass: NodePath<t.ClassDeclaration>) {
// 在 ./functional.ts 会把 FunctionExpression 转化为 arrowFunctionExpr
// 所以我们这里只处理一种情况
const arrowFunc = jsx.findParent(p => p.isArrowFunctionExpression())
if (arrowFunc && arrowFunc.isArrowFunctionExpression()) {
const parentPath = arrowFunc.parentPath
if (parentPath.isVariableDeclarator()) {
const id = parentPath.node.id
if (t.isIdentifier(id) && id.name.startsWith('render')) {
const funcName = `renderClosure${id.name.slice(6, id.name.length)}`
mainClass.node.body.body.push(
t.classProperty(
t.identifier(funcName),
cloneDeep(arrowFunc.node)
)
)
parentPath.scope.rename(id.name, funcName)
arrowFunc.replaceWith(t.memberExpression(
t.thisExpression(),
t.identifier(funcName)
))
}
}
}
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:25,代码来源:index.ts
示例3: handleThirdPartyComponent
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
})
开发者ID:topud,项目名称:taro,代码行数:23,代码来源:index.ts
示例4: codeFrameError
classBody = properties.map(prop => {
const key = prop.get('key')
const value = prop.get('value')
let 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
const currentStateKeys: string[] = []
if (name === 'data') {
if (value.isObjectExpression()) {
value
.get('properties')
.map(p => p.node)
.forEach(prop => {
if (t.isObjectProperty(prop)) {
let propKey = ''
if (t.isStringLiteral(prop.key)) {
propKey = prop.key.value
}
if (t.isIdentifier(prop.key)) {
propKey = prop.key.name
}
if (!isValidVarName(propKey)) {
throw codeFrameError(prop, `${propKey} 不是一个合法的 JavaScript 变量名`)
}
if (propKey) {
currentStateKeys.push(propKey)
}
}
})
}
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 (!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)
}
}
})
}
//.........这里部分代码省略.........
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:script.ts
示例5: parsePage
//.........这里部分代码省略.........
: value.isFunctionExpression() || value.isArrowFunctionExpression()
? value.node.async
: false
if (!key.isIdentifier()) {
throw codeFrameError(key.node, 'Page 对象的键值只能是字符串')
}
const name = key.node.name
const currentStateKeys: string[] = []
if (name === 'data') {
if (value.isObjectExpression()) {
value
.get('properties')
.map(p => p.node)
.forEach(prop => {
if (t.isObjectProperty(prop)) {
let propKey = ''
if (t.isStringLiteral(prop.key)) {
propKey = prop.key.value
}
if (t.isIdentifier(prop.key)) {
propKey = prop.key.name
}
if (!isValidVarName(propKey)) {
throw codeFrameError(prop, `${propKey} 不是一个合法的 JavaScript 变量名`)
}
if (propKey) {
currentStateKeys.push(propKey)
}
}
})
}
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
})
开发者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
function parsePage (
path: NodePath<t.CallExpression>,
returned: t.Expression,
json?: t.ObjectExpression,
componentType?: string
) {
const stateKeys: string[] = []
const propsKeys: string[] = []
const arg = path.get('arguments')[0]
if (!arg || !arg.isObjectExpression()) {
return
}
const defaultProps: { name: string, value: any }[] = []
const props = arg.get('properties')
const properties = props.filter(p => !p.isSpreadProperty()) as NodePath<
t.ObjectProperty | t.ObjectMethod
>[]
if (properties.length !== props.length) {
throw new Error(
'不支持编译在 Page 对象中使用解构(`...` spread property)语法'
)
}
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)
//.........这里部分代码省略.........
开发者ID:topud,项目名称:taro,代码行数:101,代码来源:script.ts
示例8: parseTemplate
export function parseTemplate (path: NodePath<t.JSXElement>, dirPath: string) {
if (!path.container) {
return
}
const openingElement = path.get('openingElement')
const attrs = openingElement.get('attributes')
const is = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'is' }))
const data = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'data' }))
// const spread = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'spread' }))
const name = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'name' }))
const refIds = new Set<string>()
const loopIds = new Set<string>()
let imports: any[] = []
if (name) {
const value = name.node.value
if (value === null || !t.isStringLiteral(value)) {
throw new Error('template 的 `name` 属性只能是字符串')
}
const className = buildTemplateName(value.value)
path.traverse(createWxmlVistor(loopIds, refIds, dirPath, [], imports))
const firstId = Array.from(refIds)[0]
refIds.forEach(id => {
if (loopIds.has(id) && id !== firstId) {
refIds.delete(id)
}
})
const block = buildBlockElement()
block.children = path.node.children
let render: t.ClassMethod
if (refIds.size === 0) {
// 无状态组件
render = buildRender(block, [], [])
} else if (refIds.size === 1) {
// 只有一个数据源
render = buildRender(block, [], Array.from(refIds), firstId)
} else {
// 使用 ...spread
render = buildRender(block, [], Array.from(refIds), [])
}
const classProp = t.classProperty(t.identifier('options'), t.objectExpression([
t.objectProperty(
t.identifier('addGlobalClass'),
t.booleanLiteral(true)
)
])) as any
classProp.static = true
const classDecl = t.classDeclaration(
t.identifier(className),
t.memberExpression(t.identifier('Taro'), t.identifier('Component')),
t.classBody([render, classProp]),
[]
)
path.remove()
return {
name: className,
ast: classDecl
}
} else if (is) {
const value = is.node.value
if (!value) {
throw new Error('template 的 `is` 属性不能为空')
}
if (t.isStringLiteral(value)) {
const className = buildTemplateName(value.value)
let attributes: t.JSXAttribute[] = []
if (data) {
attributes.push(data.node)
}
path.replaceWith(t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier(className), attributes),
t.jSXClosingElement(t.jSXIdentifier(className)),
[],
true
))
} else if (t.isJSXExpressionContainer(value)) {
if (t.isStringLiteral(value.expression)) {
const className = buildTemplateName(value.expression.value)
let attributes: t.JSXAttribute[] = []
if (data) {
attributes.push(data.node)
}
path.replaceWith(t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier(className), attributes),
t.jSXClosingElement(t.jSXIdentifier(className)),
[],
true
))
} else if (t.isConditional(value.expression)) {
const { test, consequent, alternate } = value.expression
if (!t.isStringLiteral(consequent) || !t.isStringLiteral(alternate)) {
throw new Error('当 template is 标签是三元表达式时,他的两个值都必须为字符串')
}
let attributes: t.JSXAttribute[] = []
if (data) {
attributes.push(data.node)
}
const block = buildBlockElement()
block.children = [t.jSXExpressionContainer(t.conditionalExpression(
//.........这里部分代码省略.........
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:template.ts
注:本文中的babel-types.classProperty函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论