Refactor ruby code based on AST

13c60e2a5806406b537daa542d3495da?s=47 flyerhzm
October 18, 2015

Refactor ruby code based on AST

13c60e2a5806406b537daa542d3495da?s=128

flyerhzm

October 18, 2015
Tweet

Transcript

  1. Refactor ruby code based on AST Richard Huang @flyerhzm

  2. None
  3. None
  4. Problem

  5. Code Review

  6. None
  7. None
  8. Repeat, Repeat, Repeat

  9. None
  10. None
  11. Any solution?

  12. CodeClimate

  13. None
  14. None
  15. PullReview

  16. How they work?

  17. CodeClimate / PullReview flay flog brakeman rails_best_practices … Open Source

    Gems
  18. flay analyzes code for structural similarities

  19. None
  20. flog reports the most tortured code

  21. None
  22. brakeman a static analysis security vulnerability scanner for rails applications

  23. None
  24. rails_best_practices checks the quality of rails code

  25. None
  26. How they work?

  27. flay flog brakeman rails_best_practices … ruby_parser ripper parser ruby parser

    => AST CodeClimate / PullReview
  28. ParseTree • First ruby parser • Not support ruby 1.9

    or newer
  29. ruby_parser • Most popular ruby parser • Output is 100%

    equivalent to ParseTree
  30. None
  31. None
  32. ActiveRecord::Base belongs_to :user touch :archived_at def archive

  33. ripper • Ruby built-in parser • Support ruby

  34. None
  35. ActiveRecord::Base belongs_to :user def archive touch :archived_at

  36. parser • A new ruby parser • Support rewriting

  37. None
  38. ActiveRecord::Base belongs_to :user def archive touch :archived_at

  39. Good enough?

  40. Auto rewrite!

  41. transpec the rspec syntax converter

  42. None
  43. None
  44. None
  45. None
  46. rubocop static code analyzer based on the community ruby style

    guide
  47. None
  48. None
  49. transpec rubocop parser

  50. None
  51. Generic Solution

  52. Synvert converts ruby code to better syntax

  53. DSL • within_file / within_files • with_node / within_node •

    if_exist_node / unless_exist_node • append / insert / insert_after / replace_with / remove • ……
  54. None
  55. None
  56. None
  57. Demo Convert rspec to new syntax Convert rails code from

    2.3 to 3.0 Convert rails code from 3.2 to 4.0
  58. Side Project not published yet

  59. None
  60. None
  61. Refs CodeClimate - https://codeclimate.com/ PullReview - https://www.pullreview.com/ flay - https://github.com/seattlerb/flay

    flog - https://github.com/seattlerb/flog brakeman - https://github.com/presidentbeef/brakeman rails_best_practices - https://github.com/railsbp/ rails_best_practices
  62. Refs parsetree - https://github.com/seattlerb/parsetree ruby_parser - https://github.com/seattlerb/ruby_parser parser - https://github.com/whitequark/parser

    rubocop - https://github.com/bbatsov/rubocop transpec - https://github.com/yujinakayama/transpec synvert - https://github.com/xinminlabs/synvert
  63. Thank You