Skip to content

Commit

Permalink
Stop monkey-patching String to add underscore
Browse files Browse the repository at this point in the history
This causes a confusion with the `String#underscore` method that Active
Support adds and causes problems like https://discourse.shopify.io/t/file-naming-inflection-issues-with-tapioca-dsl-verify-in-ci/19663

I moved the method onto the `Generator` class and also duplicated it
into the DSL test base class.
  • Loading branch information
paracycle committed Sep 3, 2021
1 parent 9381537 commit ffa085b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
18 changes: 0 additions & 18 deletions lib/tapioca/core_ext/string.rb

This file was deleted.

17 changes: 14 additions & 3 deletions lib/tapioca/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require "pathname"
require "thor"
require "rake"
require "tapioca/core_ext/string"

module Tapioca
class Generator < ::Thor::Shell::Color
Expand Down Expand Up @@ -372,7 +371,7 @@ def expected_rbis

sig { params(constant_name: String).returns(Pathname) }
def dsl_rbi_filename(constant_name)
config.outpath / "#{constant_name.underscore}.rbi"
config.outpath / "#{underscore(constant_name)}.rbi"
end

sig { params(gem_name: String, version: String).returns(Pathname) }
Expand Down Expand Up @@ -568,7 +567,7 @@ def compile_gem_rbi(gem)
def compile_dsl_rbi(constant_name, contents, outpath: config.outpath, quiet: false)
return if contents.nil?

rbi_name = constant_name.underscore + ".rbi"
rbi_name = underscore(constant_name) + ".rbi"
filename = outpath / rbi_name

out = String.new
Expand Down Expand Up @@ -702,5 +701,17 @@ def abort_if_pending_migrations!
Rails.application.load_tasks
Rake::Task["db:abort_if_pending_migrations"].invoke if Rake::Task.task_defined?("db:abort_if_pending_migrations")
end

sig { params(class_name: String).returns(String) }
def underscore(class_name)
return class_name unless /[A-Z-]|::/.match?(class_name)

word = class_name.to_s.gsub("::", "/")
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
end
end
15 changes: 13 additions & 2 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "content_helper"
require "template_helper"
require "isolation_helper"
require "tapioca/core_ext/string"

class DslSpec < Minitest::Spec
extend T::Sig
Expand Down Expand Up @@ -46,7 +45,19 @@ def target_class_name

sig { returns(String) }
def target_class_file
target_class_name.underscore
underscore(target_class_name)
end

sig { params(class_name: String).returns(String) }
def underscore(class_name)
return class_name unless /[A-Z-]|::/.match?(class_name)

word = class_name.to_s.gsub("::", "/")
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.tr!("-", "_")
word.downcase!
word
end

sig { params(str: String, indent: Integer).returns(String) }
Expand Down

0 comments on commit ffa085b

Please sign in to comment.