Skip to content

Commit

Permalink
Adds handling for column index in error reporting (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwessman authored Apr 29, 2024
1 parent 4f30440 commit 858d83c
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 134 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
## [Unreleased]

- Support Ruby 3.3 by handling yield in ERB specifically
- Use SyntaxTree own class for ParseError to get better error feedback
- Adds handling for keeping track of column index, to support better error messages

## [0.11.0] - 2024-04-23

Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,26 @@ Currently handles
Add this line to your application's Gemfile:

```ruby
gem "w_syntax_tree-erb", "~> 0.10", require: false
gem "w_syntax_tree-erb", "~> 0.11", require: false
```

> I added the `w_` prefix to avoid conflicts if there will ever be an official `syntax_tree-erb` gem.
## Usage

### Parsing

```sh
bundle exec stree ast --plugins=erb "./**/*.html.erb"
```

### Format

```sh
bundle exec stree --plugins=erb "./**/*.html.erb"
bundle exec stree write --plugins=erb "./**/*.html.erb"
```

From code:
### In code

```ruby
require "syntax_tree/erb"
Expand Down
3 changes: 2 additions & 1 deletion lib/syntax_tree/erb/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def visit_token(node)

# Visit a Document node.
def visit_document(node)
child_nodes = node.child_nodes.sort_by(&:location)
child_nodes =
node.child_nodes.sort_by { |node| node.location.start_char }

handle_child_nodes(child_nodes)

Expand Down
53 changes: 10 additions & 43 deletions lib/syntax_tree/erb/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,6 @@

module SyntaxTree
module ERB
# A Location represents a position for a node in the source file.
class Location
attr_reader :start_char, :end_char, :start_line, :end_line

def initialize(start_char:, end_char:, start_line:, end_line:)
@start_char = start_char
@end_char = end_char
@start_line = start_line
@end_line = end_line
end

def deconstruct_keys(keys)
{
start_char: start_char,
end_char: end_char,
start_line: start_line,
end_line: end_line
}
end

def to(other)
Location.new(
start_char: start_char,
start_line: start_line,
end_char: other.end_char,
end_line: other.end_line
)
end

def <=>(other)
start_char <=> other.start_char
end

def to_s
if start_line == end_line
"line #{start_line}, char #{start_char}..#{end_char}"
else
"line #{start_line},char #{start_char} to line #{end_line}, char #{end_char}"
end
end
end

# A parent node that contains a bit of shared functionality.
class Node
def format(q)
Expand Down Expand Up @@ -322,6 +280,7 @@ def initialize(
)
end

@content = content
@content = prepare_content(content)
@closing_tag = closing_tag
end
Expand Down Expand Up @@ -376,11 +335,19 @@ def prepare_content(content)

result
rescue SyntaxTree::Parser::ParseError => error
opening_location = opening_tag.location
content_location = content.first&.location || opening_location
raise(
SyntaxTree::Parser::ParseError.new(
"Could not parse ERB-tag: #{error.message}",
@opening_tag.location.start_line + error.lineno - 1,
error.column
(
if opening_location.start_line == error.lineno
opening_location.start_column + error.column - 1
else
content_location.start_column + error.column - 1
end
)
)
)
end
Expand Down
Loading

0 comments on commit 858d83c

Please sign in to comment.