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

Make the pretty printer correctly escape field names #1410

Merged
merged 2 commits into from
Jun 29, 2023

Conversation

vkleen
Copy link
Contributor

@vkleen vkleen commented Jun 28, 2023

Previously, the pretty printer relied on Ident::label_quoted for printing record field names. This function blindly placed an identifier between quotes if it was deemed necessary. This produces incorrect results if the identifier label itself contains quotes, for example.

@github-actions github-actions bot temporarily deployed to pull request June 28, 2023 13:30 Inactive
Copy link
Member

@jneem jneem left a comment

Choose a reason for hiding this comment

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

Unrelated to the current patch, but aren't we compiling a regex every time we pretty-print a label?

@vkleen
Copy link
Contributor Author

vkleen commented Jun 28, 2023

Looks like we are, yes. We probably should stop doing that 😅

@vkleen vkleen added this pull request to the merge queue Jun 28, 2023
@@ -42,6 +42,13 @@ fn sorted_map<K: Ord, V>(m: &'_ IndexMap<K, V>) -> Vec<(&'_ K, &'_ V)> {
ret
}

pub fn escape(s: &str) -> String {
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick, but shouldn't this function be moved to indentifier or even to term::string if it's used inside identifier? it's not obvious that identifier should be depending on pretty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point. It felt too unrelated to Ident to go into identifier to me. In fact, Ident::label_quoted is used only in the pretty printer. So maybe the correct course of action to remove this dependency would be to move label_quoted into pretty? I'm not sure.

Copy link
Member

@yannham yannham Jun 28, 2023

Choose a reason for hiding this comment

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

Hmm, good point as well. I think both are reasonable:

  • Leave label_quoted in identifier and bring back escape either in identifier or term::string, because I believe what you're doing in the end is to escape a Nickel string. It would be like implementing Display: I think it's fair to have it inside identifier (or, should we just implement Display, by the way, instead of label_quoted?)
  • Move both to pretty, because it's about pretty-printing stuff

I guess the current in-between with one inside identifier and one inside pretty is what's feeling itchy

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 think I'll go with moving label_quoted into pretty as well, for now.

@vkleen vkleen removed this pull request from the merge queue due to a manual request Jun 28, 2023
Previously, the pretty printer relied on `Ident::label_quoted` for
printing record field names. This function blindly placed an identifier
between quotes if it was deemed necessary. This produces incorrect
results if the identifier label itself contains quotes, for example.
@vkleen vkleen force-pushed the pretty/field-name-quoting branch from 8e5f768 to 577a526 Compare June 28, 2023 15:36
@vkleen vkleen force-pushed the pretty/field-name-quoting branch from 577a526 to 78a10ed Compare June 28, 2023 15:37
@github-actions github-actions bot temporarily deployed to pull request June 28, 2023 15:42 Inactive
@vkleen vkleen added this pull request to the merge queue Jun 29, 2023
Merged via the queue into master with commit 44f5eb6 Jun 29, 2023
@vkleen vkleen deleted the pretty/field-name-quoting branch June 29, 2023 09:12
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