Created
September 14, 2016 19:39
-
-
Save n-johnson/cc73ef90d8a0acac4cf9e61dd7ccd764 to your computer and use it in GitHub Desktop.
Find `const` usage in javascript file
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 fs = require('fs') | |
const rocambole = require('rocambole'); | |
// Use espree instead of esprima - about twice as fast | |
const espree = require('espree'); | |
rocambole.parseFn = espree.parse; | |
rocambole.parseContext = espree; | |
const parseTime = (delta) => (delta[0] * 1000 + delta[1] / 1e6) + 'ms' | |
const args = process.argv.slice(2) | |
const fileName = args[0] | |
if (!fileName) { | |
throw new Error("File path expected as first argument"); | |
} | |
fs.statSync(fileName) // Throws if file doesn't exist | |
const start = process.hrtime() | |
const data = fs.readFileSync(fileName).toString() | |
console.log('Loaded:', fileName, 'in:', parseTime(process.hrtime(start))) | |
var constUsages = []; | |
const startAst = process.hrtime() | |
const ast = rocambole.parse(data, { ecmaVersion: 6 }) | |
console.log('Parsed AST in:', parseTime(process.hrtime(startAst))); | |
console.log('Beginning moonwalk.') | |
const startWalk = process.hrtime() | |
rocambole.moonwalk(ast,(node) => { | |
if (node.type == 'VariableDeclaration'){ | |
if (node.kind === 'const') { | |
constUsages.push(node) | |
} | |
} | |
}); | |
console.log('Treewalk finished in:', parseTime(process.hrtime(startWalk))); | |
console.log('Const Usage:', constUsages) | |
console.log('Const count:', constUsages.length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment