What is acorn-globals?
The acorn-globals npm package is used to extract global variables from JavaScript code. It leverages the Acorn JS parser to analyze scripts and identify global variables, which can be useful for various applications such as linting tools, code analysis, and optimization tools.
Extracting global variables
This feature allows you to parse JavaScript code and extract a list of global variables used in the code. The example shows how to parse a simple script and identify 'console' as a global variable.
const acornGlobals = require('acorn-globals');
const acorn = require('acorn');
const code = 'console.log("Hello, world!");';
const ast = acorn.parse(code, {ecmaVersion: 2020});
const globals = acornGlobals(ast);
console.log(globals);