Нет описания

identifyShadowedGlobals.js 2.8KB

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. isNonTopLevelDeclaration(token) &&
  31. globalNames.has(tokens.identifierNameForToken(token))
  32. ) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. function markShadowedGlobals(
  39. tokens,
  40. scopes,
  41. globalNames,
  42. ) {
  43. const scopeStack = [];
  44. let scopeIndex = scopes.length - 1;
  45. // Scopes were generated at completion time, so they're sorted by end index, so we can maintain a
  46. // good stack by going backwards through them.
  47. for (let i = tokens.tokens.length - 1; ; i--) {
  48. while (scopeStack.length > 0 && scopeStack[scopeStack.length - 1].startTokenIndex === i + 1) {
  49. scopeStack.pop();
  50. }
  51. while (scopeIndex >= 0 && scopes[scopeIndex].endTokenIndex === i + 1) {
  52. scopeStack.push(scopes[scopeIndex]);
  53. scopeIndex--;
  54. }
  55. // Process scopes after the last iteration so we can make sure we pop all of them.
  56. if (i < 0) {
  57. break;
  58. }
  59. const token = tokens.tokens[i];
  60. const name = tokens.identifierNameForToken(token);
  61. if (scopeStack.length > 1 && token.type === tt.name && globalNames.has(name)) {
  62. if (isBlockScopedDeclaration(token)) {
  63. markShadowedForScope(scopeStack[scopeStack.length - 1], tokens, name);
  64. } else if (isFunctionScopedDeclaration(token)) {
  65. let stackIndex = scopeStack.length - 1;
  66. while (stackIndex > 0 && !scopeStack[stackIndex].isFunctionScope) {
  67. stackIndex--;
  68. }
  69. if (stackIndex < 0) {
  70. throw new Error("Did not find parent function scope.");
  71. }
  72. markShadowedForScope(scopeStack[stackIndex], tokens, name);
  73. }
  74. }
  75. }
  76. if (scopeStack.length > 0) {
  77. throw new Error("Expected empty scope stack after processing file.");
  78. }
  79. }
  80. function markShadowedForScope(scope, tokens, name) {
  81. for (let i = scope.startTokenIndex; i < scope.endTokenIndex; i++) {
  82. const token = tokens.tokens[i];
  83. if (
  84. (token.type === tt.name || token.type === tt.jsxName) &&
  85. tokens.identifierNameForToken(token) === name
  86. ) {
  87. token.shadowsGlobal = true;
  88. }
  89. }
  90. }