@webassemblyjs/ast
Advanced tools
Comparing version 1.5.9 to 1.5.10
@@ -172,3 +172,4 @@ "use strict"; | ||
if ((0, _index.isBlock)(node)) { | ||
relativeBlockCount++; | ||
relativeBlockCount++; // $FlowIgnore: reference? | ||
var name = node.label || node.name; | ||
@@ -175,0 +176,0 @@ |
@@ -8,115 +8,54 @@ "use strict"; | ||
var _debug = _interopRequireDefault(require("debug")); | ||
var _nodePath = require("./node-path"); | ||
var _nodes = require("./nodes"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// recursively walks the AST starting at the given node. The callback is invoked for | ||
// and object that has a 'type' property. | ||
function walk(context, callback) { | ||
var stop = false; | ||
var debug = (0, _debug.default)("webassemblyjs:ast:traverse"); | ||
function innerWalk(context, callback) { | ||
if (stop) { | ||
return; | ||
} | ||
function removeNodeInBody(node, fromNode) { | ||
switch (fromNode.type) { | ||
case "ModuleMetadata": | ||
fromNode.sections = fromNode.sections.filter(function (n) { | ||
return n !== node; | ||
}); | ||
break; | ||
var node = context.node; | ||
case "Module": | ||
fromNode.fields = fromNode.fields.filter(function (n) { | ||
return n !== node; | ||
}); | ||
break; | ||
if (node._deleted === true) { | ||
return; | ||
} | ||
case "Program": | ||
case "Func": | ||
// $FlowIgnore it says References? | ||
fromNode.body = fromNode.body.filter(function (n) { | ||
return n !== node; | ||
}); | ||
break; | ||
var path = (0, _nodePath.createPath)(context); | ||
callback(node.type, path); | ||
default: | ||
throw new Error("Unsupported operation: removing node of type: " + String(fromNode.type)); | ||
} | ||
} | ||
function _findParent(parentPath, cb) { | ||
if (parentPath == null) { | ||
throw new Error("node is root"); | ||
} | ||
var currentPath = parentPath; | ||
while (cb(currentPath) !== false) { | ||
// Hit the root node, stop | ||
// $FlowIgnore | ||
if (currentPath.parentPath == null) { | ||
break; | ||
} // $FlowIgnore | ||
currentPath = currentPath.parentPath; | ||
} | ||
} | ||
function createPath(node, parentPath) { | ||
function remove() { | ||
if (parentPath == null) { | ||
throw new Error("Can not remove root node"); | ||
if (path.shouldStop) { | ||
stop = true; | ||
return; | ||
} | ||
var parentNode = parentPath.node; | ||
removeNodeInBody(node, parentNode); | ||
node._deleted = true; | ||
debug("delete path %s", node.type); | ||
} // TODO(sven): do it the good way, changing the node from the parent | ||
Object.keys(node).forEach(function (prop) { | ||
var value = node[prop]; | ||
if (value === null || value === undefined) { | ||
return; | ||
} | ||
function replaceWith(newNode) { | ||
// Remove all the keys first | ||
// $FlowIgnore | ||
Object.keys(node).forEach(function (k) { | ||
return delete node[k]; | ||
}); // $FlowIgnore | ||
Object.assign(node, newNode); | ||
var valueAsArray = Array.isArray(value) ? value : [value]; | ||
valueAsArray.forEach(function (childNode) { | ||
if (typeof childNode.type === "string") { | ||
var childContext = { | ||
node: childNode, | ||
parentKey: prop, | ||
parentPath: path, | ||
shouldStop: false, | ||
inList: Array.isArray(value) | ||
}; | ||
innerWalk(childContext, callback); | ||
} | ||
}); | ||
}); | ||
} | ||
return { | ||
node: node, | ||
parentPath: parentPath, | ||
// $FlowIgnore: References? | ||
findParent: function findParent(cb) { | ||
return _findParent(parentPath, cb); | ||
}, | ||
replaceWith: replaceWith, | ||
remove: remove | ||
}; | ||
} // recursively walks the AST starting at the given node. The callback is invoked for | ||
// and object that has a 'type' property. | ||
function walk(node, callback, parentPath) { | ||
if (node._deleted === true) { | ||
return; | ||
} | ||
var path = createPath(node, parentPath); // $FlowIgnore | ||
callback(node.type, path); | ||
Object.keys(node).forEach(function (prop) { | ||
var value = node[prop]; | ||
if (value === null || value === undefined) { | ||
return; | ||
} | ||
var valueAsArray = Array.isArray(value) ? value : [value]; | ||
valueAsArray.forEach(function (v) { | ||
if (typeof v.type === "string") { | ||
walk(v, callback, path); | ||
} | ||
}); | ||
}); | ||
innerWalk(context, callback); | ||
} | ||
@@ -126,6 +65,5 @@ | ||
function traverse(n, visitors) { | ||
function traverse(node, visitors) { | ||
var before = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop; | ||
var after = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : noop; | ||
var parentPath = null; | ||
Object.keys(visitors).forEach(function (visitor) { | ||
@@ -136,3 +74,10 @@ if (!_nodes.nodeAndUnionTypes.includes(visitor)) { | ||
}); | ||
walk(n, function (type, path) { | ||
var context = { | ||
node: node, | ||
inList: false, | ||
shouldStop: false, | ||
parentPath: null, | ||
parentKey: null | ||
}; | ||
walk(context, function (type, path) { | ||
if (typeof visitors[type] === "function") { | ||
@@ -157,3 +102,3 @@ before(type, path); | ||
}); | ||
}, parentPath); | ||
}); | ||
} |
{ | ||
"name": "@webassemblyjs/ast", | ||
"version": "1.5.9", | ||
"version": "1.5.10", | ||
"description": "AST utils for webassemblyjs", | ||
@@ -14,5 +14,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@webassemblyjs/helper-module-context": "1.5.9", | ||
"@webassemblyjs/helper-wasm-bytecode": "1.5.9", | ||
"@webassemblyjs/wast-parser": "1.5.9", | ||
"@webassemblyjs/helper-module-context": "1.5.10", | ||
"@webassemblyjs/helper-wasm-bytecode": "1.5.10", | ||
"@webassemblyjs/wast-parser": "1.5.10", | ||
"debug": "^3.1.0", | ||
@@ -29,3 +29,3 @@ "mamacro": "^0.0.3" | ||
"devDependencies": { | ||
"@webassemblyjs/helper-test-framework": "1.5.9", | ||
"@webassemblyjs/helper-test-framework": "1.5.10", | ||
"array.prototype.flatmap": "^1.2.1", | ||
@@ -32,0 +32,0 @@ "dump-exports": "^0.1.0" |
@@ -33,2 +33,11 @@ # @webassemblyjs/ast | ||
### Path methods | ||
- `findParent: NodeLocator` | ||
- `replaceWith: Node => void` | ||
- `remove: () => void` | ||
- `insertBefore: Node => void` | ||
- `insertAfter: Node => void` | ||
- `stop: () => void` | ||
### AST utils | ||
@@ -35,0 +44,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89674
19
2727
168
+ Added@webassemblyjs/floating-point-hex-parser@1.5.10(transitive)
+ Added@webassemblyjs/helper-api-error@1.5.10(transitive)
+ Added@webassemblyjs/helper-code-frame@1.5.10(transitive)
+ Added@webassemblyjs/helper-fsm@1.5.10(transitive)
+ Added@webassemblyjs/helper-module-context@1.5.10(transitive)
+ Added@webassemblyjs/helper-wasm-bytecode@1.5.10(transitive)
+ Added@webassemblyjs/wast-parser@1.5.10(transitive)
+ Added@webassemblyjs/wast-printer@1.5.10(transitive)
- Removed@webassemblyjs/floating-point-hex-parser@1.5.9(transitive)
- Removed@webassemblyjs/helper-api-error@1.5.9(transitive)
- Removed@webassemblyjs/helper-code-frame@1.5.9(transitive)
- Removed@webassemblyjs/helper-fsm@1.5.9(transitive)
- Removed@webassemblyjs/helper-module-context@1.5.9(transitive)
- Removed@webassemblyjs/helper-wasm-bytecode@1.5.9(transitive)
- Removed@webassemblyjs/wast-parser@1.5.9(transitive)
- Removed@webassemblyjs/wast-printer@1.5.9(transitive)