From d134875c7f3d6400e80a17397105a5161ee090ce Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 25 May 2019 16:41:16 +0900 Subject: [PATCH] Freeze some constants --- lib/haml/attribute_parser.rb | 2 +- lib/haml/error.rb | 48 ++++++++++++++++---------------- lib/haml/exec.rb | 2 +- lib/haml/helpers.rb | 2 +- lib/haml/parser.rb | 6 ++-- test/engine_test.rb | 2 +- test/template_test.rb | 2 +- test/temple_line_counter_test.rb | 2 +- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lib/haml/attribute_parser.rb b/lib/haml/attribute_parser.rb index b852418c8b..3f87505ed6 100644 --- a/lib/haml/attribute_parser.rb +++ b/lib/haml/attribute_parser.rb @@ -16,7 +16,7 @@ class UnexpectedKeyError < StandardError; end TYPE = 1 TEXT = 2 - IGNORED_TYPES = %i[on_sp on_ignored_nl] + IGNORED_TYPES = %i[on_sp on_ignored_nl].freeze class << self # @return [Boolean] - return true if AttributeParser.parse can be used. diff --git a/lib/haml/error.rb b/lib/haml/error.rb index 599bcb3b87..8a9d5c0133 100644 --- a/lib/haml/error.rb +++ b/lib/haml/error.rb @@ -5,29 +5,29 @@ module Haml class Error < StandardError MESSAGES = { - :bad_script_indent => '"%s" is indented at wrong level: expected %d, but was at %d.', - :cant_run_filter => 'Can\'t run "%s" filter; you must require its dependencies first', - :cant_use_tabs_and_spaces => "Indentation can't use both tabs and spaces.", - :deeper_indenting => "The line was indented %d levels deeper than the previous line.", - :filter_not_defined => 'Filter "%s" is not defined.', - :gem_install_filter_deps => '"%s" filter\'s %s dependency missing: try installing it or adding it to your Gemfile', - :illegal_element => "Illegal element: classes and ids must have values.", - :illegal_nesting_content => "Illegal nesting: nesting within a tag that already has content is illegal.", - :illegal_nesting_header => "Illegal nesting: nesting within a header command is illegal.", - :illegal_nesting_line => "Illegal nesting: content can't be both given on the same line as %%%s and nested within it.", - :illegal_nesting_plain => "Illegal nesting: nesting within plain text is illegal.", - :illegal_nesting_self_closing => "Illegal nesting: nesting within a self-closing tag is illegal.", - :inconsistent_indentation => "Inconsistent indentation: %s used for indentation, but the rest of the document was indented using %s.", - :indenting_at_start => "Indenting at the beginning of the document is illegal.", - :install_haml_contrib => 'To use the "%s" filter, please install the haml-contrib gem.', - :invalid_attribute_list => 'Invalid attribute list: %s.', - :invalid_filter_name => 'Invalid filter name ":%s".', - :invalid_tag => 'Invalid tag: "%s".', - :missing_if => 'Got "%s" with no preceding "if"', - :no_ruby_code => "There's no Ruby code for %s to evaluate.", - :self_closing_content => "Self-closing tags can't have content.", - :unbalanced_brackets => 'Unbalanced brackets.', - :no_end => <<-END + bad_script_indent: '"%s" is indented at wrong level: expected %d, but was at %d.', + cant_run_filter: 'Can\'t run "%s" filter; you must require its dependencies first', + cant_use_tabs_and_spaces: "Indentation can't use both tabs and spaces.", + deeper_indenting: "The line was indented %d levels deeper than the previous line.", + filter_not_defined: 'Filter "%s" is not defined.', + gem_install_filter_deps: '"%s" filter\'s %s dependency missing: try installing it or adding it to your Gemfile', + illegal_element: "Illegal element: classes and ids must have values.", + illegal_nesting_content: "Illegal nesting: nesting within a tag that already has content is illegal.", + illegal_nesting_header: "Illegal nesting: nesting within a header command is illegal.", + illegal_nesting_line: "Illegal nesting: content can't be both given on the same line as %%%s and nested within it.", + illegal_nesting_plain: "Illegal nesting: nesting within plain text is illegal.", + illegal_nesting_self_closing: "Illegal nesting: nesting within a self-closing tag is illegal.", + inconsistent_indentation: "Inconsistent indentation: %s used for indentation, but the rest of the document was indented using %s.", + indenting_at_start: "Indenting at the beginning of the document is illegal.", + install_haml_contrib: 'To use the "%s" filter, please install the haml-contrib gem.', + invalid_attribute_list: 'Invalid attribute list: %s.', + invalid_filter_name: 'Invalid filter name ":%s".', + invalid_tag: 'Invalid tag: "%s".', + missing_if: 'Got "%s" with no preceding "if"', + no_ruby_code: "There's no Ruby code for %s to evaluate.", + self_closing_content: "Self-closing tags can't have content.", + unbalanced_brackets: 'Unbalanced brackets.', + no_end: <<-END You don't need to use "- end" in Haml. Un-indent to close a block: - if foo? %strong Foo! @@ -35,7 +35,7 @@ class Error < StandardError Not foo. %p This line is un-indented, so it isn't part of the "if" block END - } + }.freeze def self.message(key, *args) string = MESSAGES[key] or raise "[HAML BUG] No error messages for #{key}" diff --git a/lib/haml/exec.rb b/lib/haml/exec.rb index 7a119a83c0..b516017176 100644 --- a/lib/haml/exec.rb +++ b/lib/haml/exec.rb @@ -121,7 +121,7 @@ def process_result @options[:input], @options[:output] = input, output end - COLORS = { :red => 31, :green => 32, :yellow => 33 } + COLORS = {red: 31, green: 32, yellow: 33}.freeze # Prints a status message about performing the given action, # colored using the given color (via terminal escapes) if possible. diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 7db87b67fa..c932aec44b 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -593,7 +593,7 @@ def haml_tag_if(condition, *tag) end # Characters that need to be escaped to HTML entities from user input - HTML_ESCAPE = { '&' => '&', '<' => '<', '>' => '>', '"' => '"', "'" => ''' } + HTML_ESCAPE = {'&' => '&', '<' => '<', '>' => '>', '"' => '"', "'" => '''}.freeze HTML_ESCAPE_REGEX = /['"><&]/ diff --git a/lib/haml/parser.rb b/lib/haml/parser.rb index 13148d96ef..c5b339ba75 100644 --- a/lib/haml/parser.rb +++ b/lib/haml/parser.rb @@ -61,7 +61,7 @@ class Parser SILENT_SCRIPT, ESCAPE, FILTER - ] + ].freeze # The value of the character that designates that a line is part # of a multiline string. @@ -75,8 +75,8 @@ class Parser # BLOCK_WITH_SPACES = /do\s*\|\s*[^\|]*\s+\|\z/ - MID_BLOCK_KEYWORDS = %w[else elsif rescue ensure end when] - START_BLOCK_KEYWORDS = %w[if begin case unless] + MID_BLOCK_KEYWORDS = %w[else elsif rescue ensure end when].freeze + START_BLOCK_KEYWORDS = %w[if begin case unless].freeze # Try to parse assignments to block starters as best as possible START_BLOCK_KEYWORD_REGEX = /(?:\w+(?:,\s*\w+)*\s*=\s*)?(#{START_BLOCK_KEYWORDS.join('|')})/ BLOCK_KEYWORD_REGEX = /^-?\s*(?:(#{MID_BLOCK_KEYWORDS.join('|')})|#{START_BLOCK_KEYWORD_REGEX.source})\b/ diff --git a/test/engine_test.rb b/test/engine_test.rb index 73147ab006..6ebe4a78cd 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -80,7 +80,7 @@ class EngineTest < Haml::TestCase "foo\n:plain\n 1\n 2\n \#{raise 'foo'}" => ["foo", 5], "= raise 'foo'\nfoo\nbar\nbaz\nbang" => ["foo", 1], "- case 1\n\n- when 1\n - raise 'foo'" => ["foo", 4], - } + }.freeze User = Struct.new('User', :id) CustomHamlClass = Struct.new(:id) do diff --git a/test/template_test.rb b/test/template_test.rb index b55ae10795..bdadea86f8 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -11,7 +11,7 @@ class TemplateTest < Haml::TestCase whitespace_handling original_engine list helpful silent_script tag_parsing just_stuff partials nuke_outer_whitespace nuke_inner_whitespace bemit - render_layout partial_layout partial_layout_erb} + render_layout partial_layout partial_layout_erb}.freeze def setup @base = create_base diff --git a/test/temple_line_counter_test.rb b/test/temple_line_counter_test.rb index c6b6e5be44..15774565bd 100644 --- a/test/temple_line_counter_test.rb +++ b/test/temple_line_counter_test.rb @@ -24,7 +24,7 @@ class TempleLineCounterTest < Haml::TestCase [:escape, true, [:static, "foo\nbar"]], [:escape, :once, [:static, "foo\nbar"]], [:escape, false, [:dynamic, "foo\nbar"]], - ] + ].freeze def test_count_lines TESTED_TEMPLES.each do |temple|