| 1 | import type Printer from "../printer"; |
| 2 | import type * as t from "@babel/types"; |
| 3 | |
| 4 | export function File(this: Printer, node: t.File) { |
| 5 | if (node.program) { |
| 6 | // Print this here to ensure that Program node 'leadingComments' still |
| 7 | // get printed after the hashbang. |
| 8 | this.print(node.program.interpreter, node); |
| 9 | } |
| 10 | |
| 11 | this.print(node.program, node); |
| 12 | } |
| 13 | |
| 14 | export function Program(this: Printer, node: t.Program) { |
| 15 | // An empty Program doesn't have any inner tokens, so |
| 16 | // we must explicitly print its inner comments. |
| 17 | this.noIndentInnerCommentsHere(); |
| 18 | this.printInnerComments(); |
| 19 | |
| 20 | const directivesLen = node.directives?.length; |
| 21 | if (directivesLen) { |
| 22 | const newline = node.body.length ? 2 : 1; |
| 23 | this.printSequence(node.directives, node, { |
| 24 | trailingCommentsLineOffset: newline, |
| 25 | }); |
| 26 | if (!node.directives[directivesLen - 1].trailingComments?.length) { |
| 27 | this.newline(newline); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | this.printSequence(node.body, node); |
| 32 | } |
| 33 | |
| 34 | export function BlockStatement(this: Printer, node: t.BlockStatement) { |
| 35 | this.token("{"); |
| 36 | |
| 37 | const directivesLen = node.directives?.length; |
| 38 | if (directivesLen) { |
| 39 | const newline = node.body.length ? 2 : 1; |
| 40 | this.printSequence(node.directives, node, { |
| 41 | indent: true, |
| 42 | trailingCommentsLineOffset: newline, |
| 43 | }); |
| 44 | if (!node.directives[directivesLen - 1].trailingComments?.length) { |
| 45 | this.newline(newline); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | this.printSequence(node.body, node, { indent: true }); |
| 50 | |
| 51 | this.sourceWithOffset("end", node.loc, 0, -1); |
| 52 | |
| 53 | this.rightBrace(); |
| 54 | } |
| 55 | |
| 56 | export function Directive(this: Printer, node: t.Directive) { |
| 57 | this.print(node.value, node); |
| 58 | this.semicolon(); |
| 59 | } |
| 60 | |
| 61 | // These regexes match an even number of \ followed by a quote |
| 62 | const unescapedSingleQuoteRE = /(?:^|[^\\])(?:\\\\)*'/; |
| 63 | const unescapedDoubleQuoteRE = /(?:^|[^\\])(?:\\\\)*"/; |
| 64 | |
| 65 | export function DirectiveLiteral(this: Printer, node: t.DirectiveLiteral) { |
| 66 | const raw = this.getPossibleRaw(node); |
| 67 | if (!this.format.minified && raw !== undefined) { |
| 68 | this.token(raw); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | const { value } = node; |
| 73 | |
| 74 | // NOTE: In directives we can't change escapings, |
| 75 | // because they change the behavior. |
| 76 | // e.g. "us\x65 strict" (\x65 is e) is not a "use strict" directive. |
| 77 | |
| 78 | if (!unescapedDoubleQuoteRE.test(value)) { |
| 79 | this.token(`"${value}"`); |
| 80 | } else if (!unescapedSingleQuoteRE.test(value)) { |
| 81 | this.token(`'${value}'`); |
| 82 | } else { |
| 83 | throw new Error( |
| 84 | "Malformed AST: it is not possible to print a directive containing" + |
| 85 | " both unescaped single and double quotes.", |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | export function InterpreterDirective( |
| 91 | this: Printer, |
| 92 | node: t.InterpreterDirective, |
| 93 | ) { |
| 94 | this.token(`#!${node.value}`); |
| 95 | this.newline(1, true); |
| 96 | } |
| 97 | |
| 98 | export function Placeholder(this: Printer, node: t.Placeholder) { |
| 99 | this.token("%%"); |
| 100 | this.print(node.name); |
| 101 | this.token("%%"); |
| 102 | |
| 103 | if (node.expectedNode === "Statement") { |
| 104 | this.semicolon(); |
| 105 | } |
| 106 | } |