From cbcbe17f3466e4b27147fba5cb50d0127baec9ec Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 19 Nov 2024 11:58:02 +0000 Subject: [PATCH] Some small fixes + clippy --- parser/examples/code_blocks_to_script.rs | 10 ++-- parser/examples/duplicate_block.rs | 8 +-- parser/examples/new.rs | 8 +-- parser/examples/parse.rs | 23 ++++++--- .../src/declarations/classes/class_member.rs | 2 +- parser/src/declarations/mod.rs | 1 + parser/src/expressions/mod.rs | 24 +++++---- parser/src/functions/mod.rs | 51 ++++++++++++------- parser/src/lib.rs | 2 +- parser/tests/expressions.rs | 8 +-- 10 files changed, 84 insertions(+), 53 deletions(-) diff --git a/parser/examples/code_blocks_to_script.rs b/parser/examples/code_blocks_to_script.rs index 4abf44f4..a06c7c3d 100644 --- a/parser/examples/code_blocks_to_script.rs +++ b/parser/examples/code_blocks_to_script.rs @@ -34,7 +34,7 @@ fn main() -> Result<(), Box> { } }); - let content = std::fs::read_to_string(&path)?; + let content = std::fs::read_to_string(path)?; let filters: Vec<&str> = vec!["import", "export"]; @@ -52,7 +52,7 @@ fn main() -> Result<(), Box> { String::new(), |mut a, s| { a.push_str(s); - a.push_str("\n"); + a.push('\n'); a }, ); @@ -82,7 +82,7 @@ fn main() -> Result<(), Box> { let under = PathBuf::from(under); for (header, code) in blocks { let mut name = heading_to_rust_identifier(&header); - name.push_str("."); + name.push('.'); name.push_str(&extension); let mut file = std::fs::File::create(under.join(name))?; // Fix for FLow @@ -247,7 +247,7 @@ fn main() -> Result<(), Box> { } for line in code.lines() { block.push_str("\n\t"); - block.push_str(&line); + block.push_str(line); } // If the block is not terminated, it can change the parsing of the next one if block.ends_with(')') { @@ -262,7 +262,7 @@ fn main() -> Result<(), Box> { } for line in code.lines() { block.push_str("\n\t"); - block.push_str(&line); + block.push_str(line); } block.push('\n'); final_blocks.push((names, block)); diff --git a/parser/examples/duplicate_block.rs b/parser/examples/duplicate_block.rs index 21183a2f..11c4c645 100644 --- a/parser/examples/duplicate_block.rs +++ b/parser/examples/duplicate_block.rs @@ -3,7 +3,7 @@ use ezno_parser::{ visiting::{Chain, ImmutableVariableOrProperty, VisitOptions, Visitor, Visitors}, ASTNode, Declaration, Expression, Module, StatementOrDeclaration, VariableField, }; -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; struct Offsets { pub offsets: Vec, @@ -16,7 +16,7 @@ struct Offsets { /// TODO abstract to library /// TODO do for funtions and types fn get_top_level_identifiers(m: &Module) -> (HashSet, HashSet) { - let (mut variables, mut types): (HashSet<_>, HashSet<_>) = Default::default(); + let (mut variables, types): (HashSet<_>, HashSet<_>) = Default::default(); for item in &m.items { match item { StatementOrDeclaration::Declaration(Declaration::Variable(variable)) => { @@ -98,7 +98,7 @@ let z = 6; total.reserve(rest.len() * (SIZE - 1)); for i in 1..SIZE { - let name = format!("{:03}", i); + let name = format!("{i:03}"); for offset in offsets.offsets.iter().copied() { let range = offset as usize..(offset as usize + 3); rest.replace_range(range, &name); @@ -107,7 +107,7 @@ let z = 6; total.push_str(&rest); } - eprintln!("{}", total); + eprintln!("{total}"); } /// TODO this could be collected in the same process as above diff --git a/parser/examples/new.rs b/parser/examples/new.rs index cd9cfc87..2202b5e6 100644 --- a/parser/examples/new.rs +++ b/parser/examples/new.rs @@ -3,23 +3,23 @@ fn main() { // new::Lexer - let s = r#"'Hello World' + class X { } + (a === 5) + something * 6 "#; + let s = r"'Hello World' + class X { } + (a === 5) + something * 6 "; let e = ezno_parser::ast::Expression::from_string(s.into(), Default::default()); assert!(e.is_ok()); // eprintln!("{:#?}", e); - let s = r#"function bunc(a, param): number { + let s = r"function bunc(a, param): number { if (a) { return (a: string) => {}; } return 2 - }"#; + }"; let e = ezno_parser::ast::Expression::from_string(s.into(), Default::default()); - eprintln!("{:#?}", e); + eprintln!("{e:#?}"); // let s = ""; } diff --git a/parser/examples/parse.rs b/parser/examples/parse.rs index 0c03dd7b..fb0758dc 100644 --- a/parser/examples/parse.rs +++ b/parser/examples/parse.rs @@ -32,8 +32,6 @@ fn main() -> Result<(), Box> { let to_string_output = args.iter().any(|item| item == "--to-string"); let pretty = args.iter().any(|item| item == "--pretty"); - let now = Instant::now(); - let parse_options = ParseOptions { comments, record_keyword_positions: display_keywords, @@ -77,6 +75,8 @@ fn main() -> Result<(), Box> { ) } +const EIGHT_MEGA_BYTES: usize = 8 * 1024 * 1024; + fn parse_path( path: &Path, timings: bool, @@ -93,7 +93,18 @@ fn parse_path( eprintln!("parsing {:?} ({:?} bytes)", path.display(), source.len()); let now = Instant::now(); - let result = Module::from_string_with_options(source.clone(), parse_options.clone(), None); + let mut local_parse_options = *parse_options; + if path.extension().and_then(std::ffi::OsStr::to_str).is_some_and(|ext| ext.ends_with("js")) { + local_parse_options.type_annotations = false; + } + + let on = source.clone(); + let result = std::thread::Builder::new() + .stack_size(EIGHT_MEGA_BYTES) + .spawn(move || Module::from_string_with_options(on, local_parse_options, None)) + .unwrap() + .join() + .unwrap(); match result { Ok((module, state)) => { @@ -127,13 +138,13 @@ fn parse_path( } if parse_imports { - for import in state.constant_imports.iter() { + for import in &state.constant_imports { // Don't reparse files (+ catches cycles) let resolved_path = path.parent().unwrap().join(import); if fs.get_paths().contains_key(&resolved_path) { continue; } - let _ = parse_path( + let () = parse_path( &resolved_path, timings, parse_imports, @@ -160,7 +171,7 @@ fn parse_path( line_column.column_start += 1; line_column.column_end += 1; } - eprintln!("error on {:?}", line_column); + eprintln!("error on {line_column:?}"); Err(Box::::from(parse_err)) } diff --git a/parser/src/declarations/classes/class_member.rs b/parser/src/declarations/classes/class_member.rs index d40c095d..2420ed50 100644 --- a/parser/src/declarations/classes/class_member.rs +++ b/parser/src/declarations/classes/class_member.rs @@ -316,7 +316,7 @@ impl FunctionBased for ClassConstructorBase { // } fn has_body(body: &Self::Body) -> bool { - body.0.is_some() + body.has_body() } fn header_and_name_from_reader( diff --git a/parser/src/declarations/mod.rs b/parser/src/declarations/mod.rs index 14e73a80..5dd7eb64 100644 --- a/parser/src/declarations/mod.rs +++ b/parser/src/declarations/mod.rs @@ -173,6 +173,7 @@ impl crate::ASTNode for Declaration { alias.position.start = start.0; Ok(Declaration::TypeAlias(alias)) } else { + #[cfg(feature = "extras")] if reader.is_keyword("namespace") { let mut namespace = crate::types::namespace::Namespace::from_reader(reader)?; namespace.is_declare = true; diff --git a/parser/src/expressions/mod.rs b/parser/src/expressions/mod.rs index ef43f2fb..0b6adf74 100644 --- a/parser/src/expressions/mod.rs +++ b/parser/src/expressions/mod.rs @@ -220,9 +220,9 @@ impl Expression { reader: &mut crate::new::Lexer, return_precedence: u8, ) -> ParseResult { - // if reader.head % 1000 == 0 { - // dbg!(reader.head); - // } + if reader.head % 1000 == 0 { + dbg!(reader.head); + } // TODO WIP if reader.get_options().partial_syntax { @@ -950,14 +950,18 @@ impl Expression { slice => unreachable!("{slice:?}"), }; top = Self::SpecialOperators(operation, position); - } else if reader.is_operator_advance("!") { - // if options.type_annotations - let position = top.get_position().union(reader.get_end()); - top = Self::SpecialOperators( - SpecialOperators::NonNullAssertion(Box::new(top)), - position, - ); } else { + #[cfg(feature = "extras")] + if reader.is_operator_advance("!") { + // if options.type_annotations + let position = top.get_position().union(reader.get_end()); + top = Self::SpecialOperators( + SpecialOperators::NonNullAssertion(Box::new(top)), + position, + ); + continue; + } + return Ok(top); } diff --git a/parser/src/functions/mod.rs b/parser/src/functions/mod.rs index d54e81cf..57614ce4 100644 --- a/parser/src/functions/mod.rs +++ b/parser/src/functions/mod.rs @@ -400,6 +400,7 @@ impl ASTNode for FunctionHeader { } fn from_reader(reader: &mut crate::new::Lexer) -> ParseResult { + #[cfg(feature = "extras")] fn parse_location(reader: &mut crate::new::Lexer) -> Option { if reader.is_keyword_advance("server") { Some(FunctionLocationModifier::Server) @@ -413,26 +414,28 @@ impl ASTNode for FunctionHeader { let start = reader.get_start(); let is_async = reader.is_operator_advance("async"); + #[cfg(feature = "extras")] if reader.is_keyword_advance("generator") { let location = parse_location(reader); let _ = reader.expect_keyword("function")?; - Ok(Self::ChadFunctionHeader { + return Ok(Self::ChadFunctionHeader { is_async, location, - // is_generator: true, position: start.union(reader.get_end()), - }) - } else { - let location = parse_location(reader); - let _ = reader.expect_keyword("function")?; - let is_generator = reader.is_operator_advance("*"); - Ok(Self::VirginFunctionHeader { - is_async, - location, - is_generator, - position: start.union(reader.get_end()), - }) + }); } + + #[cfg(feature = "extras")] + let location = parse_location(reader); + let _ = reader.expect_keyword("function")?; + let is_generator = reader.is_operator_advance("*"); + Ok(Self::VirginFunctionHeader { + is_async, + is_generator, + position: start.union(reader.get_end()), + #[cfg(feature = "extras")] + location, + }) } fn to_string_from_buffer( @@ -570,11 +573,11 @@ pub(crate) fn get_method_name( reader: &mut crate::new::Lexer, ) -> Result<(MethodHeader, WithComment>), crate::ParseError> { let after = reader.after_identifier(); - let function_header = if after.starts_with("<") - || after.starts_with("(") - || after.starts_with("}") - || after.starts_with(",") - || after.starts_with(":") + let function_header = if after.starts_with('<') + || after.starts_with('(') + || after.starts_with('}') + || after.starts_with(',') + || after.starts_with(':') { MethodHeader::default() } else { @@ -621,3 +624,15 @@ impl ASTNode for FunctionBody { } } } + +impl FunctionBody { + #[cfg(feature = "full-typescript")] + pub fn has_body(&self) { + self.0.is_some() + } + + #[cfg(not(feature = "full-typescript"))] + pub fn has_body(&self) { + true + } +} diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 16da2278..f0a34b14 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -341,7 +341,7 @@ impl ExpressionOrStatementPosition for StatementPosition { } fn has_function_body(body: &Self::FunctionBody) -> bool { - body.0.is_some() + body.has_body() } fn is_declare(&self) -> bool { diff --git a/parser/tests/expressions.rs b/parser/tests/expressions.rs index 8fe2a0d2..78565f5f 100644 --- a/parser/tests/expressions.rs +++ b/parser/tests/expressions.rs @@ -155,11 +155,11 @@ function Component(item) { #[test] fn regex_and_leading_decimal() { - let input = r#" + let input = r" for (const x in 0.4) {} for await (const [a] of 0.2) {} for (const result of /thing/) {} -"# +" .trim(); let module = Module::from_string(input.to_owned(), Default::default()).unwrap(); @@ -172,9 +172,9 @@ for (const result of /thing/) {} #[test] fn class_and_object_divides() { - let input = r#" + let input = r" const b = class Number {} / 2 -"# +" .trim(); let module = Module::from_string(input.to_owned(), Default::default()).unwrap();