Skip to content

Commit

Permalink
Babel Plugin Import JSX Pragma: Remove import visitor (#14106)
Browse files Browse the repository at this point in the history
Props to @aduth for coding this optimization.
  • Loading branch information
gziolo authored Feb 27, 2019
1 parent fd35a1b commit 1010f0d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
5 changes: 4 additions & 1 deletion packages/babel-plugin-import-jsx-pragma/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 1 addition & 27 deletions packages/babel-plugin-import-jsx-pragma/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 1010f0d

Please sign in to comment.