diff --git a/.eslintrc.json b/.eslintrc.json index ed46161f..79fafbfe 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -9,10 +9,13 @@ "arrow-parens": ["error", "as-needed"], "comma-dangle": ["error", "never"], "global-require": "off", - "no-unused-vars": ["error", { - "argsIgnorePattern": "^_", - "varsIgnorePattern": "^_" - }], + "no-unused-vars": [ + "error", + { + "argsIgnorePattern": "^_", + "varsIgnorePattern": "^_" + } + ], "object-curly-newline": "off", "prefer-spread": "off", "quotes": "off" diff --git a/.prettierignore b/.prettierignore index 01989dc4..a639810c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,3 +2,4 @@ /coverage/ /pkg/ /vendor/ +*.js diff --git a/.travis.yml b/.travis.yml index 1d80f124..469609db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,20 +1,20 @@ language: ruby rvm: -- ruby-head -- 2.6.3 -- 2.5.5 + - ruby-head + - 2.6.3 + - 2.5.5 branches: only: master before_install: bin/ci-install script: -- RUBY_VERSION=$(ruby -e 'puts RUBY_VERSION') yarn test -- yarn lint -- yarn test -- yarn print --check '**/*' -- bundle exec rake test + - RUBY_VERSION=$(ruby -e 'puts RUBY_VERSION') yarn test + - yarn lint + - yarn test + - yarn print --check '**/*' + - bundle exec rake test cache: -- bundler -- yarn + - bundler + - yarn matrix: allow_failures: - rvm: ruby-head diff --git a/CHANGELOG.md b/CHANGELOG.md index de984032..34245307 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,17 +7,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] ## [0.12.1] - 2019-04-22 + ### Changed + - If a lambda literal is nested under a `command` or `command_call` node anywhere in the heirarchy, then it needs to use the higher-precedence `{ ... }` braces as opposed to the `do ... end` delimiters. - Calling `super` with a block and no args was causing the parser to fail when attempting to inspect lambda nodes. (Thanks to @jpickwell for the report.) - Support better breaking within interpolation by grouping the interpolated content. ## [0.12.0] - 2019-04-18 + ### Added + - Automatically convert `lambda { ... }` method calls into `-> { ... }` literals. ## [0.11.0] - 2019-04-18 + ### Added + - Support for parsing things with a ruby shebang (e.g., `#!/usr/bin/env ruby` or `#!/usr/bin/ruby`). - [INTERNAL] Big tests refactor. - Make multiple `when` predicates break at 80 chars and then wrap to be inline with the other predicates. @@ -28,6 +34,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Support automatically transforming `def/begin/rescue/end/end` into `def/rescue/end`. ### Changed + - Fixed support for dynamic string hash keys. (Thanks to @deecewan.) - [INTERNAL] Moved `case/when` into its own file and added better documentation. - [INTERNAL] Moved `begin/rescue` into its own file. @@ -40,35 +47,46 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - [INTERNAL] Moved hashes into its own file. ## [0.10.0] - 2019-03-25 + ### Added + - Support for block-local variables. - Support for dyna-symbols that are using single quotes. ### Changed + - Force method calls after arrays, blocks, hashes, and xstrings to hang onto the end of the previous nodes. - Check before anything else for an invalid ruby version. ## [0.9.1] - 2019-03-24 + ### Changed + - Better support string quotes by favoring what the user chose if the string contains escape patterns. - Better support heredocs within method calls. ## [0.9.0] - 2019-03-18 + ### Added + - Support the `hasPragma` function. - Support the new `number_arg` node type in Ruby 2.7. ### Changed + - Limit the number of nodes that are allowed to turn into ternary expressions. ## [0.8.0] - 2019-03-08 + ### Added + - [INTERNAL] Add `eslint` and fix up existing violations. - Add the infra for the `prettier` ruby gem. (Thanks to @AlanFoster.) - Add a `rake` task for easier process integration for the ruby gem. - Handle direct interpolation of strings with %w array literals (i.e., `["#{foo}"]` should not be transformed into a %w array). ### Changed + - Fix string escaping for hex digit bit patterns when there's only one character after the "x". - Don't allow line breaks between brace block params. (Thanks to @AlanFoster.) - [INTERNAL] Switch over the array.rb test case to minitest. (Thanks to @johnschoeman.) @@ -79,10 +97,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Changed the ruby executable within the `prettier` gem to `rbprettier` for easier autocomplete. ### Removed + - All instances of the spread (`...`) operator so that we can support older versions of node. ## [0.7.0] - 2019-02-24 + ### Changed + - Support checking for escaping within strings to force double quotes (e.g., "\n"). - Handle cases with empty class and module declarations that need to break. (Thanks to @RossKinsella for the report.) - [INTERNAL] Align the `bin/print` and `bin/sexp` API to support `bin/print` taking a filepath. (Thanks to @AlanFoster.) @@ -101,14 +122,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Support multiple arguments to `undef`. (Thanks to @AlanFoster.) ## [0.6.3] - 2019-02-18 + ### Changed + - [INTERNAL] Switch over `binary` fixture to minitest. - [INTERNAL] Reconfigure parser into multiple layer modules so that it's easier to understand and manage. - Handle comments from within `begin`, `rescue`, `ensure`, `while`, and `until` nodes. - Properly indent heredocs without taking into account current indentation level. ## [0.6.2] - 2019-02-17 + ### Changed + - Handle regexp suffixes. (Thanks to @AlanFoster.) - [INTERNAL] Add support for testing the test fixtures with minitest. - [INTERNAL] Switch over `alias` and `regexp` tests to minitest. @@ -116,12 +141,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Handle blocks args when trailing commas are on. (Thanks to @christoomey for the report.) ## [0.6.1] - 2019-02-15 + ### Changed + - Fix Ruby 2.5 inline comments on `args_add_block` nodes. (Thanks to @meleyal for the report.) - Support passing `super()` explicitly with no arguments. (Thanks to @meleyal for the report.) ## [0.6.0] - 2019-02-14 + ### Added + - Handle non UTF-8 comments. - Handle non UTF-8 identifiers. - Handle non UTF-8 strings. @@ -129,58 +158,79 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Handle rescue with splats preceeding the exception names. ### Changed + - Use `JSON::fast_generate` to get the s-expressions back from the parser. - Handle broken lambdas from within `command` and `command_call` nodes. (Thanks to @NoahTheDuke for the report.) ## [0.5.2] - 2019-02-13 + ### Changed + - Support embedded expressions within strings that contain only keywords, as in `"#{super}"`. ## [0.5.1] - 2019-02-13 + ### Changed + - Force `do` blocks that we know have to be `do` blocks to break. (Thanks to @yuki24 for the report.) - Handle `command` and `command_call` nodes `do` blocks by forcing them to break. (Thanks to @kmcq for the report.) - Attach comments to full hash association nodes, not just the value. (Thanks to @ashfurrow for the report.) ## [0.5.0] - 2019-02-13 + ### Added + - Automatically convert arrays of all string literals to %w arrays. - Automatically convert arrays of all symbol literals to %i arrays. ### Changed + - [INTERNAL] Move the `args_add` and `args_new` handling into the parser. - Change `command_call` nodes to properly indent when broken and to not add a trailing comma. (Thanks to @uri for the report.) - Rename the `trailingComma` option to `addTrailingCommas` to not conflict with the JS option. ## [0.4.1] - 2019-02-12 + ### Changed + - [INTERNAL] Provide the `makeList` utility for the nodes that are lists from within ripper. - Again, this time for real, properly escape strings. (Thanks to @awinograd for the report.) - Fix up trailing commas on command calls. ## [0.4.0] - 2019-02-12 + ### Added + - Support the `trailingComma` configuration option (defaults to `false`). (Thanks to @Overload119 for the request.) ### Changed + - Pass the code to be formatted over `stdin`. (Thanks to @NoahTheDuke for the report.) ## [0.3.7] - 2019-02-11 + ### Changed + - Split up statements even if they started on the same line with `;`s unless they are within an embedded expression. - Properly handle escaped quotes within strings. ## [0.3.6] - 2019-02-10 + ### Changed + - Support the `not` operator properly. (Thanks to @AlanFoster for the report.) - Handle comments properly inside `if`, `unless`, and `when` nodes. (Thanks to @AlanFoster for the report.) ## [0.3.5] - 2019-02-09 + ### Changed + - Handle lonely operators in Ruby `2.5`. ## [0.3.4] - 2019-02-09 + ### Changed + - Comments are now properly attached inside `defs` nodes. - Support multiple inline comments on nodes. - Support inline comments from within the `EXPR_END|EXPR_LABEL` lexer state. @@ -189,29 +239,38 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Handle `next` node edge case with `args_add` as the body. (Thanks to @eins78 for the report.) ## [0.3.3] - 2019-02-09 + ### Changed + - Command nodes within conditionals now break parents to disallow them from being turned into ternary expressions. (Thanks to @bugthing for the report.) - Properly escape double quotes when using `preferSingleQuotes: false`. (Thanks to @awinograd for the report.) ## [0.3.2] - 2019-02-09 + ### Changed + - [INTERNAL] Don't define duplicated methods in the parser. - Let prettier know about `.rb` and `.rake` files so you don't have to specify the parser when running. - Renamed the package to @prettier/plugin-ruby. ## [0.3.1] - 2019-02-07 + ### Changed + - Automatically add parens to method declarations. - Handle comments on bare hash assocs. - Handle `method_add_block` nodes where the statements may be nested one more level. - Handle heredocs nested no matter how many levels deep. ## [0.3.0] - 2019-02-07 + ### Added + - Support squiggly heredocs. - Support straight heredocs. ### Changed + - Ignore current indentation when creating embdocs so that `=begin` is always at the beginning of the line. - [INTERNAL] Move `regexp_add` and `regexp_new` handling into the parser. - [INTERNAL] Move `xstring_add` and `xstring_new` handling into the parser. @@ -220,17 +279,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - [INTERNAL] Move `mlhs_add` and `mlhs_new` handling into the parser. ## [0.2.1] - 2019-02-06 + ### Changed + - Handle brace blocks on commands properly. - Break parent and return `do` blocks when called from a `command` node. - Handle edge cases with `if` statements where there is no body of the if (so it can't be converted to a ternary). ## [0.2.0] - 2019-02-06 + ### Added + - Handle `methref` nodes from Ruby `2.7`. - Allow `module` nodes to shorten using `;` when the block is empty. ### Changed + - Handle splat within an array, as in `[1, 2, *foo]`. - Disallow comments from being attached to intermediary regex nodes. - Fix `to_proc` transforms to reference the method called as opposed to the parameter name. @@ -242,11 +306,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - [INTERNAL] Handle intermediary array nodes in the parse for better comment handling. ## [0.1.2] - 2019-02-05 + ### Changed + - Handle guard clauses that return with no parens. ## [0.1.1] - 2019-02-05 + ### Changed + - Handle class method calls with the `::` operator. - Handle strings with apostrophes when using `preferSingleQuote`. - [INTERAL] Have travis run multiple ruby versions. @@ -254,10 +322,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Disallow comments from being attached to intermediary string nodes. ## [0.1.0] - 2019-02-04 + ### Added + - Initial release 🎉 -[Unreleased]: https://github.com/prettier/plugin-ruby/compare/v0.12.1...HEAD +[unreleased]: https://github.com/prettier/plugin-ruby/compare/v0.12.1...HEAD [0.12.1]: https://github.com/prettier/plugin-ruby/compare/v0.12.0...v0.12.1 [0.12.0]: https://github.com/prettier/plugin-ruby/compare/v0.11.0...v0.12.0 [0.11.0]: https://github.com/prettier/plugin-ruby/compare/v0.10.0...v0.11.0 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index d7fd5f1b..06a97449 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -14,22 +14,22 @@ appearance, race, religion, or sexual identity and orientation. Examples of behavior that contributes to creating a positive environment include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members Examples of unacceptable behavior by participants include: -* The use of sexualized language or imagery and unwelcome sexual attention or - advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic - address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting +- The use of sexualized language or imagery and unwelcome sexual attention or + advances +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic + address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a + professional setting ## Our Responsibilities diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bc0b0222..b2496d16 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,6 +14,7 @@ Ripper is the built-in CRuby lexer and parser that understands your Ruby code. I You can explore Ripper within an interactive Ruby terminal. The process of identifying the tokens within your source code is called lexing: + ```ruby $ irb > require 'ripper' @@ -24,6 +25,7 @@ $ irb Once the tokens are understood, Ripper constructs an abstract syntax tree: + ```ruby $ irb > require 'ripper' diff --git a/README.md b/README.md index 2252093e..6ed49ca3 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Under the hood `@prettier/plugin-ruby` uses Ruby's own `ripper` library which al For example, the below [code segment](http://www.rubyinside.com/advent2006/4-ruby-obfuscation.html): + ```ruby d=[30644250780,9003106878, 30636278846,66641217692,4501790980, @@ -48,16 +49,16 @@ when run through `@prettier/plugin-ruby` will generate: ```ruby d = [ - 30644250780, - 9003106878, - 30636278846, - 66641217692, - 4501790980, + 30_644_250_780, + 9_003_106_878, + 30_636_278_846, + 66_641_217_692, + 4_501_790_980, 671_24_603036, 131_61973916, 66_606629_920, - 30642677916, - 30643069058 + 30_642_677_916, + 30_643_069_058 ] a, s = [], $*[0] s.each_byte { |b| a << ('%036b' % d[b.chr.to_i]).scan(/\d{6}/) } diff --git a/bin/print b/bin/print index b7cbb645..90095fd7 100755 --- a/bin/print +++ b/bin/print @@ -5,6 +5,9 @@ const prettier = require("prettier"); const code = fs.existsSync(process.argv[2]) ? fs.readFileSync(process.argv[2], "utf-8") - : process.argv.slice(2).join(" ").replace(/\\n/g, "\n"); + : process.argv + .slice(2) + .join(" ") + .replace(/\\n/g, "\n"); console.log(prettier.format(code, { parser: "ruby", plugins: ["."] }));