Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
Remove activesupport as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
macournoyer committed Apr 7, 2021
1 parent ff658c8 commit 0cacdbd
Show file tree
Hide file tree
Showing 25 changed files with 300 additions and 224 deletions.
2 changes: 2 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Copyright 2020-present, Shopify Inc.

Contains code from activesupport Copyright (c) 2005-2019 David Heinemeier Hansson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Expand Down
1 change: 1 addition & 0 deletions lib/theme_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_relative "theme_check/printer"
require_relative "theme_check/shopify_liquid"
require_relative "theme_check/storage"
require_relative "theme_check/string_helpers"
require_relative "theme_check/file_system_storage"
require_relative "theme_check/in_memory_storage"
require_relative "theme_check/tags"
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def doc
end

def code_name
self.class.name.demodulize
StringHelpers.demodulize(self.class.name)
end

def ignore!
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/checks/asset_size_css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def href_to_file_size(href)
# asset_url (+ optional stylesheet_tag) variables
if href =~ /^#{VARIABLE}$/o && href =~ /asset_url/ && href =~ Liquid::QuotedString
asset_id = Regexp.last_match(0).gsub(START_OR_END_QUOTE, "")
asset = @theme.assets.find { |a| a.name.ends_with?("/" + asset_id) }
asset = @theme.assets.find { |a| a.name.end_with?("/" + asset_id) }
return if asset.nil?
asset.gzipped_size

Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/checks/asset_size_javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def src_to_file_size(src)
# More complicated liquid statements are not in scope.
if src =~ /^#{VARIABLE}$/o && src =~ /asset_url/ && src =~ Liquid::QuotedString
asset_id = Regexp.last_match(0).gsub(START_OR_END_QUOTE, "")
asset = @theme.assets.find { |a| a.name.ends_with?("/" + asset_id) }
asset = @theme.assets.find { |a| a.name.end_with?("/" + asset_id) }
return if asset.nil?
asset.gzipped_size
elsif src =~ %r{^(https?:)?//}
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/checks/img_width_and_height.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def record_offenses
def record_missing_field_offenses(img_match)
width = WIDTH_ATTRIBUTE.match(img_match[0])
height = HEIGHT_ATTRIBUTE.match(img_match[0])
return if width.present? && height.present?
return if width && height
missing_width = width.nil?
missing_height = height.nil?
error_message = if missing_width && missing_height
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/checks/matching_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize
end

def on_file(file)
return unless file.name.starts_with?("locales/")
return unless file.name.start_with?("locales/")
return unless file.content.is_a?(Hash)
return if file.name == @theme.default_locale_json&.name

Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/checks/undefined_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def ignore?(node)

def each_template
@files.each do |(name, info)|
next if name.starts_with?('snippets/')
next if name.start_with?('snippets/')
yield [name, info]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/checks/valid_html_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ValidHTMLTranslation < JsonCheck
doc docs_url(__FILE__)

def on_file(file)
return unless file.name.starts_with?("locales/")
return unless file.name.start_with?("locales/")
return unless file.content.is_a?(Hash)

visit_nested(file.content)
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def run(argv)
end

unless [:version, :init].include?(command)
@config = if config_path.present?
@config = if config_path
ThemeCheck::Config.new(
root: @path,
configuration: ThemeCheck::Config.load_file(config_path)
Expand Down
4 changes: 2 additions & 2 deletions lib/theme_check/disabled_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def comment_text(node)
end

def start_disabling?(text)
text.strip.starts_with?(DISABLE_START)
text.strip.start_with?(DISABLE_START)
end

def stop_disabling?(text)
text.strip.starts_with?(DISABLE_END)
text.strip.start_with?(DISABLE_END)
end

# Return a list of checks from a theme-check-disable comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FilterCompletionProvider < CompletionProvider
def completions(content, cursor)
return [] unless can_complete?(content, cursor)
available_labels
.select { |w| w.starts_with?(partial(content, cursor)) }
.select { |w| w.start_with?(partial(content, cursor)) }
.map { |filter| filter_to_completion(filter) }
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def completions(content, cursor)
return [] unless can_complete?(content, cursor)
partial = first_word(content) || ''
ShopifyLiquid::Object.labels
.select { |w| w.starts_with?(partial) }
.select { |w| w.start_with?(partial) }
.map { |object| object_to_completion(object) }
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def completions(content, cursor)
return [] unless cursor_on_quoted_argument?(content, cursor)
partial = snippet(content) || ''
snippets
.select { |x| x.starts_with?(partial) }
.select { |x| x.start_with?(partial) }
.map { |x| snippet_to_completion(x) }
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def completions(content, cursor)
return [] unless can_complete?(content, cursor)
partial = first_word(content) || ''
ShopifyLiquid::Tag.labels
.select { |w| w.starts_with?(partial) }
.select { |w| w.start_with?(partial) }
.map { |tag| tag_to_completion(tag) }
end

def can_complete?(content, cursor)
content.starts_with?(Liquid::TagStart) && (
content.start_with?(Liquid::TagStart) && (
cursor_on_first_word?(content, cursor) ||
cursor_on_start_content?(content, cursor, Liquid::TagStart)
)
Expand Down
3 changes: 1 addition & 2 deletions lib/theme_check/language_server/server.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true
require 'json'
require 'stringio'
require 'active_support/core_ext/string/inflections'

module ThemeCheck
module LanguageServer
Expand Down Expand Up @@ -99,7 +98,7 @@ def process_request
end

def to_snake_case(method_name)
method_name.gsub(/[^\w]/, '_').underscore
StringHelpers.underscore(method_name.gsub(/[^\w]/, '_'))
end

def initial_line
Expand Down
3 changes: 1 addition & 2 deletions lib/theme_check/node.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require 'active_support/core_ext/string/inflections'

module ThemeCheck
# A node from the Liquid AST, the result of parsing a template.
Expand Down Expand Up @@ -101,7 +100,7 @@ def line_number
# The `:under_score_name` of this type of node. Used to dispatch to the `on_<type_name>`
# and `after_<type_name>` check methods.
def type_name
@type_name ||= @value.class.name.demodulize.underscore.to_sym
@type_name ||= StringHelpers.underscore(StringHelpers.demodulize(@value.class.name)).to_sym
end

# Is this node inside a `{% liquid ... %}` block?
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def severity
end

def check_name
check.class.name.demodulize
StringHelpers.demodulize(check.class.name)
end

def doc
Expand Down
47 changes: 47 additions & 0 deletions lib/theme_check/string_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

module ThemeCheck
module StringHelpers
extend self

# Removes the module part from the expression in the string.
# Ported from ActiveSupport
#
# demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
# demodulize('Inflections') # => "Inflections"
# demodulize('::Inflections') # => "Inflections"
# demodulize('') # => ""
#
# See also #deconstantize.
def demodulize(path)
path = path.to_s
if (i = path.rindex("::"))
path[(i + 2)..-1]
else
path
end
end

# Makes an underscored, lowercase form from the expression in the string.
# Base on ActiveSupport's
#
# Changes '::' to '/' to convert namespaces to paths.
#
# underscore('ActiveModel') # => "active_model"
# underscore('ActiveModel::Errors') # => "active_model/errors"
#
# As a rule of thumb you can think of +underscore+ as the inverse of
# #camelize, though there are cases where that does not hold:
#
# camelize(underscore('SSLError')) # => "SslError"
def underscore(camel_cased_word)
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
word = camel_cased_word.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
3 changes: 1 addition & 2 deletions lib/theme_check/tags.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require "active_support/core_ext/string/starts_ends_with"

module ThemeCheck
module Tags
Expand All @@ -19,7 +18,7 @@ def initialize(tag_name, markup, options)
"Error in tag 'section' - Valid syntax: section '[type]'",
) unless match
@section_name = match[:section_name].tr(%('"), '')
@section_name.chomp!(".liquid") if @section_name.ends_with?(".liquid")
@section_name.chomp!(".liquid") if @section_name.end_with?(".liquid")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(storage)

def assets
@assets ||= @storage.files
.select { |path| path.starts_with?("assets/") }
.select { |path| path.start_with?("assets/") }
.map { |path| AssetFile.new(path, @storage) }
end

Expand Down
34 changes: 31 additions & 3 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# frozen_string_literal: true
require "test_helper"
require "active_support/testing/stream"

class CliTest < Minitest::Test
include ActiveSupport::Testing::Stream

def test_help
cli = ThemeCheck::Cli.new
assert_raises(ThemeCheck::Cli::Abort, /^usage: /) do
Expand Down Expand Up @@ -120,4 +117,35 @@ def test_init_abort_with_existing_config_file
cli.run([storage.root, "--init"])
end
end

private

# Ported from active_support/testing/stream
def silence_stream(stream)
old_stream = stream.dup
stream.reopen(IO::NULL)
stream.sync = true
yield
ensure
stream.reopen(old_stream)
old_stream.close
end

# Ported from active_support/testing/stream
def capture(stream)
stream = stream.to_s
captured_stream = Tempfile.new(stream)
stream_io = eval("$#{stream}") # # rubocop:disable Security/Eval
origin_stream = stream_io.dup
stream_io.reopen(captured_stream)

yield

stream_io.rewind
captured_stream.read
ensure
captured_stream.close
captured_stream.unlink
stream_io.reopen(origin_stream)
end
end
2 changes: 1 addition & 1 deletion test/documentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_assert_all_checks_are_documented
@checks.each do |check_class|
check = check_class.new
next if check.code_name == "MockCheck"
assert(check.doc.present?, "#{check.code_name} should have `doc docs_url(__FILE__)` in the class definition.")
assert(check.doc, "#{check.code_name} should have `doc docs_url(__FILE__)` in the class definition.")
assert(File.exist?(doc_to_file_path(check.doc)), "#{check.code_name} should be documented in docs/checks/check_class_name.md")
end
end
Expand Down
Loading

0 comments on commit 0cacdbd

Please sign in to comment.