options.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @fileoverview A collection of methods for processing Espree's options.
  3. * @author Kai Cataldo
  4. */
  5. //------------------------------------------------------------------------------
  6. // Helpers
  7. //------------------------------------------------------------------------------
  8. const SUPPORTED_VERSIONS = [
  9. 3,
  10. 5,
  11. 6,
  12. 7,
  13. 8,
  14. 9,
  15. 10,
  16. 11,
  17. 12,
  18. 13
  19. ];
  20. /**
  21. * Get the latest ECMAScript version supported by Espree.
  22. * @returns {number} The latest ECMAScript version.
  23. */
  24. export function getLatestEcmaVersion() {
  25. return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1];
  26. }
  27. /**
  28. * Get the list of ECMAScript versions supported by Espree.
  29. * @returns {number[]} An array containing the supported ECMAScript versions.
  30. */
  31. export function getSupportedEcmaVersions() {
  32. return [...SUPPORTED_VERSIONS];
  33. }
  34. /**
  35. * Normalize ECMAScript version from the initial config
  36. * @param {(number|"latest")} ecmaVersion ECMAScript version from the initial config
  37. * @throws {Error} throws an error if the ecmaVersion is invalid.
  38. * @returns {number} normalized ECMAScript version
  39. */
  40. function normalizeEcmaVersion(ecmaVersion = 5) {
  41. let version = ecmaVersion === "latest" ? getLatestEcmaVersion() : ecmaVersion;
  42. if (typeof version !== "number") {
  43. throw new Error(`ecmaVersion must be a number or "latest". Received value of type ${typeof ecmaVersion} instead.`);
  44. }
  45. // Calculate ECMAScript edition number from official year version starting with
  46. // ES2015, which corresponds with ES6 (or a difference of 2009).
  47. if (version >= 2015) {
  48. version -= 2009;
  49. }
  50. if (!SUPPORTED_VERSIONS.includes(version)) {
  51. throw new Error("Invalid ecmaVersion.");
  52. }
  53. return version;
  54. }
  55. /**
  56. * Normalize sourceType from the initial config
  57. * @param {string} sourceType to normalize
  58. * @throws {Error} throw an error if sourceType is invalid
  59. * @returns {string} normalized sourceType
  60. */
  61. function normalizeSourceType(sourceType = "script") {
  62. if (sourceType === "script" || sourceType === "module") {
  63. return sourceType;
  64. }
  65. if (sourceType === "commonjs") {
  66. return "script";
  67. }
  68. throw new Error("Invalid sourceType.");
  69. }
  70. /**
  71. * Normalize parserOptions
  72. * @param {Object} options the parser options to normalize
  73. * @throws {Error} throw an error if found invalid option.
  74. * @returns {Object} normalized options
  75. */
  76. export function normalizeOptions(options) {
  77. const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
  78. const sourceType = normalizeSourceType(options.sourceType);
  79. const ranges = options.range === true;
  80. const locations = options.loc === true;
  81. if (ecmaVersion !== 3 && options.allowReserved) {
  82. // a value of `false` is intentionally allowed here, so a shared config can overwrite it when needed
  83. throw new Error("`allowReserved` is only supported when ecmaVersion is 3");
  84. }
  85. if (typeof options.allowReserved !== "undefined" && typeof options.allowReserved !== "boolean") {
  86. throw new Error("`allowReserved`, when present, must be `true` or `false`");
  87. }
  88. const allowReserved = ecmaVersion === 3 ? (options.allowReserved || "never") : false;
  89. const ecmaFeatures = options.ecmaFeatures || {};
  90. const allowReturnOutsideFunction = options.sourceType === "commonjs" ||
  91. Boolean(ecmaFeatures.globalReturn);
  92. if (sourceType === "module" && ecmaVersion < 6) {
  93. throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options.");
  94. }
  95. return Object.assign({}, options, {
  96. ecmaVersion,
  97. sourceType,
  98. ranges,
  99. locations,
  100. allowReserved,
  101. allowReturnOutsideFunction
  102. });
  103. }