/
source-code-fixer.js
602 lines (467 loc) · 22.8 KB
/
source-code-fixer.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/**
* @fileoverview Tests for SourceCodeFixer.
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert,
espree = require("espree"),
SourceCode = require("../../../lib/util/source-code"),
SourceCodeFixer = require("../../../lib/util/source-code-fixer");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const TEST_CODE = "var answer = 6 * 7;",
TEST_AST = espree.parse(TEST_CODE, { loc: true, range: true, tokens: true, comment: true });
const INSERT_AT_END = {
message: "End",
fix: {
range: [ TEST_CODE.length, TEST_CODE.length ],
text: "// end"
}
},
INSERT_AT_START = {
message: "Start",
fix: {
range: [ 0, 0 ],
text: "// start\n"
}
},
INSERT_IN_MIDDLE = {
message: "Multiply",
fix: {
range: [ 13, 13 ],
text: "5 *"
}
},
REPLACE_ID = {
message: "foo",
fix: {
range: [4, 10],
text: "foo"
}
},
REPLACE_VAR = {
message: "let",
fix: {
range: [0, 3],
text: "let"
}
},
REPLACE_NUM = {
message: "5",
fix: {
range: [13, 14],
text: "5"
}
},
REMOVE_START = {
message: "removestart",
fix: {
range: [0, 4],
text: ""
}
},
REMOVE_MIDDLE = {
message: "removemiddle",
fix: {
range: [5, 10],
text: ""
}
},
REMOVE_END = {
message: "removeend",
fix: {
range: [14, 18],
text: ""
}
},
NO_FIX = {
message: "nofix"
},
INSERT_BOM = {
message: "insert-bom",
fix: {
range: [0, 0],
text: "\uFEFF"
}
},
INSERT_BOM_WITH_TEXT = {
message: "insert-bom",
fix: {
range: [0, 0],
text: "\uFEFF// start\n"
}
},
REMOVE_BOM = {
message: "remove-bom",
fix: {
range: [-1, 0],
text: ""
}
},
REPLACE_BOM_WITH_TEXT = {
message: "remove-bom",
fix: {
range: [-1, 0],
text: "// start\n"
}
},
NO_FIX1 = {
message: "nofix1",
line: 1,
column: 3
},
NO_FIX2 = {
message: "nofix2",
line: 1,
column: 7
},
ADD_PARENS = {
message: "add-parens",
fix: [
{
range: [13, 13],
text: "("
},
{
range: [18, 18],
text: ")"
}
]
};
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("SourceCodeFixer", function() {
describe("constructor", function() {
it("Should not be able to add anything to this", function() {
const result = new SourceCodeFixer();
assert.throws(function() {
result.test = 1;
});
});
});
describe("applyFixes() with no BOM", function() {
let sourceCode;
beforeEach(function() {
sourceCode = new SourceCode(TEST_CODE, TEST_AST);
});
it("Should have empty output if sourceCode is not provided", function() {
const result = SourceCodeFixer.applyFixes(null, [ INSERT_AT_END ]);
assert.equal(result.output.length, 0);
});
describe("Text Insertion", function() {
it("should insert text at the end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_AT_END ]);
assert.equal(result.output, TEST_CODE + INSERT_AT_END.fix.text);
assert.equal(result.messages.length, 0);
});
it("should insert text at the beginning of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_AT_START ]);
assert.equal(result.output, INSERT_AT_START.fix.text + TEST_CODE);
assert.equal(result.messages.length, 0);
});
it("should insert text in the middle of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_IN_MIDDLE ]);
assert.equal(result.output, TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`));
assert.equal(result.messages.length, 0);
});
it("should insert text at the beginning, middle, and end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END ]);
assert.equal(result.output, INSERT_AT_START.fix.text + TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`) + INSERT_AT_END.fix.text);
assert.equal(result.messages.length, 0);
});
});
describe("Text Replacement", function() {
it("should replace text at the end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_VAR ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, TEST_CODE.replace("var", "let"));
assert.isTrue(result.fixed);
});
it("should replace text at the beginning of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_ID ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
assert.isTrue(result.fixed);
});
it("should replace text in the middle of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_NUM ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, TEST_CODE.replace("6", "5"));
assert.isTrue(result.fixed);
});
it("should replace text at the beginning and end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_ID, REPLACE_VAR, REPLACE_NUM ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, "let foo = 5 * 7;");
assert.isTrue(result.fixed);
});
});
describe("Text Removal", function() {
it("should remove text at the start of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_START ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, TEST_CODE.replace("var ", ""));
assert.isTrue(result.fixed);
});
it("should remove text in the middle of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, TEST_CODE.replace("answer", "a"));
assert.isTrue(result.fixed);
});
it("should remove text towards the end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_END ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, TEST_CODE.replace(" * 7", ""));
assert.isTrue(result.fixed);
});
it("should remove text at the beginning, middle, and end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_END, REMOVE_START, REMOVE_MIDDLE ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, "a = 6;");
assert.isTrue(result.fixed);
});
});
describe("Combination", function() {
it("should replace text at the beginning, remove text in the middle, and insert text at the end", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_AT_END, REMOVE_END, REPLACE_VAR ]);
assert.equal(result.output, "let answer = 6;// end");
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should only apply one fix when ranges overlap", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE, REPLACE_ID ]);
assert.equal(result.output, TEST_CODE.replace("answer", "a"));
assert.equal(result.messages.length, 1);
assert.equal(result.messages[0].message, "foo");
assert.isTrue(result.fixed);
});
it("should apply one fix when the end of one range is the same as the start of a previous range overlap", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_START, REPLACE_ID ]);
assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
assert.equal(result.messages.length, 1);
assert.equal(result.messages[0].message, "removestart");
assert.isTrue(result.fixed);
});
it("should only apply one fix when ranges overlap and one message has no fix", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE, REPLACE_ID, NO_FIX ]);
assert.equal(result.output, TEST_CODE.replace("answer", "a"));
assert.equal(result.messages.length, 2);
assert.equal(result.messages[0].message, "nofix");
assert.equal(result.messages[1].message, "foo");
assert.isTrue(result.fixed);
});
it("should apply the same fix when ranges overlap regardless of order", function() {
const result1 = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE, REPLACE_ID]);
const result2 = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_ID, REMOVE_MIDDLE]);
assert.equal(result1.output, result2.output);
});
it("should not apply fixes from the same problem unless all of them can be applied", function() {
const result1 = SourceCodeFixer.applyFixes(sourceCode, [ ADD_PARENS, INSERT_IN_MIDDLE ]);
assert.equal(result1.output, TEST_CODE.replace("6 * 7;", "5 *6 * 7;"));
});
// TODO: (not-an-aardvark) Add more atomicity tests
});
describe("No Fixes", function() {
it("should only apply one fix when ranges overlap and one message has no fix", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ NO_FIX ]);
assert.equal(result.output, TEST_CODE);
assert.equal(result.messages.length, 1);
assert.equal(result.messages[0].message, "nofix");
assert.isFalse(result.fixed);
});
it("should sort the no fix messages correctly", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_ID, NO_FIX2, NO_FIX1 ]);
assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
assert.equal(result.messages.length, 2);
assert.equal(result.messages[0].message, "nofix1");
assert.equal(result.messages[1].message, "nofix2");
assert.isTrue(result.fixed);
});
});
describe("BOM manipulations", function() {
it("should insert BOM with an insertion of '\uFEFF' at 0", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_BOM ]);
assert.equal(result.output, `\uFEFF${TEST_CODE}`);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should insert BOM with an insertion of '\uFEFFfoobar' at 0", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_BOM_WITH_TEXT ]);
assert.equal(result.output, `\uFEFF// start\n${TEST_CODE}`);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should remove BOM with a negative range", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_BOM ]);
assert.equal(result.output, TEST_CODE);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should replace BOM with a negative range and 'foobar'", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_BOM_WITH_TEXT ]);
assert.equal(result.output, `// start\n${TEST_CODE}`);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
});
});
// This section is almost same as "with no BOM".
// Just `result.output` has BOM.
describe("applyFixes() with BOM", function() {
let sourceCode;
beforeEach(function() {
sourceCode = new SourceCode(`\uFEFF${TEST_CODE}`, TEST_AST);
});
describe("Text Insertion", function() {
it("should insert text at the end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_AT_END ]);
assert.equal(result.output, `\uFEFF${TEST_CODE}${INSERT_AT_END.fix.text}`);
assert.equal(result.messages.length, 0);
});
it("should insert text at the beginning of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_AT_START ]);
assert.equal(result.output, `\uFEFF${INSERT_AT_START.fix.text}${TEST_CODE}`);
assert.equal(result.messages.length, 0);
});
it("should insert text in the middle of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_IN_MIDDLE ]);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`)}`);
assert.equal(result.messages.length, 0);
});
it("should insert text at the beginning, middle, and end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END ]);
const insertInMiddle = TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`);
assert.equal(result.output, `\uFEFF${INSERT_AT_START.fix.text}${insertInMiddle}${INSERT_AT_END.fix.text}`);
assert.equal(result.messages.length, 0);
});
});
describe("Text Replacement", function() {
it("should replace text at the end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_VAR ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("var", "let")}`);
assert.isTrue(result.fixed);
});
it("should replace text at the beginning of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_ID ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "foo")}`);
assert.isTrue(result.fixed);
});
it("should replace text in the middle of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_NUM ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("6", "5")}`);
assert.isTrue(result.fixed);
});
it("should replace text at the beginning and end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_ID, REPLACE_VAR, REPLACE_NUM ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, "\uFEFFlet foo = 5 * 7;");
assert.isTrue(result.fixed);
});
});
describe("Text Removal", function() {
it("should remove text at the start of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_START ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("var ", "")}`);
assert.isTrue(result.fixed);
});
it("should remove text in the middle of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "a")}`);
assert.isTrue(result.fixed);
});
it("should remove text towards the end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_END ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace(" * 7", "")}`);
assert.isTrue(result.fixed);
});
it("should remove text at the beginning, middle, and end of the code", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_END, REMOVE_START, REMOVE_MIDDLE ]);
assert.equal(result.messages.length, 0);
assert.equal(result.output, "\uFEFFa = 6;");
assert.isTrue(result.fixed);
});
});
describe("Combination", function() {
it("should replace text at the beginning, remove text in the middle, and insert text at the end", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_AT_END, REMOVE_END, REPLACE_VAR ]);
assert.equal(result.output, "\uFEFFlet answer = 6;// end");
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should only apply one fix when ranges overlap", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE, REPLACE_ID ]);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "a")}`);
assert.equal(result.messages.length, 1);
assert.equal(result.messages[0].message, "foo");
assert.isTrue(result.fixed);
});
it("should apply one fix when the end of one range is the same as the start of a previous range overlap", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_START, REPLACE_ID ]);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "foo")}`);
assert.equal(result.messages.length, 1);
assert.equal(result.messages[0].message, "removestart");
assert.isTrue(result.fixed);
});
it("should only apply one fix when ranges overlap and one message has no fix", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE, REPLACE_ID, NO_FIX ]);
assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "a")}`);
assert.equal(result.messages.length, 2);
assert.equal(result.messages[0].message, "nofix");
assert.equal(result.messages[1].message, "foo");
assert.isTrue(result.fixed);
});
it("should apply the same fix when ranges overlap regardless of order", function() {
const result1 = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_MIDDLE, REPLACE_ID]);
const result2 = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_ID, REMOVE_MIDDLE]);
assert.equal(result1.output, result2.output);
});
});
describe("No Fixes", function() {
it("should only apply one fix when ranges overlap and one message has no fix", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ NO_FIX ]);
assert.equal(result.output, `\uFEFF${TEST_CODE}`);
assert.equal(result.messages.length, 1);
assert.equal(result.messages[0].message, "nofix");
assert.isFalse(result.fixed);
});
});
describe("BOM manipulations", function() {
it("should insert BOM with an insertion of '\uFEFF' at 0", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_BOM ]);
assert.equal(result.output, `\uFEFF${TEST_CODE}`);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should insert BOM with an insertion of '\uFEFFfoobar' at 0", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ INSERT_BOM_WITH_TEXT ]);
assert.equal(result.output, `\uFEFF// start\n${TEST_CODE}`);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should remove BOM with a negative range", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_BOM ]);
assert.equal(result.output, TEST_CODE);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
it("should replace BOM with a negative range and 'foobar'", function() {
const result = SourceCodeFixer.applyFixes(sourceCode, [ REPLACE_BOM_WITH_TEXT ]);
assert.equal(result.output, `// start\n${TEST_CODE}`);
assert.isTrue(result.fixed);
assert.equal(result.messages.length, 0);
});
});
});
});