Skip to content

Commit

Permalink
Add import statement when there is any undefined scope variable
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Feb 20, 2019
1 parent d07d960 commit de7c852
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions packages/babel-plugin-import-jsx-pragma/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export default function( babel ) {
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 ) {
Expand All @@ -69,18 +76,9 @@ export default function( babel ) {
}
} );
},
VariableDeclaration( path, state ) {
if ( state.hasDeclaredScopeVariable ) {
return;
}

const { scopeVariable } = getOptions( state );

state.hasDeclaredScopeVariable = ! path.scope.parent && path.scope.hasOwnBinding( scopeVariable );
},
Program: {
exit( path, state ) {
if ( ! state.hasJSX || state.hasImportedScopeVariable || state.hasDeclaredScopeVariable ) {
if ( ! state.hasJSX || state.hasImportedScopeVariable || ! state.hasUndeclaredScopeVariable ) {
return;
}

Expand Down

0 comments on commit de7c852

Please sign in to comment.