2016-07-07 17 views
6

我有一個編碼標準,暗示三元組的初始參數應該總是在括號內,而不管表達式如何。如何使用RuboCop強制執行三元括號?

E.g. foo = (thing.baz?) ? [] : thing.bar

以下應被視爲違規:

例如foo = thing.baz? ? [] : thing.bar

是否有可能與Rubocop的內置警察做到這一點,或者這也需要定製警察。如果是這樣,我將如何實現它?

+0

AFIAK,開箱即用沒有這樣的警察。在[rubocop測試](https://github.com/nevir/rubocop-rspec/tree/master/lib/rubocop/cop/rspec)中有關於如何創建自定義警察的示例和簡要文檔。 – mudasobwa

回答

12

我看到你的問題,所以我繼續實施警察爲您服務。這個名字是Style/TernaryParentheses,並且希望EnforcedStyle選項require_parentheses

# .rubocop.yml 
Style/TernaryParentheses: 
    Enabled: true 
    EnforcedStyle: require_parentheses 

你可以開始使用它,現在,通過把這個在您的Gemfile(不是默認):

gem 'rubocop', git: 'git://github.com/bbatsov/rubocop.git' 

,或者你可以等待0.42.0版本。

+0

太棒了。非常感謝你! – qnm

+0

不用擔心。不要猶豫,在GitHub倉庫中提交錯誤報告或功能請求。這一切都非常有幫助。 :-) – Drenmi