var babel = require("@babel/core");
var custom1 = function ({
types: t
}) {
return {
visitor: {
ArrowFunctionExpression(path) {
try {
if (path.node.body.type !== 'BlockStatement') {
var argument = path.node.body;
var body = t.returnStatement(argument);
var body2 = t.blockStatement([body]);
path.replaceWith(
// replace를 하면 다시 ArrowFunctionExpression 이벤트 발생.
t.arrowFunctionExpression(path.node.params, body2, path.node.async)
)
}
} catch (e) {
throw path.buildCodeFrameError("Error message here: " + e.message);
}
},
},
};
};
sourceCode = `
var a = () => 1;
var b = () => null;
`;
sourceCode = babel.transformSync(sourceCode, {
plugins: [custom1],
}).code;
console.log(sourceCode);
// 결과
// var a = () => {
// return 1;
// };
// var b = () => {
// return null;
// };
'web > babel' 카테고리의 다른 글
babel, change ternary to If-else statements (0) | 2022.11.29 |
---|---|
babel, add curly braces one-line if-statements (0) | 2022.11.29 |
babel, remove comments (0) | 2022.11.29 |