enumerate-property-names.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { CALL, CONSTRUCT, READ } = require("eslint-utils")
  7. /**
  8. * Enumerate property names of a given object recursively.
  9. * @param {object} trackMap The map for APIs to enumerate.
  10. * @param {string[]|undefined} path The path to the current map.
  11. * @returns {IterableIterator<string>} The property names of the map.
  12. */
  13. function* enumeratePropertyNames(trackMap, path = []) {
  14. for (const key of Object.keys(trackMap)) {
  15. const value = trackMap[key]
  16. if (typeof value !== "object") {
  17. continue
  18. }
  19. path.push(key)
  20. if (value[CALL]) {
  21. yield `${path.join(".")}()`
  22. }
  23. if (value[CONSTRUCT]) {
  24. yield `new ${path.join(".")}()`
  25. }
  26. if (value[READ]) {
  27. yield path.join(".")
  28. }
  29. yield* enumeratePropertyNames(value, path)
  30. path.pop()
  31. }
  32. }
  33. module.exports = enumeratePropertyNames