JS.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import * as esprima from "esprima";
  2. import escodegen from "escodegen";
  3. import esmangle from "esmangle";
  4. /**
  5. * JavaScript operations.
  6. *
  7. * @author n1474335 [n1474335@gmail.com]
  8. * @copyright Crown Copyright 2016
  9. * @license Apache-2.0
  10. *
  11. * @namespace
  12. */
  13. const JS = {
  14. /**
  15. * @constant
  16. * @default
  17. */
  18. PARSE_LOC: false,
  19. /**
  20. * @constant
  21. * @default
  22. */
  23. PARSE_RANGE: false,
  24. /**
  25. * @constant
  26. * @default
  27. */
  28. PARSE_TOKENS: false,
  29. /**
  30. * @constant
  31. * @default
  32. */
  33. PARSE_COMMENT: false,
  34. /**
  35. * @constant
  36. * @default
  37. */
  38. PARSE_TOLERANT: false,
  39. /**
  40. * JavaScript Parser operation.
  41. *
  42. * @param {string} input
  43. * @param {Object[]} args
  44. * @returns {string}
  45. */
  46. runParse: function (input, args) {
  47. let parseLoc = args[0],
  48. parseRange = args[1],
  49. parseTokens = args[2],
  50. parseComment = args[3],
  51. parseTolerant = args[4],
  52. result = {},
  53. options = {
  54. loc: parseLoc,
  55. range: parseRange,
  56. tokens: parseTokens,
  57. comment: parseComment,
  58. tolerant: parseTolerant
  59. };
  60. result = esprima.parseScript(input, options);
  61. return JSON.stringify(result, null, 2);
  62. },
  63. /**
  64. * @constant
  65. * @default
  66. */
  67. BEAUTIFY_INDENT: "\\t",
  68. /**
  69. * @constant
  70. * @default
  71. */
  72. BEAUTIFY_QUOTES: ["Auto", "Single", "Double"],
  73. /**
  74. * @constant
  75. * @default
  76. */
  77. BEAUTIFY_SEMICOLONS: true,
  78. /**
  79. * @constant
  80. * @default
  81. */
  82. BEAUTIFY_COMMENT: true,
  83. /**
  84. * JavaScript Beautify operation.
  85. *
  86. * @param {string} input
  87. * @param {Object[]} args
  88. * @returns {string}
  89. */
  90. runBeautify: function(input, args) {
  91. let beautifyIndent = args[0] || JS.BEAUTIFY_INDENT,
  92. quotes = args[1].toLowerCase(),
  93. beautifySemicolons = args[2],
  94. beautifyComment = args[3],
  95. result = "",
  96. AST;
  97. try {
  98. AST = esprima.parseScript(input, {
  99. range: true,
  100. tokens: true,
  101. comment: true
  102. });
  103. const options = {
  104. format: {
  105. indent: {
  106. style: beautifyIndent
  107. },
  108. quotes: quotes,
  109. semicolons: beautifySemicolons,
  110. },
  111. comment: beautifyComment
  112. };
  113. if (options.comment)
  114. AST = escodegen.attachComments(AST, AST.comments, AST.tokens);
  115. result = escodegen.generate(AST, options);
  116. } catch (e) {
  117. // Leave original error so the user can see the detail
  118. throw "Unable to parse JavaScript.<br>" + e.message;
  119. }
  120. return result;
  121. },
  122. /**
  123. * JavaScript Minify operation.
  124. *
  125. * @param {string} input
  126. * @param {Object[]} args
  127. * @returns {string}
  128. */
  129. runMinify: function(input, args) {
  130. let result = "",
  131. AST = esprima.parseScript(input),
  132. optimisedAST = esmangle.optimize(AST, null),
  133. mangledAST = esmangle.mangle(optimisedAST);
  134. result = escodegen.generate(mangledAST, {
  135. format: {
  136. renumber: true,
  137. hexadecimal: true,
  138. escapeless: true,
  139. compact: true,
  140. semicolons: false,
  141. parentheses: false
  142. }
  143. });
  144. return result;
  145. },
  146. };
  147. export default JS;