index.js 5.4 KB

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