prettier 3.2.2 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -2
  3. data/README.md +23 -59
  4. data/lib/prettier.rb +7 -5
  5. data/node_modules/prettier/LICENSE +5205 -0
  6. data/node_modules/prettier/README.md +109 -0
  7. data/node_modules/prettier/{bin-prettier.js → bin/prettier.cjs} +10 -5
  8. data/node_modules/prettier/doc.d.ts +240 -0
  9. data/node_modules/prettier/doc.js +1257 -1309
  10. data/node_modules/prettier/doc.mjs +1312 -0
  11. data/node_modules/prettier/index.cjs +655 -0
  12. data/node_modules/prettier/index.d.ts +915 -0
  13. data/node_modules/prettier/index.mjs +21845 -0
  14. data/node_modules/prettier/internal/cli.mjs +6978 -0
  15. data/node_modules/prettier/internal/internal.mjs +6531 -0
  16. data/node_modules/prettier/package.json +179 -6
  17. data/node_modules/prettier/plugins/acorn.d.ts +6 -0
  18. data/node_modules/prettier/plugins/acorn.js +13 -0
  19. data/node_modules/prettier/plugins/acorn.mjs +13 -0
  20. data/node_modules/prettier/plugins/angular.d.ts +8 -0
  21. data/node_modules/prettier/plugins/angular.js +2 -0
  22. data/node_modules/prettier/plugins/angular.mjs +2 -0
  23. data/node_modules/prettier/plugins/babel.d.ts +17 -0
  24. data/node_modules/prettier/plugins/babel.js +16 -0
  25. data/node_modules/prettier/plugins/babel.mjs +16 -0
  26. data/node_modules/prettier/plugins/estree.d.ts +0 -0
  27. data/node_modules/prettier/plugins/estree.js +35 -0
  28. data/node_modules/prettier/plugins/estree.mjs +35 -0
  29. data/node_modules/prettier/plugins/flow.d.ts +5 -0
  30. data/node_modules/prettier/plugins/flow.js +21 -0
  31. data/node_modules/prettier/plugins/flow.mjs +21 -0
  32. data/node_modules/prettier/plugins/glimmer.d.ts +5 -0
  33. data/node_modules/prettier/plugins/glimmer.js +30 -0
  34. data/node_modules/prettier/plugins/glimmer.mjs +30 -0
  35. data/node_modules/prettier/plugins/graphql.d.ts +5 -0
  36. data/node_modules/prettier/plugins/graphql.js +29 -0
  37. data/node_modules/prettier/plugins/graphql.mjs +29 -0
  38. data/node_modules/prettier/plugins/html.d.ts +8 -0
  39. data/node_modules/prettier/plugins/html.js +19 -0
  40. data/node_modules/prettier/plugins/html.mjs +19 -0
  41. data/node_modules/prettier/plugins/markdown.d.ts +7 -0
  42. data/node_modules/prettier/plugins/markdown.js +59 -0
  43. data/node_modules/prettier/plugins/markdown.mjs +59 -0
  44. data/node_modules/prettier/plugins/meriyah.d.ts +5 -0
  45. data/node_modules/prettier/plugins/meriyah.js +5 -0
  46. data/node_modules/prettier/plugins/meriyah.mjs +5 -0
  47. data/node_modules/prettier/plugins/postcss.d.ts +7 -0
  48. data/node_modules/prettier/plugins/postcss.js +52 -0
  49. data/node_modules/prettier/plugins/postcss.mjs +52 -0
  50. data/node_modules/prettier/plugins/typescript.d.ts +5 -0
  51. data/node_modules/prettier/plugins/typescript.js +27 -0
  52. data/node_modules/prettier/plugins/typescript.mjs +27 -0
  53. data/node_modules/prettier/plugins/yaml.d.ts +5 -0
  54. data/node_modules/prettier/plugins/yaml.js +161 -0
  55. data/node_modules/prettier/plugins/yaml.mjs +161 -0
  56. data/node_modules/prettier/standalone.d.ts +33 -0
  57. data/node_modules/prettier/standalone.js +34 -0
  58. data/node_modules/prettier/standalone.mjs +34 -0
  59. data/package.json +15 -10
  60. data/src/plugin.js +210 -14
  61. data/src/server.rb +50 -87
  62. metadata +60 -28
  63. data/CONTRIBUTING.md +0 -185
  64. data/node_modules/prettier/cli.js +0 -15085
  65. data/node_modules/prettier/index.js +0 -37282
  66. data/node_modules/prettier/parser-angular.js +0 -2
  67. data/node_modules/prettier/parser-babel.js +0 -27
  68. data/node_modules/prettier/parser-espree.js +0 -26
  69. data/node_modules/prettier/parser-flow.js +0 -35
  70. data/node_modules/prettier/parser-glimmer.js +0 -27
  71. data/node_modules/prettier/parser-graphql.js +0 -15
  72. data/node_modules/prettier/parser-html.js +0 -36
  73. data/node_modules/prettier/parser-markdown.js +0 -76
  74. data/node_modules/prettier/parser-meriyah.js +0 -19
  75. data/node_modules/prettier/parser-postcss.js +0 -76
  76. data/node_modules/prettier/parser-typescript.js +0 -280
  77. data/node_modules/prettier/parser-yaml.js +0 -150
  78. data/node_modules/prettier/third-party.js +0 -8978
  79. data/src/getInfo.js +0 -23
  80. data/src/netcat.js +0 -13
  81. data/src/parseSync.js +0 -236
data/CONTRIBUTING.md DELETED
@@ -1,185 +0,0 @@
1
- # Contributing
2
-
3
- Thanks so much for your interest in contributing! This page provides background on how prettier and this plugin work, links for understanding each step that prettier executes, various helpful utilities for analyzing the tree structures being manipulated, and frequently asked questions.
4
-
5
- Armed with the knowledge below, we would love to see your contribution! Please open an issue or a pull request at https://github.com/prettier/plugin-ruby.
6
-
7
- - [How it works](#how-it-works)
8
- - [Text to AST](#text-to-ast)
9
- - [AST to Doc](#ast-to-doc)
10
- - [Doc to text](#doc-to-text)
11
- - [FAQ](#faq)
12
- - [Useful links](#useful-links)
13
- - [Ruby](#ruby)
14
- - [Ripper](#ripper)
15
- - [Prettier](#prettier)
16
- - [Useful commands](#useful-commands)
17
- - [Testing](#testing)
18
-
19
- ## How it works
20
-
21
- In order to get printed, the code goes through a couple of transformations. The first is taking the entire file as text and parsing it into an abstract syntax tree (AST). The second is taking the AST and transforming it into prettier's intermediate representation, internally referred to as Docs. The final is taking the Doc nodes and converting them back into plain text, taking into account printing rules like max line length. Each of those steps is details below.
22
-
23
- ### Text to AST
24
-
25
- When the prettier process first spins up, it examines which files it's going to print and selects an appropriate plugin for each one. Once selected, it runs that plugin's `parse` function, seen [here](src/ruby/parser.js). For the case of the Ruby plugin, that entails spawning a Ruby process that runs [parser.rb](src/ruby/parser.rb) with the input code preloaded on stdin.
26
-
27
- `parser.rb` will read the text off of stdin and then feed it to a new `Ripper` instance, which is a Ruby standard library recursive-descent parser. Briefly, the way that `Ripper` works is by tokenizing the input and then matching those tokens against a grammar to form s-expressions. To extend `Ripper`, you overwrite the methods that control how those s-expressions are formed, e.g., to modify the s-expression that is formed when `Ripper` encounters a string literal, you would override the `#on_string_literal` method. Below is an example for seeing that in action.
28
-
29
- Let's assume you have the following code:
30
-
31
- ```ruby
32
- 1 + 1
33
- ```
34
-
35
- First, `Ripper` will tokenize:
36
-
37
- <!-- prettier-ignore -->
38
- ```ruby
39
- require 'ripper'
40
-
41
- pp Ripper.lex('1 + 1')
42
-
43
- =begin
44
- [[[1, 0], :on_int, "1", EXPR_END],
45
- [[1, 1], :on_sp, " ", EXPR_END],
46
- [[1, 2], :on_op, "+", EXPR_BEG],
47
- [[1, 3], :on_sp, " ", EXPR_BEG],
48
- [[1, 4], :on_int, "1", EXPR_END]]
49
- =end
50
- ```
51
-
52
- You can see it has location metadata (row and column), the token type, the value associated with that token type, and the lexer state when that token was encountered. Then, it will convert those tokens into s-expressions:
53
-
54
- <!-- prettier-ignore -->
55
- ```ruby
56
- require 'ripper'
57
-
58
- pp Ripper.sexp_raw('1 + 1')
59
-
60
- =begin
61
- [:program,
62
- [:stmts_add,
63
- [:stmts_new],
64
- [:binary, [:@int, "1", [1, 0]], :+, [:@int, "1", [1, 4]]]]]
65
- =end
66
- ```
67
-
68
- As you can see above, the resulting s-expressions will call the following methods in order on the instantiated `Ripper` instance: `on_int`, `on_int`, `on_stmts_new`, `on_binary`, `on_stmts_add`, `on_program`. You can hook into any part of this process by overriding any of those methods (we override all of them).
69
-
70
- Now that the text has been transformed into an AST that we can work with, `parser.rb` will serialize the result to JSON, write it back to stdout, and exit. The `parse` function will then parse that JSON by reading off the child process once it has exited, and return that value back to prettier.
71
-
72
- ### AST to Doc
73
-
74
- Once prettier has a working AST, it will take it and call the selected plugin's [`printNode` function](src/ruby/printer.js), whose purpose is to convert that AST into prettier's intermediate representation called Docs. It does this by handing the print function a `FastPath` object that keeps track of the state of the printing as it goes, and allows accessing various parts of the AST quickly.
75
-
76
- Effectively, it walks the AST in the reverse direction from the way `Ripper` built it (top-down instead of bottom-up). The first node that gets passed into the `print` function is the `program` node as that's always on top. Then it is the `program` node's responsibility to recursively call print on its child nodes as it best sees fit.
77
-
78
- As the nodes are printing themselves and their children, they're additionally building up a second AST. That AST is built using the `builder` commands from prettier core, described [here](https://github.com/prettier/prettier/blob/main/commands.md). As an example, below is how a `binary` node (like the one representing the `1 + 1` above) would handle printing itself:
79
-
80
- ```javascript
81
- const { group, indent, line } = require("prettier").doc.builders;
82
-
83
- const printBinary = (path, opts, print) =>
84
- group([
85
- [path.call(print, "body", 0), " "],
86
- path.getValue().body[1],
87
- indent([line, path.call(print, "body", 2)])
88
- ]);
89
- ```
90
-
91
- Recall that the `binary` node looks like this:
92
-
93
- ```
94
- [:binary, [:@int, "1", [1, 0]], :+, [:@int, "1", [1, 4]]]
95
- ```
96
-
97
- This means that there is a node in the `0` position of the array that represents the left-hand operand, and a node in the `2` position of the array that represents the right-hand operand. The operator is represented as a string literal in the `1` position of the array.
98
-
99
- So, the `printBinary` function is going to use the following `prettier` builders to build up the intermediate represention:
100
-
101
- - `group` - marks places where `prettier` could split text if the line gets too long; if the max line length is hit, `prettier` will break apart the outermost `group` node first
102
- - `indent` - increases the current print indent for the contents of the node if the parent node is broken, e.g., if the `binary` node is too long to fit on one line, it will indent the right-hand operand
103
- - `line` - puts a space if the group is not broken, otherwise puts a newline
104
-
105
- Once every node has been printed like the `binary` node above, the `print` node returns the intermediate representation to `prettier`.
106
-
107
- ### Doc to text
108
-
109
- At this point, this is where `prettier`'s printer takes over. Because the remainder of the process is language-agnostic and `prettier` knows how to handle its own `Doc` representation, the Ruby plugin no longer has a job to do. `prettier` will walk its own `Doc` nodes and print them out according to the rules established by the structure.
110
-
111
- ## FAQ
112
-
113
- Below are a couple of questions frequently asked about the prettier Ruby plugin.
114
-
115
- ### Why Ripper over the other available Ruby parsers?
116
-
117
- I've been asked this question a lot, and there are a lot of reasons for this:
118
-
119
- 1. Ripper is stdlib, and when the node process running prettier spawns the Ruby process performing the parsing, it's a lot easier to just require ripper than to worry about inconsistent environments having gems installed in myriad places.
120
- 2. Ripper is fast, which is nice as we need that to give the developer a good experience.
121
- 3. It was originally written in Ripper. Changing the parser at this point would entail an entire rewrite of the system, as the project is inextricably tied to the AST.
122
-
123
- ### Why are the Ruby options distinct from the JavaScript options?
124
-
125
- In order to support all preferences, the Ruby options need to be distinct from the JavaScript options, otherwise you would not be able to, for example, have single quotes in Ruby and double quotes in JavaScript. They also indicate somewhat different things. In JavaScript, nothing changes when you use single versus double quotes, whereas in Ruby single and doubles quotes change interpolation behavior.
126
-
127
- ### What versions of Ruby are supported?
128
-
129
- We support all Ruby versions that are under security maintenance or better (https://www.ruby-lang.org/en/downloads/branches/). This means at the moment we support back to Ruby 2.5. This is not to say that prettier can't parse and print code that was written while running earlier versions of Ruby, it's just that the Ruby process running prettier (whatever `ruby` resolves to when the process runs) must be >= 2.5.
130
-
131
- ### Do you support ERB files (.html.erb, .js.erb, etc.)?
132
-
133
- At the moment, prettier doesn't have baked-in support for template languages. The way it is accomplished in prettier core is to extend each of the individual parsers to have special placeholder AST nodes that then switch the printer over to the necessarily language. This is certainly possible to support ERB, but the work is still being investigated. See [this issue](https://github.com/prettier/plugin-ruby/issues/371) for more detail.
134
-
135
- ## Useful links
136
-
137
- For further understanding, below is a list of helpful resources.
138
-
139
- ### Ruby
140
-
141
- Links contained within the Ruby source that are relevant to this plugin.
142
-
143
- - [parse.y](https://github.com/ruby/ruby/blob/trunk/parse.y) - the parser generator within Ruby that gives you the names of the nodes as well as their structure
144
- - [test_parser_events.rb](https://github.com/ruby/ruby/blob/trunk/test/ripper/test_parser_events.rb) - the parser test file that gives you code examples of each kind of node
145
-
146
- ### Ripper
147
-
148
- Links relating to `ripper` and how it parses Ruby source.
149
-
150
- - [Ripper preview](https://ripper-preview.herokuapp.com) - a visualization of `ripper` and how it parses Ruby source
151
- - [How ripper parses variables](https://rmosolgo.github.io/blog/2018/05/21/how-ripper-parses-variables/) - blog post neatly explaining how `ripper` parses variables
152
- - [Ripper events](https://rmosolgo.github.io/ripper_events/) - documentation for each type of `ripper` node
153
-
154
- ### Prettier
155
-
156
- Links relating to `prettier` and its plugins.
157
-
158
- - [Prettier plugin documentation](https://prettier.io/docs/en/plugins.html) - documentation around `prettier`'s plugin system
159
- - [Builder commands](https://github.com/prettier/prettier/blob/main/commands.md) - the functions used to build the `prettier` IR
160
- - [Writing a Prettier plugin](https://medium.com/@fvictorio/how-to-write-a-plugin-for-prettier-a0d98c845e70) - a nice tutorial on how to build a `prettier` plugin
161
-
162
- ## Useful commands
163
-
164
- While developing, we've built a couple of small utilities for debugging the `prettier` printing process. To use them, first run `yarn` and `bundle` to install dependencies.
165
-
166
- - `bin/lex [file|source]` - outputs the tokens as ripper sees them
167
- - `bin/sexp [file|source]` - outputs the AST that ripper builds before it gets passed back to `prettier`
168
- - `bin/pragma [file]` - runs the `hasPragma` function against the given input file
169
- - `bin/print [file|source]` - outputs the printed source of a Ruby file after running it through `prettier`
170
-
171
- ## Testing
172
-
173
- There are two kinds of tests contained within this repository.
174
-
175
- The first are JavaScript tests (run with `jest`) that test the formatting against preconfigured fixtures to protect against regressions. They live in [test/js](test/js). To run them, run:
176
-
177
- ```
178
- $ yarn test
179
- ```
180
-
181
- The second are Ruby tests (run with `minitest`) that test the gem the wraps the `prettier` plugin as well as testing the various metadata attached to the AST nodes that `ripper` generates. They live in [test/rb](test/rb). To run them, run:
182
-
183
- ```
184
- $ bundle exec rake
185
- ```