get-try-extensions.js 1.2 KB

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