Comments (16)
I don't think that'll be necessary. At the moment the test suite runs rubocop after we've generated the test file, so that should ensure we're generating valid ruby as long as we rely on the test suite.
from plugin-ruby.
@kddeisz The existing test suite doesnt cover all cases currently, and I've hit issues with files that aren't runnable after prettier ruby has processed and updated them
I think it's functionality worth adding until prettier ruby always generates valid outputs for all code bases and we have a more extensive test suite
from plugin-ruby.
The problem is it's going to result in shelling out to two different processes and add a lot of time to the conversion. If we find breaking changes, we should put them into the test suite so that we have more coverage.
from plugin-ruby.
Either way, the most recent PR I'm pushing up should fix a lot of problems we've been having. Also don't want to dismiss your idea out of hand - let's keep talking about it if we keep running into issues.
from plugin-ruby.
I knew I wasn't being crazy; There's a flag for this sort of functionality already:
--debug-check
If you're worried that Prettier will change the correctness of your code, add --debug-check to the command. This will cause Prettier to print an error message if it detects that code correctness might have changed. Note that --write cannot be used with --debug-check.
https://prettier.io/docs/en/cli.html#debug-check
Nothing actionable here, but it just means it'll be easier to create new issues in the future with this flag
from plugin-ruby.
Example when running against rails checked out locally:
master $ yarn prettier --debug-check --plugin=. --parser=ruby "../rails/activesupport/lib/active_support/*.rb"
[etc etc]
../rails/activesupport/lib/active_support/configurable.rb[error] ../rails/activesupport/lib/active_support/configurable.rb: Error: /users/user/Documents/code/prettier-ruby-1/src/ripper.rb:33:in `block in parse': undefined method `[]' for nil:NilClass (NoMethodError)
[error] from /users/user/Documents/code/prettier-ruby-1/src/ripper.rb:30:in `tap'
[error] from /users/user/Documents/code/prettier-ruby-1/src/ripper.rb:30:in `parse'
[error] from /users/user/Documents/code/prettier-ruby-1/src/ripper.rb:228:in `<main>'
[error]
[error] at Object.module.exports [as parse] (/users/user/Documents/code/prettier-ruby-1/src/parse.js:9:11)
[error] at Object.parse$2 [as parse] (/users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:10641:19)
[error] at coreFormat (/users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:13858:23)
[error] at format (/users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:14117:73)
[error] at formatWithCursor (/users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:14133:12)
[error] at /users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:42401:15
[error] at Object.format (/users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:42420:12)
[error] at format$1 (/users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:43687:27)
[error] at /users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:43965:16
[error] at /users/user/Documents/code/prettier-ruby-1/node_modules/prettier/bin-prettier.js:43905:14
from plugin-ruby.
This is something the Python formatter Black does:
Also, as a temporary safety measure, Black will check that the reformatted code still produces a valid AST that is equivalent to the original. This slows it down. If you're feeling confident, use
--fast
.
from plugin-ruby.
Okay. I'm going to reopen this for future discussion as an option.
from plugin-ruby.
Does --debug-check
not do what you need?
from plugin-ruby.
I think given some of the issues raised recently about returning invalid code or non-formatting related bugs, having this automated would mean you can trust that the AST you start with is the same AST you're given without having to manually verify every change (which matters a lot on projects like mine which have 134k lines across 2300 files).
from plugin-ruby.
Have you tried --debug-check
@NoahTheDuke?
from plugin-ruby.
@j-f1 --debug-check
verifies that the AST is equivalent, but the Ruby prettier doesn't honour those semantics.
For instance rewriting if statements into ternaries, do ... end
blocks into single line { ... }
blocks, or even just adding brackets to method calls results in a different AST in Ruby:
$ prettier --debug-check --plugin=. --parser=ruby test/cases/next.rb
test/cases/next.rb[error] test/cases/next.rb: ast(input) !== ast(prettier(input))
[error] Index:
[error] ===================================================================
[error] ---
[error] +++
[error] @@ -187,20 +187,6 @@
[error] "body": [
[error] {
[error] - "type": "paren",
[error] - "body": [
[error] - {
[error] - "type": "stmts",
[error] - "body": [
[error] - {
[error] - "type": "@int",
[error] - "body": "1",
[error] - "start": 7,
[error] - "end": 7
[error] - }
[error] - ],
[error] - "start": 7,
[error] - "end": 7
[error] - }
[error] - ],
[error] + "type": "@int",
[error] + "body": "1",
[error] "start": 7,
[error] "end": 7
[error]
[error] Index:
[error] ===================================================================
[error] ---
[error] +++
[error] @@ -5,3 +5,3 @@
[error] [].each { next 1 }
[error]
[error] -[].each { next(1) }
[error] +[].each { next 1 }
[error]
from plugin-ruby.
My apologies, I mean that including the --debug-check
by default (making it work with --write
of course) would help overall, because you'd never accidentally forget to run it; you could "fire and forget" and be assured that using prettier-ruby wouldn't ever damage your code.
EDIT: AlanFoster made my point better for me than I could.
from plugin-ruby.
but the Ruby prettier doesn't honour those semantics
To fix that, I’d suggest wiring up a clean
function that normalizes the ASTs before debug-check
compares them.
My apologies, I mean that including the
--debug-check
by default (making it work with--write
of course) would help overall, because you'd never accidentally forget to run it; you could "fire and forget" and be assured that using prettier-ruby wouldn't ever damage your code.
I believe the reason we don’t enable it by default is that it’s slower due to having to parse the code twice and it isn’t usually necessary as long as the test suite is good enough.
from plugin-ruby.
(running the test suite against the rails codebase and checking for errors would be sweet)
from plugin-ruby.
Okay I'm going to close this again as it's pretty much stalled. Writing a clean function would require doing a pretty massive refactor and rerunning the parser would be very costly for performance. If there's another idea or PR, let's open up another one as this is starting to spam a lot of folks.
from plugin-ruby.
Related Issues (20)
- 4.0 fails on large array of hashes
- No parser could be inferred for file xyz.rb HOT 2
- HAML Formatting with long line HOT 1
- plugin going wrong in testing environment HOT 1
- unfriendly when use API prettier.format HOT 1
- Failed to get connection options from parse server in time HOT 1
- plugin-ruby does not support tab indentation HOT 4
- Change defaultOption.trailingComma to "all" to match Prettier for better diffs and easier code manipulation HOT 3
- Couldn't resolve parser "ruby" HOT 1
- Require blank line between function definitions HOT 1
- Prettier stopped working for Ruby files in VSCode HOT 8
- Any way to force prettier to use the plugin-ruby parser? HOT 1
- Can't run plugin HOT 1
- New release HOT 2
- Formatting is failing for certain files after #1407 HOT 2
- Change in method call chaining style
- Non-stable formatting with a multi-line comment in a method chain
- Weird chained method call block indent
- Option to keep curly braces HOT 1
- Error: undefined method `value' for :call:Symbol HOT 13
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 plugin-ruby.