-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jindy
committed
Jun 21, 2022
1 parent
4aa6a11
commit 750af23
Showing
4 changed files
with
68 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { NodeTypes } from "./ast" | ||
|
||
export function transform(root, options) { | ||
const context = createTransformContext(root, options) | ||
// 1.遍历,深度优先搜索 ----》递归 | ||
traverseNode(root, context) | ||
// 2.修改 text content | ||
} | ||
|
||
function createTransformContext(root, options) { | ||
const context = { | ||
root, | ||
nodeTransforms: options.nodeTransforms || [], | ||
} | ||
return context | ||
} | ||
|
||
function traverseNode(node, context) { | ||
console.log("node:", node) | ||
|
||
// if (node.type === NodeTypes.TEXT) { | ||
// node.content = node.content + "mini-vue" | ||
// } | ||
const nodeTransforms = context.nodeTransforms | ||
for (let index = 0; index < nodeTransforms.length; index++) { | ||
const transform = nodeTransforms[index] | ||
transform(node) | ||
} | ||
|
||
traverseChildren(node, context) | ||
} | ||
|
||
function traverseChildren(node, context) { | ||
const children = node.children | ||
if (children) { | ||
for (let index = 0; index < children.length; index++) { | ||
const node = children[index] | ||
traverseNode(node, context) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NodeTypes } from "../src/ast" | ||
import { baseParse } from "../src/parse" | ||
import { transform } from "../src/transform" | ||
|
||
describe("transform", () => { | ||
it("happy path", () => { | ||
const ast = baseParse("<div>hi,{{message}}</div>") | ||
|
||
const plugin = (node) => { | ||
if (node.type === NodeTypes.TEXT) { | ||
node.content = node.content + "mini-vue" | ||
} | ||
} | ||
transform(ast, { | ||
nodeTransforms: [plugin], | ||
}) | ||
const nodeText = ast.children[0].children[0] | ||
expect(nodeText.content).toBe("hi,mini-vue") | ||
}) | ||
}) |