2023-12-18 13:12:25 +08:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , {
value : true
} ) ;
exports . default = void 0 ;
2024-01-16 21:26:16 +08:00
var _helperPluginUtils = require ( "@babel/helper-plugin-utils" ) ;
var _helperSkipTransparentExpressionWrappers = require ( "@babel/helper-skip-transparent-expression-wrappers" ) ;
var _core = require ( "@babel/core" ) ;
var _default = exports . default = ( 0 , _helperPluginUtils . declare ) ( ( api , options ) => {
var _api$assumption , _options$allowArrayLi ;
2023-12-18 13:12:25 +08:00
api . assertVersion ( 7 ) ;
2024-01-16 21:26:16 +08:00
const iterableIsArray = ( _api$assumption = api . assumption ( "iterableIsArray" ) ) != null ? _api$assumption : options . loose ;
const arrayLikeIsIterable = ( _options$allowArrayLi = options . allowArrayLike ) != null ? _options$allowArrayLi : api . assumption ( "arrayLikeIsIterable" ) ;
2023-12-18 13:12:25 +08:00
function getSpreadLiteral ( spread , scope ) {
2024-01-16 21:26:16 +08:00
if ( iterableIsArray && ! _core . types . isIdentifier ( spread . argument , {
2023-12-18 13:12:25 +08:00
name : "arguments"
} ) ) {
return spread . argument ;
} else {
2024-01-16 21:26:16 +08:00
return scope . toArray ( spread . argument , true , arrayLikeIsIterable ) ;
2023-12-18 13:12:25 +08:00
}
}
2024-01-16 21:26:16 +08:00
function hasHole ( spread ) {
return spread . elements . some ( el => el === null ) ;
}
2023-12-18 13:12:25 +08:00
function hasSpread ( nodes ) {
for ( let i = 0 ; i < nodes . length ; i ++ ) {
2024-01-16 21:26:16 +08:00
if ( _core . types . isSpreadElement ( nodes [ i ] ) ) {
2023-12-18 13:12:25 +08:00
return true ;
}
}
return false ;
}
function push ( _props , nodes ) {
if ( ! _props . length ) return _props ;
2024-01-16 21:26:16 +08:00
nodes . push ( _core . types . arrayExpression ( _props ) ) ;
2023-12-18 13:12:25 +08:00
return [ ] ;
}
2024-01-16 21:26:16 +08:00
function build ( props , scope , file ) {
2023-12-18 13:12:25 +08:00
const nodes = [ ] ;
let _props = [ ] ;
for ( const prop of props ) {
2024-01-16 21:26:16 +08:00
if ( _core . types . isSpreadElement ( prop ) ) {
2023-12-18 13:12:25 +08:00
_props = push ( _props , nodes ) ;
2024-01-16 21:26:16 +08:00
let spreadLiteral = getSpreadLiteral ( prop , scope ) ;
if ( _core . types . isArrayExpression ( spreadLiteral ) && hasHole ( spreadLiteral ) ) {
spreadLiteral = _core . types . callExpression ( file . addHelper ( "arrayWithoutHoles" ) , [ spreadLiteral ] ) ;
}
nodes . push ( spreadLiteral ) ;
2023-12-18 13:12:25 +08:00
} else {
_props . push ( prop ) ;
}
}
push ( _props , nodes ) ;
return nodes ;
}
return {
name : "transform-spread" ,
visitor : {
ArrayExpression ( path ) {
const {
node ,
scope
} = path ;
const elements = node . elements ;
if ( ! hasSpread ( elements ) ) return ;
2024-01-16 21:26:16 +08:00
const nodes = build ( elements , scope , this . file ) ;
2023-12-18 13:12:25 +08:00
let first = nodes [ 0 ] ;
if ( nodes . length === 1 && first !== elements [ 0 ] . argument ) {
path . replaceWith ( first ) ;
return ;
}
2024-01-16 21:26:16 +08:00
if ( ! _core . types . isArrayExpression ( first ) ) {
first = _core . types . arrayExpression ( [ ] ) ;
2023-12-18 13:12:25 +08:00
} else {
nodes . shift ( ) ;
}
2024-01-16 21:26:16 +08:00
path . replaceWith ( _core . types . callExpression ( _core . types . memberExpression ( first , _core . types . identifier ( "concat" ) ) , nodes ) ) ;
2023-12-18 13:12:25 +08:00
} ,
CallExpression ( path ) {
const {
node ,
scope
} = path ;
const args = node . arguments ;
if ( ! hasSpread ( args ) ) return ;
2024-01-16 21:26:16 +08:00
const calleePath = ( 0 , _helperSkipTransparentExpressionWrappers . skipTransparentExprWrappers ) ( path . get ( "callee" ) ) ;
if ( calleePath . isSuper ( ) ) {
throw path . buildCodeFrameError ( "It's not possible to compile spread arguments in `super()` without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration." ) ;
}
2023-12-18 13:12:25 +08:00
let contextLiteral = scope . buildUndefinedNode ( ) ;
node . arguments = [ ] ;
let nodes ;
2024-01-16 21:26:16 +08:00
if ( args . length === 1 && _core . types . isIdentifier ( args [ 0 ] . argument , {
name : "arguments"
} ) ) {
2023-12-18 13:12:25 +08:00
nodes = [ args [ 0 ] . argument ] ;
} else {
2024-01-16 21:26:16 +08:00
nodes = build ( args , scope , this . file ) ;
2023-12-18 13:12:25 +08:00
}
const first = nodes . shift ( ) ;
if ( nodes . length ) {
2024-01-16 21:26:16 +08:00
node . arguments . push ( _core . types . callExpression ( _core . types . memberExpression ( first , _core . types . identifier ( "concat" ) ) , nodes ) ) ;
2023-12-18 13:12:25 +08:00
} else {
node . arguments . push ( first ) ;
}
2024-01-16 21:26:16 +08:00
const callee = calleePath . node ;
if ( _core . types . isMemberExpression ( callee ) ) {
2023-12-18 13:12:25 +08:00
const temp = scope . maybeGenerateMemoised ( callee . object ) ;
if ( temp ) {
2024-01-16 21:26:16 +08:00
callee . object = _core . types . assignmentExpression ( "=" , temp , callee . object ) ;
2023-12-18 13:12:25 +08:00
contextLiteral = temp ;
} else {
2024-01-16 21:26:16 +08:00
contextLiteral = _core . types . cloneNode ( callee . object ) ;
2023-12-18 13:12:25 +08:00
}
}
2024-01-16 21:26:16 +08:00
node . callee = _core . types . memberExpression ( node . callee , _core . types . identifier ( "apply" ) ) ;
if ( _core . types . isSuper ( contextLiteral ) ) {
contextLiteral = _core . types . thisExpression ( ) ;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
node . arguments . unshift ( _core . types . cloneNode ( contextLiteral ) ) ;
2023-12-18 13:12:25 +08:00
} ,
NewExpression ( path ) {
const {
node ,
scope
} = path ;
2024-01-16 21:26:16 +08:00
if ( ! hasSpread ( node . arguments ) ) return ;
const nodes = build ( node . arguments , scope , this . file ) ;
2023-12-18 13:12:25 +08:00
const first = nodes . shift ( ) ;
2024-01-16 21:26:16 +08:00
let args ;
2023-12-18 13:12:25 +08:00
if ( nodes . length ) {
2024-01-16 21:26:16 +08:00
args = _core . types . callExpression ( _core . types . memberExpression ( first , _core . types . identifier ( "concat" ) ) , nodes ) ;
2023-12-18 13:12:25 +08:00
} else {
args = first ;
}
2024-01-16 21:26:16 +08:00
path . replaceWith ( _core . types . callExpression ( path . hub . addHelper ( "construct" ) , [ node . callee , args ] ) ) ;
2023-12-18 13:12:25 +08:00
}
}
} ;
} ) ;
2024-01-16 21:26:16 +08:00
//# sourceMappingURL=index.js.map