Created
September 22, 2017 10:41
-
-
Save speier/3b281ed823ce3121e83c1199e5079929 to your computer and use it in GitHub Desktop.
get code comments
This file contains 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
const acorn = require('acorn'); | |
const json5 = require('json5'); | |
const getComments = (input) => { | |
const opts = { | |
onComment: [] | |
}; | |
try { | |
const ast = acorn.parse(input, opts); | |
} catch (e) { | |
return {}; | |
} | |
const comments = opts.onComment.map(comment => { | |
try { | |
return json5.parse(`{${comment.value}}`); | |
} catch (e) { | |
return {}; | |
} | |
}); | |
return comments.reduce((o, i) => Object.assign(o, i), {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment