@babel/core#traverse TypeScript Examples
The following examples show how to use
@babel/core#traverse.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: runtimeHelpers.ts From vidact with MIT License | 6 votes |
function getInlineRuntimeHelpers() {
if (inlineFiles) {
return inlineFiles;
}
inlineFiles = new Map<string, any>();
for (const module of runtimeModules) {
const code = fs
.readFileSync(`${__dirname}/../../src/runtime/${module}.js`)
.toString();
const ast = parseSync(code, { babelrc: false });
traverse(ast, {
ExportNamedDeclaration(path) {
path.replaceWith(path.node.declaration);
},
ImportDeclaration(path) {
path.remove();
},
});
inlineFiles.set(module, ast);
}
return inlineFiles;
}
Example #2
Source File: index.ts From engine with MIT License | 5 votes |
typeGenerator = (code: string) => {
const codes = `
const foo = '123'
const a: producer = ({
_bam = bam,
foo = observe.bam.boo[foo]
}) => {}
`;
const result = parse(
codes,
{ plugins: ["@babel/plugin-syntax-typescript"] },
(err, ast) => {
if (err) {
throw err;
}
traverse(ast, {
VariableDeclarator: (path) => {
const id = path.node.id;
if (
!(t.isIdentifier(id) || t.isObjectPattern(id)) ||
!t.isTSTypeAnnotation(id.typeAnnotation) ||
!t.isTSTypeReference(id.typeAnnotation.typeAnnotation) ||
!t.isIdentifier(id.typeAnnotation.typeAnnotation.typeName) ||
!(
id.typeAnnotation.typeAnnotation.typeName.name ===
EngineKeywords.VIEW ||
id.typeAnnotation.typeAnnotation.typeName.name ===
EngineKeywords.PRODUCER
)
) {
return;
}
if (t.isObjectPattern(id)) {
return;
}
const node = path.node;
const fn = node.init as t.ArrowFunctionExpression;
const param = fn.params[0];
if (param && !t.isObjectPattern(param)) {
return;
}
const parsedParams = paramParser(Babel, param);
const result = compileParams(parsedParams);
console.log("ok", JSON.stringify(parsedParams, null, " "));
},
});
}
);
}