options.js 3.8 KB

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