Skip to content

Treat string literals as frozen#162

Merged
k0kubun merged 8 commits into
k0kubun:masterfrom
aliismayilov:frozen-strings
Oct 2, 2020
Merged

Treat string literals as frozen#162
k0kubun merged 8 commits into
k0kubun:masterfrom
aliismayilov:frozen-strings

Conversation

@aliismayilov

Copy link
Copy Markdown
Contributor

Once this PR is merged, it should be "safe" to use this gem in a project with "RUBYOPT=--enable-frozen-string-literal", where the strings will be treated as frozen literals by default.

@aliismayilov aliismayilov changed the title Treat strings as frozen literals Treat strings literals as frozen Oct 1, 2020
@aliismayilov aliismayilov changed the title Treat strings literals as frozen Treat string literals as frozen Oct 1, 2020
Comment thread .travis.yml Outdated
- "RUBYOPT='-w' bundle exec rake $TASK"
- bundle exec rake $TASK
env:
- RUBYOPT='-w --enable-frozen-string-literal'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with introducing frozen_string_literal: true and supporting --enable-frozen-string-literal with best-effort basis, however, I don't think it's a good idea to run CI with the non-default behavior. Besides, as long as we specify frozen_string_literal: true in every file, the behavior would be sufficiently tested anyway, at least inside Hamlit.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, I'd be okay with adding the following config:

    - rvm: 2.7.0
      env: TASK=test RUBYOPT='-w --enable-frozen-string-literal'

in the matrix.include if you like.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I see with frozen_string_literal: true is that in the long term the files will get moved around and new files will be introduced, and the comment might get forgotten.

I'd be happy with an extra job which runs the suite with --enable-frozen-string-literal option.

Comment thread lib/hamlit/compiler/comment_compiler.rb Outdated
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Existing files don't put a space after the pragma.

Comment thread lib/hamlit/parser/haml_helpers.rb Outdated
def find_and_preserve(input = nil, tags = haml_buffer.options[:preserve], &block)
return find_and_preserve(capture_haml(&block), input || tags) if block
tags = tags.each_with_object('') do |t, s|
tags = tags.each_with_object(+'') do |t, s|

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is forked from Haml. The long-term idea is to carve out haml-parser.gem from haml.gem and share it between Haml and Hamlit, but it hasn't been done yet. So currently we're maintaining the files under lib/hamlit/parser/*.rb by basically copying Haml's files and replacing class names.

The original lib/haml/helpers.rb already supports frozen_string_literal: true, but in a different manner. To avoid further deviations, could you synchronize the implementation from https://github.com/haml/haml/blob/770962fec4cfe9ad2d9c6cc676b63d92932e7634/lib/haml/helpers.rb#L111-L119?

Comment thread lib/hamlit/parser/haml_parser.rb Outdated
static_attributes[name] = val
else
dynamic_attributes << "#{inspect_obj(name)} => #{val},"
dynamic_attributes += "#{inspect_obj(name)} => #{val},"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Comment thread lib/hamlit/parser/haml_util.rb Outdated
# `["Foo (Bar (Baz bang) bop)", " (Bang (bop bip))"]` in the example above.
def balance(scanner, start, finish, count = 0)
str = ''
str = +''

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Comment thread test/haml/engine_test.rb Outdated

def test_encoding_error # encoding
render("foo\nbar\nb\xFEaz".force_encoding("utf-8"))
render("foo\nbar\nb\xFEaz".encode("utf-8"))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k0kubun

k0kubun commented Oct 1, 2020

Copy link
Copy Markdown
Owner

To resolve most of the above problems without waiting for haml-parser.gem, I can work on automating Haml parser/test synchronization to this repository, which should be done regardless of this PR.

If you agree with the above comments, I can take over this work from you. Please let me know if you like it.

@aliismayilov

Copy link
Copy Markdown
Contributor Author

To resolve most of the above problems without waiting for haml-parser.gem, I can work on automating Haml parser/test synchronization to this repository, which should be done regardless of this PR.

If you agree with the above comments, I can take over this work from you. Please let me know if you like it.

Yep, I agree with the comments. Feel free to take over :)

@k0kubun

k0kubun commented Oct 2, 2020

Copy link
Copy Markdown
Owner

I completed the automation at #163 and added a minor modification to this branch. Now it's ready to be merged. Thank you!

@k0kubun k0kubun merged commit 20eaa40 into k0kubun:master Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants