Remove node wrapper around NonCodeMeta

Trying to fix TS unit test errors deserializing JSON AST in Rust.
This commit is contained in:
Jonathan Tran
2024-10-28 15:55:25 -04:00
parent 5aef2b72cb
commit 7fd9eb3322
5 changed files with 20 additions and 25 deletions

View File

@ -41,8 +41,6 @@ export class KclManager {
nonCodeMeta: { nonCodeMeta: {
nonCodeNodes: {}, nonCodeNodes: {},
startNodes: [], startNodes: [],
start: 0,
end: 0,
}, },
} }
private _execState: ExecState = emptyExecState() private _execState: ExecState = emptyExecState()
@ -209,8 +207,6 @@ export class KclManager {
nonCodeMeta: { nonCodeMeta: {
nonCodeNodes: {}, nonCodeNodes: {},
startNodes: [], startNodes: [],
start: 0,
end: 0,
}, },
} }
} }

View File

@ -124,7 +124,7 @@ describe('Testing addSketchTo', () => {
body: [], body: [],
start: 0, start: 0,
end: 0, end: 0,
nonCodeMeta: { nonCodeNodes: {}, startNodes: [], start: 0, end: 0 }, nonCodeMeta: { nonCodeNodes: {}, startNodes: [] },
}, },
'yz' 'yz'
) )

View File

@ -127,7 +127,7 @@ pub type NodeRef<'a, T> = &'a UnboxedNode<T>;
pub struct Program { pub struct Program {
pub body: Vec<BodyItem>, pub body: Vec<BodyItem>,
#[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")] #[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")]
pub non_code_meta: UnboxedNode<NonCodeMeta>, pub non_code_meta: NonCodeMeta,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)] #[ts(optional)]
@ -1847,7 +1847,7 @@ impl From<UnboxedNode<PipeSubstitution>> for Expr {
pub struct ArrayExpression { pub struct ArrayExpression {
pub elements: Vec<Expr>, pub elements: Vec<Expr>,
#[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")] #[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")]
pub non_code_meta: UnboxedNode<NonCodeMeta>, pub non_code_meta: NonCodeMeta,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)] #[ts(optional)]
@ -1984,7 +1984,7 @@ impl ArrayRangeExpression {
pub struct ObjectExpression { pub struct ObjectExpression {
pub properties: NodeList<ObjectProperty>, pub properties: NodeList<ObjectProperty>,
#[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")] #[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")]
pub non_code_meta: UnboxedNode<NonCodeMeta>, pub non_code_meta: NonCodeMeta,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)] #[ts(optional)]
@ -2492,7 +2492,7 @@ pub struct PipeExpression {
// The rest will be CallExpression, and the AST type should reflect this. // The rest will be CallExpression, and the AST type should reflect this.
pub body: Vec<Expr>, pub body: Vec<Expr>,
#[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")] #[serde(default, skip_serializing_if = "NonCodeMeta::is_empty")]
pub non_code_meta: UnboxedNode<NonCodeMeta>, pub non_code_meta: NonCodeMeta,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)] #[ts(optional)]

View File

@ -45,7 +45,7 @@ fn program(i: TokenSlice) -> PResult<UnboxedNode<Program>> {
// Add the shebang to the non-code meta. // Add the shebang to the non-code meta.
if let Some(shebang) = shebang { if let Some(shebang) = shebang {
out.non_code_meta.inner.start_nodes.insert(0, shebang); out.non_code_meta.start_nodes.insert(0, shebang);
} }
// Match original parser behaviour, for now. // Match original parser behaviour, for now.
// Once this is merged and stable, consider changing this as I think it's more accurate // Once this is merged and stable, consider changing this as I think it's more accurate
@ -136,7 +136,7 @@ fn non_code_node_no_leading_whitespace(i: TokenSlice) -> PResult<UnboxedNode<Non
} }
fn pipe_expression(i: TokenSlice) -> PResult<UnboxedNode<PipeExpression>> { fn pipe_expression(i: TokenSlice) -> PResult<UnboxedNode<PipeExpression>> {
let mut non_code_meta = UnboxedNode::<NonCodeMeta>::default(); let mut non_code_meta = NonCodeMeta::default();
let (head, noncode): (_, Vec<_>) = terminated( let (head, noncode): (_, Vec<_>) = terminated(
( (
expression_but_not_pipe, expression_but_not_pipe,
@ -544,11 +544,11 @@ pub(crate) fn array_elem_by_elem(i: TokenSlice) -> PResult<UnboxedNode<ArrayExpr
(elements, non_code_nodes) (elements, non_code_nodes)
}, },
); );
let non_code_meta = UnboxedNode::no_src(NonCodeMeta { let non_code_meta = NonCodeMeta {
non_code_nodes, non_code_nodes,
start_nodes: Vec::new(), start_nodes: Vec::new(),
digest: None, digest: None,
}); };
Ok(UnboxedNode::new( Ok(UnboxedNode::new(
ArrayExpression { ArrayExpression {
elements, elements,
@ -652,10 +652,10 @@ pub(crate) fn object(i: TokenSlice) -> PResult<UnboxedNode<ObjectExpression>> {
ignore_trailing_comma(i); ignore_trailing_comma(i);
ignore_whitespace(i); ignore_whitespace(i);
let end = close_brace(i)?.end; let end = close_brace(i)?.end;
let non_code_meta = UnboxedNode::no_src(NonCodeMeta { let non_code_meta = NonCodeMeta {
non_code_nodes, non_code_nodes,
..Default::default() ..Default::default()
}); };
Ok(UnboxedNode::new( Ok(UnboxedNode::new(
ObjectExpression { ObjectExpression {
properties, properties,
@ -1103,7 +1103,7 @@ pub fn function_body(i: TokenSlice) -> PResult<UnboxedNode<Program>> {
} }
let mut body = Vec::new(); let mut body = Vec::new();
let mut non_code_meta = UnboxedNode::<NonCodeMeta>::default(); let mut non_code_meta = NonCodeMeta::default();
let mut end = 0; let mut end = 0;
let mut start = leading_whitespace_start; let mut start = leading_whitespace_start;
for thing_in_body in things_within_body { for thing_in_body in things_within_body {
@ -1125,7 +1125,7 @@ pub fn function_body(i: TokenSlice) -> PResult<UnboxedNode<Program>> {
} }
end = nc.end; end = nc.end;
if body.is_empty() { if body.is_empty() {
non_code_meta.inner.start_nodes.push(nc); non_code_meta.start_nodes.push(nc);
} else { } else {
non_code_meta.insert(body.len() - 1, nc); non_code_meta.insert(body.len() - 1, nc);
} }
@ -2070,7 +2070,7 @@ mod tests {
let mut slice = tokens.as_slice(); let mut slice = tokens.as_slice();
let expr = function_expression.parse_next(&mut slice).unwrap(); let expr = function_expression.parse_next(&mut slice).unwrap();
assert_eq!(expr.params, vec![]); assert_eq!(expr.params, vec![]);
let comment_start = expr.body.non_code_meta.inner.start_nodes.first().unwrap(); let comment_start = expr.body.non_code_meta.start_nodes.first().unwrap();
let comment0 = &expr.body.non_code_meta.non_code_nodes.get(&0).unwrap()[0]; let comment0 = &expr.body.non_code_meta.non_code_nodes.get(&0).unwrap()[0];
let comment1 = &expr.body.non_code_meta.non_code_nodes.get(&1).unwrap()[0]; let comment1 = &expr.body.non_code_meta.non_code_nodes.get(&1).unwrap()[0];
assert_eq!(comment_start.value(), "comment 0"); assert_eq!(comment_start.value(), "comment 0");
@ -2099,7 +2099,7 @@ comment */
const mySk1 = startSketchAt([0, 0])"#; const mySk1 = startSketchAt([0, 0])"#;
let tokens = crate::token::lexer(test_program).unwrap(); let tokens = crate::token::lexer(test_program).unwrap();
let program = program.parse(&tokens).unwrap(); let program = program.parse(&tokens).unwrap();
let mut starting_comments = program.inner.non_code_meta.inner.start_nodes; let mut starting_comments = program.inner.non_code_meta.start_nodes;
assert_eq!(starting_comments.len(), 2); assert_eq!(starting_comments.len(), 2);
let start0 = starting_comments.remove(0); let start0 = starting_comments.remove(0);
let start1 = starting_comments.remove(0); let start1 = starting_comments.remove(0);
@ -2179,7 +2179,7 @@ const mySk1 = startSketchAt([0, 0])"#;
25, 25,
33, 33,
))], ))],
non_code_meta: UnboxedNode::no_src(NonCodeMeta { non_code_meta: NonCodeMeta {
non_code_nodes: Default::default(), non_code_nodes: Default::default(),
start_nodes: vec![UnboxedNode::new( start_nodes: vec![UnboxedNode::new(
NonCodeNode { NonCodeNode {
@ -2190,7 +2190,7 @@ const mySk1 = startSketchAt([0, 0])"#;
25, 25,
)], )],
digest: None, digest: None,
}), },
digest: None, digest: None,
}, },
7, 7,
@ -2256,7 +2256,7 @@ const mySk1 = startSketchAt([0, 0])"#;
0, 0,
20, 20,
)], )],
non_code_meta.inner.start_nodes, non_code_meta.start_nodes,
); );
assert_eq!( assert_eq!(
@ -2891,7 +2891,7 @@ const mySk1 = startSketchAt([0, 0])"#;
0, 0,
4, 4,
))], ))],
non_code_meta: UnboxedNode::<NonCodeMeta>::default(), non_code_meta: NonCodeMeta::default(),
digest: None, digest: None,
}, },
0, 0,

View File

@ -38,11 +38,10 @@ impl Program {
.fold(String::new(), |mut output, (index, recast_str)| { .fold(String::new(), |mut output, (index, recast_str)| {
let start_string = if index == 0 { let start_string = if index == 0 {
// We need to indent. // We need to indent.
if self.non_code_meta.inner.start_nodes.is_empty() { if self.non_code_meta.start_nodes.is_empty() {
indentation.to_string() indentation.to_string()
} else { } else {
self.non_code_meta self.non_code_meta
.inner
.start_nodes .start_nodes
.iter() .iter()
.map(|start| start.format(&indentation)) .map(|start| start.format(&indentation))