Make ArrayExpression validator accept nulls as holes in the array

This commit is contained in:
phantom10111 2015-11-25 23:39:35 +01:00
parent d0f63c1a7b
commit 11a8086432
2 changed files with 23 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import {
import defineType, {
assertValueType,
assertNodeType,
assertNodeOrValueType,
assertEach,
chain,
assertOneOf,
@ -20,7 +21,7 @@ import defineType, {
defineType("ArrayExpression", {
fields: {
elements: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("Expression", "SpreadElement")))
validate: chain(assertValueType("array"), assertEach(assertNodeOrValueType("null", "Expression", "SpreadElement")))
}
},
visitor: ["elements"],

View File

@ -61,6 +61,27 @@ export function assertNodeType(...types: Array<string>): Function {
return validate;
}
export function assertNodeOrValueType(...types: Array<string>): Function {
function validate(node, key, val) {
let valid = false;
for (let type of types) {
if (getType(val) === type || t.is(type, val)) {
valid = true;
break;
}
}
if (!valid) {
throw new TypeError(`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} but instead got ${JSON.stringify(val && val.type)}`);
}
}
validate.oneOfNodeOrValueTypes = types;
return validate;
}
export function assertValueType(type: string): Function {
function validate(node, key, val) {
let valid = getType(val) === type;