diff --git a/packages/babel-types/src/definitions/es2015.js b/packages/babel-types/src/definitions/es2015.js index fb42b1390583..ea2390d3f42a 100644 --- a/packages/babel-types/src/definitions/es2015.js +++ b/packages/babel-types/src/definitions/es2015.js @@ -1,5 +1,6 @@ // @flow import defineType, { + assertShape, assertNodeType, assertValueType, chain, @@ -537,7 +538,10 @@ defineType("TemplateElement", { builder: ["value", "tail"], fields: { value: { - // todo: flatten `raw` into main node + validate: assertShape({ + raw: assertValueType("string"), + cooked: assertValueType("string"), + }), }, tail: { validate: assertValueType("boolean"), diff --git a/packages/babel-types/src/definitions/utils.js b/packages/babel-types/src/definitions/utils.js index c1d2ce762daa..6109d33a1f50 100644 --- a/packages/babel-types/src/definitions/utils.js +++ b/packages/babel-types/src/definitions/utils.js @@ -161,6 +161,25 @@ export function assertValueType(type: string): Validator { return validate; } +export function assertShape(shape: {| [string]: Validator |}): Validator { + function validate(node, key, val) { + for (const property of Object.keys(shape)) { + try { + const validator = shape[property]; + validator(val[property]); + } catch (error) { + if (error instanceof TypeError) { + throw new TypeError( + `Property ${key} of ${node.type} expected to have ${error.message}`, + ); + } + throw error; + } + } + } + return validate; +} + export function chain(...fns: Array): Validator { function validate(...args) { for (const fn of fns) {