You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1 KiB

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