prettier 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24ce6592d56391ec133891b51f4b0fc3092b735ae547547c79a75f953a891d90
4
- data.tar.gz: 9b60c471f9d6953ebbcd1f7bd7080d306a3e8fba19a147f2a65ba9fd52542a9a
3
+ metadata.gz: 1d669fc2ab950c7a9163d2b9f63620ec0300536a72985fbe9378054cc988490d
4
+ data.tar.gz: 9a99a3897282616faa104f47094afcf0b2b1f0a748a3376ddecb1aa55bc5f7f9
5
5
  SHA512:
6
- metadata.gz: d258d7f5838c43e5e2d17a6bc2c02645dc5f347b801e2d4cad9dab298040cf7fc65850562f6f1d36dd35719196f986d4b8fd9243e4da9ea0df3c88a8cec44d6e
7
- data.tar.gz: de06627e53a1c7994868f6184dc6d5fbb88f05c19e5a7a3c242361e320afc31b2f4254a388452b38f1d15581dcc760fa7a345c90d13a42c745c4c37cbbda3369
6
+ metadata.gz: 060eb9299ef84812b305c145c87e8fe59c440028df12e16e29509caa23abb0acde2aec7e43951a43f4fd6ad2b2a6cff8d45af5b358350ac75920582e87982f58
7
+ data.tar.gz: bd1ffb84df6cc449db31bdca82f15cd3a15cb7842718e2ae1b79eb8849ac26f6d8f6d1164d9322e0b91dcdbc0a32aef2722686417f7285007424e771d8fc2ab7
@@ -6,6 +6,14 @@ 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.1] - 2021-01-27
10
+
11
+ ### Changed
12
+
13
+ - [#799](https://github.com/prettier/plugin-ruby/issues/799) - jscheid, kddeisz - Multi-byte characters shouldn't give invalid source string bounds.
14
+ - [#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.
15
+ - [#803](https://github.com/prettier/plugin-ruby/issues/803) - jscheid, kddeisz - Fix `formatWithCursor` support to match new parser format.
16
+
9
17
  ## [1.5.0] - 2021-01-21
10
18
 
11
19
  ### Added
@@ -1063,7 +1071,9 @@ would previously result in `array[]`, but now prints properly.
1063
1071
 
1064
1072
  - Initial release 🎉
1065
1073
 
1066
- [unreleased]: https://github.com/prettier/plugin-ruby/compare/v1.4.0...HEAD
1074
+ [unreleased]: https://github.com/prettier/plugin-ruby/compare/v1.5.1...HEAD
1075
+ [1.5.1]: https://github.com/prettier/plugin-ruby/compare/v1.5.0...v1.5.1
1076
+ [1.5.0]: https://github.com/prettier/plugin-ruby/compare/v1.4.0...v1.5.0
1067
1077
  [1.4.0]: https://github.com/prettier/plugin-ruby/compare/v1.3.0...v1.4.0
1068
1078
  [1.3.0]: https://github.com/prettier/plugin-ruby/compare/v1.2.5...v1.3.0
1069
1079
  [1.2.5]: https://github.com/prettier/plugin-ruby/compare/v1.2.4...v1.2.5
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prettier/plugin-ruby",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "prettier plugin for the Ruby programming language",
5
5
  "main": "src/plugin.js",
6
6
  "scripts": {
@@ -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 printSpecialArrayWord(path, opts, print) {
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 printSpecialArrayParts(parts) {
73
+ function printArrayLiteral(start, parts) {
74
74
  return group(
75
75
  concat([
76
- parts[0],
76
+ start,
77
77
  "[",
78
- indent(concat([softline, join(line, parts.slice(1))])),
78
+ indent(concat([softline, join(line, parts)])),
79
79
  concat([softline, "]"])
80
80
  ])
81
81
  );
82
82
  }
83
83
 
84
- // Generates a print function with an embedded special start character for the
85
- // specific type of array literal that we're dealing with. The print function
86
- // returns an array as it expects to eventually be handed off to
87
- // printSpecialArrayParts.
88
- function printSpecialArray(start) {
89
- return function printSpecialArrayWithStart(path, opts, print) {
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
- // If we have an array that contains only simple string literals with no
109
- // spaces or interpolation, then we're going to print a %w array.
110
- if (opts.rubyArrayLiteral && isStringArray(args)) {
111
- const printString = (stringPath) => stringPath.call(print, "body", 0);
112
- const parts = path.map(printString, "body", 0, "body");
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
- return printSpecialArrayParts(["%w"].concat(parts));
115
- }
112
+ return printArrayLiteral("%w", parts);
113
+ }
116
114
 
117
- // If we have an array that contains only simple symbol literals with no
118
- // interpolation, then we're going to print a %i array.
119
- if (opts.rubyArrayLiteral && isSymbolArray(args)) {
120
- const printSymbol = (symbolPath) => symbolPath.call(print, "body", 0);
121
- const parts = path.map(printSymbol, "body", 0, "body");
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
- return printSpecialArrayParts(["%i"].concat(parts));
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 printSpecialArrayParts function.
128
+ // send that over to the printArrayLiteral function.
130
129
  if (!["args", "args_add_star"].includes(args.type)) {
131
- return printSpecialArrayParts(path.call(print, "body", 0));
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
- qsymbols: printSpecialArray("%i"),
155
- qwords: printSpecialArray("%w"),
156
- symbols: printSpecialArray("%I"),
157
- word: printSpecialArrayWord,
158
- words: printSpecialArray("%W")
161
+ word: printArrayLiteralWord
159
162
  };
@@ -73,7 +73,7 @@ function printCommand(path, opts, print) {
73
73
  } else if (hasDef(path.getValue())) {
74
74
  breakArgs = joinedArgs;
75
75
  } else {
76
- breakArgs = align(command.length + 1, joinedArgs);
76
+ breakArgs = align(docLength(command) + 1, joinedArgs);
77
77
  }
78
78
 
79
79
  return group(
@@ -21,7 +21,40 @@ module Prettier
21
21
  end
22
22
 
23
23
  class Prettier::Parser < Ripper
24
- attr_reader :source, :lines, :scanner_events, :line_counts
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 = [0]
76
+ @line_counts = []
77
+
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
44
83
 
45
- @source.lines.each { |line| @line_counts << @line_counts.last + line.size }
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] + column
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
@@ -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
- return node.body;
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
 
@@ -1,4 +1,11 @@
1
- const needsParens = ["args", "assign", "assoc_new", "massign", "opassign"];
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.0
4
+ version: 1.5.1
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-01-22 00:00:00.000000000 Z
11
+ date: 2021-01-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: