1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.File = File;
|
---|
7 | exports.Program = Program;
|
---|
8 | exports.BlockStatement = BlockStatement;
|
---|
9 | exports.Directive = Directive;
|
---|
10 | exports.DirectiveLiteral = DirectiveLiteral;
|
---|
11 | exports.InterpreterDirective = InterpreterDirective;
|
---|
12 | exports.Placeholder = Placeholder;
|
---|
13 |
|
---|
14 | var t = require("@babel/types");
|
---|
15 |
|
---|
16 | function File(node) {
|
---|
17 | if (node.program) {
|
---|
18 | this.print(node.program.interpreter, node);
|
---|
19 | }
|
---|
20 |
|
---|
21 | this.print(node.program, node);
|
---|
22 | }
|
---|
23 |
|
---|
24 | function Program(node) {
|
---|
25 | this.printInnerComments(node, false);
|
---|
26 | this.printSequence(node.directives, node);
|
---|
27 | if (node.directives && node.directives.length) this.newline();
|
---|
28 | this.printSequence(node.body, node);
|
---|
29 | }
|
---|
30 |
|
---|
31 | function BlockStatement(node) {
|
---|
32 | var _node$directives;
|
---|
33 |
|
---|
34 | this.token("{");
|
---|
35 | this.printInnerComments(node);
|
---|
36 | const hasDirectives = (_node$directives = node.directives) == null ? void 0 : _node$directives.length;
|
---|
37 |
|
---|
38 | if (node.body.length || hasDirectives) {
|
---|
39 | this.newline();
|
---|
40 | this.printSequence(node.directives, node, {
|
---|
41 | indent: true
|
---|
42 | });
|
---|
43 | if (hasDirectives) this.newline();
|
---|
44 | this.printSequence(node.body, node, {
|
---|
45 | indent: true
|
---|
46 | });
|
---|
47 | this.removeTrailingNewline();
|
---|
48 | this.source("end", node.loc);
|
---|
49 | if (!this.endsWith("\n")) this.newline();
|
---|
50 | this.rightBrace();
|
---|
51 | } else {
|
---|
52 | this.source("end", node.loc);
|
---|
53 | this.token("}");
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | function Directive(node) {
|
---|
58 | this.print(node.value, node);
|
---|
59 | this.semicolon();
|
---|
60 | }
|
---|
61 |
|
---|
62 | const unescapedSingleQuoteRE = /(?:^|[^\\])(?:\\\\)*'/;
|
---|
63 | const unescapedDoubleQuoteRE = /(?:^|[^\\])(?:\\\\)*"/;
|
---|
64 |
|
---|
65 | function DirectiveLiteral(node) {
|
---|
66 | const raw = this.getPossibleRaw(node);
|
---|
67 |
|
---|
68 | if (raw != null) {
|
---|
69 | this.token(raw);
|
---|
70 | return;
|
---|
71 | }
|
---|
72 |
|
---|
73 | const {
|
---|
74 | value
|
---|
75 | } = node;
|
---|
76 |
|
---|
77 | if (!unescapedDoubleQuoteRE.test(value)) {
|
---|
78 | this.token(`"${value}"`);
|
---|
79 | } else if (!unescapedSingleQuoteRE.test(value)) {
|
---|
80 | this.token(`'${value}'`);
|
---|
81 | } else {
|
---|
82 | throw new Error("Malformed AST: it is not possible to print a directive containing" + " both unescaped single and double quotes.");
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | function InterpreterDirective(node) {
|
---|
87 | this.token(`#!${node.value}\n`);
|
---|
88 | }
|
---|
89 |
|
---|
90 | function Placeholder(node) {
|
---|
91 | this.token("%%");
|
---|
92 | this.print(node.name);
|
---|
93 | this.token("%%");
|
---|
94 |
|
---|
95 | if (node.expectedNode === "Statement") {
|
---|
96 | this.semicolon();
|
---|
97 | }
|
---|
98 | } |
---|