Нема описа

identifyShadowedGlobals.js 3.0KB

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