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

Cop idea: Enforce preferred style for format string sequences #3438

Closed
backus opened this issue Aug 23, 2016 · 10 comments
Closed

Cop idea: Enforce preferred style for format string sequences #3438

backus opened this issue Aug 23, 2016 · 10 comments

Comments

@backus
Copy link
Contributor

backus commented Aug 23, 2016

You should use one of these styles consistently:

# Positional format sequences with field type characters
"Hi, my name is %s and I am %d years old. %0.1f to be exact." % ["John", 22, 22.45]

# Named format sequences without field type characters
"Hi, my name is %{name} and I am %{age} years old. %{precise_age} to be exact." % { name: "John", age: 22, precise_age: 22.45 }

# Named format sequences with field type characters
"Hi, my name is %<name>s and I am %<age>d years old. %<age>0.1f to be exact." % { name: "John", age:  22.45 }

I prefer the third option because you get to name your variables and also get some type hinting and coercion.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 24, 2016

Yeah, that'd be nice.

backus added a commit to backus/rubocop that referenced this issue Apr 29, 2017
Enforces using one of two named format string token styles:

 * Annotated: `format('%<greeting>s', greeting: 'Hello')`
 * Template:  `format('%{greeting}', greeting: 'Hello')`
@cdenneen
Copy link

Anyone currently using existing code with %{variable} (especially when using rubocop tests with puppet code) just got bit with errors because of this commit. Setting default back to template fixes it.

@backus
Copy link
Contributor Author

backus commented Jul 19, 2017

Yeah it was a new rule that came out somewhat recently. Not sure what you mean by "bit with errors." It is just recommending one of two styles

@cdenneen
Copy link

cdenneen commented Jul 19, 2017

@backus

Rakefile:52:63: C: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over template tokens (like %{foo}).
  config.log_format = '%{path}:%{linenumber}:%{check}:%{KIND}:%{message}'
                                                              ^^^^^^^^^^
spec/spec_helper_acceptance.rb:23:15: C: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over template tokens (like %{foo}).
        'role/%{role}',
              ^^^^^^^

But changing either of these to %<role> will break puppet-lint rake task

rake aborted!
ArgumentError: malformed format string - %:
~/puppet/.bundle/gems/fast_gettext-1.1.0/lib/fast_gettext/vendor/string.rb:70:in `%'
~/puppet/.bundle/gems/fast_gettext-1.1.0/lib/fast_gettext/vendor/string.rb:70:in `%'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint.rb:103:in `format_message'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint.rb:152:in `block in report'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint.rb:142:in `each'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint.rb:142:in `report'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint.rb:201:in `print_problems'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint/tasks/puppet-lint.rb:84:in `block (3 levels) in define'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint/tasks/puppet-lint.rb:81:in `each'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint/tasks/puppet-lint.rb:81:in `block (2 levels) in define'
~/puppet/.bundle/gems/puppet-lint-2.3.0/lib/puppet-lint/tasks/puppet-lint.rb:75:in `block in define'
~/puppet/.bundle/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/opt/boxen/rbenv/versions/2.2.4/bin/bundle:23:in `load'
/opt/boxen/rbenv/versions/2.2.4/bin/bundle:23:in `<main>'
Tasks: TOP => lint
(See full trace by running task with --trace)

So then to fix you have to update the .rubocop.yml

Style/FormatStringToken:
  EnforcedStyle: template

@backus
Copy link
Contributor Author

backus commented Jul 19, 2017

It should be %<role>s not %<role> if you want to use the default style @cdenneen

@cdenneen
Copy link

@backus thanks I think that will fix the issues with puppet-lint failing (wish the stacktrace was clearer).

@backus
Copy link
Contributor Author

backus commented Jul 19, 2017

Yeah the stacktrace from your fast_gettext dependency isn't that helpful but rubocop does try to nudge you in the right direction with its message. The reason this is recommended by default is because you can encode a tiny bit of type information into the tokens. For example, you might be able to write %<linenumber>d if that value is expected to be an integer.

@cdenneen
Copy link

cdenneen commented Jul 19, 2017 via email

@backus
Copy link
Contributor Author

backus commented Jul 19, 2017

I think so yeah.

@rhymes
Copy link

rhymes commented Dec 12, 2017

I found one issue with this cop. It flags things like this:

Time.current.strftime('%Y-%m-%d.%H.%M.%S.%N')

octopusinvitro added a commit to survival/donation-system that referenced this issue Dec 13, 2017
Since this is a gem it is a best practice to not include a Gemfile.lock, however
that also means that travis will download always the last versions of all gems.

In the last Travis run, the version of Rubocop was updated, throwing an error in
a rule that we may not neccessarily want to adopt, hence I am excluding it just
for the file that triggers it.

Relevant Rubocop issue:
  rubocop/rubocop#3438
octopusinvitro added a commit to survival/donation-system that referenced this issue Dec 13, 2017
Since this is a gem it is a best practice to not include a Gemfile.lock, however
that also means that travis will download always the last versions of all gems.

In the last Travis run, the version of Rubocop was updated, throwing an error in
a rule that we may not neccessarily want to adopt, hence I am excluding it just
for the file that triggers it.

Relevant Rubocop issue:
  rubocop/rubocop#3438
Cruikshanks added a commit to DEFRA/waste-carriers-acceptance-tests that referenced this issue Dec 14, 2017
[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

rubocop/rubocop#3438

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.
Cruikshanks pushed a commit to DEFRA/waste-carriers-acceptance-tests that referenced this issue Dec 14, 2017
* Bundle Update on 2017-12-14

* Resolve issues with rubocop breaking build

[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

rubocop/rubocop#3438

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.
Cruikshanks added a commit to DEFRA/flood-risk-acceptance-tests that referenced this issue Dec 14, 2017
[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

rubocop/rubocop#3438

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.
Cruikshanks pushed a commit to DEFRA/flood-risk-acceptance-tests that referenced this issue Dec 14, 2017
* Bundle Update on 2017-12-14

* Resolve issues with rubocop breaking build

[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

rubocop/rubocop#3438

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.
Cruikshanks added a commit to DEFRA/pafs-acceptance-tests that referenced this issue Dec 14, 2017
[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

rubocop/rubocop#3438

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.
Cruikshanks pushed a commit to DEFRA/pafs-acceptance-tests that referenced this issue Dec 14, 2017
* Bundle Update on 2017-12-14

* Resolve issues with rubocop breaking build

[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

rubocop/rubocop#3438

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.
Cruikshanks added a commit to DEFRA/flood-risk-front-office that referenced this issue Dec 15, 2017
[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

<rubocop/rubocop#3438>

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.
Cruikshanks pushed a commit to DEFRA/flood-risk-front-office that referenced this issue Dec 15, 2017
* Bundle Update on 2017-12-15

* Resolve issues with rubocop breaking build

[Deppbot](https://www.deppbot.com/) has updated rubocop and this has caused a break in the build due to new errors being found.

The one cop we have had to disable is `Style/FormatStringToken` which was introduced in 0.49.0.

<rubocop/rubocop#3438>

The only issue its highlighting in the code is `strftime()`, and we feel the way we are using it is as expected so are choosing to disable this cop for now.

* Update FactoryGirl to FactoryBot

The people behind ThoughtBot have decided to change the name of this gem, which breaks build if we don't rename it in our code. This change updates the gem and the code to use the new name.

* Fix error following rubocop change

We didn't implement a rubocop fix properly. This change corrects that mistake.

* Update flood_risk_engine to latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants