当前位置: 首页>>代码示例>>TypeScript>>正文


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

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


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

示例1: parseJSXElement

 .reduce((str, child) => {
   if (t.isJSXText(child)) {
     return str + child.value
   }
   if (t.isJSXElement(child)) {
     return str + parseJSXElement(child)
   }
   if (t.isJSXExpressionContainer(child)) {
     if (t.isJSXElement(child.expression)) {
       return str + parseJSXElement(child.expression)
     }
     return str + `{${
       decodeUnicode(
         generate(child, {
           quotes: 'single',
           jsonCompatibleStrings: true
         })
         .code
       )
       .replace(/(this\.props\.)|(this\.state\.)/g, '')
       .replace(/(props\.)|(state\.)/g, '')
       .replace(/this\./, '')
     }}`
   }
   return str
 }, '')
开发者ID:topud,项目名称:taro,代码行数:26,代码来源:jsx.ts

示例2: parseJSXElement

 .reduce((str, child) => {
   if (t.isJSXText(child)) {
     const strings: string[] = []
     child.value.split(/(\r?\n\s*)/).forEach((val) => {
       const value = val.replace(/\u00a0/g, ' ')
       if (!value) {
         return
       }
       if (value.startsWith('\n')) {
         return
       }
       strings.push(value)
     })
     return str + strings.join('')
   }
   if (t.isJSXElement(child)) {
     return str + parseJSXElement(child)
   }
   if (t.isJSXExpressionContainer(child)) {
     if (t.isJSXElement(child.expression)) {
       return str + parseJSXElement(child.expression)
     }
     return str + `{${generateJSXAttr(child)}}`
   }
   return str
 }, '')
开发者ID:YangShaoQun,项目名称:taro,代码行数:26,代码来源:jsx.ts

示例3: getWXS

function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>, imports: Imports[]): WXS {
  let moduleName: string | null = null
  let src: string | null = null

  for (const attr of attrs) {
    if (t.isJSXIdentifier(attr.name)) {
      const attrName = attr.name.name
      const attrValue = attr.value
      let value: string | null = null
      if (attrValue === null) {
        throw new Error('WXS 标签的属性值不得为空')
      }
      if (t.isStringLiteral(attrValue)) {
        value = attrValue.value
      } else if (
        t.isJSXExpressionContainer(attrValue) &&
        t.isStringLiteral(attrValue.expression)
      ) {
        value = attrValue.expression.value
      }
      if (attrName === 'module') {
        moduleName = value
      }
      if (attrName === 'src') {
        src = value
      }
    }
  }

  if (!src) {
    const { children: [ script ] } = path.node
    if (!t.isJSXText(script)) {
      throw new Error('wxs 如果没有 src 属性,标签内部必须有 wxs 代码。')
    }
    src = './wxs__' + moduleName
    imports.push({
      ast: parseCode(script.value),
      name: moduleName as string,
      wxs: true
    })
  }

  if (!moduleName || !src) {
    throw new Error('一个 WXS 需要同时存在两个属性:`wxs`, `src`')
  }

  path.remove()

  return {
    module: moduleName,
    src
  }
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:53,代码来源:wxml.ts

示例4: parseJSXElement

 .reduce((str, child) => {
   if (t.isJSXText(child)) {
     return str + child.value
   }
   if (t.isJSXElement(child)) {
     return str + parseJSXElement(child)
   }
   if (t.isJSXExpressionContainer(child)) {
     if (t.isJSXElement(child.expression)) {
       return str + parseJSXElement(child.expression)
     }
     return str + `{${
       generate(child)
       .code
       .replace(/(this\.props\.)|(this\.state\.)/, '')
       .replace(/this\./, '')
     }}`
   }
   return str
 }, '')
开发者ID:teachat8,项目名称:taro,代码行数:20,代码来源:jsx.ts

示例5: parseJSXElement

 .reduce((str, child) => {
   if (t.isJSXText(child)) {
     const strings: string[] = []
     child.value.split(/(\r?\n\s*)/).forEach((val) => {
       const value = val.replace(/\u00a0/g, '&nbsp;').trimLeft()
       if (!value) {
         return
       }
       if (value.startsWith('\n')) {
         return
       }
       strings.push(value)
     })
     return str + strings.join('')
   }
   if (t.isJSXElement(child)) {
     return str + parseJSXElement(child)
   }
   if (t.isJSXExpressionContainer(child)) {
     if (t.isJSXElement(child.expression)) {
       return str + parseJSXElement(child.expression)
     }
     return str + `{${
       decodeUnicode(
         generate(child, {
           quotes: 'single',
           jsonCompatibleStrings: true
         })
         .code
       )
       .replace(/(this\.props\.)|(this\.data\.)/g, '')
       .replace(/(props\.)|(data\.)/g, '')
       .replace(/this\./g, '')
       .replace(/</g, lessThanSignPlacehold)
     }}`
   }
   return str
 }, '')
开发者ID:AlloyTeam,项目名称:Nuclear,代码行数:38,代码来源:jsx.ts

示例6:

 .filter(child => {
   return !(t.isJSXText(child) && child.value.trim() === '')
 })
开发者ID:AlloyTeam,项目名称:Nuclear,代码行数:3,代码来源:jsx.ts

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

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