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