# API
this
is bindd to an instance of PluginPass
# useDangerousUDFHelpers
Load the helpers
into the internal store and define addUDFHelper
and listUDFHelper
in the PluginPass instance and File instance with the core extension.
# definition
function useDangerousUDFHelpers(pass: babel.PluginPass, { helpers }: UDFUsePluginOptions): void
# types
type UDFHelper = {
ast: () => t.Program;
};
type UDFUsePluginOptions = {
helpers: { [key: string]: UDFHelper };
};
# example
import { useDangerousUDFHelpers } from 'babel-udf-helpers';
import helpers from './helpers';
export default function({types: t}){
pre(){
useDangerousUDFHelpers(this, { helpers });
}
}
# addUDFHelper(name)
Add a helper function to the beginning of the program body of the code you are trying to traverse.
The return type is t.Identifier
.
# definition
addUDFHelper(name: string): t.Identifier | Array<t.Identifier>
# example
visitor: {
Program(path){
// e.g.) identifier is { type: 'Identifier', name: '_programHelper' }
// e.g.) identifier is [{ type: 'Identifier', name: '_programHelper' }, { type: 'Identifier', name: '_programHelper2' }]
const identifier = this.addUDFHelper("programHelper")
}
}
Remark
If the result comes back in an array, it means that a helper with the same name is defined between plugins. We recommend changing the name of the helper if possible.
# listUDFHelper
Returns a list of helpers you have defined.
The return type is UDFHelperList
.
# definition
listUDFHelper(): UDFHelperList
# types
type UDFHelperList = {
available: string[];
unavailable: string[];
};
# example
Below is an example when a helper called objectWithoutProperties
defined in @babel/helpers is defined as UDF.
objectWithoutProperties
cannot be used as UDFHelpers, so it returns as unavailable.
visitor: {
Program(path){
/**
* e.g.) result is
*
* {
* "available": [
* "parentHelper",
* "child1",
* "child2",
* "grandchild",
* ],
* "unavailable": [
* "objectWithoutPropertiesLoose",
* "objectWithoutProperties",
* ],
* }
*
*
*/
const result = this.listUDFHelper()
}
}
# clearAllUDFHelpers
Initialize the store that holds helpers read by useDangerousUDFHelpers
.
After doing this, the UDF helpers will not be recognized and you will not be able to use addUDFHelper
or listUDFHelper
.
If you run useDangerousUDFHelpers(this, { helpers })
again, you will be able to use it again.
# definition
funciton clearAllUDFHelpers(): void
See Usage#Tips
# helper(tpl)
Provides methods to define UDF helpers.
# definition
helper(tpl: TemplateStringsArray): UDFHelper
# types
type UDFHelper = {
ast: () => t.Program;
};