prettier 0.17.0 → 0.19.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.
@@ -1,11 +1,31 @@
1
1
  const { spawnSync } = require("child_process");
2
2
  const path = require("path");
3
3
 
4
+ // In order to properly parse ruby code, we need to tell the ruby process to
5
+ // parse using UTF-8. Unfortunately, the way that you accomplish this looks
6
+ // differently depending on your platform. This object below represents all of
7
+ // the possible values of process.platform per:
8
+ // https://nodejs.org/api/process.html#process_process_platform
9
+ const LANG = {
10
+ aix: "C.UTF-8",
11
+ darwin: "en_US.UTF-8",
12
+ freebsd: "C.UTF-8",
13
+ linux: "C.UTF-8",
14
+ openbsd: "C.UTF-8",
15
+ sunos: "C.UTF-8",
16
+ win32: ".UTF-8"
17
+ }[process.platform];
18
+
4
19
  module.exports = (text, _parsers, _opts) => {
5
- const child = spawnSync("ruby", [path.join(__dirname, "./ripper.rb")], {
6
- input: text,
7
- maxBuffer: 10 * 1024 * 1024 // 10MB
8
- });
20
+ const child = spawnSync(
21
+ "ruby",
22
+ ["--disable-gems", path.join(__dirname, "./ripper.rb")],
23
+ {
24
+ env: { LANG },
25
+ input: text,
26
+ maxBuffer: 10 * 1024 * 1024 // 10MB
27
+ }
28
+ );
9
29
 
10
30
  const error = child.stderr.toString();
11
31
  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,13 @@
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
+
7
+ if (major < 2) || ((major == 2) && (minor < 5))
5
8
  warn(
6
9
  "Ruby version #{RUBY_VERSION} not supported. " \
7
- "Please upgrade to #{REQUIRED_VERSION} or above."
10
+ 'Please upgrade to 2.5.0 or above.'
8
11
  )
9
12
 
10
13
  exit 1
@@ -166,6 +169,7 @@ class RipperJS < Ripper
166
169
  assoc_splat: [:@op, '**'],
167
170
  arg_paren: :@lparen,
168
171
  args_add_star: [:@op, '*'],
172
+ args_forward: [:@op, '...'],
169
173
  begin: [:@kw, 'begin'],
170
174
  blockarg: [:@op, '&'],
171
175
  brace_block: :@lbrace,
@@ -290,7 +294,8 @@ class RipperJS < Ripper
290
294
  super(*body).merge!(
291
295
  start: node[:start],
292
296
  char_start: node[:char_start],
293
- char_end: char_pos
297
+ char_end: char_pos,
298
+ quote: node[:body]
294
299
  )
295
300
  end
296
301
  end
@@ -319,14 +324,28 @@ class RipperJS < Ripper
319
324
  # child node.
320
325
  %i[dyna_symbol symbol_literal].each do |event|
321
326
  define_method(:"on_#{event}") do |*body|
322
- char_start =
327
+ options =
323
328
  if scanner_events.any? { |sevent| sevent[:type] == :@symbeg }
324
- find_scanner_event(:@symbeg)[:char_start]
329
+ symbeg = find_scanner_event(:@symbeg)
330
+
331
+ {
332
+ char_start: symbeg[:char_start],
333
+ char_end: char_pos,
334
+ quote: symbeg[:body][1]
335
+ }
336
+ elsif scanner_events.any? { |sevent| sevent[:type] == :@label_end }
337
+ label_end = find_scanner_event(:@label_end)
338
+
339
+ {
340
+ char_start: char_start_for(body),
341
+ char_end: char_pos,
342
+ quote: label_end[:body][0]
343
+ }
325
344
  else
326
- char_start_for(body)
345
+ { char_start: char_start_for(body), char_end: char_pos }
327
346
  end
328
347
 
329
- super(*body).merge!(char_start: char_start, char_end: char_pos)
348
+ super(*body).merge!(options)
330
349
  end
331
350
  end
332
351
 
@@ -630,6 +649,8 @@ class RipperJS < Ripper
630
649
  end
631
650
  )
632
651
 
652
+ # This module contains miscellaneous fixes required to get the right
653
+ # structure.
633
654
  prepend(
634
655
  Module.new do
635
656
  private
@@ -660,18 +681,6 @@ class RipperJS < Ripper
660
681
  super(*body).tap { |node| node[:body][0][:body] << __end__ if __end__ }
661
682
  end
662
683
 
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
684
  # Normally access controls are reported as vcall nodes. This creates a
676
685
  # new node type to explicitly track those nodes instead, so that the
677
686
  # 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:
@@ -49,6 +49,7 @@ module.exports = {
49
49
  filenames: [
50
50
  ".irbrc",
51
51
  ".pryrc",
52
+ ".simplecov",
52
53
  "Appraisals",
53
54
  "Berksfile",
54
55
  "Brewfile",
@@ -78,7 +79,8 @@ module.exports = {
78
79
  {
79
80
  name: "HAML",
80
81
  parsers: ["haml"],
81
- extensions: [".haml"]
82
+ extensions: [".haml"],
83
+ vscodeLanguageIds: ["haml"]
82
84
  }
83
85
  ],
84
86
  parsers: {
@@ -141,6 +143,13 @@ module.exports = {
141
143
  default: true,
142
144
  description:
143
145
  "When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals."
146
+ },
147
+ toProcTransform: {
148
+ type: "boolean",
149
+ category: "Global",
150
+ default: true,
151
+ description:
152
+ "When possible, convert blocks to the more concise Symbol#to_proc syntax."
144
153
  }
145
154
  },
146
155
  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.17.0
4
+ version: 0.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Deisz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-12 00:00:00.000000000 Z
11
+ date: 2020-08-21 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
@@ -52,8 +52,8 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '13.0'
55
- description:
56
- email:
55
+ description:
56
+ email:
57
57
  executables:
58
58
  - rbprettier
59
59
  extensions: []
@@ -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
@@ -124,7 +123,7 @@ homepage: https://github.com/prettier/plugin-ruby#readme
124
123
  licenses:
125
124
  - MIT
126
125
  metadata: {}
127
- post_install_message:
126
+ post_install_message:
128
127
  rdoc_options: []
129
128
  require_paths:
130
129
  - lib
@@ -139,8 +138,8 @@ 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
143
- signing_key:
141
+ rubygems_version: 3.0.3
142
+ signing_key:
144
143
  specification_version: 4
145
144
  summary: prettier plugin for the Ruby programming language
146
145
  test_files: []
@@ -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("|")})`);