base.js 2.2 KB

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