prettier 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8e2166de3bdb098137b5445e9db41d9c6eb1f1677fe8edfd9bbb7fb25704091
4
- data.tar.gz: 8284b60434648ad65daae6a2d24677ec197cde21b986e69e88adcbc0560592bf
3
+ metadata.gz: 4601f1c4bcd732027d5e7077aeece6ce80485b33f6d6122149903a3b768bae01
4
+ data.tar.gz: 7b8934c54be0b0125b25325d45a82d801314c9a4f6074033f9e719db8e873ffc
5
5
  SHA512:
6
- metadata.gz: 9d94d15c6f8c4b295596bfd821884937954ebc854cc6d2c292a8ea77a8ba23d99bf3c80cda43bcd8c5fb60b72e334b7f90ae18729b884c8c2a7eab6b44bda0d3
7
- data.tar.gz: 563ab1b61b66e0ade505742063c9c949c16087942d68853eb07ec9ee4b4d56ec69219355ab80a230028efa44af1c9012a30c1556743f19025ffe625c8f2affbd
6
+ metadata.gz: 5b9adb4c97a45dd1323cd36af7e8fbd6b542a5bb15f9c5c5824155ee09ea2c6cd2d4b0e27eedcfb4c3d79b1045fa7454cfba3ead575262a1740352a74f47798d
7
+ data.tar.gz: 3e6bd216100a983f702ca001c1f2578609361fe11e7e7a3ffa70da6b8a0db988d23afd3d7ba5328dfea8f0b1fe5d124bd9767f498708c931aef27e6f05994c40
data/CHANGELOG.md CHANGED
@@ -6,6 +6,88 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.15.0] - 2019-08-06
10
+
11
+ ### Changed
12
+
13
+ - If xstring literals (command line calls surrounded by backticks) break, then we indent and place the command on a new line. Previously, this was resulting in new lines getting added each time the code was formatted. Now this happens correctly. (Thanks to @dudeofawesome for the report.)
14
+ - When a `while` or `until` loop modifies a `begin...end` statement, it must remain in the modifier form or else it changes sematic meaning. For example,
15
+
16
+ <!-- prettier-ignore -->
17
+ ```ruby
18
+ begin
19
+ foo
20
+ end while bar
21
+ ```
22
+
23
+ cannot be transformed into:
24
+
25
+ <!-- prettier-ignore -->
26
+ ```ruby
27
+ while bar
28
+ foo
29
+ end
30
+ ```
31
+
32
+ because that would never execute `foo` if `bar` is falsy, whereas in the initial example it would have. (Thanks to @krachtstefan for the report.)
33
+
34
+ - When transforming a block into the `Symbol#to_proc` syntax from within a list of arguments inside of an `aref` node (i.e., `foo[:bar].each`), we can't put the block syntax inside the brackets. (Thanks to @jviney for the report.)
35
+ - Values for the `return` keyword that broke the line were previously just printed as they were, which breaks if you have a block expression like an `if` or `while`. For example,
36
+
37
+ <!-- prettier-ignore -->
38
+ ```ruby
39
+ return foo ? bar : baz
40
+ ```
41
+
42
+ if the line was set to very short would be printed as:
43
+
44
+ <!-- prettier-ignore -->
45
+ ```ruby
46
+ return if foo
47
+ bar
48
+ else
49
+ baz
50
+ end
51
+ ```
52
+
53
+ which wouldn't work. Instead, they now get printed with parentheses around the value, as in:
54
+
55
+ <!-- prettier-ignore -->
56
+ ```ruby
57
+ return(
58
+ if foo
59
+ bar
60
+ else
61
+ baz
62
+ end
63
+ )
64
+ ```
65
+
66
+ (Thanks to @jakeprime for the report.)
67
+
68
+ - When switching from a double-quoted string to a single-quoted string that contained escaped double quotes, the backslashes would stay in the string. As in:
69
+
70
+ <!-- prettier-ignore -->
71
+ ```ruby
72
+ "Foo \"Bar\" Baz"
73
+ ```
74
+
75
+ would get formatted as:
76
+
77
+ <!-- prettier-ignore -->
78
+ ```ruby
79
+ 'Foo \"Bar\" Baz'
80
+ ```
81
+
82
+ but now gets formatted as:
83
+
84
+ <!-- prettier-ignore -->
85
+ ```ruby
86
+ 'Foo "Bar" Baz'
87
+ ```
88
+
89
+ (Thanks to @jakeprime for the report.)
90
+
9
91
  ## [0.14.0] - 2019-07-17
10
92
 
11
93
  ### Added
@@ -463,7 +545,8 @@ would previously result in `array[]`, but now prints properly. (Thanks to @xipgr
463
545
 
464
546
  - Initial release 🎉
465
547
 
466
- [unreleased]: https://github.com/prettier/plugin-ruby/compare/v0.14.0...HEAD
548
+ [unreleased]: https://github.com/prettier/plugin-ruby/compare/v0.15.0...HEAD
549
+ [0.15.0]: https://github.com/prettier/plugin-ruby/compare/v0.14.0...v0.15.0
467
550
  [0.14.0]: https://github.com/prettier/plugin-ruby/compare/v0.13.0...v0.14.0
468
551
  [0.13.0]: https://github.com/prettier/plugin-ruby/compare/v0.12.3...v0.13.0
469
552
  [0.12.3]: https://github.com/prettier/plugin-ruby/compare/v0.12.2...v0.12.3
data/README.md CHANGED
@@ -159,7 +159,8 @@ The package is available as open source under the terms of the [MIT License](htt
159
159
  Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
160
160
 
161
161
  <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
162
- <!-- prettier-ignore -->
162
+ <!-- prettier-ignore-start -->
163
+ <!-- markdownlint-disable -->
163
164
  <table>
164
165
  <tr>
165
166
  <td align="center"><a href="https://kevindeisz.com"><img src="https://avatars2.githubusercontent.com/u/5093358?v=4" width="100px;" alt="Kevin Deisz"/><br /><sub><b>Kevin Deisz</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/commits?author=kddeisz" title="Code">💻</a> <a href="https://github.com/prettier/plugin-ruby/commits?author=kddeisz" title="Documentation">📖</a> <a href="https://github.com/prettier/plugin-ruby/commits?author=kddeisz" title="Tests">⚠️</a> <a href="https://github.com/prettier/plugin-ruby/issues?q=author%3Akddeisz" title="Bug reports">🐛</a></td>
@@ -184,9 +185,18 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
184
185
  <td align="center"><a href="https://www.andywaite.com"><img src="https://avatars1.githubusercontent.com/u/13400?v=4" width="100px;" alt="Andy Waite"/><br /><sub><b>Andy Waite</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/commits?author=andyw8" title="Documentation">📖</a></td>
185
186
  <td align="center"><a href="https://github.com/jviney"><img src="https://avatars3.githubusercontent.com/u/7051?v=4" width="100px;" alt="Jonathan Viney"/><br /><sub><b>Jonathan Viney</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/issues?q=author%3Ajviney" title="Bug reports">🐛</a></td>
186
187
  <td align="center"><a href="https://github.com/acrewdson"><img src="https://avatars0.githubusercontent.com/u/10353074?v=4" width="100px;" alt="acrewdson"/><br /><sub><b>acrewdson</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/issues?q=author%3Aacrewdson" title="Bug reports">🐛</a></td>
188
+ <td align="center"><a href="https://orleans.io"><img src="https://avatars0.githubusercontent.com/u/1683595?v=4" width="100px;" alt="Louis Orleans"/><br /><sub><b>Louis Orleans</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/issues?q=author%3Adudeofawesome" title="Bug reports">🐛</a></td>
189
+ <td align="center"><a href="https://github.com/cvoege"><img src="https://avatars2.githubusercontent.com/u/6777709?v=4" width="100px;" alt="Colton Voege"/><br /><sub><b>Colton Voege</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/issues?q=author%3Acvoege" title="Bug reports">🐛</a></td>
190
+ <td align="center"><a href="https://stefankracht.de"><img src="https://avatars1.githubusercontent.com/u/529711?v=4" width="100px;" alt="Stefan Kracht"/><br /><sub><b>Stefan Kracht</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/issues?q=author%3Akrachtstefan" title="Bug reports">🐛</a></td>
191
+ </tr>
192
+ <tr>
193
+ <td align="center"><a href="https://github.com/jakeprime"><img src="https://avatars1.githubusercontent.com/u/1019036?v=4" width="100px;" alt="jakeprime"/><br /><sub><b>jakeprime</b></sub></a><br /><a href="https://github.com/prettier/plugin-ruby/issues?q=author%3Ajakeprime" title="Bug reports">🐛</a></td>
187
194
  </tr>
188
195
  </table>
189
196
 
197
+ <!-- markdownlint-enable -->
198
+ <!-- prettier-ignore-end -->
199
+
190
200
  <!-- ALL-CONTRIBUTORS-LIST:END -->
191
201
 
192
202
  This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prettier/plugin-ruby",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "prettier plugin for the Ruby programming language",
5
5
  "main": "src/ruby.js",
6
6
  "scripts": {
data/src/nodes/args.js CHANGED
@@ -51,17 +51,30 @@ module.exports = {
51
51
  const args = path.map(print, "body");
52
52
  let blockNode = null;
53
53
 
54
+ // Look up the chain to see if these arguments are contained within a
55
+ // method_add_block node, and if they are that that node has a block
56
+ // associated with it. If it does, we're going to attempt to transform it
57
+ // into the to_proc shorthand and add it to the list of arguments.
54
58
  [1, 2, 3].find(parent => {
55
59
  const parentNode = path.getParentNode(parent);
56
60
  blockNode =
57
61
  parentNode &&
58
62
  parentNode.type === "method_add_block" &&
59
63
  parentNode.body[1];
64
+
60
65
  return blockNode;
61
66
  });
62
67
 
63
68
  const proc = blockNode && toProc(blockNode);
64
- if (proc) {
69
+
70
+ // If we have a successful to_proc transformation, but we're part of an aref
71
+ // node, that means it's something to the effect of
72
+ //
73
+ // foo[:bar].each(&:to_s)
74
+ //
75
+ // In this case we need to just return regular arguments, otherwise we would
76
+ // end up putting &:to_s inside the brackets accidentally.
77
+ if (proc && path.getParentNode(1).type !== "aref") {
65
78
  args.push(proc);
66
79
  }
67
80
 
data/src/nodes/calls.js CHANGED
@@ -53,9 +53,11 @@ module.exports = {
53
53
  ])
54
54
  );
55
55
  }
56
+
56
57
  if (proc) {
57
58
  return path.call(print, "body", 0);
58
59
  }
60
+
59
61
  return concat(path.map(print, "body"));
60
62
  },
61
63
  vcall: first
data/src/nodes/flow.js CHANGED
@@ -1,4 +1,11 @@
1
- const { concat, join } = require("../prettier");
1
+ const {
2
+ concat,
3
+ group,
4
+ ifBreak,
5
+ indent,
6
+ join,
7
+ softline
8
+ } = require("../prettier");
2
9
  const { literal } = require("../utils");
3
10
 
4
11
  module.exports = {
@@ -42,15 +49,23 @@ module.exports = {
42
49
  return "return";
43
50
  }
44
51
 
52
+ let value = join(", ", path.call(print, "body", 0));
53
+
54
+ // If the body of the return contains parens, then just skip directly to the
55
+ // content of the parens so that we can skip printing parens if we don't
56
+ // want them.
45
57
  if (args.body[0] && args.body[0].type === "paren") {
46
- // Ignoring the parens node and just going straight to the content
47
- return concat([
48
- "return ",
49
- path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)
50
- ]);
58
+ value = path.call(print, "body", 0, "body", 0, "body", 0, "body", 0);
51
59
  }
52
60
 
53
- return concat(["return ", join(", ", path.call(print, "body", 0))]);
61
+ return group(
62
+ concat([
63
+ "return",
64
+ ifBreak("(", " "),
65
+ indent(concat([softline, value])),
66
+ concat([softline, ifBreak(")", "")])
67
+ ])
68
+ );
54
69
  },
55
70
  return0: literal("return"),
56
71
  yield: (path, opts, print) => {
data/src/nodes/loops.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const {
2
- breakParent,
3
2
  concat,
4
3
  group,
5
4
  hardline,
@@ -8,23 +7,45 @@ const {
8
7
  softline
9
8
  } = require("../prettier");
10
9
 
11
- const printLoop = keyword => (path, { inlineLoops }, print) =>
12
- group(
13
- ifBreak(
14
- concat([
15
- concat([`${keyword} `, path.call(print, "body", 0)]),
16
- indent(concat([softline, path.call(print, "body", 1)])),
17
- concat([softline, "end"])
18
- ]),
19
- concat([
20
- inlineLoops ? "" : breakParent,
21
- path.call(print, "body", 1),
22
- ` ${keyword} `,
23
- path.call(print, "body", 0)
24
- ])
25
- )
26
- );
10
+ const printLoop = (keyword, modifier) => (path, { inlineLoops }, print) => {
11
+ const inlineLoop = concat([
12
+ path.call(print, "body", 1),
13
+ ` ${keyword} `,
14
+ path.call(print, "body", 0)
15
+ ]);
16
+
17
+ // If we're in the modifier form and we're modifying a `begin`, then this is a
18
+ // special case where we need to explicitly use the modifier form because
19
+ // otherwise the semantic meaning changes. This looks like:
20
+ //
21
+ // begin
22
+ // foo
23
+ // end while bar
24
+ //
25
+ // The above is effectively a `do...while` loop (which we don't have in ruby).
26
+ if (modifier && path.getValue().body[1].type === "begin") {
27
+ return inlineLoop;
28
+ }
29
+
30
+ const blockLoop = concat([
31
+ concat([`${keyword} `, path.call(print, "body", 0)]),
32
+ indent(concat([softline, path.call(print, "body", 1)])),
33
+ concat([softline, "end"])
34
+ ]);
35
+
36
+ if (!inlineLoops) {
37
+ return blockLoop;
38
+ }
39
+
40
+ return group(ifBreak(blockLoop, inlineLoop));
41
+ };
27
42
 
43
+ // Technically this is incorrect. A `for` loop actually introduces and modifies
44
+ // a local variable that then remains in the outer scope. Additionally, if the
45
+ // `each` method was somehow missing from the enumerable (it's possible...),
46
+ // then this transformation would fail. However - I've never actually seen a
47
+ // `for` loop used in production. If someone actually calls me on it, I'll fix
48
+ // this, but for now I'm leaving it.
28
49
  const printFor = (path, opts, print) =>
29
50
  group(
30
51
  concat([
@@ -38,9 +59,9 @@ const printFor = (path, opts, print) =>
38
59
  );
39
60
 
40
61
  module.exports = {
41
- while: printLoop("while"),
42
- while_mod: printLoop("while"),
43
- until: printLoop("until"),
44
- until_mod: printLoop("until"),
62
+ while: printLoop("while", false),
63
+ while_mod: printLoop("while", true),
64
+ until: printLoop("until", false),
65
+ until_mod: printLoop("until", true),
45
66
  for: printFor
46
67
  };
data/src/nodes/strings.js CHANGED
@@ -40,7 +40,7 @@ const getStringQuote = (string, preferSingleQuotes) => {
40
40
  const quotePattern = new RegExp("\\\\([\\s\\S])|(['\"])", "g");
41
41
 
42
42
  const makeString = (content, enclosingQuote) => {
43
- const otherQuote = enclosingQuote === '"' ? "'" : enclosingQuote;
43
+ const otherQuote = enclosingQuote === '"' ? "'" : '"';
44
44
 
45
45
  // Escape and unescape single and double quotes as needed to be able to
46
46
  // enclose `content` with `enclosingQuote`.
@@ -139,12 +139,24 @@ module.exports = {
139
139
  word_add: concatBody,
140
140
  word_new: empty,
141
141
  xstring: makeList,
142
- xstring_literal: (path, opts, print) =>
143
- group(
142
+ xstring_literal: (path, opts, print) => {
143
+ const parts = path.call(print, "body", 0);
144
+
145
+ if (typeof parts[0] === "string") {
146
+ parts[0] = parts[0].replace(/^\s+/, "");
147
+ }
148
+
149
+ const lastIndex = parts.length - 1;
150
+ if (typeof parts[lastIndex] === "string") {
151
+ parts[lastIndex] = parts[lastIndex].replace(/\s+$/, "");
152
+ }
153
+
154
+ return group(
144
155
  concat([
145
156
  "`",
146
- indent(concat([softline, join(softline, path.call(print, "body", 0))])),
157
+ indent(concat([softline, join(softline, parts)])),
147
158
  concat([softline, "`"])
148
159
  ])
149
- )
160
+ );
161
+ }
150
162
  };
data/src/ripper.rb CHANGED
@@ -2,8 +2,12 @@
2
2
 
3
3
  REQUIRED_VERSION = Gem::Version.new('2.5')
4
4
  if Gem::Version.new(RUBY_VERSION) < REQUIRED_VERSION
5
- raise "Ruby version #{RUBY_VERSION} not supported. " \
6
- "Please upgrade to #{REQUIRED_VERSION} or above."
5
+ warn(
6
+ "Ruby version #{RUBY_VERSION} not supported. " \
7
+ "Please upgrade to #{REQUIRED_VERSION} or above."
8
+ )
9
+
10
+ exit 1
7
11
  end
8
12
 
9
13
  require 'json' unless defined?(JSON)
@@ -757,8 +761,13 @@ if $0 == __FILE__
757
761
  builder = RipperJS.new($stdin.read)
758
762
  response = builder.parse
759
763
 
760
- if !response && builder.error?
761
- STDERR.puts 'Invalid ruby'
764
+ if !response || builder.error?
765
+ warn(
766
+ '@prettier/plugin-ruby encountered an error when attempting to parse ' \
767
+ 'the ruby source. This usually means there was a syntax error in the ' \
768
+ 'file in question. You can verify by running `ruby -i [path/to/file]`.'
769
+ )
770
+
762
771
  exit 1
763
772
  end
764
773
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prettier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Deisz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-17 00:00:00.000000000 Z
11
+ date: 2019-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler