From c656c1d40a8204968dd85e8de446ad32ec04a582 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 14 Jun 2024 07:04:14 +0100 Subject: [PATCH] Renames and fix for #161 --- checker/src/synthesis/classes.rs | 12 ++-- checker/src/synthesis/functions.rs | 2 +- checker/src/synthesis/interfaces.rs | 2 +- checker/src/synthesis/mod.rs | 2 +- parser/examples/parse.rs | 63 +++---------------- .../src/declarations/classes/class_member.rs | 2 +- parser/src/expressions/assignments.rs | 2 +- parser/src/expressions/object_literal.rs | 4 +- parser/src/functions/mod.rs | 2 +- parser/src/property_key.rs | 18 +++--- parser/src/statements/mod.rs | 2 +- parser/src/tokens.rs | 3 + parser/src/types/type_annotations.rs | 13 ++-- parser/src/variable_fields.rs | 2 +- parser/src/visiting.rs | 4 +- 15 files changed, 48 insertions(+), 85 deletions(-) diff --git a/checker/src/synthesis/classes.rs b/checker/src/synthesis/classes.rs index 1ea7884d..9a9b9fc9 100644 --- a/checker/src/synthesis/classes.rs +++ b/checker/src/synthesis/classes.rs @@ -109,7 +109,7 @@ pub(super) fn synthesise_class_declaration< match &member.on { ClassMember::Method(false, method) => { let publicity = match method.name.get_ast_ref() { - ParserPropertyKey::Ident( + ParserPropertyKey::Identifier( _, _, parser::property_key::PublicOrPrivate::Private, @@ -133,7 +133,7 @@ pub(super) fn synthesise_class_declaration< } }; - let internal_marker = if let (true, ParserPropertyKey::Ident(name, _, _)) = + let internal_marker = if let (true, ParserPropertyKey::Identifier(name, _, _)) = (is_declare, method.name.get_ast_ref()) { get_internal_function_effect_from_decorators( @@ -179,7 +179,7 @@ pub(super) fn synthesise_class_declaration< } ClassMember::Property(false, property) => { let publicity = match property.key.get_ast_ref() { - ParserPropertyKey::Ident( + ParserPropertyKey::Identifier( _, _, parser::property_key::PublicOrPrivate::Private, @@ -256,7 +256,7 @@ pub(super) fn synthesise_class_declaration< match &member.on { ClassMember::Method(true, method) => { let publicity_kind = match method.name.get_ast_ref() { - ParserPropertyKey::Ident( + ParserPropertyKey::Identifier( _, _, parser::property_key::PublicOrPrivate::Private, @@ -264,7 +264,7 @@ pub(super) fn synthesise_class_declaration< _ => Publicity::Public, }; - let internal_marker = if let (true, ParserPropertyKey::Ident(name, _, _)) = + let internal_marker = if let (true, ParserPropertyKey::Identifier(name, _, _)) = (is_declare, method.name.get_ast_ref()) { get_internal_function_effect_from_decorators( @@ -321,7 +321,7 @@ pub(super) fn synthesise_class_declaration< } ClassMember::Property(true, property) => { let publicity_kind = match property.key.get_ast_ref() { - ParserPropertyKey::Ident( + ParserPropertyKey::Identifier( _, _, parser::property_key::PublicOrPrivate::Private, diff --git a/checker/src/synthesis/functions.rs b/checker/src/synthesis/functions.rs index 2620b55f..a6d0666f 100644 --- a/checker/src/synthesis/functions.rs +++ b/checker/src/synthesis/functions.rs @@ -531,7 +531,7 @@ pub(super) fn variable_field_to_string(param: &VariableField) -> String { } parser::ObjectDestructuringField::Map { from, name, .. } => { match from { - parser::PropertyKey::Ident(ident, _, _) => { + parser::PropertyKey::Identifier(ident, _, _) => { buf.push_str(ident); } parser::PropertyKey::StringLiteral(_, _, _) => todo!(), diff --git a/checker/src/synthesis/interfaces.rs b/checker/src/synthesis/interfaces.rs index ac5e8ff3..cf006657 100644 --- a/checker/src/synthesis/interfaces.rs +++ b/checker/src/synthesis/interfaces.rs @@ -61,7 +61,7 @@ impl SynthesiseInterfaceBehavior for OnToType { ( if matches!( key, - parser::PropertyKey::Ident( + parser::PropertyKey::Identifier( _, _, parser::property_key::PublicOrPrivate::Private diff --git a/checker/src/synthesis/mod.rs b/checker/src/synthesis/mod.rs index 8a07daf0..c3254f74 100644 --- a/checker/src/synthesis/mod.rs +++ b/checker/src/synthesis/mod.rs @@ -206,7 +206,7 @@ pub(super) fn parser_property_key_to_checker_property_key< perform_side_effect_computed: bool, ) -> PropertyKey<'static> { match property_key { - ParserPropertyKey::StringLiteral(value, ..) | ParserPropertyKey::Ident(value, ..) => { + ParserPropertyKey::StringLiteral(value, ..) | ParserPropertyKey::Identifier(value, ..) => { PropertyKey::String(std::borrow::Cow::Owned(value.clone())) } ParserPropertyKey::NumberLiteral(number, _) => { diff --git a/parser/examples/parse.rs b/parser/examples/parse.rs index 038b9e81..bc3457dc 100644 --- a/parser/examples/parse.rs +++ b/parser/examples/parse.rs @@ -22,9 +22,6 @@ fn main() -> Result<(), Box> { let type_definition_module = args.iter().any(|item| item == "--type-definition-module"); let type_annotations = !args.iter().any(|item| item == "--no-type-annotations"); - // `parse -> print -> parse -> print` and compare difference (same as fuzzing process) - let double = args.iter().any(|item| item == "--double"); - let print_ast = args.iter().any(|item| item == "--ast"); // double => pretty and render thus `|| double` @@ -52,23 +49,10 @@ fn main() -> Result<(), Box> { ..ParseOptions::default() }; - // let parse_options = ParseOptions { - // stack_size: Some(STACK_SIZE_MB * 1024 * 1024), - // jsx: false, - // type_annotations: false, - // retain_blank_lines: true, - // ..Default::default() - // }; - let mut fs = source_map::MapFileStore::::default(); let source = std::fs::read_to_string(path.clone())?; - // let source = String::from_utf8([0x2f, 0x8, 0x2f, 0xa].to_vec()).unwrap(); - // let source = "if (this) return; else switch (this) {\n}\n\n\n".to_string(); - // let source = ";\n\n".to_string(); - // let source = "{};;;".to_string(); - let source_id = fs.new_source_id(path.into(), source.clone()); eprintln!("parsing {:?} bytes", source.len()); @@ -86,17 +70,15 @@ fn main() -> Result<(), Box> { if source_maps || render_output || double || render_timings { let now = Instant::now(); - // let to_string_options = ToStringOptions { - // expect_markers: true, - // include_type_annotations: type_annotations, - // pretty, - // comments: if pretty { Comments::All } else { Comments::None }, - // // 60 is temp - // max_line_length: if pretty { 60 } else { u8::MAX }, - // ..Default::default() - // }; - - let to_string_options = ToStringOptions::default(); + let to_string_options = ToStringOptions { + expect_markers: true, + include_type_annotations: type_annotations, + pretty, + comments: if pretty { Comments::All } else { Comments::None }, + // 60 is temp + max_line_length: if pretty { 60 } else { u8::MAX }, + ..Default::default() + }; let (output, source_map) = module.to_string_with_source_map(&to_string_options, source_id, &fs); @@ -111,33 +93,6 @@ fn main() -> Result<(), Box> { if render_output { println!("{output}"); } - - if double { - let result2 = - Module::from_string_with_options(output.clone(), parse_options, None); - return match result2 { - Ok((module2, _state)) => { - let output2 = module2 - .to_string_with_source_map(&to_string_options, source_id, &fs) - .0; - - if output == output2 { - eprintln!("{output:?} == {output2:?}"); - eprintln!("re-parse was equal ✅"); - Ok(()) - } else { - eprintln!("{output:?} != {output2:?} (original = {source:?})"); - eprintln!("initial {:?}", module); - eprintln!("re-parsed {:?}", module2); - Err(Box::::from("not equal")) - } - } - Err(parse_err) => { - eprintln!("error parsing output: {output:?} from {module:?}"); - Err(Box::::from(parse_err)) - } - }; - } } if display_keywords { diff --git a/parser/src/declarations/classes/class_member.rs b/parser/src/declarations/classes/class_member.rs index 39b96490..07032a07 100644 --- a/parser/src/declarations/classes/class_member.rs +++ b/parser/src/declarations/classes/class_member.rs @@ -322,7 +322,7 @@ impl FunctionBased for ClassFunctionBase { } fn get_name(name: &Self::Name) -> Option<&str> { - if let PropertyKey::Ident(name, ..) = name.get_ast_ref() { + if let PropertyKey::Identifier(name, ..) = name.get_ast_ref() { Some(name.as_str()) } else { None diff --git a/parser/src/expressions/assignments.rs b/parser/src/expressions/assignments.rs index 7ceeaadc..3c112c2b 100644 --- a/parser/src/expressions/assignments.rs +++ b/parser/src/expressions/assignments.rs @@ -300,7 +300,7 @@ impl TryFrom for LHSOfAssignment { } ObjectLiteralMember::Property { assignment, key, position, value } => { if assignment { - if let PropertyKey::Ident(name, pos, _) = key.get_ast() { + if let PropertyKey::Identifier(name, pos, _) = key.get_ast() { ObjectDestructuringField::Name( crate::VariableIdentifier::Standard(name, pos), (), diff --git a/parser/src/expressions/object_literal.rs b/parser/src/expressions/object_literal.rs index 4673c99f..57484b05 100644 --- a/parser/src/expressions/object_literal.rs +++ b/parser/src/expressions/object_literal.rs @@ -141,7 +141,7 @@ impl FunctionBased for ObjectLiteralMethodBase { } fn get_name(name: &Self::Name) -> Option<&str> { - if let PropertyKey::Ident(name, ..) = name.get_ast_ref() { + if let PropertyKey::Identifier(name, ..) = name.get_ast_ref() { Some(name.as_str()) } else { None @@ -248,7 +248,7 @@ impl ASTNode for ObjectLiteralMember { return crate::throw_unexpected_token(reader, &[TSXToken::OpenParentheses]); } if let Some(Token(TSXToken::Comma | TSXToken::CloseBrace, _)) = reader.peek() { - if let PropertyKey::Ident(name, position, _) = key.get_ast() { + if let PropertyKey::Identifier(name, position, _) = key.get_ast() { Ok(Self::Shorthand(name, position)) } else { let token = reader.next().ok_or_else(parse_lexing_error)?; diff --git a/parser/src/functions/mod.rs b/parser/src/functions/mod.rs index 8de28f03..271cc536 100644 --- a/parser/src/functions/mod.rs +++ b/parser/src/functions/mod.rs @@ -672,7 +672,7 @@ pub(crate) fn get_method_name( let new_public = T::new_public(); ( MethodHeader::default(), - WithComment::None(PropertyKey::Ident(name.to_owned(), position, new_public)), + WithComment::None(PropertyKey::Identifier(name.to_owned(), position, new_public)), ) } else { (MethodHeader::from_reader(reader), WithComment::from_reader(reader, state, options)?) diff --git a/parser/src/property_key.rs b/parser/src/property_key.rs index 98c4b98f..386d4ed9 100644 --- a/parser/src/property_key.rs +++ b/parser/src/property_key.rs @@ -15,7 +15,7 @@ use crate::{ }; pub trait PropertyKeyKind: Debug + PartialEq + Eq + Clone + Sized + Send + Sync + 'static { - fn parse_ident( + fn parse_identifier( first: Token, reader: &mut impl TokenReader, ) -> ParseResult<(String, Span, Self)>; @@ -37,7 +37,7 @@ pub struct AlwaysPublic; // "; impl PropertyKeyKind for AlwaysPublic { - fn parse_ident( + fn parse_identifier( first: Token, _reader: &mut impl TokenReader, ) -> ParseResult<(String, Span, Self)> { @@ -68,7 +68,7 @@ pub enum PublicOrPrivate { // "; impl PropertyKeyKind for PublicOrPrivate { - fn parse_ident( + fn parse_identifier( first: Token, reader: &mut impl TokenReader, ) -> ParseResult<(String, Span, Self)> { @@ -95,7 +95,7 @@ impl PropertyKeyKind for PublicOrPrivate { #[derive(Debug, PartialEq, Eq, Clone, get_field_by_type::GetFieldByType)] #[get_field_by_type_target(Span)] pub enum PropertyKey { - Ident(String, Span, T), + Identifier(String, Span, T), StringLiteral(String, Quoted, Span), NumberLiteral(NumberRepresentation, Span), /// Includes anything in the `[...]` maybe a symbol @@ -105,7 +105,7 @@ pub enum PropertyKey { impl PropertyKey { pub fn is_private(&self) -> bool { match self { - PropertyKey::Ident(_, _, p) => U::is_private(p), + PropertyKey::Identifier(_, _, p) => U::is_private(p), _ => false, } } @@ -114,7 +114,7 @@ impl PropertyKey { impl PartialEq for PropertyKey { fn eq(&self, other: &str) -> bool { match self { - PropertyKey::Ident(name, _, _) | PropertyKey::StringLiteral(name, _, _) => { + PropertyKey::Identifier(name, _, _) | PropertyKey::StringLiteral(name, _, _) => { name == other } PropertyKey::NumberLiteral(_, _) | PropertyKey::Computed(_, _) => false, @@ -151,8 +151,8 @@ impl ASTNode for PropertyKey { // TODO could add marker? Self::from_reader(reader, state, options) } else { - let (name, position, private) = U::parse_ident(token, reader)?; - Ok(Self::Ident(name, position, private)) + let (name, position, private) = U::parse_identifier(token, reader)?; + Ok(Self::Identifier(name, position, private)) } } } @@ -165,7 +165,7 @@ impl ASTNode for PropertyKey { local: crate::LocalToStringInformation, ) { match self { - Self::Ident(ident, _pos, _) => buf.push_str(ident.as_str()), + Self::Identifier(ident, _pos, _) => buf.push_str(ident.as_str()), Self::NumberLiteral(number, _) => buf.push_str(&number.to_string()), Self::StringLiteral(string, quoted, _) => { buf.push(quoted.as_char()); diff --git a/parser/src/statements/mod.rs b/parser/src/statements/mod.rs index 6599fc07..4bfd06a6 100644 --- a/parser/src/statements/mod.rs +++ b/parser/src/statements/mod.rs @@ -375,7 +375,7 @@ impl ASTNode for VarVariableStatement { } }; - Ok(VarVariableStatement { position, declarations }) + Ok(VarVariableStatement { declarations, position }) } fn to_string_from_buffer( diff --git a/parser/src/tokens.rs b/parser/src/tokens.rs index 6dc7427d..05e2cf65 100644 --- a/parser/src/tokens.rs +++ b/parser/src/tokens.rs @@ -417,6 +417,9 @@ impl TSXToken { | TSXToken::LogicalNot | TSXToken::LogicalAnd | TSXToken::LogicalOr + | TSXToken::BitwiseNot + | TSXToken::BitwiseAnd + | TSXToken::BitwiseOr | TSXToken::Multiply | TSXToken::Add | TSXToken::Subtract diff --git a/parser/src/types/type_annotations.rs b/parser/src/types/type_annotations.rs index e90e3068..ef27e6c9 100644 --- a/parser/src/types/type_annotations.rs +++ b/parser/src/types/type_annotations.rs @@ -350,7 +350,12 @@ impl ASTNode for TypeAnnotation { } buf.push('`'); } - Self::Symbol { .. } => buf.push_str("symbol"), + Self::Symbol { unique, .. } => { + if *unique { + buf.push_str("unique "); + } + buf.push_str("symbol"); + } Self::Extends { item, extends, .. } => { item.to_string_from_buffer(buf, options, local); buf.push_str(" extends "); @@ -482,8 +487,8 @@ impl TypeAnnotation { } } t @ Token(TSXToken::Keyword(TSXKeyword::Unique), _) => { - let sym_pos = reader.expect_next(TSXToken::Keyword(TSXKeyword::Symbol))?; - let position = t.get_span().union(sym_pos.with_length("symbol".len())); + let kw_pos = reader.expect_next(TSXToken::Keyword(TSXKeyword::Symbol))?; + let position = t.get_span().union(kw_pos.with_length("symbol".len())); #[cfg(feature = "extras")] let name = reader.conditional_next(|t| matches!(t, TSXToken::StringLiteral(..))).map( @@ -496,7 +501,7 @@ impl TypeAnnotation { }, ); Self::Symbol { - unique: false, + unique: true, position, #[cfg(feature = "extras")] name, diff --git a/parser/src/variable_fields.rs b/parser/src/variable_fields.rs index da74b0f0..a1fce75c 100644 --- a/parser/src/variable_fields.rs +++ b/parser/src/variable_fields.rs @@ -356,7 +356,7 @@ impl ASTNode for ObjectDestructuringField { }; Ok(Self::Map { from: key, annotation, name, default_value, position }) - } else if let PropertyKey::Ident(name, key_pos, _) = key { + } else if let PropertyKey::Identifier(name, key_pos, _) = key { let default_value = reader .conditional_next(|t| matches!(t, TSXToken::Assign)) .is_some() diff --git a/parser/src/visiting.rs b/parser/src/visiting.rs index 5b9a6ae6..223dfde6 100644 --- a/parser/src/visiting.rs +++ b/parser/src/visiting.rs @@ -389,7 +389,7 @@ mod structures { // Just want variable names None // match property.get_ast_ref() { - // PropertyKey::Ident(ident, _, _) + // PropertyKey::Identifier(ident, _, _) // | PropertyKey::StringLiteral(ident, _, _) => Some(ident.as_str()), // PropertyKey::NumberLiteral(_, _) | PropertyKey::Computed(_, _) => None, // } @@ -398,7 +398,7 @@ mod structures { // Just want variable names None // match property.get_ast_ref() { - // PropertyKey::Ident(ident, _, _) + // PropertyKey::Identifier(ident, _, _) // | PropertyKey::StringLiteral(ident, _, _) => Some(ident.as_str()), // PropertyKey::NumberLiteral(_, _) | PropertyKey::Computed(_, _) => None, // }