83 lines
2.6 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "forwardable"
module RuboCop
module Cask
module AST
class StanzaBlock
extend T::Helpers
sig { returns(RuboCop::AST::BlockNode) }
attr_reader :block_node
sig { returns(T::Array[Parser::Source::Comment]) }
attr_reader :comments
sig { params(block_node: RuboCop::AST::BlockNode, comments: T::Array[Parser::Source::Comment]).void }
def initialize(block_node, comments)
@block_node = block_node
@comments = comments
end
sig { returns(T::Array[Stanza]) }
def stanzas
return [] unless (block_body = block_node.block_body)
# If a block only contains one stanza, it is that stanza's direct parent, otherwise
# stanzas are grouped in a nested block and the block is that nested block's parent.
is_stanza = if block_body.begin_block?
->(node) { node.parent.parent == block_node }
else
->(node) { node.parent == block_node }
end
@stanzas ||= block_body.each_node
.select(&:stanza?)
.select(&is_stanza)
.map { |node| Stanza.new(node, comments) }
end
end
# 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 < StanzaBlock
extend Forwardable
def cask_node
block_node
end
def_delegator :cask_node, :block_body, :cask_body
def header
@header ||= CaskHeader.new(block_node.method_node)
end
# TODO: Use `StanzaBlock#stanzas` for all cops, where possible.
def stanzas
return [] unless cask_body
@stanzas ||= cask_body.each_node
.select(&:stanza?)
.map { |node| Stanza.new(node, comments) }
end
def toplevel_stanzas
2023-03-21 14:43:39 +01:00
# If a `cask` block only contains one stanza, it is that stanza's direct parent,
# otherwise stanzas are grouped in a block and `cask` is that block's parent.
is_toplevel_stanza = if cask_body.begin_block?
->(stanza) { stanza.parent_node.parent.cask_block? }
else
->(stanza) { stanza.parent_node.cask_block? }
end
@toplevel_stanzas ||= stanzas.select(&is_toplevel_stanza)
end
end
end
end
end