Skip to content

Instantly share code, notes, and snippets.

@capaj
Created January 4, 2018 17:44
Show Gist options
  • Save capaj/5aeab3c2044b72ef78a39f034ddb9497 to your computer and use it in GitHub Desktop.
Save capaj/5aeab3c2044b72ef78a39f034ddb9497 to your computer and use it in GitHub Desktop.
find getters javascript
import traverse from 'traverse'
const findGetters = obj => {
const getters = []
traverse(obj).forEach(function (x) {
if (this.isRoot) {
return
}
const descriptor = Object.getOwnPropertyDescriptor(
this.parent.node,
this.key
)
if (descriptor.get) {
getters.push(this.path.join('.'))
}
})
return getters
}
export default findGetters
import findGetters from './find-getters'
import test from 'ava'
test('finds', t => {
const def = {
a: 1,
b: 2,
get sum () {
return this.a + this.b
},
get sum2 () {
return this.a + this.b
}
}
t.deepEqual(findGetters(def), ['sum', 'sum2'])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment