Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@speier
Created September 22, 2017 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save speier/3b281ed823ce3121e83c1199e5079929 to your computer and use it in GitHub Desktop.
Save speier/3b281ed823ce3121e83c1199e5079929 to your computer and use it in GitHub Desktop.
get code comments
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