Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Extending Foodcritic with new rules

Andrew Crump
February 04, 2014

Extending Foodcritic with new rules

Config Management Camp, Gent on 4th February 2014

Andrew Crump

February 04, 2014
Tweet

More Decks by Andrew Crump

Other Decks in Programming

Transcript

  1. $ foodcritic . FC006: Mode should be quoted or fully

    specified when setting file permissions: ./ recipes/default.rb:9
  2. $ foodcritic . FC041: Execute resource used to run curl

    or wget commands: ./recipes/ default.rb:9
  3. # Specify rules on
 # the command line $ foodcritic


    -I your_rule.rb . ! # You can also
 # package your rules
 # as a gem
  4. Peter Zotov @whitequark
 fucking ruby and its fucking parser, how

    does it even work?!
 I *still* have no idea
  5. => [:program, [[:method_add_block, [:command, [:@ident, "file", [2, 0]], [:args_add_block, [[:string_literal,

    [:string_content, [:@tstring_content, "/ etc/foo", [2, 6]]]]], false]], [:do_block, nil, [[:command, [:@ident, "mode", [3, 2]], [:args_add_block, [[:@int, "0600", [3, 7]]], false]], [:command, [:@ident, "content", [4, 2]], [:args_add_block, [[:string_literal, [:string_content, [:@tstring_content, "bar", [4, 11]]]]], false]]]]]]]
  6. # We pass an array of tags in, common tags

    include 'correctness' and 'style' tags %w{correctness files}
  7. # any mode identifier // ident[@value='mode']/ ! # we need

    to go up one parent::command/ ! # a literal integer descendant::int
  8. # filtering again [ ! # needs to be less


    # than five digits
 # long string-length(@value) < 5
  9. # match 644 not 0644 and not( starts-with(@value, "0") and

    string length(@value) = 4 ) ! # stop filtering ]
  10. # choose the line num
 # to show to the

    end
 # user /ancestor:: method_add_block # do this if the AST # node does not have # a descendant node
 # with a position
  11. Drop a pry binding in your rule and explore pry>

    ls pry> a_method() pry> whereami
  12. Examples: | environment_name | show_warning | | production | should

    not | | pre_production | should not | | production-eu | should not | | production2 | should not | | Production | should not | | EU West | should | | production (eu-west) | should |