prettier 0.20.1 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,36 +2,28 @@
2
2
 
3
3
  var os = require('os');
4
4
  var path = require('path');
5
- var module$1 = require('module');
5
+ var Module = require('module');
6
6
  var fs = require('fs');
7
7
  var util = require('util');
8
- var buffer$1 = require('buffer');
9
- var stream = require('stream');
10
8
 
11
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
10
 
13
11
  var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
14
12
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
15
- var module__default = /*#__PURE__*/_interopDefaultLegacy(module$1);
13
+ var Module__default = /*#__PURE__*/_interopDefaultLegacy(Module);
16
14
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
17
15
  var util__default = /*#__PURE__*/_interopDefaultLegacy(util);
18
- var buffer__default = /*#__PURE__*/_interopDefaultLegacy(buffer$1);
19
- var stream__default = /*#__PURE__*/_interopDefaultLegacy(stream);
20
16
 
21
17
  function createCommonjsModule(fn, basedir, module) {
22
18
  return module = {
23
- path: basedir,
24
- exports: {},
25
- require: function (path, base) {
26
- return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
27
- }
19
+ path: basedir,
20
+ exports: {},
21
+ require: function (path, base) {
22
+ return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
23
+ }
28
24
  }, fn(module, module.exports), module.exports;
29
25
  }
30
26
 
31
- function getCjsExportFromNamespace (n) {
32
- return n && n['default'] || n;
33
- }
34
-
35
27
  function commonjsRequire () {
36
28
  throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
37
29
  }
@@ -59,10 +51,10 @@ const resolveFrom = (fromDir, moduleId, silent) => {
59
51
 
60
52
  const fromFile = path__default['default'].join(fromDir, 'noop.js');
61
53
 
62
- const resolveFileName = () => module__default['default']._resolveFilename(moduleId, {
54
+ const resolveFileName = () => Module__default['default']._resolveFilename(moduleId, {
63
55
  id: fromFile,
64
56
  filename: fromFile,
65
- paths: module__default['default']._nodeModulePaths(fromDir)
57
+ paths: Module__default['default']._nodeModulePaths(fromDir)
66
58
  });
67
59
 
68
60
  if (silent) {
@@ -10164,133 +10156,41 @@ var findParentDir = createCommonjsModule(function (module, exports) {
10164
10156
  });
10165
10157
 
10166
10158
  const {
10167
- PassThrough: PassThroughStream
10168
- } = stream__default['default'];
10159
+ stdin
10160
+ } = process;
10169
10161
 
10170
- var bufferStream = options => {
10171
- options = Object.assign({}, options);
10172
- const {
10173
- array
10174
- } = options;
10175
- let {
10176
- encoding
10177
- } = options;
10178
- const isBuffer = encoding === 'buffer';
10179
- let objectMode = false;
10162
+ var getStdin = async () => {
10163
+ let result = '';
10180
10164
 
10181
- if (array) {
10182
- objectMode = !(encoding || isBuffer);
10183
- } else {
10184
- encoding = encoding || 'utf8';
10185
- }
10186
-
10187
- if (isBuffer) {
10188
- encoding = null;
10165
+ if (stdin.isTTY) {
10166
+ return result;
10189
10167
  }
10190
10168
 
10191
- const stream = new PassThroughStream({
10192
- objectMode
10193
- });
10169
+ stdin.setEncoding('utf8');
10194
10170
 
10195
- if (encoding) {
10196
- stream.setEncoding(encoding);
10171
+ for await (const chunk of stdin) {
10172
+ result += chunk;
10197
10173
  }
10198
10174
 
10199
- let length = 0;
10200
- const chunks = [];
10201
- stream.on('data', chunk => {
10202
- chunks.push(chunk);
10203
-
10204
- if (objectMode) {
10205
- length = chunks.length;
10206
- } else {
10207
- length += chunk.length;
10208
- }
10209
- });
10210
-
10211
- stream.getBufferedValue = () => {
10212
- if (array) {
10213
- return chunks;
10214
- }
10215
-
10216
- return isBuffer ? Buffer.concat(chunks, length) : chunks.join('');
10217
- };
10218
-
10219
- stream.getBufferedLength = () => length;
10220
-
10221
- return stream;
10175
+ return result;
10222
10176
  };
10223
10177
 
10224
- const {
10225
- constants: BufferConstants
10226
- } = buffer__default['default'];
10227
- const {
10228
- promisify: promisify$1
10229
- } = util__default['default'];
10230
- const streamPipelinePromisified = promisify$1(stream__default['default'].pipeline);
10178
+ var buffer = async () => {
10179
+ const result = [];
10180
+ let length = 0;
10231
10181
 
10232
- class MaxBufferError extends Error {
10233
- constructor() {
10234
- super('maxBuffer exceeded');
10235
- this.name = 'MaxBufferError';
10182
+ if (stdin.isTTY) {
10183
+ return Buffer.concat([]);
10236
10184
  }
10237
10185
 
10238
- }
10239
-
10240
- async function getStream(inputStream, options) {
10241
- if (!inputStream) {
10242
- throw new Error('Expected a stream');
10186
+ for await (const chunk of stdin) {
10187
+ result.push(chunk);
10188
+ length += chunk.length;
10243
10189
  }
10244
10190
 
10245
- options = Object.assign({
10246
- maxBuffer: Infinity
10247
- }, options);
10248
- const {
10249
- maxBuffer
10250
- } = options;
10251
- const stream = bufferStream(options);
10252
- await new Promise((resolve, reject) => {
10253
- const rejectPromise = error => {
10254
- // Don't retrieve an oversized buffer.
10255
- if (error && stream.getBufferedLength() <= BufferConstants.MAX_LENGTH) {
10256
- error.bufferedData = stream.getBufferedValue();
10257
- }
10258
-
10259
- reject(error);
10260
- };
10261
-
10262
- (async () => {
10263
- try {
10264
- await streamPipelinePromisified(inputStream, stream);
10265
- resolve();
10266
- } catch (error) {
10267
- rejectPromise(error);
10268
- }
10269
- })();
10270
-
10271
- stream.on('data', () => {
10272
- if (stream.getBufferedLength() > maxBuffer) {
10273
- rejectPromise(new MaxBufferError());
10274
- }
10275
- });
10276
- });
10277
- return stream.getBufferedValue();
10278
- }
10279
-
10280
- var getStream_1 = getStream;
10281
-
10282
- var buffer = (stream, options) => getStream(stream, Object.assign({}, options, {
10283
- encoding: 'buffer'
10284
- }));
10285
-
10286
- var array = (stream, options) => getStream(stream, Object.assign({}, options, {
10287
- array: true
10288
- }));
10289
-
10290
- var MaxBufferError_1 = MaxBufferError;
10291
- getStream_1.buffer = buffer;
10292
- getStream_1.array = array;
10293
- getStream_1.MaxBufferError = MaxBufferError_1;
10191
+ return Buffer.concat(result, length);
10192
+ };
10193
+ getStdin.buffer = buffer;
10294
10194
 
10295
10195
  var vendors = [
10296
10196
  {
@@ -10502,25 +10402,18 @@ var vendors = [
10502
10402
  }
10503
10403
  ];
10504
10404
 
10505
- var vendors$1 = /*#__PURE__*/Object.freeze({
10506
- __proto__: null,
10507
- 'default': vendors
10508
- });
10509
-
10510
- var vendors$2 = getCjsExportFromNamespace(vendors$1);
10511
-
10512
10405
  var ciInfo = createCommonjsModule(function (module, exports) {
10513
10406
 
10514
10407
  var env = process.env; // Used for testing only
10515
10408
 
10516
10409
  Object.defineProperty(exports, '_vendors', {
10517
- value: vendors$2.map(function (v) {
10410
+ value: vendors.map(function (v) {
10518
10411
  return v.constant;
10519
10412
  })
10520
10413
  });
10521
10414
  exports.name = null;
10522
10415
  exports.isPR = null;
10523
- vendors$2.forEach(function (vendor) {
10416
+ vendors.forEach(function (vendor) {
10524
10417
  var envs = Array.isArray(vendor.env) ? vendor.env : [vendor.env];
10525
10418
  var isCI = envs.every(function (obj) {
10526
10419
  return checkEnv(obj);
@@ -10576,7 +10469,7 @@ var thirdParty = {
10576
10469
  cosmiconfig: dist$2.cosmiconfig,
10577
10470
  cosmiconfigSync: dist$2.cosmiconfigSync,
10578
10471
  findParentDir: findParentDir.sync,
10579
- getStream: getStream_1,
10472
+ getStdin: getStdin,
10580
10473
  isCI: () => ciInfo.isCI
10581
10474
  };
10582
10475
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prettier/plugin-ruby",
3
- "version": "0.20.1",
3
+ "version": "0.21.0",
4
4
  "description": "prettier plugin for the Ruby programming language",
5
5
  "main": "src/ruby.js",
6
6
  "scripts": {
@@ -1,6 +1,7 @@
1
1
  module.exports = Object.assign(
2
2
  {},
3
3
  require("./nodes/alias"),
4
+ require("./nodes/aref"),
4
5
  require("./nodes/args"),
5
6
  require("./nodes/arrays"),
6
7
  require("./nodes/assign"),
@@ -1,32 +1,75 @@
1
- const { concat, join } = require("../prettier");
1
+ const {
2
+ addTrailingComment,
3
+ align,
4
+ concat,
5
+ group,
6
+ hardline,
7
+ line
8
+ } = require("../prettier");
2
9
 
3
- const usingSymbols = (path) => {
4
- const [left, right] = path.getValue().body.map((node) => node.body[0].type);
5
- return left === "symbol" && right === "symbol";
6
- };
7
-
8
- const identFromSymbol = (path, print, index) =>
9
- path.call(print, "body", index, "body", 0, "body", 0);
10
+ /* In general, return the printed doc of the argument at the provided index.
11
+ * Special handling is given for symbol literals that are not bare words, as we
12
+ * convert those into bare words by just pulling out the ident node.
13
+ */
14
+ function printAliasArgument(path, _opts, print, argIndex) {
15
+ const node = path.getValue().body[argIndex];
10
16
 
11
- const aliasError = (_path, _opts, _print) => {
12
- throw new Error("can't make alias for the number variables");
13
- };
17
+ if (node.type === "symbol_literal" && node.body[0].type === "symbol") {
18
+ // If we're going to descend into the symbol literal to grab out the ident
19
+ // node, then we need to make sure we copy over any comments as well,
20
+ // otherwise we could accidentally skip printing them.
21
+ if (node.comments) {
22
+ node.comments.forEach((comment) => {
23
+ addTrailingComment(node.body[0].body[0], comment);
24
+ });
25
+ }
14
26
 
15
- const aliasVars = (path, opts, print) => {
16
- if (usingSymbols(path)) {
17
- return join(" ", [
18
- identFromSymbol(path, print, 0),
19
- identFromSymbol(path, print, 1)
20
- ]);
27
+ return path.call(print, "body", argIndex, "body", 0, "body", 0);
21
28
  }
22
- return join(" ", path.map(print, "body"));
23
- };
24
29
 
25
- const alias = (path, opts, print) =>
26
- concat(["alias ", aliasVars(path, opts, print)]);
30
+ return path.call(print, "body", argIndex);
31
+ }
32
+
33
+ /* The `alias` keyword is used to make a method respond to another name as well
34
+ * as the current one. For example, to get the method `foo` to also respond to
35
+ * `bar`, you would:
36
+ *
37
+ * alias bar foo
38
+ *
39
+ * Now, in the current context you can call `bar` and it will execute the `foo`
40
+ * method.
41
+ *
42
+ * When you're aliasing two methods, you can either provide bare words (like the
43
+ * example above) or you can provide symbols (note that this includes dynamic
44
+ * symbols like :"foo-#{bar}-baz"). In general, to be consistent with the ruby
45
+ * style guide, we prefer bare words:
46
+ *
47
+ * https://github.com/rubocop-hq/ruby-style-guide#alias-method-lexically
48
+ *
49
+ * The `alias` node contains two children. The left and right align with the
50
+ * arguments passed to the keyword. So, for the above example the left would be
51
+ * the symbol literal `bar` and the right could be the symbol literal `foo`.
52
+ */
53
+ function printAlias(path, opts, print) {
54
+ const keyword = "alias ";
55
+
56
+ const rightSide = concat([
57
+ // If the left child has any comments, then we need to explicitly break this
58
+ // into two lines
59
+ path.getValue().body[0].comments ? hardline : line,
60
+ printAliasArgument(path, opts, print, 1)
61
+ ]);
62
+
63
+ return group(
64
+ concat([
65
+ keyword,
66
+ printAliasArgument(path, opts, print, 0),
67
+ group(align(keyword, rightSide))
68
+ ])
69
+ );
70
+ }
27
71
 
28
72
  module.exports = {
29
- alias,
30
- alias_error: aliasError,
31
- var_alias: alias
73
+ alias: printAlias,
74
+ var_alias: printAlias
32
75
  };
@@ -0,0 +1,55 @@
1
+ const { concat, group, indent, join, line, softline } = require("../prettier");
2
+
3
+ /* `aref` nodes are when you're pulling a value out of a collection at a
4
+ * specific index. Put another way, it's any time you're calling the method
5
+ * `#[]`.
6
+ *
7
+ * The nodes usually contains two children, details below in the
8
+ * `printArefField` function. In some cases, you don't necessarily have the
9
+ * second child node, because you can call procs with a pretty esoteric syntax.
10
+ * In the following example, you wouldn't have a second child, and `"foo"` would
11
+ * be the first child.
12
+ *
13
+ * foo[]
14
+ */
15
+ function printAref(path, opts, print) {
16
+ const indexNode = path.getValue().body[1];
17
+
18
+ if (!indexNode) {
19
+ return concat([path.call(print, "body", 0), "[]"]);
20
+ }
21
+
22
+ return printArefField(path, opts, print);
23
+ }
24
+
25
+ /* `aref_field` nodes are for assigning values into collections at specific
26
+ * indices. Put another way, it's any time you're calling the method `#[]=`.
27
+ * The `aref_field` node itself is just the left side of the assignment, and
28
+ * they're always wrapped in `assign` nodes.
29
+ *
30
+ * The nodes always contain two children, the name of the array (usually a
31
+ * `vcall` node and the index (usually an `args_add_block` node). The
32
+ * `args_add_block` is one of a couple nodes that has special handling where its
33
+ * printed form is actually an array to make joining easier.
34
+ *
35
+ * So in the following example, `"foo"` is the array and `["bar"]` is the index.
36
+ *
37
+ * foo[bar] = baz
38
+ */
39
+ function printArefField(path, opts, print) {
40
+ const [printedArray, printedIndex] = path.map(print, "body");
41
+
42
+ return group(
43
+ concat([
44
+ printedArray,
45
+ "[",
46
+ indent(concat([softline, join(concat([",", line]), printedIndex)])),
47
+ concat([softline, "]"])
48
+ ])
49
+ );
50
+ }
51
+
52
+ module.exports = {
53
+ aref: printAref,
54
+ aref_field: printArefField
55
+ };
@@ -33,21 +33,6 @@ const getSpecialArrayParts = (path, print, args) =>
33
33
  path.call(print, "body", 0, "body", index, "body", 0, "body", 0)
34
34
  );
35
35
 
36
- const printAref = (path, opts, print) =>
37
- group(
38
- concat([
39
- path.call(print, "body", 0),
40
- "[",
41
- indent(
42
- concat([
43
- softline,
44
- join(concat([",", line]), path.call(print, "body", 1))
45
- ])
46
- ),
47
- concat([softline, "]"])
48
- ])
49
- );
50
-
51
36
  const printSpecialArray = (parts) =>
52
37
  group(
53
38
  concat([
@@ -78,14 +63,6 @@ const getElements = (node, elementPath) => {
78
63
  };
79
64
 
80
65
  module.exports = {
81
- aref: (path, opts, print) => {
82
- if (!path.getValue().body[1]) {
83
- return concat([path.call(print, "body", 0), "[]"]);
84
- }
85
-
86
- return printAref(path, opts, print);
87
- },
88
- aref_field: printAref,
89
66
  array: (path, { addTrailingCommas }, print) => {
90
67
  const args = path.getValue().body[0];
91
68
 
@@ -120,24 +97,15 @@ module.exports = {
120
97
  elements.forEach(({ element, elementPath }, index) => {
121
98
  const isInner = index !== elements.length - 1;
122
99
 
123
- const isStraightHeredoc = element.type === "heredoc";
124
- const isSquigglyHeredoc =
125
- element.type === "string_literal" && element.body[0].type === "heredoc";
126
-
127
- if (isStraightHeredoc || isSquigglyHeredoc) {
128
- const heredocNode = isStraightHeredoc ? element : element.body[0];
129
- const heredocPath = [print].concat(elementPath);
130
-
131
- if (isSquigglyHeredoc) {
132
- heredocPath.push("body", 0);
133
- }
134
-
100
+ if (element.type === "heredoc") {
135
101
  normalDocs.push(
136
- heredocNode.beging,
102
+ element.beging,
137
103
  isInner || addTrailingCommas ? "," : "",
138
104
  literalline,
139
- concat(path.map.apply(path, heredocPath.concat("body"))),
140
- heredocNode.ending,
105
+ concat(
106
+ path.map.apply(path, [print].concat(elementPath).concat("body"))
107
+ ),
108
+ element.ending,
141
109
  isInner ? line : ""
142
110
  );
143
111
  } else {