index.js 4.9 KB

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