ast-types
Advanced tools
+32
-22
@@ -226,2 +226,14 @@ var assert = require("assert"); | ||
| Fp.getValue = function(obj) { | ||
| var value = obj[this.name]; | ||
| if (!isUndefined.check(value)) | ||
| return value; | ||
| if (this.defaultFn) | ||
| value = this.defaultFn.call(obj); | ||
| return value; | ||
| }; | ||
| // Define a type whose name is registered in a namespace (the defCache) so | ||
@@ -285,3 +297,3 @@ // that future definitions will return the same type given the same name. | ||
| Dp.checkAllFields = function(value, deep) { | ||
| var allFields = this.allFields | ||
| var allFields = this.allFields; | ||
| assert.strictEqual(this.finalized, true); | ||
@@ -292,23 +304,3 @@ | ||
| var type = field.type; | ||
| var child = value[name]; | ||
| // If a property with this name is defined on value, simply check | ||
| // the type of the child value. | ||
| if (!isUndefined.check(child)) | ||
| return type.check(child, deep); | ||
| // If there is no default function for this field, treat the | ||
| // missing property as a type error. | ||
| if (!field.defaultFn) | ||
| return false; | ||
| // If there is a default function for this field, and we are not | ||
| // checking deeply, just assume that the default function would | ||
| // return something that matches field.type. | ||
| if (!deep) | ||
| return true; | ||
| // If we are checking deeply, go ahead and deeply check the | ||
| // result returned by the default function. | ||
| child = field.defaultFn.call(value); | ||
| var child = field.getValue(value); | ||
| var result = type.check(child, deep); | ||
@@ -513,2 +505,20 @@ if (result) { | ||
| // Get the value of an object property, taking object.type and default | ||
| // functions into account. | ||
| Object.defineProperty(exports, "getFieldValue", { | ||
| value: function(object, fieldName) { | ||
| isObject.assert(object); | ||
| var d = Def.fromValue(object); | ||
| if (d) { | ||
| var field = d.allFields[fieldName]; | ||
| if (field) { | ||
| return field.getValue(object); | ||
| } | ||
| } | ||
| return object[fieldName]; | ||
| } | ||
| }); | ||
| // This property will be overridden as true by individual Def instances | ||
@@ -515,0 +525,0 @@ // when they are finalized. |
+1
-0
@@ -18,2 +18,3 @@ var types = require("./lib/types"); | ||
| exports.builders = types.builders; | ||
| exports.getFieldValue = types.getFieldValue; | ||
| exports.finalize = types.finalize; |
+1
-1
@@ -21,3 +21,3 @@ { | ||
| ], | ||
| "version": "0.2.11", | ||
| "version": "0.2.12", | ||
| "homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
+48
-0
@@ -161,1 +161,49 @@ var types = require("../main"); | ||
| }; | ||
| exports.testGetFieldValue = function(t, assert) { | ||
| assert.strictEqual( | ||
| types.getFieldValue({ | ||
| type: "CatchClause" | ||
| }, "guard"), | ||
| null | ||
| ); | ||
| assert.strictEqual( | ||
| types.getFieldValue({ | ||
| type: "CatchClause" | ||
| }, "asdf"), | ||
| void 0 | ||
| ); | ||
| assert.strictEqual( | ||
| types.getFieldValue({ | ||
| type: "CatchClause" | ||
| }, "type"), | ||
| "CatchClause" | ||
| ); | ||
| assert.strictEqual( | ||
| types.getFieldValue({ | ||
| type: "CatchClause", | ||
| guard: b.identifier("test") | ||
| }, "guard").name, | ||
| "test" | ||
| ); | ||
| assert.deepEqual( | ||
| types.getFieldValue({ | ||
| type: "TryStatement", | ||
| }, "guardedHandlers"), | ||
| [] | ||
| ); | ||
| assert.deepEqual( | ||
| types.getFieldValue({ | ||
| type: "TryStatement", | ||
| guardedHandlers: void 0 | ||
| }, "guardedHandlers"), | ||
| [] | ||
| ); | ||
| t.finish(); | ||
| }; |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
10670
0.46%377410
-0.4%14
-6.67%