prettier 1.2.5 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a18f7e5ef9f61a53a91bda2f64e3e2a1e6e6541676a581a99128f363468b0c4
4
- data.tar.gz: 374f7b95865b7ec4cf2bd16276b6c71f8b4bd4f62b34d860a773f3186bf8f308
3
+ metadata.gz: 657db7d1eef6e33d72337eb6b666678675c9eced4cc19716466b4bd74c039cfc
4
+ data.tar.gz: d69ffa4e3b2b48146c112a9ceb0d4ddab3351119c1bb2b9d92848f7beb3f40e4
5
5
  SHA512:
6
- metadata.gz: b793f8694680859cca6c85b0786e3a4a608b3e0bb8cca23387995b694d03dcc8098821d259e855cb3ff690ceb2fd4d43587423d61945aec5dde8c8a48b4910f4
7
- data.tar.gz: cb27a126a66d8ca08319dd45fc9207e7549ea084e0f4c5c787d05c36bc92c300ed396d1d9c8be664d697d625e1901abb907d031dcfdedc518f4c6beb5b0504f2
6
+ metadata.gz: d7c4d27daddee1e5d07393c5358e48a419c75a6e1aff62b36779d093f16f3da613c938b98c188752c93835e66ddd9578ebf6595fcde1a1f69f40f2613b492ddd
7
+ data.tar.gz: c586e33dcd0057779689df71ab68d8858aac27c9e89cf2f500edd2b55779b26693cf6756815cc616054c364b6bc4e1adff05263036f74025425218c04676969c
@@ -6,6 +6,13 @@ 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.3.0] - 2021-01-05
10
+
11
+ ### Added
12
+
13
+ - [@kddeisz] - Handling of the RBS language.
14
+ - [@kddeisz] - Incorporate the HAML plugin.
15
+
9
16
  ## [1.2.5] - 2021-01-04
10
17
 
11
18
  ### Changed
@@ -1028,7 +1035,8 @@ would previously result in `array[]`, but now prints properly.
1028
1035
 
1029
1036
  - Initial release 🎉
1030
1037
 
1031
- [unreleased]: https://github.com/prettier/plugin-ruby/compare/v1.2.5...HEAD
1038
+ [unreleased]: https://github.com/prettier/plugin-ruby/compare/v1.3.0...HEAD
1039
+ [1.3.0]: https://github.com/prettier/plugin-ruby/compare/v1.2.5...v1.3.0
1032
1040
  [1.2.5]: https://github.com/prettier/plugin-ruby/compare/v1.2.4...v1.2.5
1033
1041
  [1.2.4]: https://github.com/prettier/plugin-ruby/compare/v1.2.3...v1.2.4
1034
1042
  [1.2.3]: https://github.com/prettier/plugin-ruby/compare/v1.2.2...v1.2.3
@@ -1102,6 +1110,7 @@ would previously result in `array[]`, but now prints properly.
1102
1110
  [@cldevs]: https://github.com/cldevs
1103
1111
  [@codingitwrong]: https://github.com/CodingItWrong
1104
1112
  [@coiti]: https://github.com/coiti
1113
+ [@cvoege]: https://github.com/cvoege
1105
1114
  [@deecewan]: https://github.com/deecewan
1106
1115
  [@dudeofawesome]: https://github.com/dudeofawesome
1107
1116
  [@eins78]: https://github.com/eins78
@@ -1152,6 +1161,7 @@ would previously result in `array[]`, but now prints properly.
1152
1161
  [@sviccari]: https://github.com/SViccari
1153
1162
  [@tobyndockerill]: https://github.com/tobyndockerill
1154
1163
  [@uri]: https://github.com/uri
1164
+ [@valscion]: https://github.com/valscion
1155
1165
  [@xipgroc]: https://github.com/xipgroc
1156
1166
  [@ykpythemind]: https://github.com/ykpythemind
1157
1167
  [@yuki24]: https://github.com/yuki24
data/README.md CHANGED
@@ -72,6 +72,12 @@ To run `prettier` with the Ruby plugin, you're going to need [`ruby`](https://ww
72
72
 
73
73
  Note that currently the editor integrations work best with the `npm` package, as most of the major editor plugins expect a `node_modules` directory. You can get them to work with the Ruby gem, but it requires manually configuring the paths.
74
74
 
75
+ This plugin currently supports formatting the following kinds of files:
76
+
77
+ - All varieties of Ruby source files (e.g., `*.rb`, `*.gemspec`, `Gemfile`, etc.)
78
+ - [RBS type language](https://github.com/ruby/rbs) files - requires having the `rbs` gem in your gem path
79
+ - [HAML template language](https://haml.info/) files - requires having the `haml` gem in your gem path
80
+
75
81
  ### Ruby gem
76
82
 
77
83
  Add this line to your application's Gemfile:
@@ -95,7 +101,7 @@ gem install prettier
95
101
  The `rbprettier` executable is now installed and ready for use:
96
102
 
97
103
  ```bash
98
- bundle exec rbprettier --write '**/*.rb'
104
+ bundle exec rbprettier --write '**/*'
99
105
  ```
100
106
 
101
107
  ### `npm` package
@@ -115,7 +121,7 @@ yarn add --dev prettier @prettier/plugin-ruby
115
121
  The `prettier` executable is now installed and ready for use:
116
122
 
117
123
  ```bash
118
- ./node_modules/.bin/prettier --write '**/*.rb'
124
+ ./node_modules/.bin/prettier --write '**/*'
119
125
  ```
120
126
 
121
127
  ## Configuration
@@ -146,7 +152,7 @@ file](https://prettier.io/docs/en/configuration.html). For example:
146
152
  Or, they can be passed to `prettier` as arguments:
147
153
 
148
154
  ```bash
149
- prettier --ruby-single-quote false --write '**/*.rb'
155
+ prettier --ruby-single-quote false --write '**/*'
150
156
  ```
151
157
 
152
158
  ### Usage with RuboCop
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@prettier/plugin-ruby",
3
- "version": "1.2.5",
3
+ "version": "1.3.0",
4
4
  "description": "prettier plugin for the Ruby programming language",
5
5
  "main": "src/plugin.js",
6
6
  "scripts": {
7
7
  "check-format": "prettier --check '**/*'",
8
8
  "lint": "eslint --cache .",
9
- "test": "jest"
9
+ "test": "PORT=$(bin/port) jest"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
@@ -0,0 +1,87 @@
1
+ const {
2
+ concat,
3
+ hardline,
4
+ indent,
5
+ literalline,
6
+ markAsRoot,
7
+ mapDoc,
8
+ stripTrailingHardline
9
+ } = require("../prettier");
10
+
11
+ // Get the name of the parser that is represented by the given element node,
12
+ // return null if a matching parser cannot be found
13
+ function getParser(name, opts) {
14
+ let parser = name;
15
+
16
+ // We don't want to deal with some weird recursive parser situation, so we
17
+ // need to explicitly call out the HAML parser here and just return null
18
+ if (parser === "haml") {
19
+ return null;
20
+ }
21
+
22
+ // In HAML the name of the JS filter is :javascript, whereas in prettier the
23
+ // name of the JS parser is babel. Here we explicitly handle that conversion.
24
+ if (parser === "javascript") {
25
+ parser = "babel";
26
+ }
27
+
28
+ // If there is a plugin that has a parser that matches the name of this
29
+ // element, then we're going to assume that's correct for embedding and go
30
+ // ahead and switch to that parser
31
+ if (
32
+ opts.plugins.some(
33
+ (plugin) =>
34
+ plugin.parsers &&
35
+ Object.prototype.hasOwnProperty.call(plugin.parsers, parser)
36
+ )
37
+ ) {
38
+ return parser;
39
+ }
40
+
41
+ return null;
42
+ }
43
+
44
+ // This function is in here because it handles embedded parser values. I don't
45
+ // have a test that exercises it because I'm not sure for which parser it is
46
+ // necessary, but since it's in prettier core I'm keeping it here.
47
+ /* istanbul ignore next */
48
+ function replaceNewlines(doc) {
49
+ return mapDoc(doc, (currentDoc) =>
50
+ typeof currentDoc === "string" && currentDoc.includes("\n")
51
+ ? concat(
52
+ currentDoc
53
+ .split(/(\n)/g)
54
+ .map((v, i) => (i % 2 === 0 ? v : literalline))
55
+ )
56
+ : currentDoc
57
+ );
58
+ }
59
+
60
+ function embed(path, _print, textToDoc, opts) {
61
+ const node = path.getValue();
62
+ if (node.type !== "filter") {
63
+ return null;
64
+ }
65
+
66
+ const parser = getParser(node.value.name, opts);
67
+ if (!parser) {
68
+ return null;
69
+ }
70
+
71
+ return markAsRoot(
72
+ concat([
73
+ ":",
74
+ node.value.name,
75
+ indent(
76
+ concat([
77
+ hardline,
78
+ replaceNewlines(
79
+ stripTrailingHardline(textToDoc(node.value.text, { parser }))
80
+ )
81
+ ])
82
+ )
83
+ ])
84
+ );
85
+ }
86
+
87
+ module.exports = embed;
@@ -0,0 +1,27 @@
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;
@@ -0,0 +1,34 @@
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;
@@ -0,0 +1,16 @@
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;
@@ -0,0 +1,21 @@
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;
@@ -0,0 +1,6 @@
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;
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,33 @@
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;
@@ -0,0 +1,59 @@
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;
@@ -0,0 +1,193 @@
1
+ const {
2
+ align,
3
+ concat,
4
+ fill,
5
+ group,
6
+ hardline,
7
+ ifBreak,
8
+ indent,
9
+ join,
10
+ line,
11
+ softline
12
+ } = require("../../prettier");
13
+
14
+ function getDynamicAttributes(header, attributes) {
15
+ const pairs = attributes
16
+ .slice(1, -2)
17
+ .split(",")
18
+ .map((pair) => pair.slice(1).split('" => '));
19
+
20
+ const parts = [concat([pairs[0][0], "=", pairs[0][1]])];
21
+ pairs.slice(1).forEach((pair) => {
22
+ parts.push(line, concat([pair[0], "=", pair[1]]));
23
+ });
24
+
25
+ return group(concat(["(", align(header + 1, fill(parts)), ")"]));
26
+ }
27
+
28
+ function getHashValue(value, opts) {
29
+ if (typeof value !== "string") {
30
+ return value.toString();
31
+ }
32
+
33
+ // This is a very special syntax created by the parser to let us know that
34
+ // this should be printed literally instead of as a string.
35
+ if (value.startsWith("&")) {
36
+ return value.slice(1);
37
+ }
38
+
39
+ const quote = opts.rubySingleQuote ? "'" : '"';
40
+ return `${quote}${value}${quote}`;
41
+ }
42
+
43
+ function getHashKey(key, opts) {
44
+ let quoted = key;
45
+ const joiner = opts.rubyHashLabel ? ":" : " =>";
46
+
47
+ if (key.includes(":") || key.includes("-")) {
48
+ const quote = opts.rubySingleQuote ? "'" : '"';
49
+ quoted = `${quote}${key}${quote}`;
50
+ }
51
+
52
+ return `${opts.rubyHashLabel ? "" : ":"}${quoted}${joiner}`;
53
+ }
54
+
55
+ function getKeyValuePair(key, value, opts) {
56
+ return `${getHashKey(key, opts)} ${getHashValue(value, opts)}`;
57
+ }
58
+
59
+ function getStaticAttributes(header, attributes, opts) {
60
+ const keys = Object.keys(attributes).filter(
61
+ (name) => !["class", "id"].includes(name)
62
+ );
63
+
64
+ const parts = [getKeyValuePair(keys[0], attributes[keys[0]], opts)];
65
+
66
+ keys.slice(1).forEach((key) => {
67
+ parts.push(",", line, getKeyValuePair(key, attributes[key], opts));
68
+ });
69
+
70
+ return group(concat(["{", align(header + 1, fill(parts)), "}"]));
71
+ }
72
+
73
+ function getAttributesObject(object, opts, level = 0) {
74
+ if (typeof object !== "object") {
75
+ return getHashValue(object, opts);
76
+ }
77
+
78
+ const boundary = level === 0 ? softline : line;
79
+ const parts = Object.keys(object).map((key) =>
80
+ concat([
81
+ getHashKey(key, opts),
82
+ " ",
83
+ getAttributesObject(object[key], opts, level + 1)
84
+ ])
85
+ );
86
+
87
+ return group(
88
+ concat([
89
+ "{",
90
+ indent(group(concat([boundary, join(concat([",", line]), parts)]))),
91
+ boundary,
92
+ "}"
93
+ ])
94
+ );
95
+ }
96
+
97
+ function getHeader(value, opts) {
98
+ const { attributes } = value;
99
+ const parts = [];
100
+
101
+ if (value.name !== "div") {
102
+ parts.push(`%${value.name}`);
103
+ }
104
+
105
+ if (attributes.class) {
106
+ parts.push(`.${attributes.class.replace(/ /g, ".")}`);
107
+ }
108
+
109
+ if (attributes.id) {
110
+ parts.push(`#${attributes.id}`);
111
+ }
112
+
113
+ if (value.dynamic_attributes.new) {
114
+ parts.push(
115
+ getDynamicAttributes(parts.join("").length, value.dynamic_attributes.new)
116
+ );
117
+ }
118
+
119
+ if (
120
+ Object.keys(attributes).some((name) => name !== "class" && name !== "id")
121
+ ) {
122
+ parts.push(getStaticAttributes(parts.join("").length, attributes, opts));
123
+ }
124
+
125
+ if (value.dynamic_attributes.old) {
126
+ if (parts.length === 0) {
127
+ parts.push("%div");
128
+ }
129
+
130
+ if (typeof value.dynamic_attributes.old === "string") {
131
+ parts.push(value.dynamic_attributes.old);
132
+ } else {
133
+ parts.push(getAttributesObject(value.dynamic_attributes.old, opts));
134
+ }
135
+ }
136
+
137
+ if (value.object_ref) {
138
+ if (parts.length === 0) {
139
+ parts.push("%div");
140
+ }
141
+ parts.push(value.object_ref);
142
+ }
143
+
144
+ if (value.nuke_outer_whitespace) {
145
+ parts.push(">");
146
+ }
147
+
148
+ if (value.nuke_inner_whitespace) {
149
+ parts.push("<");
150
+ }
151
+
152
+ if (value.self_closing) {
153
+ parts.push("/");
154
+ }
155
+
156
+ if (value.value) {
157
+ const prefix = value.parse ? "= " : ifBreak("", " ");
158
+
159
+ return group(
160
+ concat([
161
+ group(concat(parts)),
162
+ indent(concat([softline, prefix, value.value]))
163
+ ])
164
+ );
165
+ }
166
+
167
+ // In case none of the other if statements have matched and we're printing a
168
+ // div, we need to explicitly add it back into the array.
169
+ if (parts.length === 0 && value.name === "div") {
170
+ parts.push("%div");
171
+ }
172
+
173
+ return group(concat(parts));
174
+ }
175
+
176
+ // https://haml.info/docs/yardoc/file.REFERENCE.html#element-name-
177
+ function tag(path, opts, print) {
178
+ const { children, value } = path.getValue();
179
+ const header = getHeader(value, opts);
180
+
181
+ if (children.length === 0) {
182
+ return header;
183
+ }
184
+
185
+ return group(
186
+ concat([
187
+ header,
188
+ indent(concat([hardline, join(hardline, path.map(print, "children"))]))
189
+ ])
190
+ );
191
+ }
192
+
193
+ module.exports = tag;