prettier 0.18.1 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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;
@@ -1,64 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup' if ENV['CI']
4
- require 'haml'
5
-
6
- class Haml::Parser::ParseNode
7
- ESCAPE = /Haml::Helpers.html_escape\(\((.+)\)\)/.freeze
8
-
9
- def as_json
10
- case type
11
- when :comment, :doctype, :plain, :silent_script
12
- to_h.tap do |json|
13
- json.delete(:parent)
14
- json[:children] = children.map(&:as_json)
15
- end
16
- when :filter, :haml_comment
17
- to_h.tap { |json| json.delete(:parent) }
18
- when :root
19
- to_h.tap { |json| json[:children] = children.map(&:as_json) }
20
- when :script
21
- to_h.tap do |json|
22
- json.delete(:parent)
23
- json[:children] = children.map(&:as_json)
24
-
25
- if json[:value][:text].match?(ESCAPE)
26
- json[:value][:text].gsub!(ESCAPE) { $1 }
27
- json[:value].merge!(escape_html: 'escape_html', interpolate: true)
28
- end
29
- end
30
- when :tag
31
- to_h.tap do |json|
32
- json.delete(:parent)
33
-
34
- # For some reason this is actually using a symbol to represent a null
35
- # object ref instead of nil itself, so just replacing it here for
36
- # simplicity in the printer
37
- json[:value][:object_ref] = nil if json[:value][:object_ref] == :nil
38
-
39
- json.merge!(
40
- children: children.map(&:as_json),
41
- value:
42
- value.merge(dynamic_attributes: value[:dynamic_attributes].to_h)
43
- )
44
- end
45
- else
46
- raise ArgumentError, "Unsupported type: #{type}"
47
- end
48
- end
49
- end
50
-
51
- # If this is the main file we're executing, then most likely this is being
52
- # executed from the haml.js spawn. In that case, read the ruby source from
53
- # stdin and report back the AST over stdout.
54
- if $0 == __FILE__
55
- # Don't explicitly require JSON if there is already as JSON loaded, as this
56
- # can lead to all kinds of trouble where one version of it was already
57
- # "activated" by rubygems.
58
- require 'json' unless defined?(JSON)
59
-
60
- parser = Haml::Parser.new({})
61
- template = $stdin.read
62
-
63
- puts JSON.fast_generate(parser.call(template).as_json)
64
- end
@@ -1,38 +0,0 @@
1
- const { concat, hardline, join, markAsRoot } = require("../prettier");
2
-
3
- const comment = require("./nodes/comment");
4
- const doctype = require("./nodes/doctype");
5
- const filter = require("./nodes/filter");
6
- const hamlComment = require("./nodes/hamlComment");
7
- const script = require("./nodes/script");
8
- const silentScript = require("./nodes/silentScript");
9
- const tag = require("./nodes/tag");
10
-
11
- const nodes = {
12
- comment,
13
- doctype,
14
- filter,
15
- haml_comment: hamlComment,
16
- plain: (path, _opts, _print) => {
17
- const { value } = path.getValue();
18
-
19
- return value.text.startsWith("=") ? `\\${value.text}` : value.text;
20
- },
21
- root: (path, opts, print) =>
22
- markAsRoot(concat([join(hardline, path.map(print, "children")), hardline])),
23
- script,
24
- silent_script: silentScript,
25
- tag
26
- };
27
-
28
- const genericPrint = (path, opts, print) => {
29
- const { type } = path.getValue();
30
-
31
- if (!(type in nodes)) {
32
- throw new Error(`Unsupported node encountered: ${type}`);
33
- }
34
-
35
- return nodes[type](path, opts, print);
36
- };
37
-
38
- module.exports = genericPrint;