Skip to content

Commit

Permalink
Merge pull request #677 from Shopify/uk-fix-xpath-problems
Browse files Browse the repository at this point in the history
Fix various DSL compilers matching `XPath`
  • Loading branch information
paracycle authored Dec 25, 2021
2 parents 4f4dc55 + d23669e commit 5fc0939
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ group(:development, :test) do
gem("config", require: false)
gem("aasm", require: false)
gem("bcrypt", require: false)
gem("xpath", require: false)
end
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ GEM
websocket-driver (0.7.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
yard (0.9.26)
yard-sorbet (0.5.3)
sorbet-runtime (>= 0.5)
Expand Down Expand Up @@ -318,6 +320,7 @@ DEPENDENCIES
sqlite3
state_machines
tapioca!
xpath

BUNDLED WITH
2.2.32
4 changes: 2 additions & 2 deletions lib/tapioca/compilers/dsl/rails_generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
all_modules.select do |const|
all_classes.select do |const|
name = qualified_name_of(const)

name &&
!name.match?(BUILT_IN_MATCHER) &&
const < ::Rails::Generators::Base
::Rails::Generators::Base > const
end
end

Expand Down
11 changes: 8 additions & 3 deletions lib/tapioca/compilers/dsl/url_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,14 @@ def create_mixins_for(mod, constant, helper_module)

sig { params(mod: Module, helper: Module).returns(T::Boolean) }
def includes_helper?(mod, helper)
superclass_ancestors = mod.superclass&.ancestors if Class === mod
superclass_ancestors ||= []
(mod.ancestors - superclass_ancestors).include?(helper)
superclass_ancestors = []

if Class === mod
superclass = superclass_of(mod)
superclass_ancestors = ancestors_of(superclass) if superclass
end

(ancestors_of(mod) - superclass_ancestors).any? { |ancestor| helper == ancestor }
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/tapioca/compilers/dsl/rails_generators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ module Entirely
assert_equal(["AppGenerator", "NamedGenerator", "UnnamedGenerator"], gathered_constants)
end

it("does not gather XPath") do
add_ruby_file("xpath.rb", <<~RUBY)
require "xpath"
RUBY

assert_empty(gathered_constants)
end

it("ignores generator classes without a name") do
add_ruby_file("content.rb", <<~RUBY)
unnamed = Class.new(::Rails::Generators::Base)
Expand Down
16 changes: 16 additions & 0 deletions spec/tapioca/compilers/dsl/url_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ class MyClass < SuperClass
"SuperClass",
], gathered_constants)
end

it("does not gather XPath") do
add_ruby_file("xpath.rb", <<~RUBY)
require "xpath"
class Application < Rails::Application
end
RUBY

assert_equal([
"ActionDispatch::IntegrationTest",
"ActionView::Helpers",
"GeneratedPathHelpersModule",
"GeneratedUrlHelpersModule",
], gathered_constants)
end
end

describe("#decorate") do
Expand Down

0 comments on commit 5fc0939

Please sign in to comment.