@@ -950,16 +950,22 @@ export default function (babel) {
950950 return test ;
951951 }
952952
953+ function containsAliasReference ( path , alias ) {
954+ let containsAlias = false ;
955+ path . traverse ( {
956+ ReferencedIdentifier ( refPath ) {
957+ if ( refPath . node . name === alias . name ) {
958+ containsAlias = true ;
959+ refPath . stop ( ) ;
960+ }
961+ }
962+ } ) ;
963+ return containsAlias ;
964+ }
965+
953966 function transformMatchCases ( argRef , cases ) {
954967 return cases . reduce ( ( rootIf , path ) => {
955968
956- // fill in placeholders
957- path . get ( "test" ) . traverse ( {
958- PlaceholderExpression ( placeholderPath ) {
959- placeholderPath . replaceWith ( argRef ) ;
960- }
961- } ) ;
962-
963969 // add in ===, etc
964970 transformMatchCaseTest ( path . get ( "test" ) , argRef ) ;
965971
@@ -1243,9 +1249,6 @@ export default function (babel) {
12431249 // only allowed in MatchCase, so don't alias to Expression
12441250 } ) ;
12451251
1246- definePluginType ( "PlaceholderExpression" , {
1247- aliases : [ "Expression" ]
1248- } ) ;
12491252
12501253 // traverse as top-level item so as to run before other babel plugins
12511254 // (and avoid traversing any of their output)
@@ -1547,9 +1550,9 @@ export default function (babel) {
15471550 } ,
15481551
15491552 MatchExpression ( path ) {
1550- const { discriminant } = path . node ;
1553+ const { discriminant, alias = null } = path . node ;
15511554
1552- const argRef = path . scope . generateUidIdentifier ( "it" ) ;
1555+ const argRef = alias || t . identifier ( "it" ) ;
15531556 const matchBody = transformMatchCases ( argRef , path . get ( "cases" ) ) ;
15541557
15551558 const iife = t . callExpression (
@@ -1561,12 +1564,13 @@ export default function (babel) {
15611564
15621565 MatchStatement ( path ) {
15631566 const { discriminant } = path . node ;
1567+ const alias = path . node . alias || t . identifier ( "it" ) ;
15641568
15651569 let argRef ;
1566- if ( t . isIdentifier ( discriminant ) ) {
1570+ if ( t . isIdentifier ( discriminant ) && ! containsAliasReference ( path , alias ) ) {
15671571 argRef = discriminant ;
15681572 } else {
1569- argRef = path . scope . generateUidIdentifier ( "it" ) ;
1573+ argRef = alias ;
15701574 path . insertBefore ( t . variableDeclaration ( "const" , [
15711575 t . variableDeclarator ( argRef , discriminant )
15721576 ] ) ) ;
0 commit comments