base.js 2.2 KB

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