mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
ce03ac12dc
Follow-on after #25720. Fix inaccuracies in JSDoc comments, as pointed out by AI review https://github.com/oxc-project/oxc/pull/25720#pullrequestreview-4944174621. For some reason, it got merged before my change to that PR was integrated.
54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
// A fast JavaScript code generator from an ESTree-compliant AST.
|
|
//
|
|
// A faithful port of Oxc's Rust `oxc_codegen` crate (pretty-printing mode).
|
|
// Output is byte-identical to `oxc_codegen` with default options (tab indentation, double quotes, comments off).
|
|
//
|
|
// The printer is one function per construct, split across this directory by area.
|
|
// `printStatement` in `statement.ts` and `printExpression` in `expression.ts` are the two dispatch
|
|
// points the rest hangs off.
|
|
//
|
|
// Reference: `oxc/crates/oxc_codegen/src/{gen.rs,lib.rs,str.rs,binary_expr_visitor.rs}`
|
|
|
|
import { debugAssert, typeAssertIs } from "../asserts.ts";
|
|
import { emitMappings } from "./source_map.ts";
|
|
import { printProgram, printStatement } from "./statement.ts";
|
|
|
|
import type { Options } from "./options.ts";
|
|
import type { State } from "../state.ts";
|
|
import type * as ESTree from "../../../../npm/oxc-types/types.d.ts";
|
|
|
|
/**
|
|
* Print `node`, returning an object including the generated code.
|
|
*
|
|
* One of these exists per build. The package entry point picks the build and calls it -
|
|
* callers of the package never reach this directly.
|
|
*
|
|
* The result is an object so that further outputs (a source map) can be added without a breaking change.
|
|
*
|
|
* @param state - Created by the entry point, so that all 4 builds share one class and so one object shape
|
|
* @param options - The same options `state` was created from, for the parts only this build acts on
|
|
* @returns Object holding the generated code
|
|
*/
|
|
export function printSync(node: ESTree.Node, state: State, options: Options): { code: string } {
|
|
if (node.type === "Program") {
|
|
printProgram(node, state);
|
|
} else {
|
|
typeAssertIs<ESTree.Statement>(node);
|
|
printStatement(node, state);
|
|
}
|
|
|
|
// This is removed by minifier in non-sourcemap builds
|
|
if (SOURCEMAPS) {
|
|
debugAssert(
|
|
options.sourceMap != null,
|
|
"`options.sourceMap` should be defined when `SOURCEMAPS` is `true`",
|
|
);
|
|
|
|
emitMappings(state, options.sourceMap);
|
|
}
|
|
|
|
return { code: state.output };
|
|
}
|
|
|
|
export type { Mapping, Options, Position, SourceMapGenerator } from "./options.ts";
|