options.js 3.8 KB

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