prettier 0.20.0 → 1.0.0.pre.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +118 -4
  3. data/CONTRIBUTING.md +8 -6
  4. data/README.md +67 -60
  5. data/node_modules/prettier/bin-prettier.js +12317 -50112
  6. data/node_modules/prettier/index.js +33352 -27419
  7. data/node_modules/prettier/third-party.js +5678 -7676
  8. data/package.json +4 -4
  9. data/src/embed.js +27 -8
  10. data/src/nodes.js +6 -2
  11. data/src/nodes/alias.js +65 -24
  12. data/src/nodes/aref.js +55 -0
  13. data/src/nodes/args.js +55 -47
  14. data/src/nodes/arrays.js +150 -137
  15. data/src/nodes/assign.js +32 -32
  16. data/src/nodes/blocks.js +8 -3
  17. data/src/nodes/calls.js +129 -70
  18. data/src/nodes/case.js +11 -7
  19. data/src/nodes/class.js +74 -0
  20. data/src/nodes/commands.js +36 -31
  21. data/src/nodes/conditionals.js +48 -46
  22. data/src/nodes/constants.js +39 -21
  23. data/src/nodes/flow.js +45 -17
  24. data/src/nodes/hashes.js +126 -112
  25. data/src/nodes/heredocs.js +34 -0
  26. data/src/nodes/hooks.js +36 -7
  27. data/src/nodes/ints.js +27 -20
  28. data/src/nodes/lambdas.js +69 -52
  29. data/src/nodes/loops.js +19 -29
  30. data/src/nodes/massign.js +87 -65
  31. data/src/nodes/methods.js +48 -73
  32. data/src/nodes/operators.js +70 -39
  33. data/src/nodes/params.js +26 -16
  34. data/src/nodes/patterns.js +108 -33
  35. data/src/nodes/regexp.js +38 -14
  36. data/src/nodes/rescue.js +72 -59
  37. data/src/nodes/statements.js +86 -44
  38. data/src/nodes/strings.js +94 -90
  39. data/src/nodes/super.js +35 -0
  40. data/src/nodes/undef.js +42 -0
  41. data/src/parser.js +71 -0
  42. data/src/parser.rb +2554 -0
  43. data/src/printer.js +90 -0
  44. data/src/ruby.js +20 -61
  45. data/src/toProc.js +4 -4
  46. data/src/utils.js +24 -88
  47. data/src/utils/inlineEnsureParens.js +42 -0
  48. data/src/utils/isEmptyStmts.js +7 -0
  49. data/src/utils/literalLineNoBreak.js +7 -0
  50. metadata +15 -20
  51. data/src/haml.js +0 -21
  52. data/src/haml/embed.js +0 -58
  53. data/src/haml/nodes/comment.js +0 -27
  54. data/src/haml/nodes/doctype.js +0 -32
  55. data/src/haml/nodes/filter.js +0 -16
  56. data/src/haml/nodes/hamlComment.js +0 -21
  57. data/src/haml/nodes/script.js +0 -29
  58. data/src/haml/nodes/silentScript.js +0 -59
  59. data/src/haml/nodes/tag.js +0 -157
  60. data/src/haml/parse.js +0 -18
  61. data/src/haml/parse.rb +0 -64
  62. data/src/haml/print.js +0 -38
  63. data/src/nodes/scopes.js +0 -61
  64. data/src/parse.js +0 -37
  65. data/src/print.js +0 -23
  66. data/src/ripper.rb +0 -811
@@ -1,21 +0,0 @@
1
- const embed = require("./haml/embed");
2
- const parse = require("./haml/parse");
3
- const print = require("./haml/print");
4
-
5
- const pragmaPattern = /^\s*-#\s*@(prettier|format)/;
6
- const hasPragma = (text) => pragmaPattern.test(text);
7
-
8
- // These functions are just placeholders until we can actually perform this
9
- // properly. The functions are necessary otherwise the format with cursor
10
- // functions break.
11
- const locStart = (_node) => 0;
12
- const locEnd = (_node) => 0;
13
-
14
- module.exports = {
15
- embed,
16
- hasPragma,
17
- locStart,
18
- locEnd,
19
- parse,
20
- print
21
- };
@@ -1,58 +0,0 @@
1
- const {
2
- concat,
3
- hardline,
4
- indent,
5
- literalline,
6
- mapDoc,
7
- markAsRoot,
8
- stripTrailingHardline
9
- } = require("../prettier");
10
-
11
- const parsers = {
12
- css: "css",
13
- javascript: "babel",
14
- less: "less",
15
- markdown: "markdown",
16
- ruby: "ruby",
17
- scss: "scss"
18
- };
19
-
20
- const replaceNewlines = (doc) =>
21
- mapDoc(doc, (currentDoc) =>
22
- typeof currentDoc === "string" && currentDoc.includes("\n")
23
- ? concat(
24
- currentDoc
25
- .split(/(\n)/g)
26
- .map((v, i) => (i % 2 === 0 ? v : literalline))
27
- )
28
- : currentDoc
29
- );
30
-
31
- const embed = (path, print, textToDoc, _opts) => {
32
- const node = path.getValue();
33
- if (node.type !== "filter") {
34
- return null;
35
- }
36
-
37
- const parser = parsers[node.value.name];
38
- if (!parser) {
39
- return null;
40
- }
41
-
42
- return markAsRoot(
43
- concat([
44
- ":",
45
- node.value.name,
46
- indent(
47
- concat([
48
- hardline,
49
- replaceNewlines(
50
- stripTrailingHardline(textToDoc(node.value.text, { parser }))
51
- )
52
- ])
53
- )
54
- ])
55
- );
56
- };
57
-
58
- module.exports = embed;
@@ -1,27 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- // http://haml.info/docs/yardoc/file.REFERENCE.html#html-comments-
4
- const 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,32 +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
- // http://haml.info/docs/yardoc/file.REFERENCE.html#doctype-
15
- const 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 (value.version in versions) {
22
- parts.push(versions[value.version]);
23
- }
24
-
25
- if (value.encoding) {
26
- parts.push(value.encoding);
27
- }
28
-
29
- return join(" ", parts);
30
- };
31
-
32
- module.exports = doctype;
@@ -1,16 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- // http://haml.info/docs/yardoc/file.REFERENCE.html#filters
4
- const 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
- // http://haml.info/docs/yardoc/file.REFERENCE.html#haml-comments--
4
- const 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,29 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- // http://haml.info/docs/yardoc/file.REFERENCE.html#inserting-ruby-
4
- const 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
- parts.push(" ", value.text.trim());
19
-
20
- if (children.length > 0) {
21
- parts.push(
22
- indent(concat([hardline, join(hardline, path.map(print, "children"))]))
23
- );
24
- }
25
-
26
- return group(concat(parts));
27
- };
28
-
29
- module.exports = script;
@@ -1,59 +0,0 @@
1
- const { concat, group, hardline, indent, join } = require("../../prettier");
2
-
3
- const 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
- // http://haml.info/docs/yardoc/file.REFERENCE.html#running-ruby--
20
- const 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;
@@ -1,157 +0,0 @@
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
- const getDynamicAttributes = (header, attributes) => {
15
- const pairs = attributes
16
- .slice(1, -2)
17
- .split(",")
18
- .map((pair) => pair.slice(1).split('" => '));
19
- const parts = [concat([pairs[0][0], "=", pairs[0][1]])];
20
-
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
- const getHashValue = (value, opts) => {
29
- if (typeof value === "string") {
30
- const quote = opts.preferSingleQuotes ? "'" : '"';
31
- return `${quote}${value}${quote}`;
32
- }
33
-
34
- return value;
35
- };
36
-
37
- const getHashRocket = (key, value, opts) => {
38
- const quote = opts.preferSingleQuotes ? "'" : '"';
39
- const leftSide = key.includes(":") ? `:${quote}${key}${quote}` : `:${key}`;
40
-
41
- return `${leftSide} => ${getHashValue(value, opts)}`;
42
- };
43
-
44
- const getHashLabel = (key, value, opts) => {
45
- const quote = opts.preferSingleQuotes ? "'" : '"';
46
- const leftSide = key.includes(":") ? `${quote}${key}${quote}` : key;
47
-
48
- return `${leftSide}: ${getHashValue(value, opts)}`;
49
- };
50
-
51
- const getStaticAttributes = (header, attributes, opts) => {
52
- const keys = Object.keys(attributes).filter(
53
- (name) => !["class", "id"].includes(name)
54
- );
55
-
56
- const getKeyValuePair = opts.preferHashLabels ? getHashLabel : getHashRocket;
57
- const parts = [getKeyValuePair(keys[0], attributes[keys[0]], opts)];
58
-
59
- keys.slice(1).forEach((key) => {
60
- parts.push(",", line, getKeyValuePair(key, attributes[key], opts));
61
- });
62
-
63
- return group(concat(["{", align(header + 1, fill(parts)), "}"]));
64
- };
65
-
66
- const getHeader = (value, opts) => {
67
- const { attributes } = value;
68
- const parts = [];
69
-
70
- if (value.name !== "div") {
71
- parts.push(`%${value.name}`);
72
- }
73
-
74
- if (attributes.class) {
75
- parts.push(`.${attributes.class.replace(/ /g, ".")}`);
76
- }
77
-
78
- if (attributes.id) {
79
- parts.push(`#${attributes.id}`);
80
- }
81
-
82
- if (value.dynamic_attributes.new) {
83
- parts.push(
84
- getDynamicAttributes(parts.join("").length, value.dynamic_attributes.new)
85
- );
86
- }
87
-
88
- if (
89
- Object.keys(attributes).some((name) => name !== "class" && name !== "id")
90
- ) {
91
- parts.push(getStaticAttributes(parts.join("").length, attributes, opts));
92
- }
93
-
94
- if (value.dynamic_attributes.old) {
95
- if (parts.length === 0) {
96
- parts.push("%div");
97
- }
98
- parts.push(value.dynamic_attributes.old);
99
- }
100
-
101
- if (value.object_ref) {
102
- if (parts.length === 0) {
103
- parts.push("%div");
104
- }
105
- parts.push(value.object_ref);
106
- }
107
-
108
- if (value.nuke_outer_whitespace) {
109
- parts.push(">");
110
- }
111
-
112
- if (value.nuke_inner_whitespace) {
113
- parts.push("<");
114
- }
115
-
116
- if (value.self_closing) {
117
- parts.push("/");
118
- }
119
-
120
- if (value.value) {
121
- const prefix = value.parse ? "= " : ifBreak("", " ");
122
-
123
- return group(
124
- concat([
125
- group(concat(parts)),
126
- indent(concat([softline, prefix, value.value]))
127
- ])
128
- );
129
- }
130
-
131
- // In case none of the other if statements have matched and we're printing a
132
- // div, we need to explicitly add it back into the array.
133
- if (parts.length === 0 && value.name === "div") {
134
- parts.push("%div");
135
- }
136
-
137
- return group(concat(parts));
138
- };
139
-
140
- // http://haml.info/docs/yardoc/file.REFERENCE.html#element-name-
141
- const tag = (path, opts, print) => {
142
- const { children, value } = path.getValue();
143
- const header = getHeader(value, opts);
144
-
145
- if (children.length === 0) {
146
- return header;
147
- }
148
-
149
- return group(
150
- concat([
151
- header,
152
- indent(concat([hardline, join(hardline, path.map(print, "children"))]))
153
- ])
154
- );
155
- };
156
-
157
- module.exports = tag;
@@ -1,18 +0,0 @@
1
- const { spawnSync } = require("child_process");
2
- const path = require("path");
3
-
4
- const parse = (text, _parsers, _opts) => {
5
- const child = spawnSync("ruby", [path.join(__dirname, "./parse.rb")], {
6
- input: text
7
- });
8
-
9
- const error = child.stderr.toString();
10
- if (error) {
11
- throw new Error(error);
12
- }
13
-
14
- const response = child.stdout.toString();
15
- return JSON.parse(response);
16
- };
17
-
18
- module.exports = parse;