prettier 1.5.5 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +31 -12
  5. data/node_modules/prettier/bin-prettier.js +13702 -11629
  6. data/node_modules/prettier/index.js +19198 -16572
  7. data/node_modules/prettier/parser-angular.js +61 -40
  8. data/node_modules/prettier/parser-babel.js +22 -1
  9. data/node_modules/prettier/parser-espree.js +22 -1
  10. data/node_modules/prettier/parser-flow.js +22 -1
  11. data/node_modules/prettier/parser-glimmer.js +1 -1
  12. data/node_modules/prettier/parser-graphql.js +1 -1
  13. data/node_modules/prettier/parser-html.js +82 -63
  14. data/node_modules/prettier/parser-markdown.js +24 -9
  15. data/node_modules/prettier/parser-meriyah.js +22 -1
  16. data/node_modules/prettier/parser-postcss.js +22 -1
  17. data/node_modules/prettier/parser-typescript.js +22 -1
  18. data/node_modules/prettier/parser-yaml.js +2 -2
  19. data/node_modules/prettier/third-party.js +1042 -833
  20. data/package.json +3 -3
  21. data/rubocop.yml +9 -0
  22. data/src/haml/parser.js +5 -4
  23. data/src/haml/printer.js +428 -18
  24. data/src/parser/parseSync.js +8 -6
  25. data/src/plugin.js +1 -1
  26. data/src/rbs/parser.js +1 -3
  27. data/src/rbs/printer.js +35 -7
  28. data/src/ruby/nodes/args.js +66 -22
  29. data/src/ruby/nodes/calls.js +8 -1
  30. data/src/ruby/nodes/conditionals.js +47 -45
  31. data/src/ruby/nodes/hashes.js +5 -14
  32. data/src/ruby/nodes/params.js +2 -9
  33. data/src/ruby/nodes/strings.js +95 -2
  34. data/src/ruby/parser.js +1 -3
  35. data/src/ruby/parser.rb +52 -29
  36. data/src/ruby/printer.js +10 -1
  37. data/src/utils/inlineEnsureParens.js +1 -0
  38. data/src/utils/skipAssignIndent.js +8 -1
  39. metadata +3 -12
  40. data/src/haml/nodes/comment.js +0 -27
  41. data/src/haml/nodes/doctype.js +0 -34
  42. data/src/haml/nodes/filter.js +0 -16
  43. data/src/haml/nodes/hamlComment.js +0 -21
  44. data/src/haml/nodes/plain.js +0 -6
  45. data/src/haml/nodes/root.js +0 -8
  46. data/src/haml/nodes/script.js +0 -33
  47. data/src/haml/nodes/silentScript.js +0 -59
  48. data/src/haml/nodes/tag.js +0 -232
data/src/ruby/parser.js CHANGED
@@ -8,12 +8,10 @@ function parse(text, _parsers, opts) {
8
8
  return parseSync("ruby", text, opts);
9
9
  }
10
10
 
11
- const pragmaPattern = /#\s*@(prettier|format)/;
12
-
13
11
  // This function handles checking whether or not the source string has the
14
12
  // pragma for prettier. This is an optional workflow for incremental adoption.
15
13
  function hasPragma(text) {
16
- return pragmaPattern.test(text);
14
+ return /^\s*#[^\S\n]*@(format|prettier)\s*(\n|$)/.test(text);
17
15
  }
18
16
 
19
17
  // This function is critical for comments and cursor support, and is responsible
data/src/ruby/parser.rb CHANGED
@@ -13,8 +13,7 @@ if (RUBY_MAJOR < 2) || ((RUBY_MAJOR == 2) && (RUBY_MINOR < 5))
13
13
  exit 1
14
14
  end
15
15
 
16
- require 'delegate'
17
- require 'json'
16
+ require 'json' unless defined?(JSON)
18
17
  require 'ripper'
19
18
 
20
19
  module Prettier
@@ -54,6 +53,35 @@ class Prettier::Parser < Ripper
54
53
  end
55
54
  end
56
55
 
56
+ # This is a small wrapper around the value of a node for those specific events
57
+ # that need extra handling. (For example: statement, body statement, and
58
+ # rescue nodes which all need extra information to determine their character
59
+ # boundaries.)
60
+ class Node
61
+ attr_reader :parser, :value
62
+
63
+ def initialize(parser, value)
64
+ @parser = parser
65
+ @value = value
66
+ end
67
+
68
+ def [](key)
69
+ value[key]
70
+ end
71
+
72
+ def dig(*keys)
73
+ value.dig(*keys)
74
+ end
75
+
76
+ def to_json(*opts)
77
+ value.to_json(*opts)
78
+ end
79
+
80
+ def pretty_print(q)
81
+ q.pp_hash(self)
82
+ end
83
+ end
84
+
57
85
  attr_reader :source, :lines, :scanner_events
58
86
 
59
87
  # This is an attr_accessor so Stmts objects can grab comments out of this
@@ -617,19 +645,19 @@ class Prettier::Parser < Ripper
617
645
  # bodystmt can't actually determine its bounds appropriately because it
618
646
  # doesn't necessarily know where it started. So the parent node needs to
619
647
  # report back down into this one where it goes.
620
- class BodyStmt < SimpleDelegator
648
+ class BodyStmt < Node
621
649
  def bind(sc, ec)
622
- merge!(sc: sc, ec: ec)
623
- parts = self[:body]
650
+ value.merge!(sc: sc, ec: ec)
651
+ parts = value[:body]
624
652
 
625
653
  # Here we're going to determine the bounds for the stmts
626
654
  consequent = parts[1..-1].compact.first
627
- self[:body][0].bind(sc, consequent ? consequent[:sc] : ec)
655
+ value[:body][0].bind(sc, consequent ? consequent[:sc] : ec)
628
656
 
629
657
  # Next we're going to determine the rescue clause if there is one
630
658
  if parts[1]
631
659
  consequent = parts[2..-1].compact.first
632
- self[:body][1].bind_end(consequent ? consequent[:sc] : ec)
660
+ value[:body][1].bind_end(consequent ? consequent[:sc] : ec)
633
661
  end
634
662
  end
635
663
  end
@@ -638,6 +666,7 @@ class Prettier::Parser < Ripper
638
666
  # of clauses within the body of a method or block.
639
667
  def on_bodystmt(stmts, rescued, ensured, elsed)
640
668
  BodyStmt.new(
669
+ self,
641
670
  type: :bodystmt,
642
671
  body: [stmts, rescued, ensured, elsed],
643
672
  sl: lineno,
@@ -1063,7 +1092,7 @@ class Prettier::Parser < Ripper
1063
1092
 
1064
1093
  beging.merge(
1065
1094
  type: :dyna_symbol,
1066
- quote: beging[:body][1],
1095
+ quote: beging[:body],
1067
1096
  body: string[:body],
1068
1097
  el: ending[:el],
1069
1098
  ec: ending[:ec]
@@ -1845,12 +1874,12 @@ class Prettier::Parser < Ripper
1845
1874
  # doesn't really have all of the information that it needs in order to
1846
1875
  # determine its ending. Therefore it relies on its parent bodystmt node to
1847
1876
  # report its ending to it.
1848
- class Rescue < SimpleDelegator
1877
+ class Rescue < Node
1849
1878
  def bind_end(ec)
1850
- merge!(ec: ec)
1879
+ value.merge!(ec: ec)
1851
1880
 
1852
- stmts = self[:body][1]
1853
- consequent = self[:body][2]
1881
+ stmts = value[:body][1]
1882
+ consequent = value[:body][2]
1854
1883
 
1855
1884
  if consequent
1856
1885
  consequent.bind_end(ec)
@@ -1886,6 +1915,7 @@ class Prettier::Parser < Ripper
1886
1915
  end
1887
1916
 
1888
1917
  Rescue.new(
1918
+ self,
1889
1919
  beging.merge!(
1890
1920
  type: :rescue,
1891
1921
  body: [rescue_ex, stmts, consequent],
@@ -1986,36 +2016,29 @@ class Prettier::Parser < Ripper
1986
2016
  # stmts nodes will report back down the location information. We then
1987
2017
  # propagate that onto void_stmt nodes inside the stmts in order to make sure
1988
2018
  # all comments get printed appropriately.
1989
- class Stmts < SimpleDelegator
1990
- attr_reader :parser
1991
-
1992
- def initialize(parser, values)
1993
- @parser = parser
1994
- __setobj__(values)
1995
- end
1996
-
2019
+ class Stmts < Node
1997
2020
  def bind(sc, ec)
1998
- merge!(sc: sc, ec: ec)
2021
+ value.merge!(sc: sc, ec: ec)
1999
2022
 
2000
- if self[:body][0][:type] == :void_stmt
2001
- self[:body][0].merge!(sc: sc, ec: sc)
2023
+ if value[:body][0][:type] == :void_stmt
2024
+ value[:body][0].merge!(sc: sc, ec: sc)
2002
2025
  end
2003
2026
 
2004
2027
  attach_comments(sc, ec)
2005
2028
  end
2006
2029
 
2007
2030
  def bind_end(ec)
2008
- merge!(ec: ec)
2031
+ value.merge!(ec: ec)
2009
2032
  end
2010
2033
 
2011
2034
  def <<(statement)
2012
- if self[:body].any?
2013
- merge!(statement.slice(:el, :ec))
2035
+ if value[:body].any?
2036
+ value.merge!(statement.slice(:el, :ec))
2014
2037
  else
2015
- merge!(statement.slice(:sl, :el, :sc, :ec))
2038
+ value.merge!(statement.slice(:sl, :el, :sc, :ec))
2016
2039
  end
2017
2040
 
2018
- self[:body] << statement
2041
+ value[:body] << statement
2019
2042
  self
2020
2043
  end
2021
2044
 
@@ -2032,7 +2055,7 @@ class Prettier::Parser < Ripper
2032
2055
  return if attachable.empty?
2033
2056
 
2034
2057
  parser.comments -= attachable
2035
- self[:body] = (self[:body] + attachable).sort_by! { |node| node[:sc] }
2058
+ value[:body] = (value[:body] + attachable).sort_by! { |node| node[:sc] }
2036
2059
  end
2037
2060
  end
2038
2061
 
data/src/ruby/printer.js CHANGED
@@ -127,6 +127,14 @@ function isBlockComment(comment) {
127
127
  return comment.type === "@embdoc";
128
128
  }
129
129
 
130
+ // This function handles adding the format pragma to a source string. This is an
131
+ // optional workflow for incremental adoption.
132
+ function insertPragma(text) {
133
+ const boundary = text.startsWith("#") ? "\n" : "\n\n";
134
+
135
+ return `# @format${boundary}${text}`;
136
+ }
137
+
130
138
  module.exports = {
131
139
  embed,
132
140
  print: printNode,
@@ -134,5 +142,6 @@ module.exports = {
134
142
  canAttachComment,
135
143
  getCommentChildNodes,
136
144
  printComment,
137
- isBlockComment
145
+ isBlockComment,
146
+ insertPragma
138
147
  };
@@ -2,6 +2,7 @@ const needsParens = [
2
2
  "args",
3
3
  "assign",
4
4
  "assoc_new",
5
+ "binary",
5
6
  "call",
6
7
  "massign",
7
8
  "opassign"
@@ -1,4 +1,11 @@
1
- const skippable = ["array", "hash", "heredoc", "lambda", "regexp_literal"];
1
+ const skippable = [
2
+ "array",
3
+ "dyna_symbol",
4
+ "hash",
5
+ "heredoc",
6
+ "lambda",
7
+ "regexp_literal"
8
+ ];
2
9
 
3
10
  function skipAssignIndent(node) {
4
11
  return (
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.5
4
+ version: 1.6.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: 2021-03-25 00:00:00.000000000 Z
11
+ date: 2021-06-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -44,15 +44,6 @@ files:
44
44
  - package.json
45
45
  - rubocop.yml
46
46
  - src/haml/embed.js
47
- - src/haml/nodes/comment.js
48
- - src/haml/nodes/doctype.js
49
- - src/haml/nodes/filter.js
50
- - src/haml/nodes/hamlComment.js
51
- - src/haml/nodes/plain.js
52
- - src/haml/nodes/root.js
53
- - src/haml/nodes/script.js
54
- - src/haml/nodes/silentScript.js
55
- - src/haml/nodes/tag.js
56
47
  - src/haml/parser.js
57
48
  - src/haml/parser.rb
58
49
  - src/haml/printer.js
@@ -133,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
124
  - !ruby/object:Gem::Version
134
125
  version: '0'
135
126
  requirements: []
136
- rubygems_version: 3.1.4
127
+ rubygems_version: 3.2.3
137
128
  signing_key:
138
129
  specification_version: 4
139
130
  summary: prettier plugin for the Ruby programming language
@@ -1,27 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- // https://haml.info/docs/yardoc/file.REFERENCE.html#html-comments-
4
- function comment(path, _opts, print) {
5
- const { children, value } = path.getValue();
6
- const parts = ["/"];
7
-
8
- if (value.revealed) {
9
- parts.push("!");
10
- }
11
-
12
- if (value.conditional) {
13
- parts.push(value.conditional);
14
- } else if (value.text) {
15
- parts.push(" ", value.text);
16
- }
17
-
18
- if (children.length > 0) {
19
- parts.push(
20
- indent(concat([hardline, join(hardline, path.map(print, "children"))]))
21
- );
22
- }
23
-
24
- return group(concat(parts));
25
- }
26
-
27
- module.exports = comment;
@@ -1,34 +0,0 @@
1
- const { join } = require("../../prettier");
2
-
3
- const types = {
4
- basic: "Basic",
5
- frameset: "Frameset",
6
- mobile: "Mobile",
7
- rdfa: "RDFa",
8
- strict: "Strict",
9
- xml: "XML"
10
- };
11
-
12
- const versions = ["1.1", "5"];
13
-
14
- // https://haml.info/docs/yardoc/file.REFERENCE.html#doctype-
15
- function doctype(path, _opts, _print) {
16
- const { value } = path.getValue();
17
- const parts = ["!!!"];
18
-
19
- if (value.type in types) {
20
- parts.push(types[value.type]);
21
- } else if (versions.includes(value.version)) {
22
- parts.push(value.version);
23
- } else {
24
- parts.push(value.type);
25
- }
26
-
27
- if (value.encoding) {
28
- parts.push(value.encoding);
29
- }
30
-
31
- return join(" ", parts);
32
- }
33
-
34
- module.exports = doctype;
@@ -1,16 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- // https://haml.info/docs/yardoc/file.REFERENCE.html#filters
4
- function filter(path, _opts, _print) {
5
- const { value } = path.getValue();
6
-
7
- return group(
8
- concat([
9
- ":",
10
- value.name,
11
- indent(concat([hardline, join(hardline, value.text.trim().split("\n"))]))
12
- ])
13
- );
14
- }
15
-
16
- module.exports = filter;
@@ -1,21 +0,0 @@
1
- const { concat, hardline, indent, join } = require("../../prettier");
2
-
3
- // https://haml.info/docs/yardoc/file.REFERENCE.html#haml-comments--
4
- function hamlComment(path, opts, _print) {
5
- const node = path.getValue();
6
- const parts = ["-#"];
7
-
8
- if (node.value.text) {
9
- if (opts.originalText.split("\n")[node.line - 1].trim() === "-#") {
10
- const lines = node.value.text.trim().split("\n");
11
-
12
- parts.push(indent(concat([hardline, join(hardline, lines)])));
13
- } else {
14
- parts.push(" ", node.value.text.trim());
15
- }
16
- }
17
-
18
- return concat(parts);
19
- }
20
-
21
- module.exports = hamlComment;
@@ -1,6 +0,0 @@
1
- // https://haml.info/docs/yardoc/file.REFERENCE.html#plain-text
2
- function plain(path, _opts, _print) {
3
- return path.getValue().value.text;
4
- }
5
-
6
- module.exports = plain;
@@ -1,8 +0,0 @@
1
- const { concat, hardline, join } = require("../../prettier");
2
-
3
- // The root node in the AST
4
- function root(path, _opts, print) {
5
- return concat([join(hardline, path.map(print, "children")), hardline]);
6
- }
7
-
8
- module.exports = root;
@@ -1,33 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- // https://haml.info/docs/yardoc/file.REFERENCE.html#inserting_ruby
4
- function script(path, opts, print) {
5
- const { children, value } = path.getValue();
6
- const parts = [];
7
-
8
- if (value.escape_html) {
9
- parts.unshift("&");
10
- }
11
-
12
- if (value.preserve) {
13
- parts.push("~");
14
- } else if (!value.interpolate) {
15
- parts.push("=");
16
- }
17
-
18
- if (value.escape_html && !value.preserve && value.interpolate) {
19
- parts.push(" ", value.text.trim().slice(1, -1));
20
- } else {
21
- parts.push(" ", value.text.trim());
22
- }
23
-
24
- if (children.length > 0) {
25
- parts.push(
26
- indent(concat([hardline, join(hardline, path.map(print, "children"))]))
27
- );
28
- }
29
-
30
- return group(concat(parts));
31
- }
32
-
33
- module.exports = script;
@@ -1,59 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- function findKeywordIndices(children, keywords) {
4
- const indices = [];
5
-
6
- children.forEach((child, index) => {
7
- if (child.type !== "silent_script") {
8
- return;
9
- }
10
-
11
- if (keywords.includes(child.value.keyword)) {
12
- indices.push(index);
13
- }
14
- });
15
-
16
- return indices;
17
- }
18
-
19
- // https://haml.info/docs/yardoc/file.REFERENCE.html#running-ruby--
20
- function silentScript(path, _opts, print) {
21
- const { children, value } = path.getValue();
22
- const parts = [`- ${value.text.trim()}`];
23
-
24
- if (children.length > 0) {
25
- const scripts = path.map(print, "children");
26
-
27
- if (value.keyword === "case") {
28
- const keywordIndices = findKeywordIndices(children, ["when", "else"]);
29
-
30
- parts.push(
31
- concat(
32
- scripts.map((script, index) => {
33
- const concated = concat([hardline, script]);
34
-
35
- return keywordIndices.includes(index) ? concated : indent(concated);
36
- })
37
- )
38
- );
39
- } else if (["if", "unless"].includes(value.keyword)) {
40
- const keywordIndices = findKeywordIndices(children, ["elsif", "else"]);
41
-
42
- parts.push(
43
- concat(
44
- scripts.map((script, index) => {
45
- const concated = concat([hardline, script]);
46
-
47
- return keywordIndices.includes(index) ? concated : indent(concated);
48
- })
49
- )
50
- );
51
- } else {
52
- parts.push(indent(concat([hardline, join(hardline, scripts)])));
53
- }
54
- }
55
-
56
- return group(concat(parts));
57
- }
58
-
59
- module.exports = silentScript;