Skip to content

Commit

Permalink
feat(ast): remove serde skip for symbol_id and reference_id (#2220)
Browse files Browse the repository at this point in the history
We want to see symbol_id and reference_id in ast
  • Loading branch information
Dunqing authored Jan 30, 2024
1 parent 81e33a3 commit f673e41
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 54 deletions.
7 changes: 2 additions & 5 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<ReferenceId>>,
#[cfg_attr(feature = "serde", serde(skip))]
pub reference_flag: ReferenceFlag,
}

Expand All @@ -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<Option<SymbolId>>,
}

Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"]
8 changes: 6 additions & 2 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 2 additions & 42 deletions crates/oxc_semantic/src/node.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions crates/oxc_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions crates/oxc_syntax/src/node.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 3 additions & 1 deletion crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit f673e41

Please sign in to comment.