From 7dd2effe9620c1196cadfd1271cc18120802b077 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 5 Nov 2018 11:20:34 +0000 Subject: [PATCH] Syntax for snapshot assertions --- packages/syntax/src/generators.js | 4 ++++ packages/syntax/src/parser.js | 1 + packages/syntax/src/plugin.js | 6 ++++-- packages/syntax/src/types.js | 7 +++++-- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/syntax/src/generators.js b/packages/syntax/src/generators.js index 61eb76b..0d9748b 100644 --- a/packages/syntax/src/generators.js +++ b/packages/syntax/src/generators.js @@ -52,6 +52,10 @@ export function FieldDeclarationStatement(node) { export function AssertionEndpointStatement(node) { this.word("assert"); this.space(); + if (!node.isDynamic) { + this.word(":snapshot"); + this.space(); + } this.print(node.template, node); if (node.test) { this.space(); diff --git a/packages/syntax/src/parser.js b/packages/syntax/src/parser.js index 2304763..9047e5b 100644 --- a/packages/syntax/src/parser.js +++ b/packages/syntax/src/parser.js @@ -94,6 +94,7 @@ export default class SyndicateParser extends _original_Parser { if (this.isContextual("assert")) { this.next(); const node = this.startNode(); + node.isDynamic = this.parseMaybeSnapshot(); node.template = this.parseExpression(); if (this.eatContextual("when")) { this.expect(tt.parenL); diff --git a/packages/syntax/src/plugin.js b/packages/syntax/src/plugin.js index a6dcaff..82640e7 100644 --- a/packages/syntax/src/plugin.js +++ b/packages/syntax/src/plugin.js @@ -335,17 +335,19 @@ export default declare((api, options) => { if (node.test) { path.replaceWith(template(`DATASPACE._currentFacet.addEndpoint(function () { return (TEST) ? [TEMPLATE, null] : [void 0, null]; - });`)({ + }, ISDYNAMIC);`)({ DATASPACE: state.DataspaceID, TEST: node.test, TEMPLATE: node.template, + ISDYNAMIC: t.booleanLiteral(node.isDynamic), })); } else { path.replaceWith(template(`DATASPACE._currentFacet.addEndpoint(function () { return [TEMPLATE, null]; - });`)({ + }, ISDYNAMIC);`)({ DATASPACE: state.DataspaceID, TEMPLATE: node.template, + ISDYNAMIC: t.booleanLiteral(node.isDynamic), })); } }, diff --git a/packages/syntax/src/types.js b/packages/syntax/src/types.js index 4a78e43..e4a68e7 100644 --- a/packages/syntax/src/types.js +++ b/packages/syntax/src/types.js @@ -61,10 +61,13 @@ defineType("FieldDeclarationStatement", { }); defineType("AssertionEndpointStatement", { - builder: ["template", "test"], - visitor: ["template", "test"], + builder: ["isDynamic", "template", "test"], + visitor: ["isDynamic", "template", "test"], aliases: ["Statement"], fields: { + isDynamic: { + validate: assertOneOf(true, false), + }, template: { validate: assertNodeType("Expression"), },