| 1 |
let { deepStrictEqual, throws } = require('node:assert') |
| 2 |
let { test } = require('node:test') |
| 3 |
let { Input } = require('postcss') |
| 4 |
|
| 5 |
let preprocess = require('../preprocess') |
| 6 |
|
| 7 |
function run(lines, result) { |
| 8 |
deepStrictEqual(preprocess(new Input(''), lines), result) |
| 9 |
} |
| 10 |
|
| 11 |
let defaults = { |
| 12 |
atrule: false, |
| 13 |
before: '', |
| 14 |
colon: false, |
| 15 |
comment: false, |
| 16 |
indent: '', |
| 17 |
lastComma: false, |
| 18 |
number: 1 |
| 19 |
} |
| 20 |
|
| 21 |
let end = { before: '', end: true } |
| 22 |
|
| 23 |
test('cleans from lines', () => { |
| 24 |
run( |
| 25 |
[ |
| 26 |
[['newline', '\n', 1]], |
| 27 |
[ |
| 28 |
['space', ' '], |
| 29 |
['newline', '\n', 2] |
| 30 |
], |
| 31 |
[ |
| 32 |
['word', 'a'], |
| 33 |
['newline', '\n', 3] |
| 34 |
], |
| 35 |
[ |
| 36 |
['space', ' '], |
| 37 |
['newline', '\n', 4] |
| 38 |
] |
| 39 |
], |
| 40 |
[ |
| 41 |
{ |
| 42 |
...defaults, |
| 43 |
before: '\n \n', |
| 44 |
number: 3, |
| 45 |
tokens: [['word', 'a']] |
| 46 |
}, |
| 47 |
{ |
| 48 |
...end, |
| 49 |
before: '\n \n' |
| 50 |
} |
| 51 |
] |
| 52 |
) |
| 53 |
}) |
| 54 |
|
| 55 |
test('separates indent = require(other tokens', () => { |
| 56 |
run( |
| 57 |
[ |
| 58 |
[ |
| 59 |
['space', ' '], |
| 60 |
['word', 'ab'] |
| 61 |
] |
| 62 |
], |
| 63 |
[ |
| 64 |
{ |
| 65 |
...defaults, |
| 66 |
indent: ' ', |
| 67 |
tokens: [['word', 'ab']] |
| 68 |
}, |
| 69 |
end |
| 70 |
] |
| 71 |
) |
| 72 |
}) |
| 73 |
|
| 74 |
test('works with indentless strings', () => { |
| 75 |
run( |
| 76 |
[[['word', 'ab']]], |
| 77 |
[ |
| 78 |
{ |
| 79 |
...defaults, |
| 80 |
indent: '', |
| 81 |
tokens: [['word', 'ab']] |
| 82 |
}, |
| 83 |
end |
| 84 |
] |
| 85 |
) |
| 86 |
}) |
| 87 |
|
| 88 |
test('collects line number', () => { |
| 89 |
run( |
| 90 |
[ |
| 91 |
[ |
| 92 |
['word', 'a'], |
| 93 |
['newline', '\n', 1] |
| 94 |
], |
| 95 |
[ |
| 96 |
['word', 'b'], |
| 97 |
['newline', '\n', 2] |
| 98 |
] |
| 99 |
], |
| 100 |
[ |
| 101 |
{ |
| 102 |
...defaults, |
| 103 |
number: 1, |
| 104 |
tokens: [['word', 'a']] |
| 105 |
}, |
| 106 |
{ |
| 107 |
...defaults, |
| 108 |
before: '\n', |
| 109 |
number: 2, |
| 110 |
tokens: [['word', 'b']] |
| 111 |
}, |
| 112 |
{ |
| 113 |
...end, |
| 114 |
before: '\n' |
| 115 |
} |
| 116 |
] |
| 117 |
) |
| 118 |
}) |
| 119 |
|
| 120 |
test('detects at-rules', () => { |
| 121 |
run( |
| 122 |
[ |
| 123 |
[ |
| 124 |
['at-word', '@ab'], |
| 125 |
['space', ' '] |
| 126 |
] |
| 127 |
], |
| 128 |
[ |
| 129 |
{ |
| 130 |
...defaults, |
| 131 |
atrule: true, |
| 132 |
tokens: [ |
| 133 |
['at-word', '@ab'], |
| 134 |
['space', ' '] |
| 135 |
] |
| 136 |
}, |
| 137 |
end |
| 138 |
] |
| 139 |
) |
| 140 |
}) |
| 141 |
|
| 142 |
test('detects last comma', () => { |
| 143 |
run( |
| 144 |
[ |
| 145 |
[ |
| 146 |
['word', 'ab'], |
| 147 |
[',', ','], |
| 148 |
['newline', '\n'] |
| 149 |
] |
| 150 |
], |
| 151 |
[ |
| 152 |
{ |
| 153 |
...defaults, |
| 154 |
lastComma: true, |
| 155 |
tokens: [ |
| 156 |
['word', 'ab'], |
| 157 |
[',', ','] |
| 158 |
] |
| 159 |
}, |
| 160 |
{ |
| 161 |
...end, |
| 162 |
before: '\n' |
| 163 |
} |
| 164 |
] |
| 165 |
) |
| 166 |
}) |
| 167 |
|
| 168 |
test('detects last comma with trailing spaces', () => { |
| 169 |
run( |
| 170 |
[ |
| 171 |
[ |
| 172 |
['word', 'ab'], |
| 173 |
[',', ','], |
| 174 |
['space', ' '] |
| 175 |
] |
| 176 |
], |
| 177 |
[ |
| 178 |
{ |
| 179 |
...defaults, |
| 180 |
lastComma: true, |
| 181 |
tokens: [ |
| 182 |
['word', 'ab'], |
| 183 |
[',', ','], |
| 184 |
['space', ' '] |
| 185 |
] |
| 186 |
}, |
| 187 |
end |
| 188 |
] |
| 189 |
) |
| 190 |
}) |
| 191 |
|
| 192 |
test('detects last comma with trailing comment', () => { |
| 193 |
run( |
| 194 |
[ |
| 195 |
[ |
| 196 |
['word', 'ab'], |
| 197 |
[',', ','], |
| 198 |
['comment', '// a'] |
| 199 |
] |
| 200 |
], |
| 201 |
[ |
| 202 |
{ |
| 203 |
...defaults, |
| 204 |
lastComma: true, |
| 205 |
tokens: [ |
| 206 |
['word', 'ab'], |
| 207 |
[',', ','], |
| 208 |
['comment', '// a'] |
| 209 |
] |
| 210 |
}, |
| 211 |
end |
| 212 |
] |
| 213 |
) |
| 214 |
}) |
| 215 |
|
| 216 |
test('ignore comma inside', () => { |
| 217 |
run( |
| 218 |
[ |
| 219 |
[ |
| 220 |
['word', 'ab'], |
| 221 |
[',', ','], |
| 222 |
['word', 'ba'] |
| 223 |
] |
| 224 |
], |
| 225 |
[ |
| 226 |
{ |
| 227 |
...defaults, |
| 228 |
lastComma: false, |
| 229 |
tokens: [ |
| 230 |
['word', 'ab'], |
| 231 |
[',', ','], |
| 232 |
['word', 'ba'] |
| 233 |
] |
| 234 |
}, |
| 235 |
end |
| 236 |
] |
| 237 |
) |
| 238 |
}) |
| 239 |
|
| 240 |
test('detects colon with space', () => { |
| 241 |
run( |
| 242 |
[ |
| 243 |
[ |
| 244 |
['word', 'ab'], |
| 245 |
[':', ':'], |
| 246 |
['space', ' '] |
| 247 |
] |
| 248 |
], |
| 249 |
[ |
| 250 |
{ |
| 251 |
...defaults, |
| 252 |
colon: true, |
| 253 |
tokens: [ |
| 254 |
['word', 'ab'], |
| 255 |
[':', ':'], |
| 256 |
['space', ' '] |
| 257 |
] |
| 258 |
}, |
| 259 |
end |
| 260 |
] |
| 261 |
) |
| 262 |
}) |
| 263 |
|
| 264 |
test('detects colon with newline', () => { |
| 265 |
run( |
| 266 |
[ |
| 267 |
[ |
| 268 |
['word', 'ab'], |
| 269 |
[':', ':'], |
| 270 |
['newline', '\n'] |
| 271 |
] |
| 272 |
], |
| 273 |
[ |
| 274 |
{ |
| 275 |
...defaults, |
| 276 |
colon: true, |
| 277 |
tokens: [ |
| 278 |
['word', 'ab'], |
| 279 |
[':', ':'] |
| 280 |
] |
| 281 |
}, |
| 282 |
{ |
| 283 |
...end, |
| 284 |
before: '\n' |
| 285 |
} |
| 286 |
] |
| 287 |
) |
| 288 |
}) |
| 289 |
|
| 290 |
test('ignores colon without space', () => { |
| 291 |
run( |
| 292 |
[ |
| 293 |
[ |
| 294 |
['word', 'ab'], |
| 295 |
[':', ':'], |
| 296 |
['word', 'ba'] |
| 297 |
] |
| 298 |
], |
| 299 |
[ |
| 300 |
{ |
| 301 |
...defaults, |
| 302 |
colon: false, |
| 303 |
tokens: [ |
| 304 |
['word', 'ab'], |
| 305 |
[':', ':'], |
| 306 |
['word', 'ba'] |
| 307 |
] |
| 308 |
}, |
| 309 |
end |
| 310 |
] |
| 311 |
) |
| 312 |
}) |
| 313 |
|
| 314 |
test('ignores colon inside brackets', () => { |
| 315 |
run( |
| 316 |
[ |
| 317 |
[ |
| 318 |
['(', '('], |
| 319 |
[':', ':'], |
| 320 |
['space', ' '], |
| 321 |
[')', ')'] |
| 322 |
] |
| 323 |
], |
| 324 |
[ |
| 325 |
{ |
| 326 |
...defaults, |
| 327 |
colon: false, |
| 328 |
tokens: [ |
| 329 |
['(', '('], |
| 330 |
[':', ':'], |
| 331 |
['space', ' '], |
| 332 |
[')', ')'] |
| 333 |
] |
| 334 |
}, |
| 335 |
end |
| 336 |
] |
| 337 |
) |
| 338 |
}) |
| 339 |
|
| 340 |
test('closes brackets', () => { |
| 341 |
run( |
| 342 |
[ |
| 343 |
[ |
| 344 |
['(', '('], |
| 345 |
[')', ')'], |
| 346 |
[':', ':'], |
| 347 |
['space', ' '] |
| 348 |
] |
| 349 |
], |
| 350 |
[ |
| 351 |
{ |
| 352 |
...defaults, |
| 353 |
colon: true, |
| 354 |
tokens: [ |
| 355 |
['(', '('], |
| 356 |
[')', ')'], |
| 357 |
[':', ':'], |
| 358 |
['space', ' '] |
| 359 |
] |
| 360 |
}, |
| 361 |
end |
| 362 |
] |
| 363 |
) |
| 364 |
}) |
| 365 |
|
| 366 |
test('detects comments', () => { |
| 367 |
run( |
| 368 |
[[['comment', '// a']]], |
| 369 |
[ |
| 370 |
{ |
| 371 |
...defaults, |
| 372 |
comment: true, |
| 373 |
tokens: [['comment', '// a']] |
| 374 |
}, |
| 375 |
end |
| 376 |
] |
| 377 |
) |
| 378 |
}) |
| 379 |
|
| 380 |
test('detects mixed tabs and spaces in indent', () => { |
| 381 |
throws(() => { |
| 382 |
preprocess(new Input(''), [ |
| 383 |
[ |
| 384 |
['space', '\t '], |
| 385 |
['word', 'ab'] |
| 386 |
] |
| 387 |
]) |
| 388 |
}, /<css input>:1:2: Mixed tabs and spaces are not allowed/) |
| 389 |
}) |
| 390 |
|
| 391 |
test('detects mixed tabs and spaces in indents', () => { |
| 392 |
throws(() => { |
| 393 |
preprocess(new Input(''), [ |
| 394 |
[ |
| 395 |
['space', ' '], |
| 396 |
['newline', '\n', 1] |
| 397 |
], |
| 398 |
[ |
| 399 |
['space', '\t'], |
| 400 |
['word', 'ab'] |
| 401 |
] |
| 402 |
]) |
| 403 |
}, /<css input>:2:1: Mixed tabs and spaces are not allowed/) |
| 404 |
}) |
| 405 |
|
| 406 |
test('shows correct error position', () => { |
| 407 |
throws(() => { |
| 408 |
preprocess(new Input(''), [ |
| 409 |
[ |
| 410 |
['comment', '/*\n*/'], |
| 411 |
['newline', '\n', 2] |
| 412 |
], |
| 413 |
[ |
| 414 |
['space', '\t'], |
| 415 |
['word', 'ab'], |
| 416 |
['newline', '\n', 3] |
| 417 |
], |
| 418 |
[ |
| 419 |
['space', '\t '], |
| 420 |
['word', 'ab'], |
| 421 |
['newline', '\n', 4] |
| 422 |
] |
| 423 |
]) |
| 424 |
}, /<css input>:4:2: Mixed tabs and spaces are not allowed/) |
| 425 |
}) |