Skip to content
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

Merged
merged 8 commits into from
Jan 9, 2025

Conversation

diemogebhardt
Copy link
Contributor

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:

We were not able to create your project as `Project_Name` does not have the
correct format. Project names may only contain lowercase letters.

Would you like to name your project 'project_name' instead? [y/n]: 

Lets take this as a basis to discuss what valid project names we might want to suggest for which InvalidProjectNameReason.

@diemogebhardt diemogebhardt force-pushed the suggest-valid-project-names branch from e22a663 to acc8f59 Compare November 29, 2024 19:23
@diemogebhardt
Copy link
Contributor Author

Is there any guideline when to use at the top of the file or when to use the fully qualified names? I couldn't recognize a pattern.

@GearsDatapacks
Copy link
Member

I use pretty much everything. I think it's up to you really

Copy link
Member

@lpil lpil left a 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!

}),
}
}
Err(error) => Err(error),
Copy link
Member

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 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like so?

Some(format!("{}_app", invalid_name))
}
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)),
InvalidProjectNameReason::GleamReservedModule => None,
Copy link
Member

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?

Copy link
Contributor Author

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! :)

@lpil lpil marked this pull request as draft December 2, 2024 17:01
@diemogebhardt diemogebhardt force-pushed the suggest-valid-project-names branch 2 times, most recently from 88549dc to de48a84 Compare December 4, 2024 14:06
@diemogebhardt diemogebhardt marked this pull request as ready for review December 4, 2024 16:24
}
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)),
InvalidProjectNameReason::GleamReservedModule => Some(format!("{}_app", invalid_name)),
InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()),
Copy link
Member

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!

Copy link
Contributor Author

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:

} 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:

"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?

Copy link
Member

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!

Copy link
Contributor Author

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?

image

Copy link
Contributor Author

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.

Copy link
Member

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

@lpil lpil marked this pull request as draft December 9, 2024 16:18
@diemogebhardt diemogebhardt marked this pull request as ready for review December 10, 2024 21:05
@diemogebhardt diemogebhardt force-pushed the suggest-valid-project-names branch from a8f8f29 to 7d5edec Compare December 10, 2024 21:40
@lpil
Copy link
Member

lpil commented Dec 17, 2024

Looking good but! There's some comments of mine above that still need to be addressed!

@lpil lpil marked this pull request as draft December 17, 2024 14:32
@diemogebhardt
Copy link
Contributor Author

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:

image

Feel free to close this unless you plan to read and reply to my replies to your comments from above. How frustrating.

@lpil
Copy link
Member

lpil commented Dec 20, 2024

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_") {
Copy link
Contributor Author

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)),
Copy link
Contributor Author

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 => {
Copy link
Contributor Author

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)),
Copy link
Contributor Author

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?

Some(format!("{}_app", invalid_name))
}
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)),
InvalidProjectNameReason::GleamReservedModule => None,
Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Some(format!("{}_app", invalid_name))
}
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)),
InvalidProjectNameReason::GleamReservedModule => None,
Copy link
Contributor Author

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! :)

}),
}
}
Err(error) => Err(error),
Copy link
Contributor Author

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()),
Copy link
Contributor Author

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:

} 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:

"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()),
Copy link
Contributor Author

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?

image

}
InvalidProjectNameReason::GleamReservedWord => Some(format!("{}_app", invalid_name)),
InvalidProjectNameReason::GleamReservedModule => Some(format!("{}_app", invalid_name)),
InvalidProjectNameReason::FormatNotLowercase => Some(invalid_name.to_lowercase()),
Copy link
Contributor Author

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.

@diemogebhardt
Copy link
Contributor Author

diemogebhardt commented Dec 23, 2024

Any comment with the "pending" thingy on it is a draft that you have not published yet, I cannot see them!

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!

@diemogebhardt
Copy link
Contributor Author

Looking good but! There's some comments of mine above that still need to be addressed!

Can you have a look at this #3913 (comment) please? This should explain why the handling case of InvalidProjectNameReason::FormatNotLowercase cannot be invalid.

@diemogebhardt diemogebhardt marked this pull request as ready for review December 23, 2024 23:52
@diemogebhardt diemogebhardt force-pushed the suggest-valid-project-names branch from 7d5edec to 9eece7f Compare December 24, 2024 00:19
Copy link
Member

@lpil lpil left a 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()),
Copy link
Member

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

@lpil lpil force-pushed the suggest-valid-project-names branch from 9eece7f to c999e7b Compare January 9, 2025 13:54
@lpil lpil merged commit 097677e into gleam-lang:main Jan 9, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants