本文整理汇总了TypeScript中babel-types.nullLiteral函数的典型用法代码示例。如果您正苦于以下问题:TypeScript nullLiteral函数的具体用法?TypeScript nullLiteral怎么用?TypeScript nullLiteral使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了nullLiteral函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: generateAnonymousState
export function generateAnonymousState (
scope: Scope,
expression: NodePath<t.Expression>,
refIds: Set<t.Identifier>,
isLogical?: boolean
) {
let variableName = `anonymousState_${scope.generateUid()}`
let statementParent = expression.getStatementParent()
if (!statementParent) {
throw codeFrameError(expression.node.loc, '无法生成匿名 State,尝试先把值赋到一个变量上再把变量调换。')
}
const jsx = isLogical ? expression : expression.findParent(p => p.isJSXElement())
const callExpr = jsx.findParent(p => p.isCallExpression() && isArrayMapCallExpression(p)) as NodePath<t.CallExpression>
const conditionExpr = jsx.findParent(p => p.isConditionalExpression())
const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }))
let expr = cloneDeep(expression.node)
if (conditionExpr && conditionExpr.isConditionalExpression()) {
const consequent = conditionExpr.get('consequent')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(conditionExpr.get('test').node as any, expr, t.nullLiteral())
}
}
if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
const consequent = logicExpr.get('right')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(logicExpr.get('left').node as any, expr, t.nullLiteral())
}
}
if (!callExpr) {
refIds.add(t.identifier(variableName))
statementParent.insertBefore(
buildConstVariableDeclaration(variableName, expr)
)
} else {
variableName = `${LOOP_STATE}_${callExpr.scope.generateUid()}`
const func = callExpr.node.arguments[0]
if (t.isArrowFunctionExpression(func)) {
if (!t.isBlockStatement(func.body)) {
func.body = t.blockStatement([
buildConstVariableDeclaration(variableName, expr),
t.returnStatement(func.body)
])
} else {
statementParent.insertBefore(
buildConstVariableDeclaration(variableName, expr)
)
}
}
}
expression.replaceWith(
t.identifier(variableName)
)
}
开发者ID:ApolloRobot,项目名称:taro,代码行数:53,代码来源:utils.ts
示例2: parseWXML
export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean): {
wxses: WXS[]
wxml?: t.Node
imports: Imports[]
refIds: Set<string>
} {
if (!parseImport) {
errors.length = 0
usedComponents.clear()
}
usedComponents.add('Block')
let wxses: WXS[] = []
let imports: Imports[] = []
const refIds = new Set<string>()
const loopIds = new Set<string>()
if (!wxml) {
return {
wxses,
imports,
refIds,
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, createWxmlVistor(loopIds, refIds, dirPath, wxses, imports))
refIds.forEach(id => {
if (loopIds.has(id) || imports.filter(i => i.wxs).map(i => i.name).includes(id)) {
refIds.delete(id)
}
})
return {
wxses,
imports,
wxml: hydrate(ast),
refIds
}
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:50,代码来源:wxml.ts
示例3: convertArrayToAstExpression
return arr.map(value => {
if (typeof value === 'string') {
return t.stringLiteral(value)
}
if (typeof value === 'number') {
return t.numericLiteral(value)
}
if (typeof value === 'boolean') {
return t.booleanLiteral(value)
}
if (Array.isArray(value)) {
return convertArrayToAstExpression(value)
}
if (typeof value === 'object') {
return t.objectExpression(convertObjectToAstExpression(value))
}
return t.nullLiteral()
})
开发者ID:YangShaoQun,项目名称:taro,代码行数:18,代码来源:astConvert.ts
示例4:
const objArr = Object.keys(obj).map(key => {
const value = obj[key]
if (typeof value === 'string') {
return t.objectProperty(t.stringLiteral(key), t.stringLiteral(value))
}
if (typeof value === 'number') {
return t.objectProperty(t.stringLiteral(key), t.numericLiteral(value))
}
if (typeof value === 'boolean') {
return t.objectProperty(t.stringLiteral(key), t.booleanLiteral(value))
}
if (Array.isArray(value)) {
return t.objectProperty(t.stringLiteral(key), t.arrayExpression(convertArrayToAstExpression(value as [])))
}
if (typeof value === 'object') {
return t.objectProperty(t.stringLiteral(key), t.objectExpression(convertObjectToAstExpression(value)))
}
return t.objectProperty(t.stringLiteral(key), t.nullLiteral())
})
开发者ID:YangShaoQun,项目名称:taro,代码行数:19,代码来源:astConvert.ts
示例5: JSXElement
if (t.isFunctionExpression(node)) {
node.params = [t.identifier('param')];
}
}
});
if (t.isBinaryExpression(ast)) {
ast.left;
ast.right;
ast.operator;
}
t.assertBinaryExpression(ast);
t.assertBinaryExpression(ast, { operator: "*" });
const exp: t.Expression = t.nullLiteral();
// React examples:
// https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-plugin-transform-react-inline-elements/src/index.js#L61
traverse(ast, {
JSXElement(path, file) {
const { node } = path;
const open = node.openingElement;
// init
const type = open.name;
let newType: t.StringLiteral;
if (t.isJSXIdentifier(type) && t.react.isCompatTag(type.name)) {
newType = t.stringLiteral(type.name);
}
开发者ID:AlexGalays,项目名称:DefinitelyTyped,代码行数:31,代码来源:babel-types-tests.ts
示例6: 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
示例7: analyzeImportUrl
function analyzeImportUrl ({
astPath,
value,
sourceFilePath,
filePath,
styleFiles,
scriptFiles,
jsonFiles,
mediaFiles
}: IAnalyzeImportUrlOptions): void {
const valueExtname = path.extname(value)
const node = astPath.node
const {
nodeModulesPath,
npmOutputDir,
sourceDir,
outputDir,
npmConfig
} = getBuildData()
if (value.indexOf('.') === 0) {
let importPath = path.resolve(path.dirname(sourceFilePath), value)
importPath = resolveScriptPath(importPath)
if (isFileToBePage(importPath)) {
astPath.remove()
} else {
if (REG_SCRIPT.test(valueExtname) || REG_TYPESCRIPT.test(valueExtname)) {
const vpath = path.resolve(sourceFilePath, '..', value)
let fPath = value
if (fs.existsSync(vpath) && vpath !== sourceFilePath) {
fPath = vpath
}
if (scriptFiles.indexOf(fPath) < 0) {
scriptFiles.push(fPath)
}
} else if (REG_JSON.test(valueExtname)) {
const vpath = path.resolve(sourceFilePath, '..', value)
if (jsonFiles.indexOf(vpath) < 0) {
jsonFiles.push(vpath)
}
if (fs.existsSync(vpath)) {
const obj = JSON.parse(fs.readFileSync(vpath).toString())
const specifiers = node.specifiers
let defaultSpecifier = null
specifiers.forEach(item => {
if (item.type === 'ImportDefaultSpecifier') {
defaultSpecifier = item.local.name
}
})
if (defaultSpecifier) {
let objArr: t.NullLiteral | t.Expression = t.nullLiteral()
if (Array.isArray(obj)) {
objArr = t.arrayExpression(convertArrayToAstExpression(obj))
} else {
objArr = t.objectExpression(convertObjectToAstExpression(obj))
}
astPath.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(defaultSpecifier), objArr)]))
}
}
} else if (REG_FONT.test(valueExtname) || REG_IMAGE.test(valueExtname) || REG_MEDIA.test(valueExtname)) {
const vpath = path.resolve(sourceFilePath, '..', value)
if (!fs.existsSync(vpath)) {
printLog(processTypeEnum.ERROR, '引用文件', `文件 ${sourceFilePath} 中引用 ${value} 不存在!`)
return
}
if (mediaFiles.indexOf(vpath) < 0) {
mediaFiles.push(vpath)
}
const specifiers = node.specifiers
let defaultSpecifier = null
specifiers.forEach(item => {
if (item.type === 'ImportDefaultSpecifier') {
defaultSpecifier = item.local.name
}
})
let showPath
if (NODE_MODULES_REG.test(vpath)) {
showPath = vpath.replace(nodeModulesPath, `/${npmConfig.name}`)
} else {
showPath = vpath.replace(sourceDir, '')
}
if (defaultSpecifier) {
astPath.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(defaultSpecifier), t.stringLiteral(showPath.replace(/\\/g, '/')))]))
} else {
astPath.remove()
}
} else if (REG_STYLE.test(valueExtname)) {
const stylePath = path.resolve(path.dirname(sourceFilePath), value)
if (styleFiles.indexOf(stylePath) < 0) {
styleFiles.push(stylePath)
}
astPath.remove()
} else {
let vpath = resolveScriptPath(path.resolve(sourceFilePath, '..', value))
let outputVpath
if (NODE_MODULES_REG.test(vpath)) {
outputVpath = vpath.replace(nodeModulesPath, npmOutputDir)
} else {
outputVpath = vpath.replace(sourceDir, outputDir)
}
//.........这里部分代码省略.........
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:astProcess.ts
示例8: parseAst
//.........这里部分代码省略.........
const value = args[0].value
const valueExtname = path.extname(value)
if (value.indexOf('.') === 0) {
let importPath = path.resolve(path.dirname(sourceFilePath), value)
importPath = resolveScriptPath(importPath)
if (isFileToBePage(importPath)) {
if (astPath.parent.type === 'AssignmentExpression' || 'ExpressionStatement') {
astPath.parentPath.remove()
} else if (astPath.parent.type === 'VariableDeclarator') {
astPath.parentPath.parentPath.remove()
} else {
astPath.remove()
}
} else {
if (REG_STYLE.test(valueExtname)) {
const stylePath = path.resolve(path.dirname(sourceFilePath), value)
if (styleFiles.indexOf(stylePath) < 0) {
styleFiles.push(stylePath)
}
if (astPath.parent.type === 'AssignmentExpression' || 'ExpressionStatement') {
astPath.parentPath.remove()
} else if (astPath.parent.type === 'VariableDeclarator') {
astPath.parentPath.parentPath.remove()
} else {
astPath.remove()
}
} else if (REG_JSON.test(valueExtname)) {
const vpath = path.resolve(sourceFilePath, '..', value)
if (jsonFiles.indexOf(vpath) < 0) {
jsonFiles.push(vpath)
}
if (fs.existsSync(vpath)) {
const obj = JSON.parse(fs.readFileSync(vpath).toString())
let objArr: t.NullLiteral | t.Expression | t.ObjectProperty[] = t.nullLiteral()
if (Array.isArray(obj)) {
objArr = t.arrayExpression(convertArrayToAstExpression(obj))
} else {
objArr = convertObjectToAstExpression(obj)
}
astPath.replaceWith(t.objectExpression(objArr as any))
}
} else if (REG_SCRIPT.test(valueExtname) || REG_TYPESCRIPT.test(valueExtname)) {
const vpath = path.resolve(sourceFilePath, '..', value)
let fPath = value
if (fs.existsSync(vpath) && vpath !== sourceFilePath) {
fPath = vpath
}
if (scriptFiles.indexOf(fPath) < 0) {
scriptFiles.push(fPath)
}
} else if (REG_FONT.test(valueExtname) || REG_IMAGE.test(valueExtname) || REG_MEDIA.test(valueExtname)) {
const vpath = path.resolve(sourceFilePath, '..', value)
if (mediaFiles.indexOf(vpath) < 0) {
mediaFiles.push(vpath)
}
let showPath
if (NODE_MODULES_REG.test(vpath)) {
showPath = vpath.replace(nodeModulesPath, `/${npmConfig.name}`)
} else {
showPath = vpath.replace(sourceDir, '')
}
astPath.replaceWith(t.stringLiteral(showPath.replace(/\\/g, '/')))
} else {
let vpath = resolveScriptPath(path.resolve(sourceFilePath, '..', value))
let outputVpath
if (NODE_MODULES_REG.test(vpath)) {
开发者ID:YangShaoQun,项目名称:taro,代码行数:67,代码来源:astProcess.ts
示例9: setParentCondition
export function setParentCondition (jsx: NodePath<t.Node>, expr: t.Expression, array = false) {
const conditionExpr = jsx.findParent(p => p.isConditionalExpression())
const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }))
if (array) {
const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => a.name.name === Adapter.if)) as NodePath<t.JSXElement>
if (logicalJSX) {
const attr = logicalJSX.node.openingElement.attributes.find(a => a.name.name === Adapter.if)
if (attr && t.isJSXExpressionContainer(attr.value)) {
expr = t.conditionalExpression(attr.value.expression, expr, t.arrayExpression())
return expr
}
}
}
if (conditionExpr && conditionExpr.isConditionalExpression()) {
const consequent = conditionExpr.get('consequent')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(conditionExpr.get('test').node as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
}
}
if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
const consequent = logicExpr.get('right')
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
expr = t.conditionalExpression(logicExpr.get('left').node as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
}
}
return expr
}
开发者ID:topud,项目名称:taro,代码行数:27,代码来源:utils.ts
示例10: 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
注:本文中的babel-types.nullLiteral函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论