Skip to content

Commit

Permalink
Template literal validation (#10492)
Browse files Browse the repository at this point in the history
* add template literal validation

* avoid null/undefined error when validating

* simplify validation logic and fix tests

Co-authored-by: Michael J. Currie <michaeljcurrie136@gmail.com>
  • Loading branch information
Basaingeal authored and nicolo-ribaudo committed Sep 26, 2019
1 parent 8d4f95d commit 66062c2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/babel-types/src/definitions/es2015.js
Expand Up @@ -574,6 +574,16 @@ defineType("TemplateLiteral", {
validate: chain(
assertValueType("array"),
assertEach(assertNodeType("Expression")),
function(node, key, val) {
if (node.quasis.length !== val.length + 1) {
throw new TypeError(
`Number of ${
node.type
} quasis should be exactly one more than the number of expressions.\nExpected ${val.length +
1} quasis but got ${node.quasis.length}`,
);
}
},
),
},
},
Expand Down
Expand Up @@ -35,3 +35,61 @@ exports[`builders es2015 templateElement should validate 5`] = `
"Property value of TemplateElement expected to have the following:
Property raw expected type of string but got undefined"
`;

exports[`builders es2015 templateLiteral should validate 1`] = `
Object {
"expressions": Array [],
"quasis": Array [
Object {
"tail": false,
"type": "TemplateElement",
"value": Object {
"raw": "foo",
},
},
],
"type": "TemplateLiteral",
}
`;

exports[`builders es2015 templateLiteral should validate 2`] = `
Object {
"expressions": Array [
Object {
"type": "StringLiteral",
"value": "baz",
},
],
"quasis": Array [
Object {
"tail": false,
"type": "TemplateElement",
"value": Object {
"raw": "foo",
},
},
Object {
"tail": false,
"type": "TemplateElement",
"value": Object {
"raw": "bar",
},
},
],
"type": "TemplateLiteral",
}
`;

exports[`builders es2015 templateLiteral should validate 3`] = `
"Number of TemplateLiteral quasis should be exactly one more than the number of expressions.
Expected 3 quasis but got 2"
`;

exports[`builders es2015 templateLiteral should validate 4`] = `
"Number of TemplateLiteral quasis should be exactly one more than the number of expressions.
Expected 1 quasis but got 2"
`;

exports[`builders es2015 templateLiteral should validate 5`] = `"Property quasis expected type of array but got object"`;

exports[`builders es2015 templateLiteral should validate 6`] = `"Property expressions expected type of array but got null"`;
29 changes: 29 additions & 0 deletions packages/babel-types/test/builders/es2015/templateElement.js
Expand Up @@ -21,5 +21,34 @@ describe("builders", function() {
expect(() => t.templateElement("foo")).toThrowErrorMatchingSnapshot();
});
});
describe("templateLiteral", function() {
it("should validate", function() {
const foo = t.templateElement({ raw: "foo" });
const bar = t.templateElement({ raw: "bar" });

const baz = t.stringLiteral("baz");
const qux = t.stringLiteral("qux");

expect(t.templateLiteral([foo], [])).toMatchSnapshot();

expect(t.templateLiteral([foo, bar], [baz])).toMatchSnapshot();

expect(() =>
t.templateLiteral([foo, bar], [baz, qux]),
).toThrowErrorMatchingSnapshot();

expect(() =>
t.templateLiteral([foo, bar], []),
).toThrowErrorMatchingSnapshot();

expect(() =>
t.templateLiteral({}, [baz]),
).toThrowErrorMatchingSnapshot();

expect(() =>
t.templateLiteral([foo, bar]),
).toThrowErrorMatchingSnapshot();
});
});
});
});

0 comments on commit 66062c2

Please sign in to comment.