prettier 1.5.0 → 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -2
- data/package.json +1 -1
- data/src/parser/server.rb +2 -2
- data/src/ruby/embed.js +7 -5
- data/src/ruby/nodes/arrays.js +36 -33
- data/src/ruby/nodes/commands.js +1 -1
- data/src/ruby/nodes/heredocs.js +5 -3
- data/src/ruby/parser.rb +57 -4
- data/src/ruby/printer.js +8 -3
- data/src/utils.js +1 -1
- data/src/utils/inlineEnsureParens.js +8 -1
- data/src/utils/literallineWithoutBreakParent.js +7 -0
- metadata +3 -3
- data/src/utils/literalLineNoBreak.js +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97cf5963c684d5bf17079800c59b79fb15f787c89c1b1d7bd315c3851307737f
|
4
|
+
data.tar.gz: 721e04eaa93f654d873e2b972b718b3d1db41d7acca278b8eec743aa92daf755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe8997d3c57f9ed1d6cc23d198555c51d234dd856d0e3803879fb4a92809204fcb5d8b70e0a02421e902d9fd4d33e34e02e7e6ccd778caef203ce3aa7b6c6037
|
7
|
+
data.tar.gz: 91656de653ca7b90469dfbc3e6363259d3e9977693e649846d0bc4fd39e0585798f985334b1895790efbb9613f39ba35dce3193aecb59844d0f419d0da2c5bf0
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.5.2] - 2021-02-03
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
|
13
|
+
- kddeisz - Fix up `binary` node comparison operators so that it will handle either Symbol literals or the `@op` nodes which are incorrectly coming from JRuby (https://github.com/jruby/jruby/issues/6548).
|
14
|
+
|
15
|
+
## [1.5.1] - 2021-01-27
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
|
19
|
+
- [#799](https://github.com/prettier/plugin-ruby/issues/799) - jscheid, kddeisz - Multi-byte characters shouldn't give invalid source string bounds.
|
20
|
+
- [#801](https://github.com/prettier/plugin-ruby/issues/801) - jscheid, kddeisz - When converting a conditional to the modifier form, make sure to add parentheses if there is a chained method call.
|
21
|
+
- [#803](https://github.com/prettier/plugin-ruby/issues/803) - jscheid, kddeisz - Fix `formatWithCursor` support to match new parser format.
|
22
|
+
|
9
23
|
## [1.5.0] - 2021-01-21
|
10
24
|
|
11
25
|
### Added
|
@@ -191,7 +205,7 @@ end
|
|
191
205
|
- kddeisz - Fix up a bug with constant aliases, e.g., `alias in IN`.
|
192
206
|
- andyw8, kddeisz - Ensure `rescue` comments stay on the same line as their declaration.
|
193
207
|
|
194
|
-
|
208
|
+
## [0.22.0] - 2020-12-08
|
195
209
|
|
196
210
|
### Changed
|
197
211
|
|
@@ -1063,7 +1077,10 @@ would previously result in `array[]`, but now prints properly.
|
|
1063
1077
|
|
1064
1078
|
- Initial release 🎉
|
1065
1079
|
|
1066
|
-
[unreleased]: https://github.com/prettier/plugin-ruby/compare/v1.
|
1080
|
+
[unreleased]: https://github.com/prettier/plugin-ruby/compare/v1.5.2...HEAD
|
1081
|
+
[1.5.2]: https://github.com/prettier/plugin-ruby/compare/v1.5.1...v1.5.2
|
1082
|
+
[1.5.1]: https://github.com/prettier/plugin-ruby/compare/v1.5.0...v1.5.1
|
1083
|
+
[1.5.0]: https://github.com/prettier/plugin-ruby/compare/v1.4.0...v1.5.0
|
1067
1084
|
[1.4.0]: https://github.com/prettier/plugin-ruby/compare/v1.3.0...v1.4.0
|
1068
1085
|
[1.3.0]: https://github.com/prettier/plugin-ruby/compare/v1.2.5...v1.3.0
|
1069
1086
|
[1.2.5]: https://github.com/prettier/plugin-ruby/compare/v1.2.4...v1.2.5
|
data/package.json
CHANGED
data/src/parser/server.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'bundler/setup' if ENV['
|
3
|
+
require 'bundler/setup' if ENV['PLUGIN_RUBY_CI']
|
4
4
|
require 'socket'
|
5
5
|
require 'json'
|
6
6
|
|
@@ -14,7 +14,7 @@ $PROGRAM_NAME = 'prettier-ruby-parser'
|
|
14
14
|
# Make sure we trap these signals to be sure we get the quit command coming from
|
15
15
|
# the parent node process
|
16
16
|
quit = false
|
17
|
-
trap(:QUIT) { quit = true }
|
17
|
+
trap(:QUIT) { quit = true } if RUBY_PLATFORM != 'java'
|
18
18
|
trap(:INT) { quit = true }
|
19
19
|
trap(:TERM) { quit = true }
|
20
20
|
|
data/src/ruby/embed.js
CHANGED
@@ -8,7 +8,7 @@ const {
|
|
8
8
|
stripTrailingHardline
|
9
9
|
} = require("../prettier");
|
10
10
|
|
11
|
-
const {
|
11
|
+
const { literallineWithoutBreakParent } = require("../utils");
|
12
12
|
|
13
13
|
const parsers = {
|
14
14
|
css: "css",
|
@@ -30,7 +30,7 @@ function replaceNewlines(doc) {
|
|
30
30
|
? concat(
|
31
31
|
currentDoc
|
32
32
|
.split(/(\n)/g)
|
33
|
-
.map((v, i) => (i % 2 === 0 ? v :
|
33
|
+
.map((v, i) => (i % 2 === 0 ? v : literallineWithoutBreakParent))
|
34
34
|
)
|
35
35
|
: currentDoc
|
36
36
|
);
|
@@ -106,7 +106,7 @@ function embed(path, print, textToDoc, _opts) {
|
|
106
106
|
|
107
107
|
// Pass that content into the embedded parser. Get back the doc node.
|
108
108
|
const formatted = concat([
|
109
|
-
|
109
|
+
literallineWithoutBreakParent,
|
110
110
|
replaceNewlines(stripTrailingHardline(textToDoc(content, { parser })))
|
111
111
|
]);
|
112
112
|
|
@@ -119,7 +119,7 @@ function embed(path, print, textToDoc, _opts) {
|
|
119
119
|
group(
|
120
120
|
concat([
|
121
121
|
indent(markAsRoot(formatted)),
|
122
|
-
|
122
|
+
literallineWithoutBreakParent,
|
123
123
|
ending.trim()
|
124
124
|
])
|
125
125
|
)
|
@@ -132,7 +132,9 @@ function embed(path, print, textToDoc, _opts) {
|
|
132
132
|
return markAsRoot(
|
133
133
|
concat([
|
134
134
|
path.call(print, "beging"),
|
135
|
-
lineSuffix(
|
135
|
+
lineSuffix(
|
136
|
+
group(concat([formatted, literallineWithoutBreakParent, ending.trim()]))
|
137
|
+
)
|
136
138
|
])
|
137
139
|
);
|
138
140
|
}
|
data/src/ruby/nodes/arrays.js
CHANGED
@@ -63,33 +63,30 @@ function isSymbolArray(args) {
|
|
63
63
|
// Prints out a word that is a part of a special array literal that accepts
|
64
64
|
// interpolation. The body is an array of either plain strings or interpolated
|
65
65
|
// expressions.
|
66
|
-
function
|
66
|
+
function printArrayLiteralWord(path, opts, print) {
|
67
67
|
return concat(path.map(print, "body"));
|
68
68
|
}
|
69
69
|
|
70
70
|
// Prints out a special array literal. Accepts the parts of the array literal as
|
71
71
|
// an argument, where the first element of the parts array is a string that
|
72
72
|
// contains the special start.
|
73
|
-
function
|
73
|
+
function printArrayLiteral(start, parts) {
|
74
74
|
return group(
|
75
75
|
concat([
|
76
|
-
|
76
|
+
start,
|
77
77
|
"[",
|
78
|
-
indent(concat([softline, join(line, parts
|
78
|
+
indent(concat([softline, join(line, parts)])),
|
79
79
|
concat([softline, "]"])
|
80
80
|
])
|
81
81
|
);
|
82
82
|
}
|
83
83
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
return [start].concat(path.map(print, "body"));
|
91
|
-
};
|
92
|
-
}
|
84
|
+
const arrayLiteralStarts = {
|
85
|
+
qsymbols: "%i",
|
86
|
+
qwords: "%w",
|
87
|
+
symbols: "%I",
|
88
|
+
words: "%W"
|
89
|
+
};
|
93
90
|
|
94
91
|
// An array node is any literal array in Ruby. This includes all of the special
|
95
92
|
// array literals as well as regular arrays. If it is a special array literal
|
@@ -105,30 +102,40 @@ function printArray(path, opts, print) {
|
|
105
102
|
return printEmptyCollection(path, opts, "[", "]");
|
106
103
|
}
|
107
104
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
105
|
+
if (opts.rubyArrayLiteral) {
|
106
|
+
// If we have an array that contains only simple string literals with no
|
107
|
+
// spaces or interpolation, then we're going to print a %w array.
|
108
|
+
if (isStringArray(args)) {
|
109
|
+
const printString = (stringPath) => stringPath.call(print, "body", 0);
|
110
|
+
const parts = path.map(printString, "body", 0, "body");
|
113
111
|
|
114
|
-
|
115
|
-
|
112
|
+
return printArrayLiteral("%w", parts);
|
113
|
+
}
|
116
114
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
115
|
+
// If we have an array that contains only simple symbol literals with no
|
116
|
+
// interpolation, then we're going to print a %i array.
|
117
|
+
if (isSymbolArray(args)) {
|
118
|
+
const printSymbol = (symbolPath) => symbolPath.call(print, "body", 0);
|
119
|
+
const parts = path.map(printSymbol, "body", 0, "body");
|
122
120
|
|
123
|
-
|
121
|
+
return printArrayLiteral("%i", parts);
|
122
|
+
}
|
124
123
|
}
|
125
124
|
|
126
125
|
// If we don't have a regular args node at this point then we have a special
|
127
126
|
// array literal. In that case we're going to print out the body (which will
|
128
127
|
// return to us an array with the first one being the start of the array) and
|
129
|
-
// send that over to the
|
128
|
+
// send that over to the printArrayLiteral function.
|
130
129
|
if (!["args", "args_add_star"].includes(args.type)) {
|
131
|
-
return
|
130
|
+
return path.call(
|
131
|
+
(arrayPath) =>
|
132
|
+
printArrayLiteral(
|
133
|
+
arrayLiteralStarts[arrayPath.getValue().type],
|
134
|
+
arrayPath.map(print, "body")
|
135
|
+
),
|
136
|
+
"body",
|
137
|
+
0
|
138
|
+
);
|
132
139
|
}
|
133
140
|
|
134
141
|
// Here we have a normal array of any type of object with no special literal
|
@@ -151,9 +158,5 @@ function printArray(path, opts, print) {
|
|
151
158
|
|
152
159
|
module.exports = {
|
153
160
|
array: printArray,
|
154
|
-
|
155
|
-
qwords: printSpecialArray("%w"),
|
156
|
-
symbols: printSpecialArray("%I"),
|
157
|
-
word: printSpecialArrayWord,
|
158
|
-
words: printSpecialArray("%W")
|
161
|
+
word: printArrayLiteralWord
|
159
162
|
};
|
data/src/ruby/nodes/commands.js
CHANGED
data/src/ruby/nodes/heredocs.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
const { concat, group, lineSuffix, join } = require("../../prettier");
|
2
|
-
const {
|
2
|
+
const { literallineWithoutBreakParent } = require("../../utils");
|
3
3
|
|
4
4
|
function printHeredoc(path, opts, print) {
|
5
5
|
const { body, ending } = path.getValue();
|
@@ -11,7 +11,7 @@ function printHeredoc(path, opts, print) {
|
|
11
11
|
}
|
12
12
|
|
13
13
|
// In this case, the part of the string is just regular string content
|
14
|
-
return join(
|
14
|
+
return join(literallineWithoutBreakParent, part.body.split("\n"));
|
15
15
|
});
|
16
16
|
|
17
17
|
// We use a literalline break because matching indentation is required
|
@@ -23,7 +23,9 @@ function printHeredoc(path, opts, print) {
|
|
23
23
|
concat([
|
24
24
|
path.call(print, "beging"),
|
25
25
|
lineSuffix(
|
26
|
-
group(
|
26
|
+
group(
|
27
|
+
concat([literallineWithoutBreakParent].concat(parts).concat(ending))
|
28
|
+
)
|
27
29
|
)
|
28
30
|
])
|
29
31
|
);
|
data/src/ruby/parser.rb
CHANGED
@@ -21,7 +21,40 @@ module Prettier
|
|
21
21
|
end
|
22
22
|
|
23
23
|
class Prettier::Parser < Ripper
|
24
|
-
|
24
|
+
# Represents a line in the source. If this class is being used, it means that
|
25
|
+
# every character in the string is 1 byte in length, so we can just return the
|
26
|
+
# start of the line + the index.
|
27
|
+
class SingleByteString
|
28
|
+
def initialize(start)
|
29
|
+
@start = start
|
30
|
+
end
|
31
|
+
|
32
|
+
def [](byteindex)
|
33
|
+
@start + byteindex
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Represents a line in the source. If this class is being used, it means that
|
38
|
+
# there are characters in the string that are multi-byte, so we will build up
|
39
|
+
# an array of indices, such that array[byteindex] will be equal to the index
|
40
|
+
# of the character within the string.
|
41
|
+
class MultiByteString
|
42
|
+
def initialize(start, line)
|
43
|
+
@indices = []
|
44
|
+
|
45
|
+
line
|
46
|
+
.each_char
|
47
|
+
.with_index(start) do |char, index|
|
48
|
+
char.bytesize.times { @indices << index }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def [](byteindex)
|
53
|
+
@indices[byteindex]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
attr_reader :source, :lines, :scanner_events
|
25
58
|
|
26
59
|
# This is an attr_accessor so Stmts objects can grab comments out of this
|
27
60
|
# array and attach them to themselves.
|
@@ -40,9 +73,23 @@ class Prettier::Parser < Ripper
|
|
40
73
|
@heredocs = []
|
41
74
|
|
42
75
|
@scanner_events = []
|
43
|
-
@line_counts = [
|
76
|
+
@line_counts = []
|
44
77
|
|
45
|
-
|
78
|
+
# Here we're going to build up a list of SingleByteString or MultiByteString
|
79
|
+
# objects. They're each going to represent a string in the source. They are
|
80
|
+
# used by the `char_pos` method to determine where we are in the source
|
81
|
+
# string.
|
82
|
+
last_index = 0
|
83
|
+
|
84
|
+
@source.lines.each do |line|
|
85
|
+
if line.size == line.bytesize
|
86
|
+
@line_counts << SingleByteString.new(last_index)
|
87
|
+
else
|
88
|
+
@line_counts << MultiByteString.new(last_index, line)
|
89
|
+
end
|
90
|
+
|
91
|
+
last_index += line.size
|
92
|
+
end
|
46
93
|
end
|
47
94
|
|
48
95
|
def self.parse(source)
|
@@ -60,7 +107,7 @@ class Prettier::Parser < Ripper
|
|
60
107
|
# this line, then we add the number of columns into this line that we've gone
|
61
108
|
# through.
|
62
109
|
def char_pos
|
63
|
-
line_counts[lineno - 1]
|
110
|
+
@line_counts[lineno - 1][column]
|
64
111
|
end
|
65
112
|
|
66
113
|
# As we build up a list of scanner events, we'll periodically need to go
|
@@ -518,6 +565,12 @@ class Prettier::Parser < Ripper
|
|
518
565
|
# binary is a parser event that represents a binary operation between two
|
519
566
|
# values.
|
520
567
|
def on_binary(left, oper, right)
|
568
|
+
# On most Ruby implementations, oper is a Symbol that represents that
|
569
|
+
# operation being performed. For instance in the example `1 < 2`, the `oper`
|
570
|
+
# object would be `:<`. However, on JRuby, it's an `@op` node, so here we're
|
571
|
+
# going to explicitly convert it into the same normalized form.
|
572
|
+
oper = scanner_events.delete(oper)[:body] unless oper.is_a?(Symbol)
|
573
|
+
|
521
574
|
{
|
522
575
|
type: :binary,
|
523
576
|
body: [left, oper, right],
|
data/src/ruby/printer.js
CHANGED
@@ -38,7 +38,8 @@ const noComments = [
|
|
38
38
|
"args_add_star",
|
39
39
|
"mlhs",
|
40
40
|
"mlhs_add_post",
|
41
|
-
"mlhs_add_star"
|
41
|
+
"mlhs_add_star",
|
42
|
+
"mlhs_paren"
|
42
43
|
];
|
43
44
|
|
44
45
|
// Certain nodes are used more for organizational purposed than for actually
|
@@ -97,8 +98,12 @@ function getCommentChildNodes(node) {
|
|
97
98
|
|
98
99
|
return parts;
|
99
100
|
}
|
100
|
-
default:
|
101
|
-
|
101
|
+
default: {
|
102
|
+
if (Array.isArray(node.body)) {
|
103
|
+
return node.body.filter((child) => child && typeof child === "object");
|
104
|
+
}
|
105
|
+
return [];
|
106
|
+
}
|
102
107
|
}
|
103
108
|
}
|
104
109
|
|
data/src/utils.js
CHANGED
@@ -5,7 +5,7 @@ module.exports = {
|
|
5
5
|
isEmptyStmts: require("./utils/isEmptyStmts"),
|
6
6
|
hasAncestor: require("./utils/hasAncestor"),
|
7
7
|
literal: require("./utils/literal"),
|
8
|
-
|
8
|
+
literallineWithoutBreakParent: require("./utils/literallineWithoutBreakParent"),
|
9
9
|
makeCall: require("./utils/makeCall"),
|
10
10
|
noIndent: require("./utils/noIndent"),
|
11
11
|
printEmptyCollection: require("./utils/printEmptyCollection"),
|
@@ -1,4 +1,11 @@
|
|
1
|
-
const needsParens = [
|
1
|
+
const needsParens = [
|
2
|
+
"args",
|
3
|
+
"assign",
|
4
|
+
"assoc_new",
|
5
|
+
"call",
|
6
|
+
"massign",
|
7
|
+
"opassign"
|
8
|
+
];
|
2
9
|
|
3
10
|
// If you have a modifier statement (for instance an inline if statement or an
|
4
11
|
// inline while loop) there are times when you need to wrap the entire statement
|
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: 1.5.
|
4
|
+
version: 1.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Deisz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -100,7 +100,7 @@ files:
|
|
100
100
|
- src/utils/isEmptyBodyStmt.js
|
101
101
|
- src/utils/isEmptyStmts.js
|
102
102
|
- src/utils/literal.js
|
103
|
-
- src/utils/
|
103
|
+
- src/utils/literallineWithoutBreakParent.js
|
104
104
|
- src/utils/makeCall.js
|
105
105
|
- src/utils/noIndent.js
|
106
106
|
- src/utils/printEmptyCollection.js
|