Skip to content

Commit

Permalink
Enforce that the encoding sigil is the first one
Browse files Browse the repository at this point in the history
Otherwise it's ignored by Ruby.
  • Loading branch information
byroot committed Mar 24, 2020
1 parent 184f556 commit 66b2d6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/sorbet/sigils/enforce_sigil_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def autocorrect(_node)
FROZEN_REGEX = /#\s+frozen_string_literal:(?:\s+([\w]+))?/

PREFERRED_ORDER = {
SIGIL_REGEX => 'typed',
CODING_REGEX => 'encoding',
SIGIL_REGEX => 'typed',
INDENT_REGEX => 'warn_indent',
FROZEN_REGEX => 'frozen_string_literal',
}.freeze
Expand Down
26 changes: 13 additions & 13 deletions spec/cop/sorbet/sigils/enforce_sigil_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Foo; end

it('makes no offense when the magic comments are correctly ordered') do
expect_no_offenses(<<~RUBY)
# typed: true
# encoding: utf-8
# coding: utf-8
# typed: true
# warn_indent: true
# frozen_string_literal: true
class Foo; end
Expand All @@ -47,9 +47,9 @@ class Foo; end

it('makes no offense when the magic comments are correctly ordered with random comments in the middle') do
expect_no_offenses(<<~RUBY)
# coding: utf-8
# typed: true
# foo: 1
# coding: utf-8
# bar: true
# frozen_string_literal: true
# baz: "Hello, World"
Expand All @@ -60,25 +60,25 @@ class Foo; end
it('makes offense when two magic comments are not correctly ordered') do
expect_offense(<<~RUBY)
# frozen_string_literal: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: typed, encoding, warn_indent, frozen_string_literal.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: encoding, typed, warn_indent, frozen_string_literal.
# typed: true
^^^^^^^^^^^^^ Magic comments should be in the following order: typed, encoding, warn_indent, frozen_string_literal.
^^^^^^^^^^^^^ Magic comments should be in the following order: encoding, typed, warn_indent, frozen_string_literal.
class Foo; end
RUBY
end

it('makes offense when all magic comments are not correctly ordered') do
expect_offense(<<~RUBY)
# encoding: utf-8
^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: typed, encoding, warn_indent, frozen_string_literal.
^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: encoding, typed, warn_indent, frozen_string_literal.
# frozen_string_literal: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: typed, encoding, warn_indent, frozen_string_literal.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: encoding, typed, warn_indent, frozen_string_literal.
# warn_indent: true
^^^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: typed, encoding, warn_indent, frozen_string_literal.
^^^^^^^^^^^^^^^^^^^ Magic comments should be in the following order: encoding, typed, warn_indent, frozen_string_literal.
# typed: true
^^^^^^^^^^^^^ Magic comments should be in the following order: typed, encoding, warn_indent, frozen_string_literal.
^^^^^^^^^^^^^ Magic comments should be in the following order: encoding, typed, warn_indent, frozen_string_literal.
# coding: utf-8
^^^^^^^^^^^^^^^ Magic comments should be in the following order: typed, encoding, warn_indent, frozen_string_literal.
^^^^^^^^^^^^^^^ Magic comments should be in the following order: encoding, typed, warn_indent, frozen_string_literal.
class Foo; end
RUBY
end
Expand Down Expand Up @@ -109,9 +109,9 @@ class Foo; end
RUBY
expect(autocorrect_source(source))
.to(eq(<<~RUBY))
# typed: true
# encoding: utf-8
# coding: utf-8
# typed: true
# warn_indent: true
# frozen_string_literal: true
class Foo; end
Expand All @@ -133,11 +133,11 @@ class Foo; end
RUBY
expect(autocorrect_source(source))
.to(eq(<<~RUBY))
# typed: true
# foo
# encoding: utf-8
# bar: true
# foo
# coding: utf-8
# bar: true
# typed: true
# baz: "Hello"
# warn_indent: true
# frozen_string_literal: true
Expand Down

0 comments on commit 66b2d6f

Please sign in to comment.