is-extraneous.js 618 B

123456789101112131415161718192021222324252627
  1. 'use strict'
  2. module.exports = isExtraneous
  3. function isExtraneous (tree) {
  4. var result = !isNotExtraneous(tree)
  5. return result
  6. }
  7. function topHasNoPjson (tree) {
  8. var top = tree
  9. while (!top.isTop) top = top.parent
  10. return top.error
  11. }
  12. function isNotExtraneous (tree, isCycle) {
  13. if (!isCycle) isCycle = {}
  14. if (tree.isTop || tree.userRequired) {
  15. return true
  16. } else if (isCycle[tree.path]) {
  17. return topHasNoPjson(tree)
  18. } else {
  19. isCycle[tree.path] = true
  20. return tree.requiredBy && tree.requiredBy.some(function (node) {
  21. return isNotExtraneous(node, Object.create(isCycle))
  22. })
  23. }
  24. }