• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript babel-types.exportDefaultDeclaration函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中babel-types.exportDefaultDeclaration函数的典型用法代码示例。如果您正苦于以下问题:TypeScript exportDefaultDeclaration函数的具体用法?TypeScript exportDefaultDeclaration怎么用?TypeScript exportDefaultDeclaration使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了exportDefaultDeclaration函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: 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)
          }
//.........这里部分代码省略.........
开发者ID:topud,项目名称:taro,代码行数:101,代码来源:wxml.ts


示例2: parseScript

export function parseScript (
  script?: string,
  returned?: t.Expression,
  json?: t.ObjectExpression,
  wxses: WXS[] = [],
  refId?: Set<string>
) {
  script = script || 'Page({})'
  if (t.isJSXText(returned as any)) {
    const block = buildBlockElement()
    block.children = [returned as any]
    returned = block
  }
  let ast = parseCode(script)
  let classDecl!: t.ClassDeclaration
  let foundWXInstance = false
  const vistor: Visitor = {
    BlockStatement (path) {
      path.scope.rename('wx', 'Taro')
    },
    CallExpression (path) {
      const callee = path.get('callee')
      if (callee.isIdentifier()) {
        const name = callee.node.name
        if (name === 'getApp' || name === 'getCurrentPages') {
          callee.replaceWith(
            t.memberExpression(t.identifier('Taro'), callee.node)
          )
        }
      }
      if (callee.isMemberExpression()) {
        const object = callee.get('object')
        if (object.isIdentifier({ name: 'wx' })) {
          object.replaceWith(t.identifier('Taro'))
        }
      }
      if (
        callee.isIdentifier({ name: 'Page' }) ||
        callee.isIdentifier({ name: 'Component' }) ||
        callee.isIdentifier({ name: 'App' })
      ) {
        foundWXInstance = true
        const componentType = callee.node.name
        classDecl = parsePage(
          path,
          returned || t.nullLiteral(),
          json,
          componentType,
          refId,
          wxses
        )
        if (componentType !== 'App' && classDecl.decorators!.length === 0) {
          classDecl.decorators = [buildDecorator(componentType)]
        }
        ast.program.body.push(
          classDecl,
          t.exportDefaultDeclaration(t.identifier(componentType !== 'App' ? defaultClassName : 'App'))
        )
        // path.insertAfter(t.exportDefaultDeclaration(t.identifier(defaultClassName)))
        path.remove()
      }
    }
  }

  traverse(ast, vistor)

  if (!foundWXInstance) {
    ast = parseCode(script + ';Component({})')
    traverse(ast, vistor)
  }

  const taroComponentsImport = buildImportStatement('@tarojs/components', [
    ...usedComponents
  ])
  const taroImport = buildImportStatement('@tarojs/taro', [], 'Taro')
  const withWeappImport = buildImportStatement(
    '@tarojs/with-weapp',
    [],
    'withWeapp'
  )
  ast.program.body.unshift(
    taroComponentsImport,
    taroImport,
    withWeappImport,
    ...wxses.filter(wxs => !wxs.src.startsWith('./wxs__')).map(wxs => buildImportStatement(wxs.src, [], wxs.module))
  )

  return ast
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:89,代码来源:script.ts


示例3: cloneDeep


//.........这里部分代码省略.........
        if (tagName === 'Slot') {
          const nameAttr = attrs.find(a => a.node.name.name === 'name')
          let slotName = ''
          if (nameAttr) {
            if (nameAttr.node.value && t.isStringLiteral(nameAttr.node.value)) {
              slotName = nameAttr.node.value.value
            } else {
              throw codeFrameError(jsxName.node, 'slot 的值必须是一个字符串')
            }
          }
          const children = t.memberExpression(
            t.memberExpression(t.thisExpression(), t.identifier('props')),
            t.identifier(slotName ? buildSlotName(slotName) : 'children')
          )
          try {
            path.replaceWith(path.parentPath.isJSXElement() ? t.jSXExpressionContainer(children) : children)
          } catch (error) {
            //
          }
        }
        if (tagName === 'Wxs') {
          wxses.push(getWXS(attrs.map(a => a.node), path, imports))
        }
        if (tagName === 'Template') {
          // path.traverse({
          //   JSXAttribute: jsxAttrVisitor
          // })
          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)
            )
            let usedTemplate = new Set<string>()

            traverse(ast, {
              JSXIdentifier (p) {
                const node = p.node
                if (node.name.endsWith('Tmpl') && node.name.length > 4 && p.parentPath.isJSXOpeningElement()) {
                  usedTemplate.add(node.name)
                }
              }
            })
            usedTemplate.forEach(componentName => {
              if (componentName !== classDecl.id.name) {
                ast.program.body.unshift(
                  buildImportStatement(`./${componentName}`, [], componentName)
                )
              }
            })
            imports.push({
              ast,
              name
            })
          }
        }
        if (tagName === 'Import') {
          const mods = parseModule(path, dirPath, 'import')
          if (mods) {
            imports.push(...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()) {
            try {
              path.replaceWith(caller)
            } catch (error) {
              //
            }
          }
        }
      }
    }
  } as Visitor
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:wxml.ts


示例4: parseScript

export function parseScript (
  script?: string,
  returned?: t.Expression,
  json?: t.ObjectExpression,
  wxses: WXS[] = []
) {
  script = script || 'Page({})'
  if (t.isJSXText(returned as any)) {
    const block = buildBlockElement()
    block.children = [returned as any]
    returned = block
  }
  const { ast } = transform(script, {
    parserOpts: {
      sourceType: 'module',
      plugins: [
        'classProperties',
        'jsx',
        'flow',
        'flowComment',
        'trailingFunctionCommas',
        'asyncFunctions',
        'exponentiationOperator',
        'asyncGenerators',
        'objectRestSpread',
        'decorators',
        'dynamicImport'
      ]
    }
  }) as { ast: t.File }
  let classDecl!: t.ClassDeclaration
  traverse(ast, {
    BlockStatement (path) {
      path.scope.rename('wx', 'Taro')
    },
    CallExpression (path) {
      const callee = path.get('callee')
      if (callee.isIdentifier()) {
        const name = callee.node.name
        if (name === 'getApp' || name === 'getCurrentPages') {
          callee.replaceWith(
            t.memberExpression(t.identifier('Taro'), callee.node)
          )
        }
      }
      if (callee.isMemberExpression()) {
        const object = callee.get('object')
        if (object.isIdentifier({ name: 'wx' })) {
          object.replaceWith(t.identifier('Taro'))
        }
      }
      if (
        callee.isIdentifier({ name: 'Page' }) ||
        callee.isIdentifier({ name: 'Component' }) ||
        callee.isIdentifier({ name: 'App' })
      ) {
        const componentType = callee.node.name
        classDecl = parsePage(
          path,
          returned || t.nullLiteral(),
          json,
          componentType
        )!
        if (componentType !== 'App') {
          classDecl.decorators = [buildDecorator(componentType)]
        }
        path.insertAfter(t.exportDefaultDeclaration(classDecl))
        path.remove()
      }
    }
  })

  const taroComponentsImport = buildImportStatement('@tarojs/components', [
    ...usedComponents
  ])
  const taroImport = buildImportStatement('@tarojs/taro', [], 'Taro')
  const withWeappImport = buildImportStatement(
    '@tarojs/with-weapp',
    [],
    'withWeapp'
  )
  ast.program.body.unshift(
    taroComponentsImport,
    taroImport,
    withWeappImport,
    ...wxses.map(wxs => buildImportStatement(wxs.src, [], wxs.module))
  )

  return ast
}
开发者ID:topud,项目名称:taro,代码行数:90,代码来源:script.ts



注:本文中的babel-types.exportDefaultDeclaration函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript babel-types.exportNamedDeclaration函数代码示例发布时间:2022-05-25
下一篇:
TypeScript babel-types.decorator函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap