Make ArrayExpression validator accept nulls as holes in the array
This commit is contained in:
parent
d0f63c1a7b
commit
11a8086432
@ -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"],
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user