prettier 0.16.0 → 0.19.0

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.
@@ -2,9 +2,14 @@ const { spawnSync } = require("child_process");
2
2
  const path = require("path");
3
3
 
4
4
  module.exports = (text, _parsers, _opts) => {
5
- const child = spawnSync("ruby", [path.join(__dirname, "./ripper.rb")], {
6
- input: text
7
- });
5
+ const child = spawnSync(
6
+ "ruby",
7
+ ["--disable-gems", path.join(__dirname, "./ripper.rb")],
8
+ {
9
+ input: text,
10
+ maxBuffer: 10 * 1024 * 1024 // 10MB
11
+ }
12
+ );
8
13
 
9
14
  const error = child.stderr.toString();
10
15
  if (error) {
@@ -3,7 +3,6 @@
3
3
  // directly, as it's been shipped with the gem.
4
4
  const source = process.env.RBPRETTIER ? "../node_modules/prettier" : "prettier";
5
5
 
6
- // eslint-disable-next-line import/no-dynamic-require
7
6
  const prettier = require(source);
8
7
 
9
8
  // Just combine all the things into one big object so that we can import
@@ -1,10 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- REQUIRED_VERSION = Gem::Version.new('2.5')
4
- if Gem::Version.new(RUBY_VERSION) < REQUIRED_VERSION
3
+ # We implement our own version checking here instead of using Gem::Version so
4
+ # that we can use the --disable-gems flag.
5
+ major, minor, * = RUBY_VERSION.split('.').map(&:to_i)
6
+ if (major < 2) || ((major == 2) && (minor < 5))
5
7
  warn(
6
- "Ruby version #{RUBY_VERSION} not supported. " \
7
- "Please upgrade to #{REQUIRED_VERSION} or above."
8
+ "Ruby version #{current_version} not supported. " \
9
+ "Please upgrade to #{required_version} or above."
8
10
  )
9
11
 
10
12
  exit 1
@@ -166,6 +168,7 @@ class RipperJS < Ripper
166
168
  assoc_splat: [:@op, '**'],
167
169
  arg_paren: :@lparen,
168
170
  args_add_star: [:@op, '*'],
171
+ args_forward: [:@op, '...'],
169
172
  begin: [:@kw, 'begin'],
170
173
  blockarg: [:@op, '&'],
171
174
  brace_block: :@lbrace,
@@ -290,7 +293,8 @@ class RipperJS < Ripper
290
293
  super(*body).merge!(
291
294
  start: node[:start],
292
295
  char_start: node[:char_start],
293
- char_end: char_pos
296
+ char_end: char_pos,
297
+ quote: node[:body]
294
298
  )
295
299
  end
296
300
  end
@@ -319,14 +323,28 @@ class RipperJS < Ripper
319
323
  # child node.
320
324
  %i[dyna_symbol symbol_literal].each do |event|
321
325
  define_method(:"on_#{event}") do |*body|
322
- char_start =
326
+ options =
323
327
  if scanner_events.any? { |sevent| sevent[:type] == :@symbeg }
324
- find_scanner_event(:@symbeg)[:char_start]
328
+ symbeg = find_scanner_event(:@symbeg)
329
+
330
+ {
331
+ char_start: symbeg[:char_start],
332
+ char_end: char_pos,
333
+ quote: symbeg[:body][1]
334
+ }
335
+ elsif scanner_events.any? { |sevent| sevent[:type] == :@label_end }
336
+ label_end = find_scanner_event(:@label_end)
337
+
338
+ {
339
+ char_start: char_start_for(body),
340
+ char_end: char_pos,
341
+ quote: label_end[:body][0]
342
+ }
325
343
  else
326
- char_start_for(body)
344
+ { char_start: char_start_for(body), char_end: char_pos }
327
345
  end
328
346
 
329
- super(*body).merge!(char_start: char_start, char_end: char_pos)
347
+ super(*body).merge!(options)
330
348
  end
331
349
  end
332
350
 
@@ -630,6 +648,8 @@ class RipperJS < Ripper
630
648
  end
631
649
  )
632
650
 
651
+ # This module contains miscellaneous fixes required to get the right
652
+ # structure.
633
653
  prepend(
634
654
  Module.new do
635
655
  private
@@ -660,18 +680,6 @@ class RipperJS < Ripper
660
680
  super(*body).tap { |node| node[:body][0][:body] << __end__ if __end__ }
661
681
  end
662
682
 
663
- # Adds the used quote type onto string nodes. This is necessary because
664
- # we're going to have to stick to whatever quote the user chose if there
665
- # are escape sequences within the string. For example, if you have '\n'
666
- # we can't switch to double quotes without changing what it means.
667
- def on_tstring_end(quote)
668
- last_sexp.merge!(quote: quote)
669
- end
670
-
671
- def on_label_end(quote)
672
- last_sexp.merge!(quote: quote[0]) # quote is ": or ':
673
- end
674
-
675
683
  # Normally access controls are reported as vcall nodes. This creates a
676
684
  # new node type to explicitly track those nodes instead, so that the
677
685
  # printer can add new lines as necessary.
@@ -4,10 +4,10 @@ const print = require("./print");
4
4
  const haml = require("./haml");
5
5
 
6
6
  const pragmaPattern = /#\s*@(prettier|format)/;
7
- const hasPragma = text => pragmaPattern.test(text);
7
+ const hasPragma = (text) => pragmaPattern.test(text);
8
8
 
9
- const locStart = node => node.char_start;
10
- const locEnd = node => node.char_end;
9
+ const locStart = (node) => node.char_start;
10
+ const locEnd = (node) => node.char_end;
11
11
 
12
12
  /*
13
13
  * metadata mostly pulled from linguist and rubocop:
@@ -78,7 +78,8 @@ module.exports = {
78
78
  {
79
79
  name: "HAML",
80
80
  parsers: ["haml"],
81
- extensions: [".haml"]
81
+ extensions: [".haml"],
82
+ vscodeLanguageIds: ["haml"]
82
83
  }
83
84
  ],
84
85
  parsers: {
@@ -141,6 +142,13 @@ module.exports = {
141
142
  default: true,
142
143
  description:
143
144
  "When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals."
145
+ },
146
+ toProcTransform: {
147
+ type: "boolean",
148
+ category: "Global",
149
+ default: true,
150
+ description:
151
+ "When possible, convert blocks to the more concise Symbol#to_proc syntax."
144
152
  }
145
153
  },
146
154
  defaultOptions: {
@@ -1,4 +1,4 @@
1
- const isCall = node => ["::", "."].includes(node) || node.type === "@period";
1
+ const isCall = (node) => ["::", "."].includes(node) || node.type === "@period";
2
2
 
3
3
  // If you have a simple block that only calls a method on the single required
4
4
  // parameter that is passed to it, then you can replace that block with the
@@ -11,8 +11,8 @@ const isCall = node => ["::", "."].includes(node) || node.type === "@period";
11
11
  // [1, 2, 3].map(&:to_s)
12
12
  //
13
13
  // This works with `do` blocks as well.
14
- const toProc = node => {
15
- if (!node) {
14
+ const toProc = (path, opts, node) => {
15
+ if (!node || !opts.toProcTransform) {
16
16
  return null;
17
17
  }
18
18
 
@@ -76,6 +76,33 @@ const toProc = node => {
76
76
  return null;
77
77
  }
78
78
 
79
+ // Ensure that we're not inside of a hash that is being passed to a key that
80
+ // corresponds to `:if` or `:unless` to avoid problems with callbacks with
81
+ // Rails. For more context, see:
82
+ // https://github.com/prettier/plugin-ruby/issues/449
83
+ let assocNode = null;
84
+
85
+ if (path.getValue().type === "method_add_block") {
86
+ assocNode = path.getParentNode();
87
+ } else if (path.getValue().type === "args") {
88
+ assocNode = path.getParentNode(2);
89
+ }
90
+
91
+ if (assocNode && assocNode.type === "assoc_new") {
92
+ const [key] = assocNode.body;
93
+
94
+ if (key.type === "@label" && ["if:", "unless:"].includes(key.body)) {
95
+ return null;
96
+ }
97
+
98
+ if (
99
+ key.type === "symbol_literal" &&
100
+ ["if", "unless"].includes(key.body[0].body[0].body)
101
+ ) {
102
+ return null;
103
+ }
104
+ }
105
+
79
106
  return `&:${method.body}`;
80
107
  };
81
108
 
@@ -10,11 +10,11 @@ const concatBody = (path, opts, print) => concat(path.map(print, "body"));
10
10
 
11
11
  // If the node is a type of assignment or if the node is a paren and nested
12
12
  // inside that paren is a node that is a type of assignment.
13
- const containsAssignment = node =>
13
+ const containsAssignment = (node) =>
14
14
  ["assign", "massign"].includes(node.type) ||
15
15
  (node.type === "paren" && node.body[0].body.some(containsAssignment));
16
16
 
17
- const docLength = doc => {
17
+ const docLength = (doc) => {
18
18
  if (doc.length) {
19
19
  return doc.length;
20
20
  }
@@ -50,7 +50,7 @@ const hasAncestor = (path, types) => {
50
50
  return false;
51
51
  };
52
52
 
53
- const literal = value => () => value;
53
+ const literal = (value) => () => value;
54
54
 
55
55
  const makeArgs = (path, opts, print, argsIndex) => {
56
56
  let argNodes = path.getValue().body[argsIndex];
@@ -103,13 +103,13 @@ const makeCall = (path, opts, print) => {
103
103
 
104
104
  const makeList = (path, opts, print) => path.map(print, "body");
105
105
 
106
- const prefix = value => (path, opts, print) =>
106
+ const prefix = (value) => (path, opts, print) =>
107
107
  concat([value, path.call(print, "body", 0)]);
108
108
 
109
109
  const printComments = (printed, start, comments) => {
110
110
  let node = printed;
111
111
 
112
- comments.forEach(comment => {
112
+ comments.forEach((comment) => {
113
113
  if (comment.start < start) {
114
114
  node = concat([
115
115
  comment.break ? breakParent : "",
@@ -129,7 +129,7 @@ const printComments = (printed, start, comments) => {
129
129
  return node;
130
130
  };
131
131
 
132
- const skipAssignIndent = node =>
132
+ const skipAssignIndent = (node) =>
133
133
  ["array", "hash", "heredoc", "lambda", "regexp_literal"].includes(
134
134
  node.type
135
135
  ) ||
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prettier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.19.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-11-14 00:00:00.000000000 Z
11
+ date: 2020-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -72,7 +72,6 @@ files:
72
72
  - node_modules/prettier/index.js
73
73
  - node_modules/prettier/third-party.js
74
74
  - package.json
75
- - src/escapePattern.js
76
75
  - src/haml.js
77
76
  - src/haml/embed.js
78
77
  - src/haml/nodes/comment.js
@@ -139,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
138
  - !ruby/object:Gem::Version
140
139
  version: '0'
141
140
  requirements: []
142
- rubygems_version: 3.0.6
141
+ rubygems_version: 3.1.2
143
142
  signing_key:
144
143
  specification_version: 4
145
144
  summary: prettier plugin for the Ruby programming language
@@ -1,45 +0,0 @@
1
- /**
2
- * String literal syntax documentation from Ruby source (2.7.0-dev)
3
- *
4
- * Double-quote strings allow escaped characters such as <tt>\n</tt> for
5
- * newline, <tt>\t</tt> for tab, etc. The full list of supported escape
6
- * sequences are as follows:
7
- *
8
- * \a bell, ASCII 07h (BEL)
9
- * \b backspace, ASCII 08h (BS)
10
- * \t horizontal tab, ASCII 09h (TAB)
11
- * \n newline (line feed), ASCII 0Ah (LF)
12
- * \v vertical tab, ASCII 0Bh (VT)
13
- * \f form feed, ASCII 0Ch (FF)
14
- * \r carriage return, ASCII 0Dh (CR)
15
- * \e escape, ASCII 1Bh (ESC)
16
- * \s space, ASCII 20h (SPC)
17
- * \\ backslash, \
18
- * \nnn octal bit pattern, where nnn is 1-3 octal digits ([0-7])
19
- * \xnn hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
20
- * \unnnn Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
21
- * \u{nnnn ...} Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
22
- * \cx or \C-x control character, where x is an ASCII printable character
23
- * \M-x meta character, where x is an ASCII printable character
24
- * \M-\C-x meta control character, where x is an ASCII printable character
25
- * \M-\cx same as above
26
- * \c\M-x same as above
27
- * \c? or \C-? delete, ASCII 7Fh (DEL)
28
- *
29
- * In addition to disabling interpolation, single-quoted strings also disable all
30
- * escape sequences except for the single-quote (<tt>\'</tt>) and backslash
31
- * (<tt>\\\\</tt>).
32
- */
33
- const patterns = [
34
- "[abtnvfres\\\\]", // simple
35
- "[0-7]{1,3}", // octal bits
36
- "x[0-9a-fA-F]{1,2}", // hex bit
37
- "u[0-9a-fA-F]{4}", // unicode char
38
- "u\\{[0-9a-fA-F ]+\\}", // unicode chars
39
- "c[ -~]|C\\-[ -~]", // control
40
- "M\\-[ -~]", // meta
41
- "M\\-\\\\C\\-[ -~]|M\\-\\\\c[ -~]|c\\\\M\\-[ -~]", // meta control
42
- "c\\?|C\\-\\?" // delete
43
- ];
44
-
45
- module.exports = new RegExp(`\\\\(${patterns.join("|")})`);