Skip to content

Commit

Permalink
chore: rename package name
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Nov 19, 2018
1 parent de8f5ae commit d450d68
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tarojs/wx-to-taro",
"name": "@tarojs/taroize",
"version": "0.0.1",
"description": "转换原生微信小程序代码为 Taro 代码",
"main": "index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface Option {
wxml: string
}

export default function parse (option: Option): t.File {
export function parse (option: Option): t.File {
const wxml = parseWXML(option.wxml)
return parseScript(option.script, wxml as t.Expression)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 16 additions & 6 deletions packages/wx-to-taro/src/wxml.ts → packages/taroize/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function buildElement (
export const usedComponents = new Set<string>()

export function parseWXML (wxml: string) {
const nodes = (parse(wxml.trim()) as AllKindNode[]).filter(removEmptyText).filter(node => node.type !== NodeType.Comment) as Node[]
const nodes = removEmptyTextAndComment(parse(wxml.trim()))
const ast = t.file(t.program([t.expressionStatement(parseNode(buildElement('block', nodes)) as t.Expression)], []))

traverse(ast, {
Expand Down Expand Up @@ -250,16 +250,26 @@ function parseElement (element: Element): t.JSXElement {
return t.jSXElement(
t.jSXOpeningElement(tagName, element.attributes.map(parseAttribute)),
t.jSXClosingElement(tagName),
element.children.filter(removEmptyText).map(parseNode),
element.children.map(parseNode),
false
)
}

function removEmptyText (node: Node) {
if (node.type === NodeType.Text && node.content.trim().length === 0) {
return false
function removEmptyTextAndComment (nodes: AllKindNode[]) {
for (let index = 0; index < nodes.length; index++) {
const node = nodes[index]
if (
(node.type === NodeType.Text && node.content.trim().length === 0)
||
(node.type === NodeType.Comment)
) {
nodes.splice(index, 1)
}
if (node.type === NodeType.Element) {
removEmptyTextAndComment(node.children)
}
}
return true
return nodes as Node[]
}

function parseText (node: Text) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions packages/wx-to-taro/.gitignore

This file was deleted.

0 comments on commit d450d68

Please sign in to comment.