prettier 1.6.1 → 2.0.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +316 -293
  3. data/CONTRIBUTING.md +6 -9
  4. data/LICENSE +1 -1
  5. data/README.md +11 -12
  6. data/dist/haml/embed.js +53 -0
  7. data/dist/haml/parser.js +31 -0
  8. data/{src → dist}/haml/parser.rb +0 -0
  9. data/dist/haml/printer.js +336 -0
  10. data/dist/parser/getInfo.js +17 -0
  11. data/{src → dist}/parser/netcat.js +1 -0
  12. data/dist/parser/parseSync.js +128 -0
  13. data/dist/parser/server.rb +140 -0
  14. data/dist/plugin.js +143 -0
  15. data/dist/prettier.js +15 -0
  16. data/dist/rbs/parser.js +34 -0
  17. data/{src → dist}/rbs/parser.rb +0 -0
  18. data/dist/rbs/printer.js +517 -0
  19. data/dist/ruby/embed.js +110 -0
  20. data/dist/ruby/nodes/alias.js +59 -0
  21. data/{src → dist}/ruby/nodes/aref.js +26 -35
  22. data/dist/ruby/nodes/args.js +165 -0
  23. data/dist/ruby/nodes/arrays.js +126 -0
  24. data/dist/ruby/nodes/assign.js +41 -0
  25. data/dist/ruby/nodes/blocks.js +68 -0
  26. data/dist/ruby/nodes/calls.js +220 -0
  27. data/dist/ruby/nodes/case.js +50 -0
  28. data/dist/ruby/nodes/class.js +54 -0
  29. data/dist/ruby/nodes/commands.js +124 -0
  30. data/dist/ruby/nodes/conditionals.js +242 -0
  31. data/dist/ruby/nodes/constants.js +38 -0
  32. data/dist/ruby/nodes/flow.js +66 -0
  33. data/dist/ruby/nodes/hashes.js +130 -0
  34. data/dist/ruby/nodes/heredocs.js +30 -0
  35. data/dist/ruby/nodes/hooks.js +35 -0
  36. data/dist/ruby/nodes/ints.js +27 -0
  37. data/dist/ruby/nodes/lambdas.js +69 -0
  38. data/dist/ruby/nodes/loops.js +73 -0
  39. data/dist/ruby/nodes/massign.js +73 -0
  40. data/dist/ruby/nodes/methods.js +70 -0
  41. data/dist/ruby/nodes/operators.js +70 -0
  42. data/dist/ruby/nodes/params.js +89 -0
  43. data/dist/ruby/nodes/patterns.js +109 -0
  44. data/dist/ruby/nodes/regexp.js +45 -0
  45. data/dist/ruby/nodes/rescue.js +82 -0
  46. data/dist/ruby/nodes/return.js +75 -0
  47. data/dist/ruby/nodes/statements.js +111 -0
  48. data/dist/ruby/nodes/strings.js +218 -0
  49. data/dist/ruby/nodes/super.js +30 -0
  50. data/dist/ruby/nodes/undef.js +26 -0
  51. data/dist/ruby/nodes.js +151 -0
  52. data/dist/ruby/parser.js +34 -0
  53. data/{src → dist}/ruby/parser.rb +1215 -252
  54. data/dist/ruby/printer.js +125 -0
  55. data/dist/ruby/toProc.js +93 -0
  56. data/dist/types/haml.js +4 -0
  57. data/dist/types/plugin.js +3 -0
  58. data/dist/types/rbs.js +4 -0
  59. data/dist/types/ruby.js +4 -0
  60. data/dist/types/utils.js +2 -0
  61. data/dist/types.js +30 -0
  62. data/dist/utils/containsAssignment.js +15 -0
  63. data/dist/utils/getTrailingComma.js +6 -0
  64. data/dist/utils/hasAncestor.js +15 -0
  65. data/{src → dist}/utils/inlineEnsureParens.js +16 -17
  66. data/dist/utils/isEmptyBodyStmt.js +10 -0
  67. data/dist/utils/isEmptyStmts.js +10 -0
  68. data/dist/utils/literal.js +8 -0
  69. data/dist/utils/literallineWithoutBreakParent.js +8 -0
  70. data/dist/utils/makeCall.js +13 -0
  71. data/dist/utils/noIndent.js +11 -0
  72. data/dist/utils/printEmptyCollection.js +44 -0
  73. data/dist/utils/skipAssignIndent.js +15 -0
  74. data/dist/utils.js +30 -0
  75. data/node_modules/prettier/bin-prettier.js +313 -190
  76. data/node_modules/prettier/doc.js +191 -323
  77. data/node_modules/prettier/index.js +2753 -3677
  78. data/node_modules/prettier/package.json +1 -1
  79. data/node_modules/prettier/parser-angular.js +13 -14
  80. data/node_modules/prettier/parser-babel.js +7 -7
  81. data/node_modules/prettier/parser-espree.js +7 -7
  82. data/node_modules/prettier/parser-flow.js +7 -7
  83. data/node_modules/prettier/parser-glimmer.js +1 -1
  84. data/node_modules/prettier/parser-graphql.js +1 -1
  85. data/node_modules/prettier/parser-html.js +17 -17
  86. data/node_modules/prettier/parser-markdown.js +9 -9
  87. data/node_modules/prettier/parser-meriyah.js +7 -7
  88. data/node_modules/prettier/parser-postcss.js +2 -2
  89. data/node_modules/prettier/parser-typescript.js +7 -7
  90. data/node_modules/prettier/parser-yaml.js +2 -2
  91. data/node_modules/prettier/third-party.js +143 -78
  92. data/package.json +26 -18
  93. metadata +74 -67
  94. data/src/haml/embed.js +0 -87
  95. data/src/haml/parser.js +0 -23
  96. data/src/haml/printer.js +0 -438
  97. data/src/parser/parseSync.js +0 -172
  98. data/src/parser/server.rb +0 -66
  99. data/src/plugin.js +0 -148
  100. data/src/prettier.js +0 -16
  101. data/src/rbs/parser.js +0 -37
  102. data/src/rbs/printer.js +0 -643
  103. data/src/ruby/embed.js +0 -142
  104. data/src/ruby/nodes/alias.js +0 -73
  105. data/src/ruby/nodes/args.js +0 -222
  106. data/src/ruby/nodes/arrays.js +0 -162
  107. data/src/ruby/nodes/assign.js +0 -47
  108. data/src/ruby/nodes/blocks.js +0 -90
  109. data/src/ruby/nodes/calls.js +0 -246
  110. data/src/ruby/nodes/case.js +0 -65
  111. data/src/ruby/nodes/class.js +0 -64
  112. data/src/ruby/nodes/commands.js +0 -131
  113. data/src/ruby/nodes/conditionals.js +0 -282
  114. data/src/ruby/nodes/constants.js +0 -43
  115. data/src/ruby/nodes/flow.js +0 -74
  116. data/src/ruby/nodes/hashes.js +0 -155
  117. data/src/ruby/nodes/heredocs.js +0 -36
  118. data/src/ruby/nodes/hooks.js +0 -34
  119. data/src/ruby/nodes/ints.js +0 -31
  120. data/src/ruby/nodes/lambdas.js +0 -76
  121. data/src/ruby/nodes/loops.js +0 -98
  122. data/src/ruby/nodes/massign.js +0 -98
  123. data/src/ruby/nodes/methods.js +0 -74
  124. data/src/ruby/nodes/operators.js +0 -83
  125. data/src/ruby/nodes/params.js +0 -106
  126. data/src/ruby/nodes/patterns.js +0 -157
  127. data/src/ruby/nodes/regexp.js +0 -56
  128. data/src/ruby/nodes/rescue.js +0 -101
  129. data/src/ruby/nodes/return.js +0 -94
  130. data/src/ruby/nodes/statements.js +0 -142
  131. data/src/ruby/nodes/strings.js +0 -272
  132. data/src/ruby/nodes/super.js +0 -35
  133. data/src/ruby/nodes/undef.js +0 -42
  134. data/src/ruby/nodes.js +0 -34
  135. data/src/ruby/parser.js +0 -37
  136. data/src/ruby/printer.js +0 -147
  137. data/src/ruby/toProc.js +0 -105
  138. data/src/utils/containsAssignment.js +0 -11
  139. data/src/utils/getTrailingComma.js +0 -5
  140. data/src/utils/hasAncestor.js +0 -17
  141. data/src/utils/isEmptyBodyStmt.js +0 -7
  142. data/src/utils/isEmptyStmts.js +0 -11
  143. data/src/utils/literal.js +0 -7
  144. data/src/utils/literallineWithoutBreakParent.js +0 -7
  145. data/src/utils/makeCall.js +0 -14
  146. data/src/utils/noIndent.js +0 -10
  147. data/src/utils/printEmptyCollection.js +0 -49
  148. data/src/utils/skipAssignIndent.js +0 -17
  149. data/src/utils.js +0 -13
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup' if ENV['PLUGIN_RUBY_CI']
4
+ require 'socket'
5
+ require 'json'
6
+ require 'fileutils'
7
+ require 'open3'
8
+
9
+ require_relative '../ruby/parser'
10
+ require_relative '../rbs/parser'
11
+ require_relative '../haml/parser'
12
+
13
+ # Make sure we trap these signals to be sure we get the quit command coming from
14
+ # the parent node process
15
+ quit = false
16
+ trap(:INT) { quit = true }
17
+ trap(:TERM) { quit = true }
18
+ trap(:QUIT) { quit = true } if Signal.list.key?('QUIT')
19
+
20
+ # The information variable stores the actual connection information, which will
21
+ # either be an IP address and port or a path to a unix socket file.
22
+ information = ''
23
+
24
+ # The candidates array is a list of potential programs that could be used to
25
+ # connect to our server. We'll run through them after the server starts to find
26
+ # the best one to use.
27
+ candidates = []
28
+
29
+ if Gem.win_platform?
30
+ # If we're on windows, we're going to start up a TCP server. The 0 here means
31
+ # to bind to some available port.
32
+ server = TCPServer.new('127.0.0.1', 0)
33
+ address = server.local_address
34
+
35
+ # Ensure that we close the server when this process exits.
36
+ at_exit { server.close }
37
+
38
+ information = "#{address.ip_address} #{address.ip_port}"
39
+ candidates = %w[nc telnet]
40
+ else
41
+ # If we're not on windows, then we're going to assume we can use unix socket
42
+ # files (since they're faster than a TCP server).
43
+ filepath = "/tmp/prettier-ruby-parser-#{Process.pid}.sock"
44
+ server = UNIXServer.new(filepath)
45
+
46
+ # Ensure that we close the server and delete the socket file when this
47
+ # process exits.
48
+ at_exit do
49
+ server.close
50
+ File.unlink(filepath)
51
+ end
52
+
53
+ information = server.local_address.unix_path
54
+ candidates = ['nc -w 3 -U', 'ncat -w 3 -U']
55
+ end
56
+
57
+ # This is the actual listening thread that will be acting as our server. We have
58
+ # to start it in another thread to begin with so that we can run through our
59
+ # candidate connection programs. Eventually we'll just join into this thread
60
+ # though and it will act as a daemon.
61
+ listener =
62
+ Thread.new do
63
+ loop do
64
+ break if quit
65
+
66
+ # Start up a new thread that will handle each successive connection.
67
+ Thread.new(server.accept_nonblock) do |socket|
68
+ parser, source = socket.read.force_encoding('UTF-8').split('|', 2)
69
+
70
+ response =
71
+ case parser
72
+ when 'ping'
73
+ 'pong'
74
+ when 'ruby'
75
+ Prettier::Parser.parse(source)
76
+ when 'rbs'
77
+ Prettier::RBSParser.parse(source)
78
+ when 'haml'
79
+ Prettier::HAMLParser.parse(source)
80
+ end
81
+
82
+ if response
83
+ socket.write(JSON.fast_generate(response))
84
+ else
85
+ socket.write('{ "error": true }')
86
+ end
87
+ rescue Prettier::Parser::ParserError => error
88
+ loc = { start: { line: error.lineno, column: error.column } }
89
+ socket.write(JSON.fast_generate(error: error.message, loc: loc))
90
+ rescue StandardError => error
91
+ begin
92
+ socket.write(JSON.fast_generate(error: error.message))
93
+ rescue Errno::EPIPE
94
+ # Do nothing, the pipe has been closed by the parent process so we don't
95
+ # actually care about writing to it anymore.
96
+ end
97
+ ensure
98
+ socket.close
99
+ end
100
+ rescue IO::WaitReadable, Errno::EINTR
101
+ # Wait for select(2) to give us a connection that has content for 1
102
+ # second. Otherwise timeout and continue on (so that we hit our
103
+ # "break if quit" pretty often).
104
+ IO.select([server], nil, nil, 1)
105
+
106
+ retry unless quit
107
+ end
108
+ end
109
+
110
+ # Map each candidate connection method to a thread that will check if it works.
111
+ candidates.map! do |candidate|
112
+ Thread.new do
113
+ Thread.current.report_on_exception = false
114
+
115
+ stdout, status =
116
+ Open3.capture2("#{candidate} #{information}", stdin_data: 'ping')
117
+
118
+ candidate if JSON.parse(stdout) == 'pong' && status.exitstatus == 0
119
+ rescue StandardError
120
+ # We don't actually care if this fails, because we'll just skip that
121
+ # connection option.
122
+ end
123
+ end
124
+
125
+ # Find the first one prefix that successfully returned the correct value.
126
+ prefix =
127
+ candidates.detect do |candidate|
128
+ value = candidate.value
129
+ break value if value
130
+ end
131
+
132
+ # Default to running the netcat.js script that we ship with the plugin. It's a
133
+ # good fallback as it will always work, but it is slower than the other options.
134
+ prefix ||= "node #{File.expand_path('netcat.js', __dir__)}"
135
+
136
+ # Write out our connection information to the file given as the first argument
137
+ # to this script.
138
+ File.write(ARGV[0], "#{prefix} #{information}")
139
+
140
+ listener.join
data/dist/plugin.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const printer_1 = __importDefault(require("./ruby/printer"));
6
+ const parser_1 = __importDefault(require("./ruby/parser"));
7
+ const printer_2 = __importDefault(require("./rbs/printer"));
8
+ const parser_2 = __importDefault(require("./rbs/parser"));
9
+ const printer_3 = __importDefault(require("./haml/printer"));
10
+ const parser_3 = __importDefault(require("./haml/parser"));
11
+ /*
12
+ * metadata mostly pulled from linguist and rubocop:
13
+ * https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
14
+ * https://github.com/rubocop/rubocop/blob/master/spec/rubocop/target_finder_spec.rb
15
+ */
16
+ const plugin = {
17
+ languages: [
18
+ {
19
+ name: "Ruby",
20
+ parsers: ["ruby"],
21
+ extensions: [
22
+ ".arb",
23
+ ".axlsx",
24
+ ".builder",
25
+ ".eye",
26
+ ".fcgi",
27
+ ".gemfile",
28
+ ".gemspec",
29
+ ".god",
30
+ ".jb",
31
+ ".jbuilder",
32
+ ".mspec",
33
+ ".opal",
34
+ ".pluginspec",
35
+ ".podspec",
36
+ ".rabl",
37
+ ".rake",
38
+ ".rb",
39
+ ".rbi",
40
+ ".rbuild",
41
+ ".rbw",
42
+ ".rbx",
43
+ ".ru",
44
+ ".ruby",
45
+ ".thor",
46
+ ".watchr"
47
+ ],
48
+ filenames: [
49
+ ".irbrc",
50
+ ".pryrc",
51
+ ".simplecov",
52
+ "Appraisals",
53
+ "Berksfile",
54
+ "Brewfile",
55
+ "Buildfile",
56
+ "Capfile",
57
+ "Cheffile",
58
+ "Dangerfile",
59
+ "Deliverfile",
60
+ "Fastfile",
61
+ "Gemfile",
62
+ "Guardfile",
63
+ "Jarfile",
64
+ "Mavenfile",
65
+ "Podfile",
66
+ "Puppetfile",
67
+ "Rakefile",
68
+ "Snapfile",
69
+ "Thorfile",
70
+ "Vagabondfile",
71
+ "Vagrantfile",
72
+ "buildfile"
73
+ ],
74
+ interpreters: ["jruby", "macruby", "rake", "rbx", "ruby"],
75
+ linguistLanguageId: 326,
76
+ vscodeLanguageIds: ["ruby"]
77
+ },
78
+ {
79
+ name: "RBS",
80
+ parsers: ["rbs"],
81
+ extensions: [".rbs"]
82
+ },
83
+ {
84
+ name: "HAML",
85
+ parsers: ["haml"],
86
+ extensions: [".haml"],
87
+ vscodeLanguageIds: ["haml"]
88
+ }
89
+ ],
90
+ parsers: {
91
+ ruby: parser_1.default,
92
+ rbs: parser_2.default,
93
+ haml: parser_3.default
94
+ },
95
+ printers: {
96
+ ruby: printer_1.default,
97
+ rbs: printer_2.default,
98
+ haml: printer_3.default
99
+ },
100
+ options: {
101
+ rubyArrayLiteral: {
102
+ type: "boolean",
103
+ category: "Ruby",
104
+ default: true,
105
+ description: "When possible, favor the use of string and symbol array literals.",
106
+ since: "1.0.0"
107
+ },
108
+ rubyHashLabel: {
109
+ type: "boolean",
110
+ category: "Ruby",
111
+ default: true,
112
+ description: "When possible, uses the shortened hash key syntax, as opposed to hash rockets.",
113
+ since: "1.0.0"
114
+ },
115
+ rubyModifier: {
116
+ type: "boolean",
117
+ category: "Ruby",
118
+ default: true,
119
+ description: "When it fits on one line, allows if, unless, while, and until statements to use the modifier form.",
120
+ since: "1.0.0"
121
+ },
122
+ rubySingleQuote: {
123
+ type: "boolean",
124
+ category: "Ruby",
125
+ default: true,
126
+ description: "When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals.",
127
+ since: "1.0.0"
128
+ },
129
+ rubyToProc: {
130
+ type: "boolean",
131
+ category: "Ruby",
132
+ default: false,
133
+ description: "When possible, convert blocks to the more concise Symbol#to_proc syntax.",
134
+ since: "1.0.0"
135
+ }
136
+ },
137
+ defaultOptions: {
138
+ printWidth: 80,
139
+ tabWidth: 2,
140
+ trailingComma: "none"
141
+ }
142
+ };
143
+ module.exports = plugin;
data/dist/prettier.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // If `RBPRETTIER` is set, then this is being run from the `Prettier::run` ruby
4
+ // method. In that case, we need to pull `prettier` from the node_modules
5
+ // directly, as it's been shipped with the gem.
6
+ /* istanbul ignore next */
7
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
8
+ const source = require(process.env.RBPRETTIER
9
+ ? "../node_modules/prettier"
10
+ : "prettier");
11
+ // Cramming everything together to make it simpler to pull in all of the right
12
+ // utilities and builders.
13
+ const builders = source.doc.builders;
14
+ const exported = { ...builders, ...source.doc.utils, ...source.util };
15
+ exports.default = exported;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const parseSync_1 = __importDefault(require("../parser/parseSync"));
7
+ const parser = {
8
+ // This function is responsible for taking an input string of text and
9
+ // returning to prettier a JavaScript object that is the equivalent AST that
10
+ // represents the code stored in that string. We accomplish this by spawning a
11
+ // new Ruby process of parser.rb and reading JSON off STDOUT.
12
+ parse(text) {
13
+ return (0, parseSync_1.default)("rbs", text);
14
+ },
15
+ astFormat: "rbs",
16
+ // This function handles checking whether or not the source string has the
17
+ // pragma for prettier. This is an optional workflow for incremental adoption.
18
+ hasPragma(text) {
19
+ return /^\s*#[^\S\n]*@(format|prettier)\s*(\n|$)/.test(text);
20
+ },
21
+ // This function is critical for comments and cursor support, and is
22
+ // responsible for returning the index of the character within the source
23
+ // string that is the beginning of the given node.
24
+ locStart(node) {
25
+ return node.location.start_pos;
26
+ },
27
+ // This function is critical for comments and cursor support, and is
28
+ // responsible for returning the index of the character within the source
29
+ // string that is the ending of the given node.
30
+ locEnd(node) {
31
+ return node.location.end_pos;
32
+ }
33
+ };
34
+ exports.default = parser;
File without changes