UNPKG

1.46 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.program = exports.expression = exports.statement = exports.statements = exports.smart = void 0;
7
8function makeStatementFormatter(fn) {
9 return {
10 code: str => `/* @babel/template */;\n${str}`,
11 validate: () => {},
12 unwrap: ast => {
13 return fn(ast.program.body.slice(1));
14 }
15 };
16}
17
18const smart = makeStatementFormatter(body => {
19 if (body.length > 1) {
20 return body;
21 } else {
22 return body[0];
23 }
24});
25exports.smart = smart;
26const statements = makeStatementFormatter(body => body);
27exports.statements = statements;
28const statement = makeStatementFormatter(body => {
29 if (body.length === 0) {
30 throw new Error("Found nothing to return.");
31 }
32
33 if (body.length > 1) {
34 throw new Error("Found multiple statements but wanted one");
35 }
36
37 return body[0];
38});
39exports.statement = statement;
40const expression = {
41 code: str => `(\n${str}\n)`,
42 validate: ({
43 program
44 }) => {
45 if (program.body.length > 1) {
46 throw new Error("Found multiple statements but wanted one");
47 }
48
49 const expression = program.body[0].expression;
50
51 if (expression.start === 0) {
52 throw new Error("Parse result included parens.");
53 }
54 },
55 unwrap: ast => ast.program.body[0].expression
56};
57exports.expression = expression;
58const program = {
59 code: str => str,
60 validate: () => {},
61 unwrap: ast => ast.program
62};
63exports.program = program;
\No newline at end of file