Skip to content

Commit 67c2488

Browse files
committed
Revert "Revert "Add readable message for exception""
This reverts commit da3f560.
1 parent 29f75ca commit 67c2488

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

lib/sort-json-core.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,27 @@ function checkIfNeedTailingComma(objectString) {
180180
return objectString.match(/,[\s\t\n]*\}/);
181181
}
182182

183+
function checkIsObjectText(selectedText) {
184+
return (
185+
selectedText.length > 2 &&
186+
selectedText[0] === '{' &&
187+
selectedText[selectedText.length - 1] === '}'
188+
);
189+
}
190+
183191
function selectedTextToSortedText(
184192
selectedText,
185193
indent,
186194
jsonParser,
187195
sortOrder,
188196
sortOptions
189197
) {
198+
var isObejct = checkIsObjectText(selectedText);
199+
200+
if (!isObejct) {
201+
throw 'Please make sure your selected text is a JS obejct!';
202+
}
203+
190204
var autoIndent = getAutoIndent(selectedText, indent);
191205

192206
var marks = [];
@@ -201,7 +215,15 @@ function selectedTextToSortedText(
201215
console.log('marks');
202216
console.log(marks);
203217

204-
var initialJSON = sorter.textToJSON(jsonParser, json);
218+
try {
219+
var initialJSON = sorter.textToJSON(jsonParser, json);
220+
} catch (e) {
221+
if (e.name == 'SyntaxError') {
222+
throw 'Please make sure your selected text is a JS obejct!';
223+
} else {
224+
throw e;
225+
}
226+
}
205227
var sortedJSON = sorter.sortJSON(initialJSON, sortOrder, sortOptions);
206228

207229
var sortedJsonText = sorter.jsonToText(jsonParser, sortedJSON, indent);

test/extension.test.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ var assert = require('assert');
1313
var sorter = require('../lib/sort-json-core');
1414
var JSON5 = require('json5');
1515

16-
suite('Extension Tests', function () {
17-
test('normal js object asc', function () {
16+
suite('Extension Tests', function() {
17+
test('normal js object asc', function() {
1818
var jsObject = `{
1919
user: 'user',
2020
aaa: 'aaa',
@@ -35,7 +35,35 @@ suite('Extension Tests', function () {
3535
);
3636
});
3737

38-
test('normal js object desc', function () {
38+
test('Not js object', function() {
39+
var jsObject = `aaa: 1`;
40+
41+
var result;
42+
try {
43+
result = sorter.sort(jsObject, 4, JSON5, ['asc'], {});
44+
} catch (e) {
45+
result = e;
46+
}
47+
48+
assert.equal(result, 'Please make sure your selected text is a JS obejct!');
49+
});
50+
51+
test('Not js object 2', function() {
52+
var jsObject = `{test('normal js object desc', function() {
53+
var jsObject = '';
54+
}`;
55+
56+
var result;
57+
try {
58+
result = sorter.sort(jsObject, 4, JSON5, ['asc'], {});
59+
} catch (e) {
60+
result = e;
61+
}
62+
63+
assert.equal(result, 'Please make sure your selected text is a JS obejct!');
64+
});
65+
66+
test('normal js object desc', function() {
3967
var jsObject = `{
4068
user: 'user',
4169
aaa: 'aaa',
@@ -56,7 +84,7 @@ suite('Extension Tests', function () {
5684
);
5785
});
5886

59-
test('normal js object indent 2', function () {
87+
test('normal js object indent 2', function() {
6088
var jsObject = `{
6189
user: 'user',
6290
aaa: 'aaa',
@@ -77,7 +105,7 @@ suite('Extension Tests', function () {
77105
);
78106
});
79107

80-
test('format js object', function () {
108+
test('format js object', function() {
81109
var jsObject = `{
82110
user : 'user',
83111
@@ -99,7 +127,7 @@ suite('Extension Tests', function () {
99127
);
100128
});
101129

102-
test('ES6 feature test', function () {
130+
test('ES6 feature test', function() {
103131
var jsObject = `{
104132
user: 'user',
105133
aaa,
@@ -120,7 +148,7 @@ suite('Extension Tests', function () {
120148
);
121149
});
122150

123-
test('Multi lines value test', function () {
151+
test('Multi lines value test', function() {
124152
var jsObject = `{
125153
b: new String('b')
126154
.length,
@@ -138,7 +166,7 @@ suite('Extension Tests', function () {
138166
);
139167
});
140168

141-
test('Auto add comma', function () {
169+
test('Auto add comma', function() {
142170
var jsObject = `{
143171
b: 'b',
144172
a: 'a',
@@ -155,7 +183,7 @@ suite('Extension Tests', function () {
155183
);
156184
});
157185

158-
test('Support line comments', function () {
186+
test('Support line comments', function() {
159187
var jsObject = `{
160188
b: 2,
161189
// some comment
@@ -180,7 +208,7 @@ suite('Extension Tests', function () {
180208
);
181209
});
182210

183-
test('Support line comments for indent 2', function () {
211+
test('Support line comments for indent 2', function() {
184212
var jsObject = `{
185213
b: 2,
186214
// some comment
@@ -205,7 +233,7 @@ suite('Extension Tests', function () {
205233
);
206234
});
207235

208-
test('Support line comments at the end of the object', function () {
236+
test('Support line comments at the end of the object', function() {
209237
var jsObject = `{
210238
b: 2,
211239
// some comment
@@ -232,7 +260,7 @@ suite('Extension Tests', function () {
232260
);
233261
});
234262

235-
test("Support ' in string", function () {
263+
test("Support ' in string", function() {
236264
var jsObject = `{
237265
b: 'test \\'',
238266
c: 'test \\' test',
@@ -251,7 +279,7 @@ suite('Extension Tests', function () {
251279
);
252280
});
253281

254-
test('Not change " in string ', function () {
282+
test('Not change " in string ', function() {
255283
var jsObject = `{
256284
d: "\\" test",
257285
b: 'test " test',
@@ -274,7 +302,7 @@ suite('Extension Tests', function () {
274302
);
275303
});
276304

277-
test('Support ES6 string key', function () {
305+
test('Support ES6 string key', function() {
278306
var jsObject = `{
279307
'b': 'test',
280308
a: 1,
@@ -294,4 +322,4 @@ suite('Extension Tests', function () {
294322
}`
295323
);
296324
});
297-
});
325+
});

0 commit comments

Comments
 (0)