UNPKG

3.83 kBJavaScriptView Raw
1var test = require('tape')
2var fmt = require('../').transform
3
4var cr = new RegExp(/\n/g)
5var crlf = '\r\n'
6
7var collapse = [
8 {
9 program:
10 'var x = 1;\n' +
11 '\n' +
12 '\n' +
13 'var z = 2;\n',
14
15 expected:
16 'var x = 1;\n' +
17 '\n' +
18 'var z = 2;\n',
19
20 msg: 'two empty lines should collapse to one'
21 },
22 {
23 program:
24 'var x = 1;\n' +
25 '\n' + '\n' + '\n' + '\n' + '\n' +
26 '\n' + '\n' + '\n' + '\n' + '\n' +
27 'var z = 2;\n',
28
29 expected:
30 'var x = 1;\n' +
31 '\n' +
32 'var z = 2;\n',
33
34 msg: 'ten empty lines should collapse to one'
35 },
36 {
37 program:
38 'var foo = function () {\n' +
39 '\n' +
40 ' bar();\n' +
41 '};\n',
42
43 expected:
44 'var foo = function () {\n' +
45 ' bar();\n' +
46 '};\n',
47 msg: 'Remove padding newlines after curly braces'
48 },
49 {
50 program:
51 'var x = 123; /* Useful comment \n' +
52 'that spans two lines */\n',
53
54 expected:
55 'var x = 123; /* Useful comment \n' +
56 'that spans two lines */\n',
57
58 msg: 'Keep semicolon from multiline comment'
59 }
60]
61
62test('multiline collapse', function (t) {
63 t.plan(collapse.length)
64 collapse.forEach(function (obj) {
65 t.equal(fmt(obj.program), obj.expected, obj.msg)
66 })
67})
68
69test('multiline collapse CRLF', function (t) {
70 t.plan(collapse.length)
71 collapse.forEach(function (obj) {
72 obj.program = obj.program.replace(cr, crlf)
73 obj.expected = obj.expected.replace(cr, crlf)
74 t.equal(fmt(obj.program), obj.expected, obj.msg)
75 })
76})
77
78var noops = [
79 {
80 program:
81 'var x = 1;\n' +
82 '\n' +
83 'var z = 2;\n',
84
85 msg: 'single empty line should be unmodified'
86 },
87 {
88 program:
89 'function getRequests (cb) {\n' +
90 ' nets({\n' +
91 " url: binUrl + '/api/v1/bins/' + bin.name + '/requests',\n" +
92 ' json: true,\n' +
93 ' headers: headers\n' +
94 ' }, function (err, resp, body) {\n' +
95 ' cb(err, resp, body);\n' +
96 ' });\n' +
97 '}\n',
98
99 msg: 'Don\'t mess with function tabbing'
100
101 },
102 {
103 program:
104 'var obj = {\n' +
105 " 'standard': {\n" +
106 " 'ignore': ['test.js', '**test/failing/**']\n" +
107 ' }\n' +
108 '};\n',
109
110 msg: 'allow single line object arrays'
111 },
112 {
113 program:
114 '/*global localStorage*/\n' +
115 ';(function () { // IIFE to ensure no global leakage!\n' +
116 '}());\n',
117 msg: 'IIFEs are not messed with'
118 },
119 {
120 program:
121 "console.log('meh');\n" +
122 ';(function a () {\n' +
123 " console.log('hiya');\n" +
124 '}());\n',
125 msg: 'IIFEs are not messed with'
126 }
127]
128
129test('multiline noop', function (t) {
130 t.plan(noops.length)
131 noops.forEach(function (obj) {
132 t.equal(fmt(obj.program), obj.program, obj.msg)
133 })
134})
135
136test('multiline noop CRLF', function (t) {
137 t.plan(noops.length)
138 noops.forEach(function (obj) {
139 obj.program = obj.program.replace(cr, crlf)
140 t.equal(fmt(obj.program), obj.program, obj.msg)
141 })
142})
143
144var semicolons = [
145 {
146 program:
147 'var x = 2\n' +
148 '[1, 2, 3].map(function () {})\n' +
149 '\n' +
150 'var y = 8\n' +
151 '(function () {\n' +
152 ' bar()\n' +
153 '}())\n',
154 expected:
155 'var x = 2;\n' +
156 ';[1, 2, 3].map(function () {});\n' +
157 '\n' +
158 'var y = 8;\n' +
159 ';(function () {\n' +
160 ' bar();\n' +
161 '}());\n',
162 msg: 'Keep semicolon before `[` and `(` if they are the first things on the line'
163 },
164 {
165 program:
166 "console.log('meh');\n" +
167 '(function a() {\n' +
168 "console.log('hiya');\n" +
169 '}());',
170 expected:
171 "console.log('meh');\n" +
172 ';(function a () {\n' +
173 " console.log('hiya');\n" +
174 '}());\n',
175 msg: 'IIFEs are not messed with'
176 }
177]
178
179test('multiline semicolons', function (t) {
180 t.plan(semicolons.length)
181 semicolons.forEach(function (obj) {
182 t.equal(fmt(obj.program), obj.expected, obj.msg)
183 })
184})