本文整理汇总了TypeScript中babel-traverse.NodePath.remove方法的典型用法代码示例。如果您正苦于以下问题:TypeScript NodePath.remove方法的具体用法?TypeScript NodePath.remove怎么用?TypeScript NodePath.remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel-traverse.NodePath
的用法示例。
在下文中一共展示了NodePath.remove方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: transformLoop
function transformLoop (
name: string,
attr: NodePath<t.JSXAttribute>,
jsx: NodePath<t.JSXElement>,
value: AttrValue
) {
if (name !== WX_FOR) {
return
}
if (!value || !t.isJSXExpressionContainer(value)) {
throw new Error('wx:for 的值必须使用 "{{}}" 包裹')
}
attr.remove()
let item = t.stringLiteral('item')
let index = t.stringLiteral('index')
jsx
.get('openingElement')
.get('attributes')
.forEach(p => {
const node = p.node
if (node.name.name === WX_FOR_ITEM) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_ITEM + ' 的值必须是一个字符串')
}
item = node.value
p.remove()
}
if (node.name.name === WX_FOR_INDEX) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_INDEX + ' 的值必须是一个字符串')
}
index = node.value
p.remove()
}
if (node.name.name === WX_KEY) {
p.get('name').replaceWith(t.jSXIdentifier('key'))
}
})
const replacement = t.jSXExpressionContainer(
t.callExpression(
t.memberExpression(value.expression, t.identifier('map')),
[
t.arrowFunctionExpression(
[t.identifier(item.value), t.identifier(index.value)],
t.blockStatement([t.returnStatement(jsx.node)])
)
]
)
)
const block = buildBlockElement()
block.children = [replacement]
jsx.replaceWith(block)
}
示例2: 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
}
}
示例3: transformIf
function transformIf (
name: string,
attr: NodePath<t.JSXAttribute>,
jsx: NodePath<t.JSXElement>,
value: AttrValue
) {
if (name !== WX_IF) {
return
}
const conditions: Condition[] = []
let siblings: NodePath<t.Node>[] = []
try {
siblings = jsx.getAllNextSiblings().filter(s => !(s.isJSXExpressionContainer() && s.get('expression').isJSXEmptyExpression()))
} catch (error) {
return
}
if (value === null || !t.isJSXExpressionContainer(value)) {
// tslint:disable-next-line
console.error('wx:if 的值需要用双括号 `{{}}` 包裹它的值')
if (value && t.isStringLiteral(value)) {
value = t.jSXExpressionContainer(buildTemplate(value.value))
}
}
conditions.push({
condition: WX_IF,
path: jsx,
tester: value as t.JSXExpressionContainer
})
attr.remove()
for (let index = 0; index < siblings.length; index++) {
const sibling = siblings[index]
const next = cloneDeep(siblings[index + 1])
const currMatches = findWXIfProps(sibling)
const nextMatches = findWXIfProps(next)
if (currMatches === null) {
break
}
conditions.push({
condition: currMatches.reg.input as string,
path: sibling as any,
tester: currMatches.tester as t.JSXExpressionContainer
})
if (nextMatches === null) {
break
}
}
handleConditions(conditions)
}
示例4: transformIf
function transformIf (
name: string,
attr: NodePath<t.JSXAttribute>,
jsx: NodePath<t.JSXElement>,
value: AttrValue
) {
if (name !== WX_IF) {
return
}
const conditions: Condition[] = []
let siblings: NodePath<t.Node>[] = []
try {
siblings = jsx.getAllNextSiblings()
} catch (error) {
return
}
conditions.push({
condition: WX_IF,
path: jsx,
tester: value as t.JSXExpressionContainer
})
attr.remove()
for (const [index, sibling] of siblings.entries()) {
const next = siblings[index + 1]
const currMatches = findWXIfProps(sibling)
const nextMatches = findWXIfProps(next)
if (currMatches === null) {
break
}
conditions.push({
condition: currMatches.reg.input as string,
path: sibling as any,
tester: currMatches.tester as t.JSXExpressionContainer
})
if (nextMatches === null) {
break
}
}
handleConditions(conditions)
}
示例5: parseModule
export function parseModule (jsx: NodePath<t.JSXElement>, dirPath: string, type: 'include' | 'import') {
const openingElement = jsx.get('openingElement')
const attrs = openingElement.get('attributes')
const src = attrs.find(attr => attr.get('name').isJSXIdentifier({ name: 'src' }))
if (!src) {
throw new Error(`${type} 标签必须包含 \`src\` 属性`)
}
const value = src.get('value')
if (!value.isStringLiteral()) {
throw new Error(`${type} 标签的 src 属性值必须是一个字符串`)
}
const srcValue = value.node.value
if (type === 'import') {
const wxml = getWXMLsource(dirPath, srcValue, type)
const { imports } = parseWXML(resolve(dirPath, srcValue), wxml, true)
try {
jsx.remove()
} catch (error) {
//
}
return imports
} else {
const { wxml } = parseWXML(dirPath, getWXMLsource(dirPath, srcValue, type), true)
const block = buildBlockElement()
try {
if (wxml) {
block.children = [wxml as any]
jsx.replaceWith(wxml)
} else {
block.children = [t.jSXExpressionContainer(t.jSXEmptyExpression())]
jsx.replaceWith(block)
}
} catch (error) {
//
}
}
}
示例6: parseTemplate
export function parseTemplate (path: NodePath<t.JSXElement>, dirPath: string) {
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>()
if (name) {
const value = name.node.value
if (value === null || !t.isStringLiteral(value)) {
throw new Error('template 的 `name` 属性只能是字符串')
}
const className = pascalName(value.value) + pascalName(basename(dirPath))
path.traverse({
Identifier (p) {
if (!p.isReferencedIdentifier()) {
return
}
const jsxExprContainer = p.findParent(p => p.isJSXExpressionContainer())
if (!jsxExprContainer || !jsxExprContainer.isJSXExpressionContainer()) {
return
}
refIds.add(p.node.name)
}
})
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), Array.from(refIds)[0])
} else {
// 使用 ...spread
render = buildRender(block, [], Array.from(refIds), [])
}
const classDecl = t.classDeclaration(
t.identifier(className),
t.memberExpression(t.identifier('Taro'), t.identifier('Component')),
t.classBody([render!]),
[]
)
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 = pascalName(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 = pascalName(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(
test,
t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier('Template'), attributes.concat(
[t.jSXAttribute(t.jSXIdentifier('is'), consequent)]
)),
t.jSXClosingElement(t.jSXIdentifier('Template')),
[],
//.........这里部分代码省略.........
示例7: babel7Transform
//.........这里部分代码省略.........
return t.isImportDeclaration(statement) && statement.source.value === ASYNC_PACKAGE_NAME
})
if (!isAsyncImported) {
ast.program.body.unshift(
t.importDeclaration([], t.stringLiteral(ASYNC_PACKAGE_NAME))
)
}
},
JSXOpeningElement (path) {
const { name } = path.node.name as t.JSXIdentifier
if (name === 'Provider') {
const modules = path.scope.getAllBindings('module')
const providerBinding = Object.values(modules).some((m: Binding) => m.identifier.name === 'Provider')
if (providerBinding) {
path.node.name = t.jSXIdentifier('View')
const store = path.node.attributes.find(attr => attr.name.name === 'store')
if (store && t.isJSXExpressionContainer(store.value) && t.isIdentifier(store.value.expression)) {
storeName = store.value.expression.name
}
path.node.attributes = []
}
}
if (IMAGE_COMPONENTS.has(name)) {
for (const attr of path.node.attributes) {
if (
t.isIdentifier(attr) &&
attr.name.name === 'src'
) {
if (t.isStringLiteral(attr.value)) {
imageSource.add(attr.value.value)
} else if (t.isJSXExpressionContainer(attr.value)) {
if (t.isStringLiteral(attr.value.expression)) {
imageSource.add(attr.value.expression.value)
}
}
}
}
}
},
ImportDeclaration (path) {
const source = path.node.source.value
const names: string[] = []
if (source === TARO_PACKAGE_NAME) {
path.node.specifiers.push(
t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
t.importSpecifier(t.identifier(INTERNAL_DYNAMIC), t.identifier(INTERNAL_DYNAMIC))
)
}
if (
source === REDUX_PACKAGE_NAME
) {
path.node.specifiers.forEach((s, index, specs) => {
if (s.local.name === 'Provider') {
specs.splice(index, 1)
specs.push(
t.importSpecifier(t.identifier('setStore'), t.identifier('setStore'))
)
}
})
}
path.traverse({
ImportDefaultSpecifier (path) {
const name = path.node.local.name
DEFAULT_Component_SET.has(name) || names.push(name)
},
ImportSpecifier (path) {
const name = path.node.local.name
DEFAULT_Component_SET.has(name) || names.push(name)
}
})
componentSourceMap.set(source, names)
}
})
const storeBinding = mainClass.scope.getBinding(storeName)
if (storeBinding) {
const statementPath = storeBinding.path.getStatementParent()
if (statementPath) {
ast.program.body.forEach((node, index, body) => {
if (node === statementPath.node) {
body.splice(index + 1, 0, t.expressionStatement(
t.callExpression(t.identifier('setStore'), [
t.identifier(storeName)
])
))
}
})
}
}
if (options.isApp) {
renderMethod.remove()
return { ast } as TransformResult
}
result = new Transformer(mainClass, options.isRoot, componentSourceMap, options.path).result
result.code = generate(ast).code
result.ast = ast
result.template = prettyPrint(result.template)
result.imageSrcs = Array.from(imageSource)
return result
}
示例8: transformLoop
function transformLoop (
name: string,
attr: NodePath<t.JSXAttribute>,
jsx: NodePath<t.JSXElement>,
value: AttrValue
) {
const jsxElement = jsx.get('openingElement')
if (!jsxElement.node) {
return
}
const attrs = jsxElement.get('attributes').map(a => a.node)
const wxForItem = attrs.find(a => a.name.name === WX_FOR_ITEM)
const hasSinglewxForItem = wxForItem && wxForItem.value && t.isJSXExpressionContainer(wxForItem.value)
if (hasSinglewxForItem || name === WX_FOR || name === 'wx:for-items') {
if (!value || !t.isJSXExpressionContainer(value)) {
throw new Error('wx:for 的值必须使用 "{{}}" 包裹')
}
attr.remove()
let item = t.stringLiteral('item')
let index = t.stringLiteral('index')
jsx
.get('openingElement')
.get('attributes')
.forEach(p => {
const node = p.node
if (node.name.name === WX_FOR_ITEM) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_ITEM + ' 的值必须是一个字符串')
}
item = node.value
p.remove()
}
if (node.name.name === WX_FOR_INDEX) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_INDEX + ' 的值必须是一个字符串')
}
index = node.value
p.remove()
}
})
const replacement = t.jSXExpressionContainer(
t.callExpression(
t.memberExpression(value.expression, t.identifier('map')),
[
t.arrowFunctionExpression(
[t.identifier(item.value), t.identifier(index.value)],
t.blockStatement([t.returnStatement(jsx.node)])
)
]
)
)
const block = buildBlockElement()
block.children = [replacement]
try {
jsx.replaceWith(block)
} catch (error) {
//
}
return {
item: item.value,
index: index.value
}
}
}
示例9: 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(
//.........这里部分代码省略.........