import-target.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Requirements
  9. //------------------------------------------------------------------------------
  10. const path = require("path")
  11. const resolve = require("resolve")
  12. //------------------------------------------------------------------------------
  13. // Helpers
  14. //------------------------------------------------------------------------------
  15. /**
  16. * Resolve the given id to file paths.
  17. * @param {boolean} isModule The flag which indicates this id is a module.
  18. * @param {string} id The id to resolve.
  19. * @param {object} options The options of node-resolve module.
  20. * It requires `options.basedir`.
  21. * @returns {string|null} The resolved path.
  22. */
  23. function getFilePath(isModule, id, options) {
  24. try {
  25. return resolve.sync(id, options)
  26. }
  27. catch (_err) {
  28. if (isModule) {
  29. return null
  30. }
  31. return path.resolve(options.basedir, id)
  32. }
  33. }
  34. /**
  35. * Gets the module name of a given path.
  36. *
  37. * e.g. `eslint/lib/ast-utils` -> `eslint`
  38. *
  39. * @param {string} nameOrPath - A path to get.
  40. * @returns {string} The module name of the path.
  41. */
  42. function getModuleName(nameOrPath) {
  43. let end = nameOrPath.indexOf("/")
  44. if (end !== -1 && nameOrPath[0] === "@") {
  45. end = nameOrPath.indexOf("/", 1 + end)
  46. }
  47. return end === -1 ? nameOrPath : nameOrPath.slice(0, end)
  48. }
  49. //------------------------------------------------------------------------------
  50. // Public Interface
  51. //------------------------------------------------------------------------------
  52. /**
  53. * Information of an import target.
  54. */
  55. module.exports = class ImportTarget {
  56. /**
  57. * Initialize this instance.
  58. * @param {ASTNode} node - The node of a `require()` or a module declaraiton.
  59. * @param {string} name - The name of an import target.
  60. * @param {object} options - The options of `node-resolve` module.
  61. */
  62. constructor(node, name, options) {
  63. const isModule = !/^(?:[./\\]|\w+:)/.test(name)
  64. /**
  65. * The node of a `require()` or a module declaraiton.
  66. * @type {ASTNode}
  67. */
  68. this.node = node
  69. /**
  70. * The name of this import target.
  71. * @type {string}
  72. */
  73. this.name = name
  74. /**
  75. * The full path of this import target.
  76. * If the target is a module and it does not exist then this is `null`.
  77. * @type {string|null}
  78. */
  79. this.filePath = getFilePath(isModule, name, options)
  80. /**
  81. * The module name of this import target.
  82. * If the target is a relative path then this is `null`.
  83. * @type {string|null}
  84. */
  85. this.moduleName = isModule ? getModuleName(name) : null
  86. }
  87. }