Skip to content

Commit 858d83c

Browse files
authored
Adds handling for column index in error reporting (#85)
1 parent 4f30440 commit 858d83c

File tree

7 files changed

+179
-134
lines changed

7 files changed

+179
-134
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
77
## [Unreleased]
88

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

1113
## [0.11.0] - 2024-04-23
1214

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,26 @@ Currently handles
3030
Add this line to your application's Gemfile:
3131

3232
```ruby
33-
gem "w_syntax_tree-erb", "~> 0.10", require: false
33+
gem "w_syntax_tree-erb", "~> 0.11", require: false
3434
```
3535

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

40+
### Parsing
41+
42+
```sh
43+
bundle exec stree ast --plugins=erb "./**/*.html.erb"
44+
```
45+
46+
### Format
47+
4048
```sh
41-
bundle exec stree --plugins=erb "./**/*.html.erb"
49+
bundle exec stree write --plugins=erb "./**/*.html.erb"
4250
```
4351

44-
From code:
52+
### In code
4553

4654
```ruby
4755
require "syntax_tree/erb"

lib/syntax_tree/erb/format.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def visit_token(node)
2020

2121
# Visit a Document node.
2222
def visit_document(node)
23-
child_nodes = node.child_nodes.sort_by(&:location)
23+
child_nodes =
24+
node.child_nodes.sort_by { |node| node.location.start_char }
2425

2526
handle_child_nodes(child_nodes)
2627

lib/syntax_tree/erb/nodes.rb

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,6 @@
22

33
module SyntaxTree
44
module ERB
5-
# A Location represents a position for a node in the source file.
6-
class Location
7-
attr_reader :start_char, :end_char, :start_line, :end_line
8-
9-
def initialize(start_char:, end_char:, start_line:, end_line:)
10-
@start_char = start_char
11-
@end_char = end_char
12-
@start_line = start_line
13-
@end_line = end_line
14-
end
15-
16-
def deconstruct_keys(keys)
17-
{
18-
start_char: start_char,
19-
end_char: end_char,
20-
start_line: start_line,
21-
end_line: end_line
22-
}
23-
end
24-
25-
def to(other)
26-
Location.new(
27-
start_char: start_char,
28-
start_line: start_line,
29-
end_char: other.end_char,
30-
end_line: other.end_line
31-
)
32-
end
33-
34-
def <=>(other)
35-
start_char <=> other.start_char
36-
end
37-
38-
def to_s
39-
if start_line == end_line
40-
"line #{start_line}, char #{start_char}..#{end_char}"
41-
else
42-
"line #{start_line},char #{start_char} to line #{end_line}, char #{end_char}"
43-
end
44-
end
45-
end
46-
475
# A parent node that contains a bit of shared functionality.
486
class Node
497
def format(q)
@@ -322,6 +280,7 @@ def initialize(
322280
)
323281
end
324282

283+
@content = content
325284
@content = prepare_content(content)
326285
@closing_tag = closing_tag
327286
end
@@ -376,11 +335,19 @@ def prepare_content(content)
376335

377336
result
378337
rescue SyntaxTree::Parser::ParseError => error
338+
opening_location = opening_tag.location
339+
content_location = content.first&.location || opening_location
379340
raise(
380341
SyntaxTree::Parser::ParseError.new(
381342
"Could not parse ERB-tag: #{error.message}",
382343
@opening_tag.location.start_line + error.lineno - 1,
383-
error.column
344+
(
345+
if opening_location.start_line == error.lineno
346+
opening_location.start_column + error.column - 1
347+
else
348+
content_location.start_column + error.column - 1
349+
end
350+
)
384351
)
385352
)
386353
end

0 commit comments

Comments
 (0)