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

Import rubocop-cask gem. #5190

Merged
merged 4 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 46 additions & 1 deletion Library/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
AllCops:
TargetRubyVersion: 2.3
Exclude:
- '**/Casks/**/*'
- '**/vendor/**/*'
DisplayCopNames: false

Expand All @@ -15,6 +14,10 @@ FormulaAudit:
FormulaAuditStrict:
Enabled: true

# enable all cask audits
Cask:
Enabled: true

# disable all formulae strict audits by default
NewFormulaAudit:
Enabled: false
Expand All @@ -39,6 +42,8 @@ Layout/CaseIndentation:
# this is a bit less "floaty"
Layout/EndAlignment:
EnforcedStyleAlignWith: start_of_line
Exclude:
- '**/Casks/**/*'

# enforce use of <<~EOS
Layout/IndentHeredoc:
Expand Down Expand Up @@ -108,10 +113,14 @@ Metrics/LineLength:
Max: 170
# ignore manpage comments and long single-line strings
IgnoredPatterns: ['#: ', ' url "', ' mirror "', ' plist_options :']
Exclude:
- '**/Casks/**/*'

# dashes in filenames are typical
Naming/FileName:
Regex: !ruby/regexp /^[\w\@\-\+\.]+(\.rb)?$/
Exclude:
- '**/Casks/**/*'

# implicitly allow EOS as we use it everywhere
Naming/HeredocDelimiterNaming:
Expand Down Expand Up @@ -176,6 +185,7 @@ Style/HashSyntax:
- '**/cmd/**/*'
- '**/lib/**/*'
- '**/spec/**/*'
- '**/Casks/**/*'

# this doesn't make sense for wide lines below maximum line length
# https://github.com/rubocop-hq/rubocop/issues/6149
Expand Down Expand Up @@ -206,10 +216,14 @@ Style/StderrPuts:
# ruby style guide favorite
Style/StringLiterals:
EnforcedStyle: double_quotes
Exclude:
- '**/Casks/**/*'

# consistency with above
Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes
Exclude:
- '**/Casks/**/*'

# consistency helps readability and helps people who don't know Ruby
Style/StringMethods:
Expand All @@ -222,6 +236,8 @@ Style/SymbolArray:
# make things a bit easier to read
Style/TernaryParentheses:
EnforcedStyle: require_parentheses_when_complex
Exclude:
- '**/Casks/**/*'

# messes with existing plist/caveats style
Style/TrailingBodyOnMethodDefinition:
Expand All @@ -238,3 +254,32 @@ Style/TrailingCommaInHashLiteral:
# a bit confusing to non-Rubyists but useful for longer arrays
Style/WordArray:
MinSize: 4
Exclude:
- '**/Casks/**/*'

# exclude some styles for casks (for now)
# TODO: make Homebrew/brew, casks and formulae more consistent
Layout/ElseAlignment:
Exclude:
- '**/Casks/**/*'
Layout/IndentArray:
Exclude:
- '**/Casks/**/*'
Layout/IndentHash:
Exclude:
- '**/Casks/**/*'
Layout/IndentationWidth:
Exclude:
- '**/Casks/**/*'
Performance/StringReplacement:
Exclude:
- '**/Casks/**/*'
Style/EmptyElse:
Exclude:
- '**/Casks/**/*'
Style/PercentLiteralDelimiters:
Exclude:
- '**/Casks/**/*'
Style/RegexpLiteral:
Exclude:
- '**/Casks/**/*'
6 changes: 3 additions & 3 deletions Library/Homebrew/cask/cmd/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def run
def install_rubocop
capture_stderr do
begin
Homebrew.install_gem_setup_path! "rubocop-cask", HOMEBREW_RUBOCOP_CASK_VERSION, "rubocop"
Homebrew.install_gem_setup_path! "rubocop"
rescue SystemExit
raise CaskError, Tty.strip_ansi($stderr.string).chomp.sub(/\AError: /, "")
end
Expand All @@ -43,8 +43,8 @@ def rubocop_args

def default_args
[
"--require", "rubocop-cask",
"--force-default-config",
"--force-exclusion",
"--config", "#{HOMEBREW_LIBRARY}/.rubocop.yml",
"--format", "simple"
]
end
Expand Down
5 changes: 0 additions & 5 deletions Library/Homebrew/constants.rb

This file was deleted.

1 change: 0 additions & 1 deletion Library/Homebrew/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ def auditing?
require "active_support/core_ext/hash/deep_merge"
require "active_support/core_ext/file/atomic"

require "constants"
require "exceptions"
require "utils"

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/manpages/brew-cask.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ graphical user interface.
Ruby regular expression.

* `style` [--fix] [ <token> ... ]:
Check the given Casks for correct style using [RuboCop Cask](https://github.com/Homebrew/rubocop-cask).
Check the given Casks for correct style using RuboCop (with custom Cask cops).
If no tokens are given on the command line, all Casks are checked.
With `--fix`, auto-correct any style errors if possible.

Expand Down
30 changes: 16 additions & 14 deletions Library/Homebrew/rubocops.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
require_relative "load_path"

require "rubocop-rspec"
require "rubocops/formula_desc_cop"
require "rubocops/components_order_cop"
require "rubocops/components_redundancy_cop"
require "rubocops/dependency_order_cop"
require "rubocops/homepage_cop"
require "rubocops/text_cop"
require "rubocops/caveats_cop"
require "rubocops/checksum_cop"
require "rubocops/patches_cop"
require "rubocops/conflicts_cop"
require "rubocops/options_cop"
require "rubocops/urls_cop"
require "rubocops/lines_cop"
require "rubocops/class_cop"
require "rubocops/formula_desc"
require "rubocops/components_order"
require "rubocops/components_redundancy"
require "rubocops/dependency_order"
require "rubocops/homepage"
require "rubocops/text"
require "rubocops/caveats"
require "rubocops/checksum"
require "rubocops/patches"
require "rubocops/conflicts"
require "rubocops/options"
require "rubocops/urls"
require "rubocops/lines"
require "rubocops/class"

require "rubocops/rubocop-cask"
74 changes: 74 additions & 0 deletions Library/Homebrew/rubocops/cask/ast/cask_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require "forwardable"

module RuboCop
module Cask
module AST
# This class wraps the AST block node that represents the entire cask
# definition. It includes various helper methods to aid cops in their
# analysis.
class CaskBlock
extend Forwardable

def initialize(block_node, comments)
@block_node = block_node
@comments = comments
end

attr_reader :block_node, :comments

alias cask_node block_node

def_delegator :cask_node, :block_body, :cask_body

def header
@header ||= CaskHeader.new(cask_node.method_node)
end

def stanzas
return [] unless cask_body

@stanzas ||= cask_body.each_node
.select(&:stanza?)
.map { |node| Stanza.new(node, stanza_comments(node)) }
end

def toplevel_stanzas
@toplevel_stanzas ||= stanzas.select(&:toplevel_stanza?)
end

def sorted_toplevel_stanzas
@sorted_toplevel_stanzas ||= sort_stanzas(toplevel_stanzas)
end

private

def sort_stanzas(stanzas)
stanzas.sort do |s1, s2|
i1 = stanza_order_index(s1)
i2 = stanza_order_index(s2)
if i1 == i2
i1 = stanzas.index(s1)
i2 = stanzas.index(s2)
end
i1 - i2
end
end

def stanza_order_index(stanza)
Constants::STANZA_ORDER.index(stanza.stanza_name)
end

def stanza_comments(stanza_node)
stanza_node.each_node.reduce([]) do |comments, node|
comments | comments_hash[node.loc]
end
end

def comments_hash
@comments_hash ||= Parser::Source::Comment
.associate_locations(cask_node, comments)
end
end
end
end
end
43 changes: 43 additions & 0 deletions Library/Homebrew/rubocops/cask/ast/cask_header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module RuboCop
module Cask
module AST
# This class wraps the AST method node that represents the cask header. It
# includes various helper methods to aid cops in their analysis.
class CaskHeader
def initialize(method_node)
@method_node = method_node
end

attr_reader :method_node

def dsl_version?
hash_node
end

def header_str
@header_str ||= source_range.source
end

def source_range
@source_range ||= method_node.loc.expression
end

def preferred_header_str
"cask '#{cask_token}'"
end

def cask_token
@cask_token ||= pair_node.val_node.children.first
end

def hash_node
@hash_node ||= method_node.each_child_node(:hash).first
end

def pair_node
@pair_node ||= hash_node.each_child_node(:pair).first
end
end
end
end
end
66 changes: 66 additions & 0 deletions Library/Homebrew/rubocops/cask/ast/stanza.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require "forwardable"

module RuboCop
module Cask
module AST
# This class wraps the AST send/block node that encapsulates the method
# call that comprises the stanza. It includes various helper methods to
# aid cops in their analysis.
class Stanza
extend Forwardable

def initialize(method_node, comments)
@method_node = method_node
@comments = comments
end

attr_reader :method_node, :comments

alias stanza_node method_node

def_delegator :stanza_node, :method_name, :stanza_name
def_delegator :stanza_node, :parent, :parent_node

def source_range
stanza_node.expression
end

def source_range_with_comments
comments.reduce(source_range) do |range, comment|
range.join(comment.loc.expression)
end
end

def_delegator :source_range, :source
def_delegator :source_range_with_comments, :source,
:source_with_comments

def stanza_group
Constants::STANZA_GROUP_HASH[stanza_name]
end

def same_group?(other)
stanza_group == other.stanza_group
end

def toplevel_stanza?
parent_node.cask_block? || parent_node.parent.cask_block?
end

def ==(other)
self.class == other.class && stanza_node == other.stanza_node
end

alias eql? ==

Constants::STANZA_ORDER.each do |stanza_name|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{stanza_name}?
stanza_name == :#{stanza_name}
end
RUBY
end
end
end
end
end