當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript babel-types.isClassMethod函數代碼示例

本文整理匯總了TypeScript中babel-types.isClassMethod函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isClassMethod函數的具體用法?TypeScript isClassMethod怎麽用?TypeScript isClassMethod使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了isClassMethod函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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
        })
      }
    }
  }
}
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:37,代碼來源:index.ts

示例2: 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

示例3: 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)
}
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:58,代碼來源:utils.ts

示例4: _getMethods

function* _getMethods(node: babel.Node) {
  if (!babel.isClassDeclaration(node) && !babel.isClassExpression(node)) {
    return;
  }
  for (const statement of node.body.body) {
    if (babel.isClassMethod(statement) && statement.kind === 'method') {
      yield statement;
    }
  }
}
開發者ID:asdfg9822,項目名稱:polymer-analyzer,代碼行數:10,代碼來源:esutil.ts

示例5: getIdentifierName

 (n) => babel.isClassMethod(n) && n.static === true &&
     n.kind === 'get' && getIdentifierName(n.key) === name) as
開發者ID:asdfg9822,項目名稱:polymer-analyzer,代碼行數:2,代碼來源:polymer2-config.ts

示例6: parsePage


//.........這裏部分代碼省略.........
        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)) {
        continue
      }
      if (t.isIdentifier(method.key, { name: Lifecycle.componentWillMount })) {
        hasWillMount = true
        method.body.body.unshift(globalData)
      }
    }
    if (!hasWillMount) {
      classBody.push(
        t.classMethod(
          'method',
          t.identifier(Lifecycle.componentWillMount),
          [],
          t.blockStatement([globalData])
        )
      )
    }
  }

  const wxsNames = new Set(wxses ? wxses.map(w => w.module) : [])

  const renderFunc = buildRender(returned, stateKeys.filter(s => !wxsNames.has(s)), propsKeys)

  const classDecl = t.classDeclaration(
    t.identifier(componentType === 'App' ? 'App' : defaultClassName),
    t.memberExpression(t.identifier('Taro'), t.identifier('Component')),
    t.classBody(
      classBody.concat(renderFunc)
    ),
    []
  )

  if (weappConf) {
    classDecl.decorators = [buildDecorator(componentType || 'Page', weappConf)]
  }

  return classDecl
}
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:101,代碼來源:script.ts

示例7: parsePage


//.........這裏部分代碼省略.........
                }
              }
              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)) {
    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)) {
        continue
      }
      if (t.isIdentifier(method.key, { name: Lifecycle.componentWillMount })) {
        hasWillMount = true
        method.body.body.unshift(globalData)
      }
    }
    if (!hasWillMount) {
      classBody.push(
        t.classMethod(
          'method',
          t.identifier(Lifecycle.componentWillMount),
          [],
          t.blockStatement([globalData])
        )
      )
    }
  }

  const renderFunc = buildRender(returned, stateKeys, propsKeys)

  return t.classDeclaration(
    t.identifier(componentType === 'App' ? 'App' : defaultClassName),
    t.memberExpression(t.identifier('Taro'), t.identifier('Component')),
    t.classBody(
      classBody.concat(renderFunc)
    ),
    []
  )
}
開發者ID:topud,項目名稱:taro,代碼行數:101,代碼來源:script.ts


注:本文中的babel-types.isClassMethod函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。