-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Showing
3 changed files
with
130 additions
and
84 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/babel-plugin-minify-mangle-names/src/bfs-traverse.js
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,48 @@ | ||
"use strict"; | ||
|
||
module.exports = function bfsTraverseCreator({ types: t, traverse }) { | ||
function getFields(path) { | ||
return t.VISITOR_KEYS[path.type]; | ||
} | ||
|
||
return function bfsTraverse(path, _visitor) { | ||
if (!path.node) { | ||
throw new Error("Not a valid path"); | ||
} | ||
const visitor = traverse.explode(_visitor); | ||
|
||
const queue = [path]; | ||
let current; // current depth | ||
|
||
while (queue.length > 0) { | ||
current = queue.shift(); | ||
|
||
// call | ||
if ( | ||
visitor && | ||
visitor[current.type] && | ||
Array.isArray(visitor[current.type].enter) | ||
) { | ||
const fns = visitor[current.type].enter; | ||
for (const fn of fns) { | ||
if (typeof fn === "function") fn(current); | ||
} | ||
} | ||
|
||
const fields = getFields(current); | ||
|
||
for (const field of fields) { | ||
const child = current.get(field); | ||
|
||
if (Array.isArray(child)) { | ||
// visit container left to right | ||
for (const c of child) { | ||
if (c.node) queue.push(c); | ||
} | ||
} else { | ||
if (child.node) queue.push(child); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
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