-
-
Notifications
You must be signed in to change notification settings - Fork 787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest valid project names on InvalidProjectName #3913
Suggest valid project names on InvalidProjectName #3913
Conversation
e22a663
to
acc8f59
Compare
Is there any guideline when to |
I |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! I've left some small things inline.
When you are ready for a review please un-draft this PR. Thank you!
compiler-cli/src/new.rs
Outdated
}), | ||
} | ||
} | ||
Err(error) => Err(error), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is quite hard to read! Could you rewrite it to use early returns rather than nested flow control please 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like so?
compiler-cli/src/new.rs
Outdated
Some(format!("{}_app", invalid_name)) | ||
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this one doesn't use this _app
suffix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, makes sense – it does now! :)
88549dc
to
de48a84
Compare
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one could be invalid still!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fn validate_name
we make sure to return FormatNotLowercase
only, if lowercase is the only issue:
Line 348 in 91445da
} else if regex::Regex::new("^[a-zA-Z][a-zA-Z0-9_]*$") |
} else if regex::Regex::new("^[a-zA-Z][a-zA-Z0-9_]*$")
.expect("failed regex to match valid but non-lowercase name format")
.is_match(name)
{
Err(Error::InvalidProjectName {
name: name.to_string(),
reason: InvalidProjectNameReason::FormatNotLowercase,
})
} else {
Thats why I added FormatNotLowercase
, to simplify the logic in fn suggest_valid_name
.
- In
fn validate_name
we focus on validation and classification only. - In
fn suggest_valid_name
we focus on suggesting only.
There is also a test for it:
gleam/compiler-cli/src/new/tests.rs
Line 409 in 91445da
"Project_Name", |
assert_eq!(
crate::new::suggest_valid_name(
"Project_Name",
&crate::new::InvalidProjectNameReason::FormatNotLowercase
),
Some("project_name".to_string())
);
There were no tests for this at all, so I added some for each InvalidProjectNameReason
.
Or did I miss something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be addressed, in case you missed my comment!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes – have you read my answer to your comment, @lpil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see In which cases this might be invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the detailed explanation, I did not get it at first, my bad
a8f8f29
to
7d5edec
Compare
Looking good but! There's some comments of mine above that still need to be addressed! |
I give up as you either don't seem to see or simply don't read my replies. They have all been addressed, @lpil: Feel free to close this unless you plan to read and reply to my replies to your comments from above. How frustrating. |
Any comment with the "pending" thingy on it is a draft that you have not published yet, I cannot see them! |
|
||
fn suggest_valid_name(invalid_name: &str, reason: &InvalidProjectNameReason) -> Option<String> { | ||
match reason { | ||
InvalidProjectNameReason::GleamPrefix => match invalid_name.strip_prefix("gleam_") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case the project name is valid after stripping the gleam_
prefix, we suggest that name.
Otherwise we don't and the Error
module does its thing.
} | ||
_ => None, | ||
}, | ||
InvalidProjectNameReason::ErlangReservedWord => Some(format!("{}_app", invalid_name)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reserved words of Erlang we suggest the project name with an _app
suffix.
Does that make sense?
_ => None, | ||
}, | ||
InvalidProjectNameReason::ErlangReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::ErlangStandardLibraryModule => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For standard library module names of Erlang we suggest the project name with an _app
suffix.
Does that make sense?
InvalidProjectNameReason::ErlangStandardLibraryModule => { | ||
Some(format!("{}_app", invalid_name)) | ||
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reserved words of Gleam we suggest the project name with an _app
suffix.
Does that make sense?
compiler-cli/src/new.rs
Outdated
Some(format!("{}_app", invalid_name)) | ||
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is nothing to suggest in case the project name is just gleam
.
So the Error
module does its thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the reserved module Gleam we suggest the project name with an _app suffix.
Does that make sense?
compiler-cli/src/new.rs
Outdated
Some(format!("{}_app", invalid_name)) | ||
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, makes sense – it does now! :)
compiler-cli/src/new.rs
Outdated
}), | ||
} | ||
} | ||
Err(error) => Err(error), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like so?
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fn validate_name
we make sure to return FormatNotLowercase
only, if lowercase is the only issue:
Line 348 in 91445da
} else if regex::Regex::new("^[a-zA-Z][a-zA-Z0-9_]*$") |
} else if regex::Regex::new("^[a-zA-Z][a-zA-Z0-9_]*$")
.expect("failed regex to match valid but non-lowercase name format")
.is_match(name)
{
Err(Error::InvalidProjectName {
name: name.to_string(),
reason: InvalidProjectNameReason::FormatNotLowercase,
})
} else {
Thats why I added FormatNotLowercase
, to simplify the logic in fn suggest_valid_name
.
- In
fn validate_name
we focus on validation and classification only. - In
fn suggest_valid_name
we focus on suggesting only.
There is also a test for it:
gleam/compiler-cli/src/new/tests.rs
Line 409 in 91445da
"Project_Name", |
assert_eq!(
crate::new::suggest_valid_name(
"Project_Name",
&crate::new::InvalidProjectNameReason::FormatNotLowercase
),
Some("project_name".to_string())
);
There were no tests for this at all, so I added some for each InvalidProjectNameReason
.
Or did I miss something?
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes – have you read my answer to your comment, @lpil?
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see In which cases this might be invalid.
Ok, this explains everything... 🤦 I wasn't aware of this and got quite frustrated that you seemed to ignore all my comments across all of my PRs. My mistake – sorry, @lpil! |
Can you have a look at this #3913 (comment) please? This should explain why the handling case of |
7d5edec
to
9eece7f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
} | ||
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::GleamReservedModule => Some(format!("{}_app", invalid_name)), | ||
InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the detailed explanation, I did not get it at first, my bad
… InvalidProjectNameReason
9eece7f
to
c999e7b
Compare
This PR is regarding issue #1728.
Depending on the
InvalidProjectNameReason
we might suggest a valid project name.In case of the newly added
InvalidProjectNameReason::FormatNotLowercase
it looks like so:Lets take this as a basis to discuss what valid project names we might want to suggest for which
InvalidProjectNameReason
.