From 19491d25661a514afc63537b7046dd439a13a1e1 Mon Sep 17 00:00:00 2001 From: Matthieu Gicquel Date: Sun, 8 Aug 2021 15:29:03 +0200 Subject: [PATCH 1/2] handle shadowed globals when they are JSX names --- src/identifyShadowedGlobals.ts | 2 +- test/imports-test.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/identifyShadowedGlobals.ts b/src/identifyShadowedGlobals.ts index 7e2a693a..0d198f9d 100644 --- a/src/identifyShadowedGlobals.ts +++ b/src/identifyShadowedGlobals.ts @@ -87,7 +87,7 @@ function markShadowedGlobals( function markShadowedForScope(scope: Scope, tokens: TokenProcessor, name: string): void { for (let i = scope.startTokenIndex; i < scope.endTokenIndex; i++) { const token = tokens.tokens[i]; - if (token.type === tt.name && tokens.identifierNameForToken(token) === name) { + if ((token.type === tt.name || token.type === tt.jsxName) && tokens.identifierNameForToken(token) === name) { token.shadowsGlobal = true; } } diff --git a/test/imports-test.ts b/test/imports-test.ts index 7986cbc0..07e38c2f 100644 --- a/test/imports-test.ts +++ b/test/imports-test.ts @@ -639,6 +639,29 @@ var _moduleName = require('moduleName'); ); }); + it("properly handles React.createElement with shadowing", () => { + assertResult( + ` + import React from 'react'; + import Foo from './Foo'; + + const fn = () => { + const Foo = div; + return ; + } + `, + `"use strict";${JSX_PREFIX}${IMPORT_DEFAULT_PREFIX} + var _react = require('react'); var _react2 = _interopRequireDefault(_react); + var _Foo = require('./Foo'); var _Foo2 = _interopRequireDefault(_Foo); + + const fn = () => { + const Foo = div; + return _react2.default.createElement(Foo, {${devProps(7)}} ); + } + `, + ); + }); + it("properly transforms imported JSX props", () => { assertResult( ` From 6188a2b06ee94ff5eee1ffeedef178549b77b3d8 Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Mon, 9 Aug 2021 00:46:55 -0700 Subject: [PATCH 2/2] Run Prettier --- src/identifyShadowedGlobals.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/identifyShadowedGlobals.ts b/src/identifyShadowedGlobals.ts index 0d198f9d..b872f2f2 100644 --- a/src/identifyShadowedGlobals.ts +++ b/src/identifyShadowedGlobals.ts @@ -87,7 +87,10 @@ function markShadowedGlobals( function markShadowedForScope(scope: Scope, tokens: TokenProcessor, name: string): void { for (let i = scope.startTokenIndex; i < scope.endTokenIndex; i++) { const token = tokens.tokens[i]; - if ((token.type === tt.name || token.type === tt.jsxName) && tokens.identifierNameForToken(token) === name) { + if ( + (token.type === tt.name || token.type === tt.jsxName) && + tokens.identifierNameForToken(token) === name + ) { token.shadowsGlobal = true; } }