Comments (1)
I'm seeing similar weird behavior regarding locations around rescue/else.
Here's a snippet that shows location ranges on a line, with some examples (all the whitespace is added so that char_pos
is a multiple of 10 when on_stmts_new
and on_stmts_add
is called by Ripper):
require "bundler/setup"
require "syntax_tree"
def loc(node, comment = nil)
location = node.location
puts " " * (location.start_char - 0).clamp(0..) +
"^" +
"-" * (location.end_char - location.start_char - 2).clamp(0..) +
"^" * (location.end_char - location.start_char - 1).clamp(0..1) +
" " * (70 - location.end_char - (location.end_char > location.start_char ? 0 : 1)).clamp(0..) +
"#{node.class} #{comment}"
end
def locations(code)
code
.tap { puts _1 }
.then { SyntaxTree.parse _1 }
.then { _1.statements.body.first }
.tap { loc _1 }
.then { _1.bodystmt }
.tap { loc _1 }
.tap { loc _1.rescue_clause }
.tap { loc _1.rescue_clause.statements, "rescue statements" }
.tap { loc _1.else_clause, "else clause" if _1.else_clause }
.tap { loc _1.ensure_clause }
end
locations("begin ; rescue; ensure ; end ;")
puts
locations("begin ; rescue ; else ; ensure ; end ;")
This outputs:
begin ; rescue; ensure ; end ;
^--------------------------------------------------------------^ SyntaxTree::Begin
^---------------------------------------------------------^ SyntaxTree::BodyStmt
^-------------------------------^ SyntaxTree::Rescue
^-------------------------^ SyntaxTree::Statements rescue statements
^------------------^ SyntaxTree::Ensure
which looks pretty much correct
and
begin ; rescue ; else ; ensure ; end ;
^--------------------------------------------------------------^ SyntaxTree::Begin
^---------------------------------------------------------^ SyntaxTree::BodyStmt
^---------------------------^ SyntaxTree::Rescue
^---------------------^ SyntaxTree::Statements rescue statements
^ SyntaxTree::Statements else clause
^----------------^ SyntaxTree::Ensure
which shows that something's wrong about the rescue statements and the else clause.
from syntax_tree.
Related Issues (20)
- Is it possible to configure indentation size? HOT 1
- VSCode does not pick up `stree` HOT 2
- Multiline blocks sometimes use braces, sometimes do/end
- ERB-formatter HOT 1
- Character literals are formatted incorrectly
- Plugin for not switching to `%w`/`%i` array syntax HOT 1
- Empty hash literal should be kept on one line
- subscript assignment should not be split into multiple lines
- Comments in long call chains regressed in 6.2.0
- Use `rubydoc.info` for API documentation
- Formatting changing type of hash key
- Constructor with block gets split onto new line even if it would fit HOT 1
- Prism integration status? HOT 1
- Block with long list of block vars doesn't get split onto multiple lines
- Disabling single-line conditional formatting
- Method chain ending with block always breaks on multiple lines due to argument
- `stree write` turns correct code into incorrect code for method call with mix of keyword types
- Issue with Ruby 3.1.5 HOT 4
- [Feature] Allow parsing top-level yield in Ruby 3.3 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from syntax_tree.