2023-12-18 13:12:25 +08:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , {
value : true
} ) ;
2024-01-16 21:26:16 +08:00
Object . defineProperty ( exports , "FEATURES" , {
enumerable : true ,
get : function ( ) {
return _features . FEATURES ;
}
} ) ;
Object . defineProperty ( exports , "buildCheckInRHS" , {
enumerable : true ,
get : function ( ) {
return _fields . buildCheckInRHS ;
}
} ) ;
2023-12-18 13:12:25 +08:00
exports . createClassFeaturePlugin = createClassFeaturePlugin ;
2024-01-16 21:26:16 +08:00
Object . defineProperty ( exports , "enableFeature" , {
2023-12-18 13:12:25 +08:00
enumerable : true ,
get : function ( ) {
2024-01-16 21:26:16 +08:00
return _features . enableFeature ;
2023-12-18 13:12:25 +08:00
}
} ) ;
2024-01-16 21:26:16 +08:00
Object . defineProperty ( exports , "injectInitialization" , {
2023-12-18 13:12:25 +08:00
enumerable : true ,
get : function ( ) {
2024-01-16 21:26:16 +08:00
return _misc . injectInitialization ;
2023-12-18 13:12:25 +08:00
}
} ) ;
2024-01-16 21:26:16 +08:00
var _core = require ( "@babel/core" ) ;
var _helperFunctionName = require ( "@babel/helper-function-name" ) ;
var _helperSplitExportDeclaration = require ( "@babel/helper-split-export-declaration" ) ;
var _decorators = require ( "./decorators.js" ) ;
var _semver = require ( "semver" ) ;
var _fields = require ( "./fields.js" ) ;
var _decorators2 = require ( "./decorators-2018-09.js" ) ;
var _misc = require ( "./misc.js" ) ;
var _features = require ( "./features.js" ) ;
var _typescript = require ( "./typescript.js" ) ;
2023-12-18 13:12:25 +08:00
const versionKey = "@babel/plugin-class-features/version" ;
function createClassFeaturePlugin ( {
name ,
feature ,
loose ,
2024-01-16 21:26:16 +08:00
manipulateOptions ,
api ,
inherits ,
decoratorVersion
2023-12-18 13:12:25 +08:00
} ) {
2024-01-16 21:26:16 +08:00
if ( feature & _features . FEATURES . decorators ) {
{
if ( decoratorVersion === "2021-12" || decoratorVersion === "2022-03" || decoratorVersion === "2023-01" || decoratorVersion === "2023-05" ) {
return ( 0 , _decorators . default ) ( api , {
loose
} , decoratorVersion , inherits ) ;
}
}
}
{
var _api ;
( _api = api ) != null ? _api : api = {
assumption : ( ) => void 0
} ;
}
const setPublicClassFields = api . assumption ( "setPublicClassFields" ) ;
const privateFieldsAsSymbols = api . assumption ( "privateFieldsAsSymbols" ) ;
const privateFieldsAsProperties = api . assumption ( "privateFieldsAsProperties" ) ;
const constantSuper = api . assumption ( "constantSuper" ) ;
const noDocumentAll = api . assumption ( "noDocumentAll" ) ;
if ( privateFieldsAsProperties && privateFieldsAsSymbols ) {
throw new Error ( ` Cannot enable both the "privateFieldsAsProperties" and ` + ` "privateFieldsAsSymbols" assumptions as the same time. ` ) ;
}
const privateFieldsAsSymbolsOrProperties = privateFieldsAsProperties || privateFieldsAsSymbols ;
if ( loose === true ) {
const explicit = [ ] ;
if ( setPublicClassFields !== undefined ) {
explicit . push ( ` "setPublicClassFields" ` ) ;
}
if ( privateFieldsAsProperties !== undefined ) {
explicit . push ( ` "privateFieldsAsProperties" ` ) ;
}
if ( privateFieldsAsSymbols !== undefined ) {
explicit . push ( ` "privateFieldsAsSymbols" ` ) ;
}
if ( explicit . length !== 0 ) {
console . warn ( ` [ ${ name } ]: You are using the "loose: true" option and you are ` + ` explicitly setting a value for the ${ explicit . join ( " and " ) } ` + ` assumption ${ explicit . length > 1 ? "s" : "" } . The "loose" option ` + ` can cause incompatibilities with the other class features ` + ` plugins, so it's recommended that you replace it with the ` + ` following top-level option: \n ` + ` \t "assumptions": { \n ` + ` \t \t "setPublicClassFields": true, \n ` + ` \t \t "privateFieldsAsSymbols": true \n ` + ` \t } ` ) ;
}
}
2023-12-18 13:12:25 +08:00
return {
name ,
manipulateOptions ,
2024-01-16 21:26:16 +08:00
inherits ,
pre ( file ) {
( 0 , _features . enableFeature ) ( file , feature , loose ) ;
{
if ( typeof file . get ( versionKey ) === "number" ) {
file . set ( versionKey , "7.23.7" ) ;
return ;
}
}
if ( ! file . get ( versionKey ) || _semver . lt ( file . get ( versionKey ) , "7.23.7" ) ) {
file . set ( versionKey , "7.23.7" ) ;
2023-12-18 13:12:25 +08:00
}
} ,
visitor : {
2024-01-16 21:26:16 +08:00
Class ( path , {
file
} ) {
var _ref ;
if ( file . get ( versionKey ) !== "7.23.7" ) return ;
if ( ! ( 0 , _features . shouldTransform ) ( path , file ) ) return ;
const pathIsClassDeclaration = path . isClassDeclaration ( ) ;
if ( pathIsClassDeclaration ) ( 0 , _typescript . assertFieldTransformed ) ( path ) ;
const loose = ( 0 , _features . isLoose ) ( file , feature ) ;
2023-12-18 13:12:25 +08:00
let constructor ;
2024-01-16 21:26:16 +08:00
const isDecorated = ( 0 , _decorators2 . hasDecorators ) ( path . node ) ;
2023-12-18 13:12:25 +08:00
const props = [ ] ;
const elements = [ ] ;
const computedPaths = [ ] ;
const privateNames = new Set ( ) ;
const body = path . get ( "body" ) ;
for ( const path of body . get ( "body" ) ) {
2024-01-16 21:26:16 +08:00
if ( ( path . isClassProperty ( ) || path . isClassMethod ( ) ) && path . node . computed ) {
2023-12-18 13:12:25 +08:00
computedPaths . push ( path ) ;
}
if ( path . isPrivate ( ) ) {
const {
name
} = path . node . key . id ;
const getName = ` get ${ name } ` ;
const setName = ` set ${ name } ` ;
2024-01-16 21:26:16 +08:00
if ( path . isClassPrivateMethod ( ) ) {
if ( path . node . kind === "get" ) {
if ( privateNames . has ( getName ) || privateNames . has ( name ) && ! privateNames . has ( setName ) ) {
throw path . buildCodeFrameError ( "Duplicate private field" ) ;
}
privateNames . add ( getName ) . add ( name ) ;
} else if ( path . node . kind === "set" ) {
if ( privateNames . has ( setName ) || privateNames . has ( name ) && ! privateNames . has ( getName ) ) {
throw path . buildCodeFrameError ( "Duplicate private field" ) ;
}
privateNames . add ( setName ) . add ( name ) ;
2023-12-18 13:12:25 +08:00
}
} else {
if ( privateNames . has ( name ) && ! privateNames . has ( getName ) && ! privateNames . has ( setName ) || privateNames . has ( name ) && ( privateNames . has ( getName ) || privateNames . has ( setName ) ) ) {
throw path . buildCodeFrameError ( "Duplicate private field" ) ;
}
privateNames . add ( name ) ;
}
}
if ( path . isClassMethod ( {
kind : "constructor"
} ) ) {
constructor = path ;
} else {
elements . push ( path ) ;
2024-01-16 21:26:16 +08:00
if ( path . isProperty ( ) || path . isPrivate ( ) || path . isStaticBlock != null && path . isStaticBlock ( ) ) {
2023-12-18 13:12:25 +08:00
props . push ( path ) ;
}
}
}
2024-01-16 21:26:16 +08:00
{
if ( ! props . length && ! isDecorated ) return ;
}
const innerBinding = path . node . id ;
2023-12-18 13:12:25 +08:00
let ref ;
2024-01-16 21:26:16 +08:00
if ( ! innerBinding || ! pathIsClassDeclaration ) {
( 0 , _helperFunctionName . default ) ( path ) ;
2023-12-18 13:12:25 +08:00
ref = path . scope . generateUidIdentifier ( "class" ) ;
}
2024-01-16 21:26:16 +08:00
const classRefForDefine = ( _ref = ref ) != null ? _ref : _core . types . cloneNode ( innerBinding ) ;
2023-12-18 13:12:25 +08:00
const privateNamesMap = ( 0 , _fields . buildPrivateNamesMap ) ( props ) ;
2024-01-16 21:26:16 +08:00
const privateNamesNodes = ( 0 , _fields . buildPrivateNamesNodes ) ( privateNamesMap , privateFieldsAsProperties != null ? privateFieldsAsProperties : loose , privateFieldsAsSymbols != null ? privateFieldsAsSymbols : false , file ) ;
( 0 , _fields . transformPrivateNamesUsage ) ( classRefForDefine , path , privateNamesMap , {
privateFieldsAsProperties : privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose ,
noDocumentAll ,
innerBinding
} , file ) ;
let keysNodes , staticNodes , instanceNodes , lastInstanceNodeReturnsThis , pureStaticNodes , classBindingNode , wrapClass ;
{
if ( isDecorated ) {
staticNodes = pureStaticNodes = keysNodes = [ ] ;
( {
instanceNodes ,
wrapClass
} = ( 0 , _decorators2 . buildDecoratedClass ) ( classRefForDefine , path , elements , file ) ) ;
} else {
keysNodes = ( 0 , _misc . extractComputedKeys ) ( path , computedPaths , file ) ;
( {
staticNodes ,
pureStaticNodes ,
instanceNodes ,
lastInstanceNodeReturnsThis ,
classBindingNode ,
wrapClass
} = ( 0 , _fields . buildFieldsInitNodes ) ( ref , path . node . superClass , props , privateNamesMap , file , setPublicClassFields != null ? setPublicClassFields : loose , privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose , constantSuper != null ? constantSuper : loose , innerBinding ) ) ;
}
2023-12-18 13:12:25 +08:00
}
if ( instanceNodes . length > 0 ) {
( 0 , _misc . injectInitialization ) ( path , constructor , instanceNodes , ( referenceVisitor , state ) => {
2024-01-16 21:26:16 +08:00
{
if ( isDecorated ) return ;
}
2023-12-18 13:12:25 +08:00
for ( const prop of props ) {
2024-01-16 21:26:16 +08:00
if ( _core . types . isStaticBlock != null && _core . types . isStaticBlock ( prop . node ) || prop . node . static ) continue ;
2023-12-18 13:12:25 +08:00
prop . traverse ( referenceVisitor , state ) ;
}
2024-01-16 21:26:16 +08:00
} , lastInstanceNodeReturnsThis ) ;
}
const wrappedPath = wrapClass ( path ) ;
wrappedPath . insertBefore ( [ ... privateNamesNodes , ... keysNodes ] ) ;
if ( staticNodes . length > 0 ) {
wrappedPath . insertAfter ( staticNodes ) ;
}
if ( pureStaticNodes . length > 0 ) {
wrappedPath . find ( parent => parent . isStatement ( ) || parent . isDeclaration ( ) ) . insertAfter ( pureStaticNodes ) ;
}
if ( classBindingNode != null && pathIsClassDeclaration ) {
wrappedPath . insertAfter ( classBindingNode ) ;
2023-12-18 13:12:25 +08:00
}
} ,
2024-01-16 21:26:16 +08:00
ExportDefaultDeclaration ( path , {
file
} ) {
{
if ( file . get ( versionKey ) !== "7.23.7" ) return ;
const decl = path . get ( "declaration" ) ;
if ( decl . isClassDeclaration ( ) && ( 0 , _decorators2 . hasDecorators ) ( decl . node ) ) {
if ( decl . node . id ) {
( 0 , _helperSplitExportDeclaration . default ) ( path ) ;
} else {
decl . node . type = "ClassExpression" ;
}
2023-12-18 13:12:25 +08:00
}
}
}
}
} ;
2024-01-16 21:26:16 +08:00
}
//# sourceMappingURL=index.js.map