options.js 3.8 KB

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