Skip to content

Commit 618b116

Browse files
authored
Enable Frozen String Literals (#42)
Add `frozen_string_literal` magic comment to enable frozen string literals for Ruby versions that support it to prepare the library for Ruby 4, ensuring all specs pass and referencing community guidance. This change reduces allocations on Ruby 2.3 and newer, and despite initial concerns, benchmarks during testing show no evidence of regression. Since the library still supports Ruby versions earlier than 2.3 and potential issues cannot be ruled out for those versions, the CI workflow now sets ``` RUBYOPT='--enable=frozen-string-literal --debug=frozen-string-literal' ``` to help prevent future regressions. Code has been automatically fixed by RuboCop by temporarily change target version to 2.3 and running: ``` be rubocop --only Style/FrozenStringLiteralComment,Layout/EmptyLineAfterMagicComment,Style/RedundantFreeze,Style/MutableConstant -A lib/**/*.rb ``` Close #41
1 parent a1e9b71 commit 618b116

54 files changed

Lines changed: 120 additions & 8 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/tests.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ jobs:
1010
matrix:
1111
ruby: [ '3.1', '3.2', '3.3', '3.4', 'ruby-head' ]
1212

13+
env:
14+
# The RUBYOPT flags can be removed if RuboCop style checks are added
15+
# to the lint workflow and support for Ruby < 2.3 is dropped.
16+
RUBYOPT: '--enable=frozen-string-literal --debug=frozen-string-literal'
17+
1318
steps:
1419
- uses: actions/checkout@v4
1520
- name: Set up Ruby ${{ matrix.ruby }}
@@ -21,4 +26,4 @@ jobs:
2126
- name: Test with Rake
2227
run: bundle exec rake
2328
- uses: codecov/codecov-action@v3
24-
if: matrix.ruby == '3.4' # match version in spec_helper.rb:1
29+
if: matrix.ruby == '3.4' # match version in spec_helper.rb:3

CHANGELOG.md

Lines changed: 4 additions & 0 deletions

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in js_regex.gemspec

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
Dir['tasks/**/*.rake'].each { |file| load(file) }
24

35
require 'bundler/gem_tasks'

bin/console

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require 'bundler/setup'
45
require 'js_regex'

js_regex.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
dir = File.expand_path(__dir__)
24
require File.join(dir, 'lib', 'js_regex', 'version')
35

lib/js_regex.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# JsRegex converts ::Regexp instances to JavaScript.
24
#
35
# Usage:

lib/js_regex/conversion.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class JsRegex
24
#
35
# This class acts as a facade, passing a Regexp to the Converters.

lib/js_regex/converter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class JsRegex
24
module Converter
35
Dir[File.join(__dir__, 'converter', '*.rb')].sort.each do |file|

lib/js_regex/converter/anchor_converter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative 'base'
24

35
class JsRegex

0 commit comments

Comments
 (0)