跳到主要内容

收集多语

2024年11月10日
柏拉文
越努力,越幸运

一、认识


二、语法


const babelTypes = require("@babel/types");
const babelParser = require("@babel/parser");
const babelTraverse = require("@babel/traverse");
const babelGenerator = require("@babel/generator");

const { parse } = babelParser;
const { default: traverse } = babelTraverse;
const { default: generate } = babelGenerator;
const { functionExpression, isBlockStatement, blockStatement } = babelTypes;

const collectLangMarks = (sourceCode) => {
const ast = parse(sourceCode);
const langMarks = [];
traverse(ast, {
CallExpression(path) {
const callee = path.node.callee;
if (babelTypes.isIdentifier(callee, { name: "lang" })) {
const [firstArg] = path.node.arguments;
if (babelTypes.isStringLiteral(firstArg)) {
langMarks.push(firstArg.value);
}
}
},
});
return langMarks;
};

const sourceCode = `var a = lang("嘻嘻"); var b = lang("哈哈"); var c = () => { var d = 3; return d;};`;
const result = collectLangMarks(sourceCode);
console.log(result);