Created
January 4, 2018 17:44
-
-
Save capaj/5aeab3c2044b72ef78a39f034ddb9497 to your computer and use it in GitHub Desktop.
find getters javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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