options.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {has, isArray} from "./util"
  2. import {SourceLocation} from "./locutil"
  3. // A second optional argument can be given to further configure
  4. // the parser process. These options are recognized:
  5. export const defaultOptions = {
  6. // `ecmaVersion` indicates the ECMAScript version to parse. Must
  7. // be either 3, 5, 6 (2015), 7 (2016), or 8 (2017). This influences support
  8. // for strict mode, the set of reserved words, and support for
  9. // new syntax features. The default is 7.
  10. ecmaVersion: 7,
  11. // `sourceType` indicates the mode the code should be parsed in.
  12. // Can be either `"script"` or `"module"`. This influences global
  13. // strict mode and parsing of `import` and `export` declarations.
  14. sourceType: "script",
  15. // `onInsertedSemicolon` can be a callback that will be called
  16. // when a semicolon is automatically inserted. It will be passed
  17. // th position of the comma as an offset, and if `locations` is
  18. // enabled, it is given the location as a `{line, column}` object
  19. // as second argument.
  20. onInsertedSemicolon: null,
  21. // `onTrailingComma` is similar to `onInsertedSemicolon`, but for
  22. // trailing commas.
  23. onTrailingComma: null,
  24. // By default, reserved words are only enforced if ecmaVersion >= 5.
  25. // Set `allowReserved` to a boolean value to explicitly turn this on
  26. // an off. When this option has the value "never", reserved words
  27. // and keywords can also not be used as property names.
  28. allowReserved: null,
  29. // When enabled, a return at the top level is not considered an
  30. // error.
  31. allowReturnOutsideFunction: false,
  32. // When enabled, import/export statements are not constrained to
  33. // appearing at the top of the program.
  34. allowImportExportEverywhere: false,
  35. // When enabled, hashbang directive in the beginning of file
  36. // is allowed and treated as a line comment.
  37. allowHashBang: false,
  38. // When `locations` is on, `loc` properties holding objects with
  39. // `start` and `end` properties in `{line, column}` form (with
  40. // line being 1-based and column 0-based) will be attached to the
  41. // nodes.
  42. locations: false,
  43. // A function can be passed as `onToken` option, which will
  44. // cause Acorn to call that function with object in the same
  45. // format as tokens returned from `tokenizer().getToken()`. Note
  46. // that you are not allowed to call the parser from the
  47. // callback—that will corrupt its internal state.
  48. onToken: null,
  49. // A function can be passed as `onComment` option, which will
  50. // cause Acorn to call that function with `(block, text, start,
  51. // end)` parameters whenever a comment is skipped. `block` is a
  52. // boolean indicating whether this is a block (`/* */`) comment,
  53. // `text` is the content of the comment, and `start` and `end` are
  54. // character offsets that denote the start and end of the comment.
  55. // When the `locations` option is on, two more parameters are
  56. // passed, the full `{line, column}` locations of the start and
  57. // end of the comments. Note that you are not allowed to call the
  58. // parser from the callback—that will corrupt its internal state.
  59. onComment: null,
  60. // Nodes have their start and end characters offsets recorded in
  61. // `start` and `end` properties (directly on the node, rather than
  62. // the `loc` object, which holds line/column data. To also add a
  63. // [semi-standardized][range] `range` property holding a `[start,
  64. // end]` array with the same numbers, set the `ranges` option to
  65. // `true`.
  66. //
  67. // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
  68. ranges: false,
  69. // It is possible to parse multiple files into a single AST by
  70. // passing the tree produced by parsing the first file as
  71. // `program` option in subsequent parses. This will add the
  72. // toplevel forms of the parsed file to the `Program` (top) node
  73. // of an existing parse tree.
  74. program: null,
  75. // When `locations` is on, you can pass this to record the source
  76. // file in every node's `loc` object.
  77. sourceFile: null,
  78. // This value, if given, is stored in every node, whether
  79. // `locations` is on or off.
  80. directSourceFile: null,
  81. // When enabled, parenthesized expressions are represented by
  82. // (non-standard) ParenthesizedExpression nodes
  83. preserveParens: false,
  84. plugins: {}
  85. }
  86. // Interpret and default an options object
  87. export function getOptions(opts) {
  88. let options = {}
  89. for (let opt in defaultOptions)
  90. options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]
  91. if (options.ecmaVersion >= 2015)
  92. options.ecmaVersion -= 2009
  93. if (options.allowReserved == null)
  94. options.allowReserved = options.ecmaVersion < 5
  95. if (isArray(options.onToken)) {
  96. let tokens = options.onToken
  97. options.onToken = (token) => tokens.push(token)
  98. }
  99. if (isArray(options.onComment))
  100. options.onComment = pushComment(options, options.onComment)
  101. return options
  102. }
  103. function pushComment(options, array) {
  104. return function (block, text, start, end, startLoc, endLoc) {
  105. let comment = {
  106. type: block ? 'Block' : 'Line',
  107. value: text,
  108. start: start,
  109. end: end
  110. }
  111. if (options.locations)
  112. comment.loc = new SourceLocation(this, startLoc, endLoc)
  113. if (options.ranges)
  114. comment.range = [start, end]
  115. array.push(comment)
  116. }
  117. }