본문 바로가기

Babel4

babel, change ternary to If-else statements var babel = require("@babel/core"); // 삼항연산자를 if문으로 수정 var ternaryTemplate = babel.template(` (function() { if (TEST) { return CONSEQUENT; } return ALTERNATE; })(); `); var custom1 = function ({ types: t }) { return { visitor: { ConditionalExpression(path) { try { path.replaceWith( ternaryTemplate({ TEST: path.node.test, CONSEQUENT: path.node.consequent, ALTERNATE: path.node.alternate, }) ); } c.. 2022. 11. 29.
babel, add curly braces in arrow function 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.par.. 2022. 11. 29.
babel, add curly braces one-line if-statements var babel = require("@babel/core"); // if문에 add curly-brace (one line) var custom1 = function ({ types: t }) { return { visitor: { // if, else if IfStatement(path) { if (path.node.consequent.type !== 'BlockStatement') { var consequent = t.blockStatement([path.node.consequent]); path.replaceWith( t.ifStatement(path.node.test, consequent, path.node.alternate) ); } }, // else ExpressionStatement(pa.. 2022. 11. 29.
babel, remove comments var babel = require("@babel/core"); var sourceCode = ` var name = 'ysh'; /* comment1 */ /** * comment2 */ // comment 3 var dept = 'dev'; `; // 주석 제거 var parsedAst = babel.parseSync(sourceCode, { parserOpts: { attachComment: false }, }); sourceCode = babel.transformFromAstSync(parsedAst, sourceCode, { }).code; console.log(sourceCode); // 결과 // var name = 'ysh'; // var dept = 'dev'; 2022. 11. 29.