check-publish.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @fileoverview Rule to check whether or not `require()` is valid.
  3. * @author Toru Nagashima
  4. * @copyright 2015 Toru Nagashima. All rights reserved.
  5. * See LICENSE file in root directory for full license.
  6. */
  7. "use strict"
  8. //------------------------------------------------------------------------------
  9. // Requirements
  10. //------------------------------------------------------------------------------
  11. const path = require("path")
  12. const getAllowModules = require("./get-allow-modules")
  13. const getConvertPath = require("./get-convert-path")
  14. const getNpmignore = require("./get-npmignore")
  15. const getPackageJson = require("./get-package-json")
  16. //------------------------------------------------------------------------------
  17. // Public Interface
  18. //------------------------------------------------------------------------------
  19. /**
  20. * Checks whether or not each requirement target is published via package.json.
  21. *
  22. * It reads package.json and checks the target exists in `dependencies`.
  23. *
  24. * @param {RuleContext} context - A context to report.
  25. * @param {string} filePath - The current file path.
  26. * @param {ImportTarget[]} targets - A list of target information to check.
  27. * @returns {void}
  28. */
  29. module.exports = function checkForPublish(context, filePath, targets) {
  30. const packageInfo = getPackageJson(filePath)
  31. if (!packageInfo) {
  32. return
  33. }
  34. const allowed = new Set(getAllowModules(context))
  35. const convertPath = getConvertPath(context)
  36. const basedir = path.dirname(packageInfo.filePath)
  37. const toRelative = function(fullPath) { // eslint-disable-line func-style
  38. const retv = path.relative(basedir, fullPath).replace(/\\/g, "/")
  39. return convertPath(retv)
  40. }
  41. const npmignore = getNpmignore(filePath)
  42. const devDependencies = new Set(
  43. Object.keys(packageInfo.devDependencies || {})
  44. )
  45. const dependencies = new Set(
  46. [].concat(
  47. Object.keys(packageInfo.dependencies || {}),
  48. Object.keys(packageInfo.peerDependencies || {}),
  49. Object.keys(packageInfo.optionalDependencies || {})
  50. )
  51. )
  52. if (!npmignore.match(toRelative(filePath))) {
  53. // This file is published, so this cannot import private files.
  54. for (const target of targets) {
  55. const isPrivateFile = (
  56. target.moduleName == null &&
  57. npmignore.match(toRelative(target.filePath))
  58. )
  59. const isDevPackage = (
  60. target.moduleName != null &&
  61. devDependencies.has(target.moduleName) &&
  62. !dependencies.has(target.moduleName) &&
  63. !allowed.has(target.moduleName)
  64. )
  65. if (isPrivateFile || isDevPackage) {
  66. context.report({
  67. node: target.node,
  68. loc: target.node.loc,
  69. message: "\"{{name}}\" is not published.",
  70. data: { name: target.moduleName || target.name },
  71. })
  72. }
  73. }
  74. }
  75. }