forked from prettier/plugin-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug
executable file
·170 lines (139 loc) · 3.59 KB
/
debug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'sinatra', require: 'sinatra/base'
end
require_relative '../src/ruby/parser'
class App < Sinatra::Base
HTML = DATA.read
get '/' do
HTML
end
post '/ast' do
builder = Prettier::Parser.new(request.body.read)
response = builder.parse
if !response || builder.error?
halt 422
else
JSON.fast_generate(response)
end
end
start!
end
__END__
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@prettier/plugin-ruby</title>
<style>
h1 {
text-align: center;
}
textarea {
box-sizing: border-box;
font-family: monospace;
font-size: inherit;
height: 10em;
padding: 0.5em;
width: 100%;
}
code {
border: solid 1px gray;
display: block;
font-size: 2em;
white-space: pre;
}
em {
background-color: yellow;
font-variant: normal;
}
</style>
</head>
<body>
<h1>@prettier/plugin-ruby</h1>
<textarea aria-label="Input" id="input"></textarea>
<code id="output"></code>
<ul id="ast"></ul>
<script>
const clearElement = element => {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
};
const input = document.querySelector("#input");
const output = document.querySelector("#output");
const ast = document.querySelector("#ast");
const highlight = event => {
const string = input.value;
const charStart = event.target.getAttribute("data-char-start");
const charEnd = event.target.getAttribute("data-char-end");
const left = string.slice(0, charStart);
const middle = string.slice(charStart, charEnd);
const right = string.slice(charEnd);
clearElement(output);
if (charStart !== 0) {
output.appendChild(document.createTextNode(string.slice(0, charStart)));
}
const em = document.createElement("em");
em.appendChild(document.createTextNode(middle));
output.appendChild(em);
if (charEnd !== string.length - 1) {
output.appendChild(document.createTextNode(string.slice(charEnd)));
}
};
const createTree = (parent, node) => {
if (!node) {
return;
}
const li = document.createElement("li");
if (typeof node === "string") {
li.appendChild(document.createTextNode(`"${node}"`));
parent.appendChild(li);
return;
}
const button = document.createElement("button");
button.onclick = highlight;
button.setAttribute("data-char-start", node.sc);
button.setAttribute("data-char-end", node.ec);
const label = `${node.type} (${node.sc}-${node.ec})`;
if (node.type.startsWith("@")) {
button.appendChild(document.createTextNode(`${label} = ${node.body}`));
li.appendChild(button);
parent.appendChild(li);
} else if (Array.isArray(node.body)) {
const ul = document.createElement("ul");
node.body.forEach(child => {
createTree(ul, child);
});
button.appendChild(document.createTextNode(label));
li.appendChild(button);
parent.appendChild(li);
parent.appendChild(ul);
}
};
input.addEventListener("keyup", event => {
output.innerText = event.target.value;
fetch("/ast", { method: "post", body: event.target.value })
.then(response => {
if (response.status === 422) {
throw new Error("syntax");
}
return response.json();
})
.then(node => {
clearElement(ast);
createTree(ast, node);
})
.catch(error => {
if (error.message !== "syntax") {
console.error(error);
}
});
});
</script>
</body>
</html>