Skip to content

[Yaml] Double-quote strings with single quote marks #41750

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

Merged
merged 1 commit into from
Dec 30, 2021

Conversation

Ostrzyciel
Copy link
Contributor

@Ostrzyciel Ostrzyciel commented Jun 19, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #41687
License MIT
Doc PR It's a tiny change, is it really required?

This changes the behavior of the Dumper to double-quote strings containing single quote marks when it's likely to improve readability (measured by the length of the quoted string). See the linked ticket for the detailed motivation behind this.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title [YAML] Double-quote strings with single quote marks [Yaml] Double-quote strings with single quote marks Jun 19, 2021
@carsonbot
Copy link

Hey!

I think @jschaedl has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@nicolas-grekas nicolas-grekas added this to the 5.4 milestone Jun 21, 2021
@stof
Copy link
Member

stof commented Jun 21, 2021

what should happen for a string containing both single quoted and double quoted strings but no other character ? Or for string containing single quotes and chars that cannot be kept unescaped in double-quoted strings ? for those 2 cases, the readability argument might still justify keeping single quotes. So maybe it should be implemented differently than putting the single quote in this regex

@Ostrzyciel
Copy link
Contributor Author

what should happen for a string containing both single quoted and double quoted strings but no other character ? Or for string containing single quotes and chars that cannot be kept unescaped in double-quoted strings ? for those 2 cases, the readability argument might still justify keeping single quotes. So maybe it should be implemented differently than putting the single quote in this regex

That's a valid point. I fiddled a bit with yamllint.com to see how their implementation works and it seems the algorithm for establishing what should be single- and double-quoted is a bit more complex. I don't speak Ruby, so I gave up on reading the code.

(sigh) At this point I'm thinking if just adding a new flag for forcing double-quoting on all strings would be easier. That would certainly make sense for some types of configuration files.

@stof
Copy link
Member

stof commented Jun 21, 2021

I don't speak Ruby, so I gave up on reading the code.

Can you link to that code ? I don't write Ruby, but I'm generally able to read it.

@Ostrzyciel
Copy link
Contributor Author

Can you link to that code ? I don't write Ruby, but I'm generally able to read it.

I think the source code for yamllint.com is here: https://github.com/gilltots/yamllint
They use this library for dumping YAML: https://github.com/afunai/ya2yaml

@fabpot fabpot modified the milestones: 5.4, 6.1 Nov 3, 2021
@gharlan
Copy link
Contributor

gharlan commented Dec 19, 2021

We could change this code:

case Escaper::requiresDoubleQuoting($value):
return Escaper::escapeWithDoubleQuotes($value);
case Escaper::requiresSingleQuoting($value):
case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value):
case Parser::preg_match(self::getHexRegex(), $value):
case Parser::preg_match(self::getTimestampRegex(), $value):
return Escaper::escapeWithSingleQuotes($value);

to:

    case Escaper::requiresDoubleQuoting($value): 
        return Escaper::escapeWithDoubleQuotes($value); 
    case Escaper::requiresSingleQuoting($value): 
        $singleQuoted = Escaper::escapeWithSingleQuotes($value); 

        if (!str_contains($value, "'") {
             return $singleQuoted;
        }

        $doubleQuoted = Escaper::escapeWithDoubleQuotes($value); 

        return \strlen($doubleQuoted) < \strlen($singleQuoted) ? $doubleQuoted : $singleQuoted;
    case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value): 
    case Parser::preg_match(self::getHexRegex(), $value): 
    case Parser::preg_match(self::getTimestampRegex(), $value): 
        return Escaper::escapeWithSingleQuotes($value); 

So if the string contains single quotes, the decision is made by resulting length of single quoted vs. double quoted string.

@Ostrzyciel
Copy link
Contributor Author

Ostrzyciel commented Dec 20, 2021

So if the string contains single quotes, the decision is made bei resulting length of single quoted vs. double quoted string.

Huh, that's smart. Thanks for the suggestion, I'll try to incorporate this into the code next week :)

@nicolas-grekas
Copy link
Member

(please rebase to remove the merge commit)

@Ostrzyciel Ostrzyciel force-pushed the yaml-double-quote-single branch from e0e73cf to d4290c6 Compare December 27, 2021 16:30
@Ostrzyciel
Copy link
Contributor Author

Yay, I think it works. I did my best trying to clean up git history, with moderate success.

@xabbuh xabbuh force-pushed the yaml-double-quote-single branch from c8e6254 to dffe98e Compare December 29, 2021 21:59
@xabbuh
Copy link
Member

xabbuh commented Dec 30, 2021

@Ostrzyciel FYI, I have rebased and squashed the changes on your fork. :)

@xabbuh
Copy link
Member

xabbuh commented Dec 30, 2021

looks good to me, great work 👍

@fabpot
Copy link
Member

fabpot commented Dec 30, 2021

Thank you @Ostrzyciel.

@fabpot fabpot merged commit cef3d5a into symfony:6.1 Dec 30, 2021
@fabpot fabpot mentioned this pull request Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Yaml] Double quote strings containing single quotes
7 participants