JavaScriptParser.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import * as esprima from "esprima";
  8. /**
  9. * JavaScript Parser operation
  10. */
  11. class JavaScriptParser extends Operation {
  12. /**
  13. * JavaScriptParser constructor
  14. */
  15. constructor() {
  16. super();
  17. this.name = "JavaScript Parser";
  18. this.module = "Code";
  19. this.description = "Returns an Abstract Syntax Tree for valid JavaScript code.";
  20. this.infoURL = "https://en.wikipedia.org/wiki/Abstract_syntax_tree";
  21. this.inputType = "string";
  22. this.outputType = "string";
  23. this.args = [
  24. {
  25. "name": "Location info",
  26. "type": "boolean",
  27. "value": false
  28. },
  29. {
  30. "name": "Range info",
  31. "type": "boolean",
  32. "value": false
  33. },
  34. {
  35. "name": "Include tokens array",
  36. "type": "boolean",
  37. "value": false
  38. },
  39. {
  40. "name": "Include comments array",
  41. "type": "boolean",
  42. "value": false
  43. },
  44. {
  45. "name": "Report errors and try to continue",
  46. "type": "boolean",
  47. "value": false
  48. }
  49. ];
  50. }
  51. /**
  52. * @param {string} input
  53. * @param {Object[]} args
  54. * @returns {string}
  55. */
  56. run(input, args) {
  57. const [parseLoc, parseRange, parseTokens, parseComment, parseTolerant] = args,
  58. options = {
  59. loc: parseLoc,
  60. range: parseRange,
  61. tokens: parseTokens,
  62. comment: parseComment,
  63. tolerant: parseTolerant
  64. };
  65. let result = {};
  66. result = esprima.parseScript(input, options);
  67. return JSON.stringify(result, null, 2);
  68. }
  69. }
  70. export default JavaScriptParser;