options.js 3.1 KB

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