identifyShadowedGlobals.js 3.0 KB

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