oxc/crates/oxc_traverse/scripts/build.mjs
overlookmotel be87ca8419
feat(transform): oxc_traverse crate (#3169)
First part of #3152.

This adds a crate `oxc_traverse`, but doesn't connect it up to the
transformer or anything else yet.

I think we could merge this now - as it doesn't affect any code that's
in use - and then iterate on it to add scopes before using it in
transformer. Please see
https://github.com/oxc-project/oxc/pull/3152#issuecomment-2094965406 for
the broader picture.
2024-05-06 09:37:04 +08:00

40 lines
1.4 KiB
JavaScript

/*
* Codegen for `traverse`.
*
* Parses Rust AST type definitions from files in `crates/oxc_ast/src/ast`, and generates:
* - `src/traverse.rs`
* - `src/ancestor.rs`
* - `src/walk.rs`
*
* This is a quick-and-dirty version written in JS for speed of implementation.
* We should do this properly with a Rust build script using `syn` etc.
*/
import {writeFile} from 'fs/promises';
import {exec} from 'child_process';
import {join as pathJoin} from 'path';
import {fileURLToPath} from 'url';
import {promisify} from 'util';
import getTypesFromCode from './lib/parse.mjs';
import generateTraverseTraitCode from './lib/traverse.mjs';
import generateAncestorsCode from './lib/ancestor.mjs';
import generateWalkFunctionsCode from './lib/walk.mjs';
const execAsync = promisify(exec);
const PREAMBLE = '// Generated by `scripts/build.mjs`.\n\n';
const types = await getTypesFromCode();
const outputDirPath = pathJoin(fileURLToPath(import.meta.url), '../../src');
await writeToFile('traverse.rs', generateTraverseTraitCode(types));
await writeToFile('ancestor.rs', generateAncestorsCode(types));
await writeToFile('walk.rs', generateWalkFunctionsCode(types));
async function writeToFile(filename, code) {
code = `${PREAMBLE}${code}`;
const path = pathJoin(outputDirPath, filename);
console.log('Writing:', path);
await writeFile(path, code);
await execAsync(`rustfmt ${JSON.stringify(path)}`);
}