identifyShadowedGlobals.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {
  2. isBlockScopedDeclaration,
  3. isFunctionScopedDeclaration,
  4. isNonTopLevelDeclaration,
  5. } from "./parser/tokenizer";
  6. import {TokenType as tt} from "./parser/tokenizer/types";
  7. /**
  8. * Traverse the given tokens and modify them if necessary to indicate that some names shadow global
  9. * variables.
  10. */
  11. export default function identifyShadowedGlobals(
  12. tokens,
  13. scopes,
  14. globalNames,
  15. ) {
  16. if (!hasShadowedGlobals(tokens, globalNames)) {
  17. return;
  18. }
  19. markShadowedGlobals(tokens, scopes, globalNames);
  20. }
  21. /**
  22. * We can do a fast up-front check to see if there are any declarations to global names. If not,
  23. * then there's no point in computing scope assignments.
  24. */
  25. // Exported for testing.
  26. export function hasShadowedGlobals(tokens, globalNames) {
  27. for (const token of tokens.tokens) {
  28. if (
  29. token.type === tt.name &&
  30. !token.isType &&
  31. isNonTopLevelDeclaration(token) &&
  32. globalNames.has(tokens.identifierNameForToken(token))
  33. ) {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. function markShadowedGlobals(
  40. tokens,
  41. scopes,
  42. globalNames,
  43. ) {
  44. const scopeStack = [];
  45. let scopeIndex = scopes.length - 1;
  46. // Scopes were generated at completion time, so they're sorted by end index, so we can maintain a
  47. // good stack by going backwards through them.
  48. for (let i = tokens.tokens.length - 1; ; i--) {
  49. while (scopeStack.length > 0 && scopeStack[scopeStack.length - 1].startTokenIndex === i + 1) {
  50. scopeStack.pop();
  51. }
  52. while (scopeIndex >= 0 && scopes[scopeIndex].endTokenIndex === i + 1) {
  53. scopeStack.push(scopes[scopeIndex]);
  54. scopeIndex--;
  55. }
  56. // Process scopes after the last iteration so we can make sure we pop all of them.
  57. if (i < 0) {
  58. break;
  59. }
  60. const token = tokens.tokens[i];
  61. const name = tokens.identifierNameForToken(token);
  62. if (scopeStack.length > 1 && !token.isType && token.type === tt.name && globalNames.has(name)) {
  63. if (isBlockScopedDeclaration(token)) {
  64. markShadowedForScope(scopeStack[scopeStack.length - 1], tokens, name);
  65. } else if (isFunctionScopedDeclaration(token)) {
  66. let stackIndex = scopeStack.length - 1;
  67. while (stackIndex > 0 && !scopeStack[stackIndex].isFunctionScope) {
  68. stackIndex--;
  69. }
  70. if (stackIndex < 0) {
  71. throw new Error("Did not find parent function scope.");
  72. }
  73. markShadowedForScope(scopeStack[stackIndex], tokens, name);
  74. }
  75. }
  76. }
  77. if (scopeStack.length > 0) {
  78. throw new Error("Expected empty scope stack after processing file.");
  79. }
  80. }
  81. function markShadowedForScope(scope, tokens, name) {
  82. for (let i = scope.startTokenIndex; i < scope.endTokenIndex; i++) {
  83. const token = tokens.tokens[i];
  84. if (
  85. (token.type === tt.name || token.type === tt.jsxName) &&
  86. tokens.identifierNameForToken(token) === name
  87. ) {
  88. token.shadowsGlobal = true;
  89. }
  90. }
  91. }