Skip to content

Commit ccdb5ea

Browse files
committed
refactor(ast_tools): refactor generating visitor type files (#14434)
Pure refactor. Move code around in codegen for ESTree visitor. Does not alter the output of the codegen, purely moves logic.
1 parent 3b26bf3 commit ccdb5ea

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

tasks/ast_tools/src/generators/estree_visit.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,14 @@ define_generator!(ESTreeVisitGenerator);
3737

3838
impl Generator for ESTreeVisitGenerator {
3939
fn generate_many(&self, _schema: &Schema, codegen: &Codegen) -> Vec<Output> {
40-
let Codes { walk_parser, walk_oxlint, visitor_keys, type_ids_map, mut visitor_type } =
41-
generate(codegen);
42-
43-
// Versions of `visitor_type` for parser and Oxlint import ESTree types from different places
44-
#[rustfmt::skip]
45-
let oxlint_visitor_type = format!("
46-
import * as ESTree from './types.d.ts';
47-
48-
{visitor_type}
49-
");
50-
51-
visitor_type.insert_str(0, "import * as ESTree from '@oxc-project/types';\n\n");
40+
let Codes {
41+
walk_parser,
42+
walk_oxlint,
43+
visitor_keys,
44+
type_ids_map,
45+
visitor_type_parser,
46+
visitor_type_oxlint,
47+
} = generate(codegen);
5248

5349
vec![
5450
Output::Javascript {
@@ -65,7 +61,7 @@ impl Generator for ESTreeVisitGenerator {
6561
},
6662
Output::Javascript {
6763
path: format!("{NAPI_PARSER_PACKAGE_PATH}/generated/visit/visitor.d.ts"),
68-
code: visitor_type,
64+
code: visitor_type_parser,
6965
},
7066
Output::Javascript {
7167
path: format!("{OXLINT_APP_PATH}/src-js/generated/walk.js"),
@@ -83,7 +79,7 @@ impl Generator for ESTreeVisitGenerator {
8379
},
8480
Output::Javascript {
8581
path: format!("{OXLINT_APP_PATH}/src-js/generated/visitor.d.ts"),
86-
code: oxlint_visitor_type,
82+
code: visitor_type_oxlint,
8783
},
8884
]
8985
}
@@ -95,7 +91,8 @@ struct Codes {
9591
walk_oxlint: String,
9692
visitor_keys: String,
9793
type_ids_map: String,
98-
visitor_type: String,
94+
visitor_type_parser: String,
95+
visitor_type_oxlint: String,
9996
}
10097

10198
/// Details of a node's name and visitor keys.
@@ -215,7 +212,7 @@ fn generate(codegen: &Codegen) -> Codes {
215212
// Leaf nodes
216213
");
217214

218-
let mut visitor_type = string!("export interface VisitorObject {\n");
215+
let mut visitor_type = string!("");
219216

220217
let mut leaf_nodes_count = None;
221218
for (node_id, node) in nodes.iter_enumerated() {
@@ -384,7 +381,31 @@ fn generate(codegen: &Codegen) -> Codes {
384381
export const LEAF_NODE_TYPES_COUNT = {leaf_nodes_count};
385382
");
386383

387-
visitor_type.push('}');
384+
// Versions of `visitor.d.ts` for parser and Oxlint import ESTree types from different places
385+
#[rustfmt::skip]
386+
let visitor_type_parser = format!("
387+
import * as ESTree from '@oxc-project/types';
388+
389+
export interface VisitorObject {{
390+
{visitor_type}
391+
}}
392+
");
393+
394+
#[rustfmt::skip]
395+
let visitor_type_oxlint = format!("
396+
import * as ESTree from './types.d.ts';
388397
389-
Codes { walk_parser, walk_oxlint, visitor_keys, type_ids_map, visitor_type }
398+
export interface VisitorObject {{
399+
{visitor_type}
400+
}}
401+
");
402+
403+
Codes {
404+
walk_parser,
405+
walk_oxlint,
406+
visitor_keys,
407+
type_ids_map,
408+
visitor_type_parser,
409+
visitor_type_oxlint,
410+
}
390411
}

0 commit comments

Comments
 (0)