Skip to content

Commit 0fc0e05

Browse files
committed
pretty-print JSON in analyze output
1 parent 826b058 commit 0fc0e05

File tree

3 files changed

+311
-10
lines changed

3 files changed

+311
-10
lines changed

lisp/core/analyze.el

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ Argument LEVEL and MSG are data from the debug log signal."
119119
(cond ((eask-json-p) ; JSON format
120120
;; Fill content with result.
121121
(when (or eask-analyze--warnings eask-analyze--errors)
122-
(setq content (json-encode
123-
`((warnings . ,eask-analyze--warnings)
124-
(errors . ,eask-analyze--errors)))))
122+
(setq content
123+
(eask-analyze--pretty-json (json-encode
124+
`((warnings . ,eask-analyze--warnings)
125+
(errors . ,eask-analyze--errors))))))
125126
;; XXX: When printing the result, no color allow.
126127
(eask--with-no-color
127128
(eask-msg (or content "{}"))))

test/jest/__snapshots__/analyze.test.js.snap

Lines changed: 294 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,300 @@
33
exports[`analyze in ./analyze/dsl handles json option 1`] = `
44
{
55
"stderr": "
6-
{"warnings":[{"range":{"start":{"line":25,"col":0,"pos":853},"end":{"line":25,"col":35,"pos":888}},"filename":"~/Eask","message":"💡 Pkg-file seems to be missing \`check-pkg.el'"},{"range":{"start":{"line":20,"col":0,"pos":709},"end":{"line":20,"col":17,"pos":726}},"filename":"~/Eask","message":"💡 Warning regarding duplicate license name, GPLv3"},{"range":{"start":{"line":17,"col":0,"pos":613},"end":{"line":17,"col":15,"pos":628}},"filename":"~/Eask","message":"💡 Warning regarding duplicate author name, name"}],"errors":[{"range":{"start":{"line":46,"col":0,"pos":1696},"end":{"line":50,"col":2,"pos":1908}},"filename":"~/Eask","message":"✗ Define dependencies with the same name \`f' with different version"},{"range":{"start":{"line":46,"col":0,"pos":1696},"end":{"line":50,"col":2,"pos":1908}},"filename":"~/Eask","message":"✗ Define dependencies with the same name \`f'"},{"range":{"start":{"line":44,"col":0,"pos":1596},"end":{"line":44,"col":27,"pos":1623}},"filename":"~/Eask","message":"✗ Define dependencies with the same name \`dash' with different version"},{"range":{"start":{"line":43,"col":0,"pos":1576},"end":{"line":43,"col":19,"pos":1595}},"filename":"~/Eask","message":"✗ Define dependencies with the same name \`dash'"},{"range":{"start":{"line":40,"col":0,"pos":1474},"end":{"line":40,"col":20,"pos":1494}},"filename":"~/Eask","message":"✗ Define dependencies with the same name \`emacs'"},{"range":{"start":{"line":37,"col":0,"pos":1304},"end":{"line":37,"col":15,"pos":1319}},"filename":"~/Eask","message":"✗ Unknown package archive \`local'"},{"range":{"start":{"line":37,"col":0,"pos":1304},"end":{"line":37,"col":15,"pos":1319}},"filename":"~/Eask","message":"✗ Invalid archive name \`local'"},{"range":{"start":{"line":35,"col":0,"pos":1229},"end":{"line":35,"col":24,"pos":1253}},"filename":"~/Eask","message":"✗ Unknown package archive \`magic-archive'"},{"range":{"start":{"line":33,"col":0,"pos":1214},"end":{"line":33,"col":13,"pos":1227}},"filename":"~/Eask","message":"✗ Multiple definition of source \`gnu'"},{"range":{"start":{"line":30,"col":0,"pos":1073},"end":{"line":30,"col":61,"pos":1134}},"filename":"~/Eask","message":"✗ Run-script with the same key name is not allowed: \`test\`"},{"range":{"start":{"line":26,"col":0,"pos":953},"end":{"line":26,"col":35,"pos":988}},"filename":"~/Eask","message":"✗ Multiple definition of \`package-descriptor'"},{"range":{"start":{"line":23,"col":0,"pos":822},"end":{"line":23,"col":29,"pos":851}},"filename":"~/Eask","message":"✗ Multiple definition of \`package-file'"},{"range":{"start":{"line":14,"col":0,"pos":517},"end":{"line":14,"col":16,"pos":533}},"filename":"~/Eask","message":"✗ Multiple definition of \`keywords'"},{"range":{"start":{"line":12,"col":0,"pos":383},"end":{"line":12,"col":55,"pos":438}},"filename":"~/Eask","message":"✗ Multiple definition of \`website-url'"},{"range":{"start":{"line":9,"col":0,"pos":290},"end":{"line":9,"col":18,"pos":308}},"filename":"~/Eask","message":"✗ Multiple definition of \`package'"}]}
6+
{
7+
"warnings": [
8+
{
9+
"range": {
10+
"start": {
11+
"line": 25,
12+
"col": 0,
13+
"pos": 853
14+
},
15+
"end": {
16+
"line": 25,
17+
"col": 35,
18+
"pos": 888
19+
}
20+
},
21+
"filename": "~/Eask",
22+
"message": "💡 Pkg-file seems to be missing \`check-pkg.el'"
23+
},
24+
{
25+
"range": {
26+
"start": {
27+
"line": 20,
28+
"col": 0,
29+
"pos": 709
30+
},
31+
"end": {
32+
"line": 20,
33+
"col": 17,
34+
"pos": 726
35+
}
36+
},
37+
"filename": "~/Eask",
38+
"message": "💡 Warning regarding duplicate license name, GPLv3"
39+
},
40+
{
41+
"range": {
42+
"start": {
43+
"line": 17,
44+
"col": 0,
45+
"pos": 613
46+
},
47+
"end": {
48+
"line": 17,
49+
"col": 15,
50+
"pos": 628
51+
}
52+
},
53+
"filename": "~/Eask",
54+
"message": "💡 Warning regarding duplicate author name, name"
55+
}
56+
],
57+
"errors": [
58+
{
59+
"range": {
60+
"start": {
61+
"line": 46,
62+
"col": 0,
63+
"pos": 1696
64+
},
65+
"end": {
66+
"line": 50,
67+
"col": 2,
68+
"pos": 1908
69+
}
70+
},
71+
"filename": "~/Eask",
72+
"message": "✗ Define dependencies with the same name \`f' with different version"
73+
},
74+
{
75+
"range": {
76+
"start": {
77+
"line": 46,
78+
"col": 0,
79+
"pos": 1696
80+
},
81+
"end": {
82+
"line": 50,
83+
"col": 2,
84+
"pos": 1908
85+
}
86+
},
87+
"filename": "~/Eask",
88+
"message": "✗ Define dependencies with the same name \`f'"
89+
},
90+
{
91+
"range": {
92+
"start": {
93+
"line": 44,
94+
"col": 0,
95+
"pos": 1596
96+
},
97+
"end": {
98+
"line": 44,
99+
"col": 27,
100+
"pos": 1623
101+
}
102+
},
103+
"filename": "~/Eask",
104+
"message": "✗ Define dependencies with the same name \`dash' with different version"
105+
},
106+
{
107+
"range": {
108+
"start": {
109+
"line": 43,
110+
"col": 0,
111+
"pos": 1576
112+
},
113+
"end": {
114+
"line": 43,
115+
"col": 19,
116+
"pos": 1595
117+
}
118+
},
119+
"filename": "~/Eask",
120+
"message": "✗ Define dependencies with the same name \`dash'"
121+
},
122+
{
123+
"range": {
124+
"start": {
125+
"line": 40,
126+
"col": 0,
127+
"pos": 1474
128+
},
129+
"end": {
130+
"line": 40,
131+
"col": 20,
132+
"pos": 1494
133+
}
134+
},
135+
"filename": "~/Eask",
136+
"message": "✗ Define dependencies with the same name \`emacs'"
137+
},
138+
{
139+
"range": {
140+
"start": {
141+
"line": 37,
142+
"col": 0,
143+
"pos": 1304
144+
},
145+
"end": {
146+
"line": 37,
147+
"col": 15,
148+
"pos": 1319
149+
}
150+
},
151+
"filename": "~/Eask",
152+
"message": "✗ Unknown package archive \`local'"
153+
},
154+
{
155+
"range": {
156+
"start": {
157+
"line": 37,
158+
"col": 0,
159+
"pos": 1304
160+
},
161+
"end": {
162+
"line": 37,
163+
"col": 15,
164+
"pos": 1319
165+
}
166+
},
167+
"filename": "~/Eask",
168+
"message": "✗ Invalid archive name \`local'"
169+
},
170+
{
171+
"range": {
172+
"start": {
173+
"line": 35,
174+
"col": 0,
175+
"pos": 1229
176+
},
177+
"end": {
178+
"line": 35,
179+
"col": 24,
180+
"pos": 1253
181+
}
182+
},
183+
"filename": "~/Eask",
184+
"message": "✗ Unknown package archive \`magic-archive'"
185+
},
186+
{
187+
"range": {
188+
"start": {
189+
"line": 33,
190+
"col": 0,
191+
"pos": 1214
192+
},
193+
"end": {
194+
"line": 33,
195+
"col": 13,
196+
"pos": 1227
197+
}
198+
},
199+
"filename": "~/Eask",
200+
"message": "✗ Multiple definition of source \`gnu'"
201+
},
202+
{
203+
"range": {
204+
"start": {
205+
"line": 30,
206+
"col": 0,
207+
"pos": 1073
208+
},
209+
"end": {
210+
"line": 30,
211+
"col": 61,
212+
"pos": 1134
213+
}
214+
},
215+
"filename": "~/Eask",
216+
"message": "✗ Run-script with the same key name is not allowed: \`test\`"
217+
},
218+
{
219+
"range": {
220+
"start": {
221+
"line": 26,
222+
"col": 0,
223+
"pos": 953
224+
},
225+
"end": {
226+
"line": 26,
227+
"col": 35,
228+
"pos": 988
229+
}
230+
},
231+
"filename": "~/Eask",
232+
"message": "✗ Multiple definition of \`package-descriptor'"
233+
},
234+
{
235+
"range": {
236+
"start": {
237+
"line": 23,
238+
"col": 0,
239+
"pos": 822
240+
},
241+
"end": {
242+
"line": 23,
243+
"col": 29,
244+
"pos": 851
245+
}
246+
},
247+
"filename": "~/Eask",
248+
"message": "✗ Multiple definition of \`package-file'"
249+
},
250+
{
251+
"range": {
252+
"start": {
253+
"line": 14,
254+
"col": 0,
255+
"pos": 517
256+
},
257+
"end": {
258+
"line": 14,
259+
"col": 16,
260+
"pos": 533
261+
}
262+
},
263+
"filename": "~/Eask",
264+
"message": "✗ Multiple definition of \`keywords'"
265+
},
266+
{
267+
"range": {
268+
"start": {
269+
"line": 12,
270+
"col": 0,
271+
"pos": 383
272+
},
273+
"end": {
274+
"line": 12,
275+
"col": 55,
276+
"pos": 438
277+
}
278+
},
279+
"filename": "~/Eask",
280+
"message": "✗ Multiple definition of \`website-url'"
281+
},
282+
{
283+
"range": {
284+
"start": {
285+
"line": 9,
286+
"col": 0,
287+
"pos": 290
288+
},
289+
"end": {
290+
"line": 9,
291+
"col": 18,
292+
"pos": 308
293+
}
294+
},
295+
"filename": "~/Eask",
296+
"message": "✗ Multiple definition of \`package'"
297+
}
298+
]
299+
}
7300
(Checked 1 file)
8301
",
9302
"stdout": "",

test/jest/analyze.test.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@ function tryJSON(s) {
1111
// both of which accept mixed JSON/text output.
1212
// So the test should filter non-JSON output in a similar way
1313
// See flycheck--json-parser for reference
14-
const lines = s.split("\n");
15-
const json = lines.filter((x) => x.startsWith("{") || x.startsWith("["));
16-
if (json.length > 1) {
17-
console.warn("command produced multiple JSON objects as output");
18-
}
1914

15+
// This is a rough approximation of the greedy JSON parsing
16+
// that flycheck uses
17+
// It will yield a false negative if:
18+
// - a non-JSON output line begins with a space
19+
// - there is more that one JSON object in the output
20+
const lines = s.split("\n");
21+
const json = lines.filter((x) => x.match("^[{}\\[\\] ]"));
2022
if (json.length == 0) {
2123
return {};
2224
} else {
23-
return JSON.parse(json[0]);
25+
const result = JSON.parse(json.join("\n"));
26+
if (typeof result == "string") {
27+
// this case is a single string prefixed with whitespace
28+
return {};
29+
}
30+
return result;
2431
}
2532
}
2633

0 commit comments

Comments
 (0)