From b7573f3ac928816cb3a71b91fee68484c8825249 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 25 Feb 2019 22:30:04 +0100 Subject: [PATCH] Babel Plugin Import JSX Pragma: Remove import visitor Props to @aduth for coding this optimization. --- .../CHANGELOG.md | 5 +++- .../babel-plugin-import-jsx-pragma/index.js | 28 +------------------ 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/packages/babel-plugin-import-jsx-pragma/CHANGELOG.md b/packages/babel-plugin-import-jsx-pragma/CHANGELOG.md index 32ad201bf1884e..7a7c426e6ead9a 100644 --- a/packages/babel-plugin-import-jsx-pragma/CHANGELOG.md +++ b/packages/babel-plugin-import-jsx-pragma/CHANGELOG.md @@ -2,9 +2,12 @@ ### Breaking Change -- Plugin skips now adding import JSX pragma when the scope variable is defined for all JSX elements ([#13809](https://github.com/WordPress/gutenberg/pull/13809)). - Stop using Babel transpilation internally and set node 8 as a minimal version required ([#13540](https://github.com/WordPress/gutenberg/pull/13540)). +### Enhancement + +- Plugin skips now adding import JSX pragma when the scope variable is defined for all JSX elements ([#13809](https://github.com/WordPress/gutenberg/pull/13809)). + ## 1.1.0 (2018-09-05) ### New Feature diff --git a/packages/babel-plugin-import-jsx-pragma/index.js b/packages/babel-plugin-import-jsx-pragma/index.js index d431b6823aea35..7953640c484ab3 100644 --- a/packages/babel-plugin-import-jsx-pragma/index.js +++ b/packages/babel-plugin-import-jsx-pragma/index.js @@ -40,42 +40,16 @@ module.exports = function( babel ) { return { visitor: { JSXElement( path, state ) { - state.hasJSX = true; if ( state.hasUndeclaredScopeVariable ) { return; } const { scopeVariable } = getOptions( state ); - state.hasUndeclaredScopeVariable = ! path.scope.hasBinding( scopeVariable ); }, - ImportDeclaration( path, state ) { - if ( state.hasImportedScopeVariable ) { - return; - } - - const { scopeVariable, isDefault } = getOptions( state ); - - // Test that at least one import specifier exists matching the - // scope variable name. The module source is not verified since - // we must avoid introducing a conflicting import name, even if - // the scope variable is referenced from a different source. - state.hasImportedScopeVariable = path.node.specifiers.some( ( specifier ) => { - switch ( specifier.type ) { - case 'ImportSpecifier': - return ( - ! isDefault && - specifier.imported.name === scopeVariable - ); - - case 'ImportDefaultSpecifier': - return isDefault; - } - } ); - }, Program: { exit( path, state ) { - if ( ! state.hasJSX || state.hasImportedScopeVariable || ! state.hasUndeclaredScopeVariable ) { + if ( ! state.hasUndeclaredScopeVariable ) { return; }