What is @babel/helper-member-expression-to-functions?
The @babel/helper-member-expression-to-functions package is a Babel helper that transforms member expressions to functions. It is used internally by Babel plugins to handle transformations of member expressions in a consistent way, especially when dealing with computed properties and ensuring that the base object is only evaluated once.
What are @babel/helper-member-expression-to-functions's main functionalities?
Transforming member expressions into function calls
This feature allows Babel plugins to transform member expressions like `object.property` or `object['property']` into function calls that can handle the logic for computed properties and ensure the base object is evaluated once. This is particularly useful when compiling ES6 code to ES5.
memberExpressionToFunctions(path, visitor, state)
Other packages similar to @babel/helper-member-expression-to-functions
@babel/helper-function-name
This package is similar in that it is also a Babel helper used to transform functions. It helps with the naming of anonymous function expressions. While it does not directly transform member expressions, it is used in the process of transforming and naming functions within Babel plugins.
@babel/helper-replace-supers
This Babel helper is used to transform super calls in methods. It is similar to @babel/helper-member-expression-to-functions in that it deals with transforming parts of the language that involve property access, but it specifically targets super property calls.
@babel/helper-optimise-call-expression
This helper is used to optimize call expressions within Babel plugins. It is similar in the sense that it transforms call expressions, which can sometimes be the result of transforming member expressions with @babel/helper-member-expression-to-functions.
@babel/helper-member-expression-to-functions
Helper function to replace certain member expressions with function calls
Usage
Designed for internal Babel use.
Traverses the path
using the supplied visitor
and an augmented state
.
const visitor = {
MemberExpression(memberPath, state) {
if (someCondition(memberPath)) {
state.handle(memberPath);
}
},
};
const state = {
get(memberPath) {
return t.callExpression(
this.file.addHelper('superGet'),
[t.thisExpression(), memberPath.node.property]
);
},
set(memberPath, value) {
return t.callExpression(
this.file.addHelper('superSet'),
[t.thisExpression(), memberPath.node.property, value]
);
},
call(memberPath, args) {
return t.callExpression(
t.memberExpression(this.get(memberPath), t.identifier("apply")),
[t.thisExpression(), t.arrayExpression(args)]
);
},
memoise(memberPath) {
const { node } = memberPath;
if (node.computed) {
MEMOISED.set(node, ...);
}
},
someState: new Set(),
};
memberExpressionToFunctions(rootPath, visitor, state);