index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 || {};
  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 declarePattern = function (node, parent) {
  53. switch (node.type) {
  54. case 'Identifier':
  55. parent.locals[node.name] = true;
  56. break;
  57. case 'ObjectPattern':
  58. node.properties.forEach(function (node) {
  59. declarePattern(node.value || node.argument, parent);
  60. });
  61. break;
  62. case 'ArrayPattern':
  63. node.elements.forEach(function (node) {
  64. if (node) declarePattern(node, parent);
  65. });
  66. break;
  67. case 'RestElement':
  68. declarePattern(node.argument, parent);
  69. break;
  70. case 'AssignmentPattern':
  71. declarePattern(node.left, parent);
  72. break;
  73. // istanbul ignore next
  74. default:
  75. throw new Error('Unrecognized pattern type: ' + node.type);
  76. }
  77. };
  78. var declareModuleSpecifier = function (node, parents) {
  79. ast.locals = ast.locals || {};
  80. ast.locals[node.local.name] = true;
  81. };
  82. walk.ancestor(ast, {
  83. 'VariableDeclaration': function (node, parents) {
  84. var parent = null;
  85. for (var i = parents.length - 1; i >= 0 && parent === null; i--) {
  86. if (node.kind === 'var' ? isScope(parents[i]) : isBlockScope(parents[i])) {
  87. parent = parents[i];
  88. }
  89. }
  90. parent.locals = parent.locals || {};
  91. node.declarations.forEach(function (declaration) {
  92. declarePattern(declaration.id, parent);
  93. });
  94. },
  95. 'FunctionDeclaration': function (node, parents) {
  96. var parent = null;
  97. for (var i = parents.length - 2; i >= 0 && parent === null; i--) {
  98. if (isScope(parents[i])) {
  99. parent = parents[i];
  100. }
  101. }
  102. parent.locals = parent.locals || {};
  103. if (node.id) {
  104. parent.locals[node.id.name] = true;
  105. }
  106. declareFunction(node);
  107. },
  108. 'Function': declareFunction,
  109. 'ClassDeclaration': function (node, parents) {
  110. var parent = null;
  111. for (var i = parents.length - 2; i >= 0 && parent === null; i--) {
  112. if (isBlockScope(parents[i])) {
  113. parent = parents[i];
  114. }
  115. }
  116. parent.locals = parent.locals || {};
  117. if (node.id) {
  118. parent.locals[node.id.name] = true;
  119. }
  120. },
  121. 'TryStatement': function (node) {
  122. if (node.handler === null) return;
  123. node.handler.locals = node.handler.locals || {};
  124. node.handler.locals[node.handler.param.name] = true;
  125. },
  126. 'ImportDefaultSpecifier': declareModuleSpecifier,
  127. 'ImportSpecifier': declareModuleSpecifier,
  128. 'ImportNamespaceSpecifier': declareModuleSpecifier
  129. });
  130. function identifier(node, parents) {
  131. var name = node.name;
  132. if (name === 'undefined') return;
  133. for (var i = 0; i < parents.length; i++) {
  134. if (name === 'arguments' && declaresArguments(parents[i])) {
  135. return;
  136. }
  137. if (parents[i].locals && name in parents[i].locals) {
  138. return;
  139. }
  140. }
  141. node.parents = parents.slice();
  142. globals.push(node);
  143. }
  144. walk.ancestor(ast, {
  145. 'VariablePattern': identifier,
  146. 'Identifier': identifier,
  147. 'ThisExpression': function (node, parents) {
  148. for (var i = 0; i < parents.length; i++) {
  149. if (declaresThis(parents[i])) {
  150. return;
  151. }
  152. }
  153. node.parents = parents.slice();
  154. globals.push(node);
  155. }
  156. });
  157. var groupedGlobals = {};
  158. globals.forEach(function (node) {
  159. var name = node.type === 'ThisExpression' ? 'this' : node.name;
  160. groupedGlobals[name] = (groupedGlobals[name] || []);
  161. groupedGlobals[name].push(node);
  162. });
  163. return Object.keys(groupedGlobals).sort().map(function (name) {
  164. return {name: name, nodes: groupedGlobals[name]};
  165. });
  166. }