| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var acorn = require('acorn');
|
|---|
| 4 | var walk = require('acorn-walk');
|
|---|
| 5 |
|
|---|
| 6 | function isScope(node) {
|
|---|
| 7 | return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression' || node.type === 'Program';
|
|---|
| 8 | }
|
|---|
| 9 | function isBlockScope(node) {
|
|---|
| 10 | return node.type === 'BlockStatement' || isScope(node);
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | function declaresArguments(node) {
|
|---|
| 14 | return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration';
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | function declaresThis(node) {
|
|---|
| 18 | return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration';
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function reallyParse(source, options) {
|
|---|
| 22 | var parseOptions = Object.assign({}, options,
|
|---|
| 23 | {
|
|---|
| 24 | allowReturnOutsideFunction: true,
|
|---|
| 25 | allowImportExportEverywhere: true,
|
|---|
| 26 | allowHashBang: true
|
|---|
| 27 | }
|
|---|
| 28 | );
|
|---|
| 29 | return acorn.parse(source, parseOptions);
|
|---|
| 30 | }
|
|---|
| 31 | module.exports = findGlobals;
|
|---|
| 32 | module.exports.parse = reallyParse;
|
|---|
| 33 | function findGlobals(source, options) {
|
|---|
| 34 | options = options || {};
|
|---|
| 35 | var globals = [];
|
|---|
| 36 | var ast;
|
|---|
| 37 | // istanbul ignore else
|
|---|
| 38 | if (typeof source === 'string') {
|
|---|
| 39 | ast = reallyParse(source, options);
|
|---|
| 40 | } else {
|
|---|
| 41 | ast = source;
|
|---|
| 42 | }
|
|---|
| 43 | // istanbul ignore if
|
|---|
| 44 | if (!(ast && typeof ast === 'object' && ast.type === 'Program')) {
|
|---|
| 45 | throw new TypeError('Source must be either a string of JavaScript or an acorn AST');
|
|---|
| 46 | }
|
|---|
| 47 | var declareFunction = function (node) {
|
|---|
| 48 | var fn = node;
|
|---|
| 49 | fn.locals = fn.locals || Object.create(null);
|
|---|
| 50 | node.params.forEach(function (node) {
|
|---|
| 51 | declarePattern(node, fn);
|
|---|
| 52 | });
|
|---|
| 53 | if (node.id) {
|
|---|
| 54 | fn.locals[node.id.name] = true;
|
|---|
| 55 | }
|
|---|
| 56 | };
|
|---|
| 57 | var declareClass = function (node) {
|
|---|
| 58 | node.locals = node.locals || Object.create(null);
|
|---|
| 59 | if (node.id) {
|
|---|
| 60 | node.locals[node.id.name] = true;
|
|---|
| 61 | }
|
|---|
| 62 | };
|
|---|
| 63 | var declarePattern = function (node, parent) {
|
|---|
| 64 | switch (node.type) {
|
|---|
| 65 | case 'Identifier':
|
|---|
| 66 | parent.locals[node.name] = true;
|
|---|
| 67 | break;
|
|---|
| 68 | case 'ObjectPattern':
|
|---|
| 69 | node.properties.forEach(function (node) {
|
|---|
| 70 | declarePattern(node.value || node.argument, parent);
|
|---|
| 71 | });
|
|---|
| 72 | break;
|
|---|
| 73 | case 'ArrayPattern':
|
|---|
| 74 | node.elements.forEach(function (node) {
|
|---|
| 75 | if (node) declarePattern(node, parent);
|
|---|
| 76 | });
|
|---|
| 77 | break;
|
|---|
| 78 | case 'RestElement':
|
|---|
| 79 | declarePattern(node.argument, parent);
|
|---|
| 80 | break;
|
|---|
| 81 | case 'AssignmentPattern':
|
|---|
| 82 | declarePattern(node.left, parent);
|
|---|
| 83 | break;
|
|---|
| 84 | // istanbul ignore next
|
|---|
| 85 | default:
|
|---|
| 86 | throw new Error('Unrecognized pattern type: ' + node.type);
|
|---|
| 87 | }
|
|---|
| 88 | };
|
|---|
| 89 | var declareModuleSpecifier = function (node, parents) {
|
|---|
| 90 | ast.locals = ast.locals || Object.create(null);
|
|---|
| 91 | ast.locals[node.local.name] = true;
|
|---|
| 92 | };
|
|---|
| 93 | walk.ancestor(ast, {
|
|---|
| 94 | 'VariableDeclaration': function (node, parents) {
|
|---|
| 95 | var parent = null;
|
|---|
| 96 | for (var i = parents.length - 1; i >= 0 && parent === null; i--) {
|
|---|
| 97 | if (node.kind === 'var' ? isScope(parents[i]) : isBlockScope(parents[i])) {
|
|---|
| 98 | parent = parents[i];
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | parent.locals = parent.locals || Object.create(null);
|
|---|
| 102 | node.declarations.forEach(function (declaration) {
|
|---|
| 103 | declarePattern(declaration.id, parent);
|
|---|
| 104 | });
|
|---|
| 105 | },
|
|---|
| 106 | 'FunctionDeclaration': function (node, parents) {
|
|---|
| 107 | var parent = null;
|
|---|
| 108 | for (var i = parents.length - 2; i >= 0 && parent === null; i--) {
|
|---|
| 109 | if (isScope(parents[i])) {
|
|---|
| 110 | parent = parents[i];
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | parent.locals = parent.locals || Object.create(null);
|
|---|
| 114 | if (node.id) {
|
|---|
| 115 | parent.locals[node.id.name] = true;
|
|---|
| 116 | }
|
|---|
| 117 | declareFunction(node);
|
|---|
| 118 | },
|
|---|
| 119 | 'Function': declareFunction,
|
|---|
| 120 | 'ClassDeclaration': function (node, parents) {
|
|---|
| 121 | var parent = null;
|
|---|
| 122 | for (var i = parents.length - 2; i >= 0 && parent === null; i--) {
|
|---|
| 123 | if (isBlockScope(parents[i])) {
|
|---|
| 124 | parent = parents[i];
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | parent.locals = parent.locals || Object.create(null);
|
|---|
| 128 | if (node.id) {
|
|---|
| 129 | parent.locals[node.id.name] = true;
|
|---|
| 130 | }
|
|---|
| 131 | declareClass(node);
|
|---|
| 132 | },
|
|---|
| 133 | 'Class': declareClass,
|
|---|
| 134 | 'TryStatement': function (node) {
|
|---|
| 135 | if (node.handler === null) return;
|
|---|
| 136 | node.handler.locals = node.handler.locals || Object.create(null);
|
|---|
| 137 | declarePattern(node.handler.param, node.handler);
|
|---|
| 138 | },
|
|---|
| 139 | 'ImportDefaultSpecifier': declareModuleSpecifier,
|
|---|
| 140 | 'ImportSpecifier': declareModuleSpecifier,
|
|---|
| 141 | 'ImportNamespaceSpecifier': declareModuleSpecifier
|
|---|
| 142 | });
|
|---|
| 143 | function identifier(node, parents) {
|
|---|
| 144 | var name = node.name;
|
|---|
| 145 | if (name === 'undefined') return;
|
|---|
| 146 | for (var i = 0; i < parents.length; i++) {
|
|---|
| 147 | if (name === 'arguments' && declaresArguments(parents[i])) {
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 | if (parents[i].locals && name in parents[i].locals) {
|
|---|
| 151 | return;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | node.parents = parents.slice();
|
|---|
| 155 | globals.push(node);
|
|---|
| 156 | }
|
|---|
| 157 | walk.ancestor(ast, {
|
|---|
| 158 | 'VariablePattern': identifier,
|
|---|
| 159 | 'Identifier': identifier,
|
|---|
| 160 | 'ThisExpression': function (node, parents) {
|
|---|
| 161 | for (var i = 0; i < parents.length; i++) {
|
|---|
| 162 | if (declaresThis(parents[i])) {
|
|---|
| 163 | return;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | node.parents = parents.slice();
|
|---|
| 167 | globals.push(node);
|
|---|
| 168 | }
|
|---|
| 169 | });
|
|---|
| 170 | var groupedGlobals = Object.create(null);
|
|---|
| 171 | globals.forEach(function (node) {
|
|---|
| 172 | var name = node.type === 'ThisExpression' ? 'this' : node.name;
|
|---|
| 173 | groupedGlobals[name] = (groupedGlobals[name] || []);
|
|---|
| 174 | groupedGlobals[name].push(node);
|
|---|
| 175 | });
|
|---|
| 176 | return Object.keys(groupedGlobals).sort().map(function (name) {
|
|---|
| 177 | return {name: name, nodes: groupedGlobals[name]};
|
|---|
| 178 | });
|
|---|
| 179 | }
|
|---|