Skip to content

Commit

Permalink
Renames and fix for #161
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave committed Jun 14, 2024
1 parent 57b5948 commit c656c1d
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 85 deletions.
12 changes: 6 additions & 6 deletions checker/src/synthesis/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -256,15 +256,15 @@ 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,
) => Publicity::Private,
_ => 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(
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion checker/src/synthesis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand Down
2 changes: 1 addition & 1 deletion checker/src/synthesis/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl SynthesiseInterfaceBehavior for OnToType {
(
if matches!(
key,
parser::PropertyKey::Ident(
parser::PropertyKey::Identifier(
_,
_,
parser::property_key::PublicOrPrivate::Private
Expand Down
2 changes: 1 addition & 1 deletion checker/src/synthesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, _) => {
Expand Down
63 changes: 9 additions & 54 deletions parser/examples/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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`
Expand Down Expand Up @@ -52,23 +49,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
..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::<source_map::NoPathMap>::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());
Expand All @@ -86,17 +70,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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);
Expand All @@ -111,33 +93,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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::<dyn std::error::Error>::from("not equal"))
}
}
Err(parse_err) => {
eprintln!("error parsing output: {output:?} from {module:?}");
Err(Box::<dyn std::error::Error>::from(parse_err))
}
};
}
}

if display_keywords {
Expand Down
2 changes: 1 addition & 1 deletion parser/src/declarations/classes/class_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion parser/src/expressions/assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl TryFrom<Expression> 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),
(),
Expand Down
4 changes: 2 additions & 2 deletions parser/src/expressions/object_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion parser/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ pub(crate) fn get_method_name<T: PropertyKeyKind + 'static>(
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)?)
Expand Down
18 changes: 9 additions & 9 deletions parser/src/property_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TSXToken, crate::TokenStart>,
reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
) -> ParseResult<(String, Span, Self)>;
Expand All @@ -37,7 +37,7 @@ pub struct AlwaysPublic;
// ";

impl PropertyKeyKind for AlwaysPublic {
fn parse_ident(
fn parse_identifier(
first: Token<TSXToken, crate::TokenStart>,
_reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
) -> ParseResult<(String, Span, Self)> {
Expand Down Expand Up @@ -68,7 +68,7 @@ pub enum PublicOrPrivate {
// ";

impl PropertyKeyKind for PublicOrPrivate {
fn parse_ident(
fn parse_identifier(
first: Token<TSXToken, crate::TokenStart>,
reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
) -> ParseResult<(String, Span, Self)> {
Expand All @@ -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<T: PropertyKeyKind> {
Ident(String, Span, T),
Identifier(String, Span, T),
StringLiteral(String, Quoted, Span),
NumberLiteral(NumberRepresentation, Span),
/// Includes anything in the `[...]` maybe a symbol
Expand All @@ -105,7 +105,7 @@ pub enum PropertyKey<T: PropertyKeyKind> {
impl<U: PropertyKeyKind> PropertyKey<U> {
pub fn is_private(&self) -> bool {
match self {
PropertyKey::Ident(_, _, p) => U::is_private(p),
PropertyKey::Identifier(_, _, p) => U::is_private(p),
_ => false,
}
}
Expand All @@ -114,7 +114,7 @@ impl<U: PropertyKeyKind> PropertyKey<U> {
impl<U: PropertyKeyKind> PartialEq<str> for PropertyKey<U> {
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,
Expand Down Expand Up @@ -151,8 +151,8 @@ impl<U: PropertyKeyKind> ASTNode for PropertyKey<U> {
// 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))
}
}
}
Expand All @@ -165,7 +165,7 @@ impl<U: PropertyKeyKind> ASTNode for PropertyKey<U> {
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());
Expand Down
2 changes: 1 addition & 1 deletion parser/src/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl ASTNode for VarVariableStatement {
}
};

Ok(VarVariableStatement { position, declarations })
Ok(VarVariableStatement { declarations, position })
}

fn to_string_from_buffer<T: source_map::ToString>(
Expand Down
3 changes: 3 additions & 0 deletions parser/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ impl TSXToken {
| TSXToken::LogicalNot
| TSXToken::LogicalAnd
| TSXToken::LogicalOr
| TSXToken::BitwiseNot
| TSXToken::BitwiseAnd
| TSXToken::BitwiseOr
| TSXToken::Multiply
| TSXToken::Add
| TSXToken::Subtract
Expand Down
13 changes: 9 additions & 4 deletions parser/src/types/type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
Expand Down Expand Up @@ -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(
Expand All @@ -496,7 +501,7 @@ impl TypeAnnotation {
},
);
Self::Symbol {
unique: false,
unique: true,
position,
#[cfg(feature = "extras")]
name,
Expand Down
2 changes: 1 addition & 1 deletion parser/src/variable_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl<T: DestructuringFieldInto> ASTNode for ObjectDestructuringField<T> {
};

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()
Expand Down
4 changes: 2 additions & 2 deletions parser/src/visiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// }
Expand All @@ -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,
// }
Expand Down

0 comments on commit c656c1d

Please sign in to comment.