get-try-extensions.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. "use strict"
  7. //------------------------------------------------------------------------------
  8. // Helpers
  9. //------------------------------------------------------------------------------
  10. const DEFAULT_VALUE = Object.freeze([".js", ".json", ".node"])
  11. /**
  12. * Gets `tryExtensions` property from a given option object.
  13. *
  14. * @param {object|undefined} option - An option object to get.
  15. * @returns {string[]|null} The `tryExtensions` value, or `null`.
  16. */
  17. function get(option) {
  18. if (option && option.tryExtensions && Array.isArray(option.tryExtensions)) {
  19. return option.tryExtensions.map(String)
  20. }
  21. return null
  22. }
  23. //------------------------------------------------------------------------------
  24. // Public Interface
  25. //------------------------------------------------------------------------------
  26. /**
  27. * Gets "tryExtensions" setting.
  28. *
  29. * 1. This checks `options` property, then returns it if exists.
  30. * 2. This checks `settings.node` property, then returns it if exists.
  31. * 3. This returns `[".js", ".json", ".node"]`.
  32. *
  33. * @param {RuleContext} context - The rule context.
  34. * @returns {string[]} A list of extensions.
  35. */
  36. module.exports = function getTryExtensions(context) {
  37. return (
  38. get(context.options && context.options[0]) ||
  39. get(context.settings && context.settings.node) ||
  40. DEFAULT_VALUE
  41. )
  42. }
  43. module.exports.schema = {
  44. type: "array",
  45. items: {
  46. type: "string",
  47. pattern: "^\\.",
  48. },
  49. uniqueItems: true,
  50. }