Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ end

# gem "rbs", path: "../rbs"
gem "rbs", git: "https://github.com/ruby/rbs.git", branch: "master"
gem "herb", "~> 0.9"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PATH
concurrent-ruby (>= 1.1.10)
csv (>= 3.0.9)
fileutils (>= 1.1.0)
herb (>= 0.9.0)
json (>= 2.1.0)
language_server-protocol (>= 3.17.0.4, < 4.0)
listen (~> 3.0)
Expand Down Expand Up @@ -58,6 +59,7 @@ GEM
erb (6.0.2)
ffi (1.17.3)
fileutils (1.8.0)
herb (0.9.0)
i18n (1.14.8)
concurrent-ruby (~> 1.0)
io-console (0.8.2)
Expand Down Expand Up @@ -122,6 +124,7 @@ PLATFORMS

DEPENDENCIES
debug
herb (~> 0.9)
majo
memory_profiler
minitest (~> 5.25)
Expand Down
8 changes: 8 additions & 0 deletions lib/steep/source.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "herb"

module Steep
class Source
attr_reader :buffer
Expand Down Expand Up @@ -38,6 +40,8 @@ def self.new_parser
end

def self.parse(source_code, path:, factory:)
source_code = extract_ruby_from_erb(source_code) if path.to_s.end_with?(".erb")

buffer = ::Parser::Source::Buffer.new(path.to_s, 1, source: source_code)
node, comments = new_parser().parse_with_comments(buffer)

Expand Down Expand Up @@ -98,6 +102,10 @@ def self.parse(source_code, path:, factory:)
new(buffer: buffer, path: path, node: node, mapping: map, comments: comments, ignores: ignores)
end

def self.extract_ruby_from_erb(source_code)
::Herb.extract_ruby(source_code, semicolons: true, comments: true)
end

def self.construct_mapping(node:, annotations:, mapping:, line_range: nil)
case node.type
when :if
Expand Down
4 changes: 4 additions & 0 deletions sig/test/cli_test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class CLITest < Minitest::Test

def test_check_failure: () -> untyped

def test_erb_check_failure: () -> untyped

def test_erb_check_success: () -> untyped

def test_check_failure_with_formatter: () -> untyped

def test_check_group__target: () -> untyped
Expand Down
1 change: 1 addition & 0 deletions steep.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "strscan", ">= 1.0.0"
spec.add_runtime_dependency "csv", ">= 3.0.9"
spec.add_runtime_dependency "uri", ">= 0.12.0"
spec.add_runtime_dependency "herb", ">= 0.9"
end
46 changes: 46 additions & 0 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,52 @@ def test_check_failure
end
end

def test_erb_check_failure
in_tmpdir do
(current_dir + "Steepfile").write(<<-EOF)
target :app do
check "app/views/**/*.erb"
end
EOF

(current_dir + "app").mkdir
(current_dir + "app/views").mkdir
(current_dir + "app/views/companies").mkdir

(current_dir + "app/views/companies/_form.html.erb").write(<<-EOF)
<div> Count: <%= 1 + "2" %> </div>
EOF

stdout, status = sh(*steep, "check")

refute_predicate status, :success?, stdout
assert_match(/Detected 1 problem from 1 file/, stdout)
end
end

def test_erb_check_success
in_tmpdir do
(current_dir + "Steepfile").write(<<-EOF)
target :app do
check "app/views/**/*.erb"
end
EOF

(current_dir + "app").mkdir
(current_dir + "app/views").mkdir
(current_dir + "app/views/companies").mkdir

(current_dir + "app/views/companies/_form.html.erb").write(<<-EOF)
<div> Count: <%= 1 + 2 %> </div>
EOF

stdout, status = sh(*steep, "check")

assert_predicate status, :success?, stdout
assert_match(/No type error detected\./, stdout)
end
end

def test_check_failure_with_formatter
in_tmpdir do
(current_dir + "Steepfile").write(<<-EOF)
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def file_scheme

def assert_any(collection, &block)
assert collection.any?(&block)
end
end

# @rbs [T] (_Each[T], ?size: Integer) { (T) -> void } -> void
def assert_any!(collection, size: nil, &block)
Expand Down