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, {
|
import defineType, {
|
||||||
assertValueType,
|
assertValueType,
|
||||||
assertNodeType,
|
assertNodeType,
|
||||||
|
assertNodeOrValueType,
|
||||||
assertEach,
|
assertEach,
|
||||||
chain,
|
chain,
|
||||||
assertOneOf,
|
assertOneOf,
|
||||||
@ -20,7 +21,7 @@ import defineType, {
|
|||||||
defineType("ArrayExpression", {
|
defineType("ArrayExpression", {
|
||||||
fields: {
|
fields: {
|
||||||
elements: {
|
elements: {
|
||||||
validate: chain(assertValueType("array"), assertEach(assertNodeType("Expression", "SpreadElement")))
|
validate: chain(assertValueType("array"), assertEach(assertNodeOrValueType("null", "Expression", "SpreadElement")))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
visitor: ["elements"],
|
visitor: ["elements"],
|
||||||
|
|||||||
@ -61,6 +61,27 @@ export function assertNodeType(...types: Array<string>): Function {
|
|||||||
return validate;
|
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 {
|
export function assertValueType(type: string): Function {
|
||||||
function validate(node, key, val) {
|
function validate(node, key, val) {
|
||||||
let valid = getType(val) === type;
|
let valid = getType(val) === type;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user