From f673e41539c1587810bb21c6d4d3c47010693b31 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 30 Jan 2024 21:03:05 +0800 Subject: [PATCH] feat(ast): remove serde skip for symbol_id and reference_id (#2220) We want to see symbol_id and reference_id in ast --- crates/oxc_ast/src/ast/js.rs | 7 ++--- crates/oxc_semantic/Cargo.toml | 3 +- crates/oxc_semantic/src/builder.rs | 8 ++++-- crates/oxc_semantic/src/lib.rs | 2 +- crates/oxc_semantic/src/node.rs | 44 ++---------------------------- crates/oxc_syntax/Cargo.toml | 2 +- crates/oxc_syntax/src/lib.rs | 1 + crates/oxc_syntax/src/node.rs | 43 +++++++++++++++++++++++++++++ crates/oxc_wasm/src/lib.rs | 4 ++- 9 files changed, 60 insertions(+), 54 deletions(-) create mode 100644 crates/oxc_syntax/src/node.rs diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 36f524036bd73..f80eac349b695 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -307,15 +307,13 @@ impl IdentifierName { /// Identifier Reference #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))] +#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type", rename_all = "camelCase"))] #[cfg_attr(all(feature = "serde", feature = "wasm"), derive(tsify::Tsify))] pub struct IdentifierReference { #[cfg_attr(feature = "serde", serde(flatten))] pub span: Span, pub name: Atom, - #[cfg_attr(feature = "serde", serde(skip))] pub reference_id: Cell>, - #[cfg_attr(feature = "serde", serde(skip))] pub reference_flag: ReferenceFlag, } @@ -334,13 +332,12 @@ impl IdentifierReference { /// Binding Identifier #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))] +#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type", rename_all = "camelCase"))] #[cfg_attr(all(feature = "serde", feature = "wasm"), derive(tsify::Tsify))] pub struct BindingIdentifier { #[cfg_attr(feature = "serde", serde(flatten))] pub span: Span, pub name: Atom, - #[cfg_attr(feature = "serde", serde(skip))] pub symbol_id: Cell>, } diff --git a/crates/oxc_semantic/Cargo.toml b/crates/oxc_semantic/Cargo.toml index 69898063c1bc7..69af3f0bb2890 100644 --- a/crates/oxc_semantic/Cargo.toml +++ b/crates/oxc_semantic/Cargo.toml @@ -26,7 +26,6 @@ oxc_diagnostics = { workspace = true } oxc_index = { workspace = true } oxc_allocator = { workspace = true } -bitflags = { workspace = true } indexmap = { workspace = true } itertools = { workspace = true } phf = { workspace = true, features = ["macros"] } @@ -50,5 +49,5 @@ rustc-hash = { workspace = true } [features] default = [] -serde = ["dep:serde", "oxc_span/serde", "oxc_syntax/serde", "oxc_index/serde"] +serde = ["dep:serde", "oxc_span/serde", "oxc_syntax/serde"] wasm = ["dep:tsify", "dep:wasm-bindgen"] diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index aa5b1bfd7d7de..4514380375242 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -7,7 +7,11 @@ use itertools::Itertools; use oxc_ast::{ast::*, AstKind, Trivias, TriviasMap, Visit}; use oxc_diagnostics::Error; use oxc_span::{Atom, SourceType, Span}; -use oxc_syntax::{module_record::ModuleRecord, operator::AssignmentOperator}; +use oxc_syntax::{ + module_record::ModuleRecord, + node::{AstNodeId, NodeFlags}, + operator::AssignmentOperator, +}; use rustc_hash::FxHashMap; use crate::{ @@ -21,7 +25,7 @@ use crate::{ jsdoc::JSDocBuilder, label::LabelBuilder, module_record::ModuleRecordBuilder, - node::{AstNode, AstNodeId, AstNodes, NodeFlags}, + node::{AstNode, AstNodes}, pg::replicate_tree_to_leaves, reference::{Reference, ReferenceFlag, ReferenceId}, scope::{ScopeFlags, ScopeId, ScopeTree}, diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 910f470e0c36d..21939cfa5c347 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -38,7 +38,7 @@ pub use crate::{ ObjectPropertyAccessAssignmentValue, Register, UnaryExpressioneAssignmentValue, UpdateAssignmentValue, }, - node::{AstNode, AstNodeId, AstNodes, NodeFlags}, + node::{AstNode, AstNodeId, AstNodes}, reference::{Reference, ReferenceFlag, ReferenceId}, scope::ScopeTree, symbol::SymbolTable, diff --git a/crates/oxc_semantic/src/node.rs b/crates/oxc_semantic/src/node.rs index 611c97c8285e7..0849231ad2186 100644 --- a/crates/oxc_semantic/src/node.rs +++ b/crates/oxc_semantic/src/node.rs @@ -1,49 +1,9 @@ -use bitflags::bitflags; use oxc_ast::AstKind; -use oxc_index::{define_index_type, IndexVec}; +use oxc_index::IndexVec; use crate::scope::ScopeId; -define_index_type! { - pub struct AstNodeId = usize; -} - -#[cfg_attr( - all(feature = "serde", feature = "wasm"), - wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section) -)] -const TS_APPEND_CONTENT: &'static str = r#" -export type AstNodeId = number; -export type NodeFlags = { - JSDoc: 1, - Class: 2, - HasYield: 4 -}; -"#; - -bitflags! { - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct NodeFlags: u8 { - const JSDoc = 1 << 0; // If the Node has a JSDoc comment attached - const Class = 1 << 1; // If Node is inside a class - const HasYield = 1 << 2; // If function has yield statement - - } -} - -impl NodeFlags { - pub fn has_jsdoc(&self) -> bool { - self.contains(Self::JSDoc) - } - - pub fn has_class(&self) -> bool { - self.contains(Self::Class) - } - - pub fn has_yield(&self) -> bool { - self.contains(Self::HasYield) - } -} +pub use oxc_syntax::node::{AstNodeId, NodeFlags}; /// Semantic node contains all the semantic information about an ast node. #[derive(Debug, Clone, Copy)] diff --git a/crates/oxc_syntax/Cargo.toml b/crates/oxc_syntax/Cargo.toml index c00aa865413f6..067646bd9518a 100644 --- a/crates/oxc_syntax/Cargo.toml +++ b/crates/oxc_syntax/Cargo.toml @@ -34,5 +34,5 @@ tsify = { workspace = true, optional = true } wasm-bindgen = { workspace = true, optional = true } [features] default = [] -serde = ["dep:serde", "bitflags/serde"] +serde = ["dep:serde", "bitflags/serde", "oxc_index/serde"] wasm = ["dep:tsify", "dep:wasm-bindgen"] diff --git a/crates/oxc_syntax/src/lib.rs b/crates/oxc_syntax/src/lib.rs index ae3e37703594c..05308be66dec9 100644 --- a/crates/oxc_syntax/src/lib.rs +++ b/crates/oxc_syntax/src/lib.rs @@ -4,6 +4,7 @@ pub mod assumptions; pub mod class; pub mod identifier; pub mod module_record; +pub mod node; pub mod operator; pub mod precedence; pub mod reference; diff --git a/crates/oxc_syntax/src/node.rs b/crates/oxc_syntax/src/node.rs new file mode 100644 index 0000000000000..c9626fd891bdf --- /dev/null +++ b/crates/oxc_syntax/src/node.rs @@ -0,0 +1,43 @@ +use bitflags::bitflags; +use oxc_index::define_index_type; + +define_index_type! { + pub struct AstNodeId = usize; +} + +#[cfg_attr( + all(feature = "serde", feature = "wasm"), + wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section) +)] +const TS_APPEND_CONTENT: &'static str = r#" +export type AstNodeId = number; +export type NodeFlags = { + JSDoc: 1, + Class: 2, + HasYield: 4 +}; +"#; + +bitflags! { + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct NodeFlags: u8 { + const JSDoc = 1 << 0; // If the Node has a JSDoc comment attached + const Class = 1 << 1; // If Node is inside a class + const HasYield = 1 << 2; // If function has yield statement + + } +} + +impl NodeFlags { + pub fn has_jsdoc(&self) -> bool { + self.contains(Self::JSDoc) + } + + pub fn has_class(&self) -> bool { + self.contains(Self::Class) + } + + pub fn has_yield(&self) -> bool { + self.contains(Self::HasYield) + } +} diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index dbabd7031853c..f51b90a493ff7 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -139,7 +139,6 @@ impl Oxc { .parse(); self.save_diagnostics(ret.errors); - self.ast = ret.program.serialize(&self.serializer)?; self.ir = format!("{:#?}", ret.program.body).into(); let program = allocator.alloc(ret.program); @@ -164,7 +163,10 @@ impl Oxc { let linter_ret = Linter::default().run(lint_ctx); let diagnostics = linter_ret.into_iter().map(|e| e.error).collect(); self.save_diagnostics(diagnostics); + } else { + semantic_builder.build(program); } + self.ast = program.serialize(&self.serializer)?; if run_options.prettier_format() { let ret = Parser::new(&allocator, source_text, source_type)