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