.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / style-search / test.js
1 var test = require("tape");
2 var styleSearch = require("./index");
3
4 function styleSearchResults(options) {
5   const results = [];
6   styleSearch(options, function(match) {
7     results.push(match.startIndex);
8   });
9   return results;
10 }
11
12 test("default options", function(t) {
13   t.deepEqual(styleSearchResults({
14     source: "abc cba",
15     target: "c",
16   }), [ 2, 4 ]);
17   t.deepEqual(styleSearchResults({
18     source: "abc cb",
19     target: "a",
20   }), [0]);
21   t.deepEqual(styleSearchResults({
22     source: "abc cba",
23     target: "b",
24   }), [ 1, 5 ]);
25   t.deepEqual(styleSearchResults({
26     source: "abc \"var(--cba)\"",
27     target: "a",
28   }), [0]);
29   t.end();
30 });
31
32 test("once", function(t) {
33   t.deepEqual(styleSearchResults({
34     source: "abc cba",
35     target: "c",
36     once: true,
37   }), [2]);
38   t.deepEqual(styleSearchResults({
39     source: "abc cba",
40     target: "a",
41     once: true,
42   }), [0]);
43   t.deepEqual(styleSearchResults({
44     source: "abc cba",
45     target: "b",
46     once: false,
47   }), [ 1, 5 ]);
48   t.end();
49 });
50
51 test("functionArguments: 'only'", function(t) {
52   t.deepEqual(styleSearchResults({
53     source: "abc var(--cba)",
54     target: "c",
55     functionArguments: "only",
56   }), [10]);
57   t.deepEqual(styleSearchResults({
58     source: "abc var(--cba)",
59     target: "a",
60     functionArguments: "only",
61   }), [12]);
62   t.deepEqual(styleSearchResults({
63     source: "abc \"var(--cba)\"",
64     target: "a",
65     functionArguments: "only",
66   }), []);
67   t.deepEqual(styleSearchResults({
68     source: "translate(1px, calc(1px * 2))",
69     target: "1",
70     functionArguments: "only",
71   }), [ 10, 20 ]);
72   t.deepEqual(styleSearchResults({
73     source: "var(--horse)",
74     target: "v",
75     functionArguments: "only",
76   }), []);
77   t.deepEqual(styleSearchResults({
78     source: "abc (abc)",
79     target: "b",
80     functionArguments: "only",
81   }), [], "parens without function is not interpreted as a function");
82   t.deepEqual(styleSearchResults({
83     source: "de$(abc)fg",
84     target: "b",
85     functionArguments: "only",
86   }), [], "parens preceded by `$`, for postcss-simple-vars interpolation, not interpreted as a function");
87   t.deepEqual(styleSearchResults({
88     source: "de$(abc)fg",
89     target: ")",
90     functionArguments: "only",
91   }), [], "closing paren of non-function is ignored");
92   t.end();
93 });
94
95 test("functionArguments: 'skip'", function(t) {
96   t.deepEqual(styleSearchResults({
97     source: "abc var(--cba)",
98     target: "c",
99     functionArguments: "skip",
100   }), [2]);
101   t.deepEqual(styleSearchResults({
102     source: "abc var(--cba)",
103     target: "a",
104     functionArguments: "skip",
105   }), [0]);
106   t.deepEqual(styleSearchResults({
107     source: "abc \"a var(--cba)\"",
108     target: "a",
109     functionArguments: "skip",
110   }), [0]);
111   t.deepEqual(styleSearchResults({
112     source: "translate(1px, calc(1px * 2))",
113     target: "1",
114     functionArguments: "skip",
115   }), []);
116   t.deepEqual(styleSearchResults({
117     source: "var(--horse)",
118     target: "v",
119     functionArguments: "skip",
120   }), []);
121   t.deepEqual(styleSearchResults({
122     source: "abc (def)",
123     target: "e",
124     functionArguments: "skip",
125   }), [6], "parens without function is not interpreted as a function");
126   t.end();
127 });
128
129 test("parentheticals: 'skip'", function(t) {
130   t.deepEqual(styleSearchResults({
131     source: "abc var(--cba)",
132     target: "c",
133     parentheticals: "skip",
134   }), [2]);
135   t.deepEqual(styleSearchResults({
136     source: "abc var(--cba)",
137     target: "a",
138     parentheticals: "skip",
139   }), [0]);
140   t.deepEqual(styleSearchResults({
141     source: "abc \"a var(--cba)\"",
142     target: "a",
143     parentheticals: "skip",
144   }), [0]);
145   t.deepEqual(styleSearchResults({
146     source: "translate(1px, calc(1px * 2))",
147     target: "1",
148     parentheticals: "skip",
149   }), []);
150   t.deepEqual(styleSearchResults({
151     source: "var(--horse)",
152     target: "v",
153     parentheticals: "skip",
154   }), []);
155   t.deepEqual(styleSearchResults({
156     source: "abc (def)",
157     target: "e",
158     parentheticals: "skip",
159   }), [], "parens without function are still ignored");
160   t.end();
161 });
162
163 test("ignores matches inside single-quote strings", function(t) {
164   t.deepEqual(styleSearchResults({
165     source: "abc 'abc'",
166     target: "c",
167   }), [2]);
168   t.deepEqual(styleSearchResults({
169     source: "abc 'abc' cba",
170     target: "c",
171   }), [ 2, 10 ]);
172   t.end();
173 });
174
175 test("ignores matches inside double-quote strings", function(t) {
176   t.deepEqual(styleSearchResults({
177     source: 'abc "abc"',
178     target: "c",
179   }), [2]);
180   t.deepEqual(styleSearchResults({
181     source: 'abc "abc" cba',
182     target: "c",
183   }), [ 2, 10 ]);
184   t.end();
185 });
186
187 test("strings: 'check'", function(t) {
188   t.deepEqual(styleSearchResults({
189     source: "abc 'abc'",
190     target: "b",
191     strings: "check",
192   }), [ 1, 6 ]);
193
194   t.deepEqual(styleSearchResults({
195     source: "abc /* 'abc' */",
196     target: "b",
197     strings: "check",
198   }), [1], "no strings inside comments");
199   t.end();
200 });
201
202 test("strings: 'only'", function(t) {
203   t.deepEqual(styleSearchResults({
204     source: 'abc "abc"',
205     target: "b",
206     strings: "only",
207   }), [6]);
208
209   t.deepEqual(styleSearchResults({
210     source: "p[href^='https://']:before { content: \"\/*\"; \n  top: 0;\n}",
211     target: "\n",
212     strings: "only",
213   }), [], "comments do not start inside strings");
214
215   t.end();
216 });
217
218 test("ignores matches inside comments", function(t) {
219   t.deepEqual(styleSearchResults({
220     source: "abc/*comment*/",
221     target: "m",
222   }), []);
223   t.deepEqual(styleSearchResults({
224     source: "abc/*command*/",
225     target: "a",
226   }), [0]);
227   t.end();
228 });
229
230 test("comments: 'check'", function(t) {
231   t.deepEqual(styleSearchResults({
232     source: "abc/*abc*/",
233     target: "b",
234     comments: "check",
235   }), [ 1, 6 ]);
236   t.end();
237 });
238
239 test("comments: 'only'", function(t) {
240   t.deepEqual(styleSearchResults({
241     source: "abc/*abc*/",
242     target: "b",
243     comments: "only",
244   }), [6]);
245   t.deepEqual(styleSearchResults({
246     source: "abc/*/abc*/",
247     target: "b",
248     comments: "only",
249   }), [7]);
250   t.deepEqual(styleSearchResults({
251     source: "ab'c/*abc*/c'",
252     target: "b",
253     comments: "only",
254   }), [], "no comments inside strings");
255   t.end();
256 });
257
258 test("ignores matches inside single-line comment", function(t) {
259   t.deepEqual(styleSearchResults({
260     source: "abc // comment",
261     target: "m",
262   }), []);
263   t.deepEqual(styleSearchResults({
264     source: "abc // command",
265     target: "a",
266   }), [0]);
267   // Triple-slash comments are used for sassdoc
268   t.deepEqual(styleSearchResults({
269     source: "abc /// it's all ok",
270     target: "a",
271   }), [0]);
272   t.end();
273 });
274
275 test("handles escaped double-quotes in double-quote strings", function(t) {
276   t.deepEqual(styleSearchResults({
277     source: 'abc "ab\\"c"',
278     target: "c",
279   }), [2]);
280   t.deepEqual(styleSearchResults({
281     source: 'abc "a\\"bc" foo cba',
282     target: "c",
283   }), [ 2, 16 ]);
284   t.end();
285 });
286
287 test("handles escaped double-quotes in single-quote strings", function(t) {
288   t.deepEqual(styleSearchResults({
289     source: "abc 'ab\\'c'",
290     target: "c",
291   }), [2]);
292   t.deepEqual(styleSearchResults({
293     source: "abc 'a\\'bc' foo cba",
294     target: "c",
295   }), [ 2, 16 ]);
296   t.end();
297 });
298
299 test("count", function(t) {
300   const endCounts = []
301   styleSearch({ source: "123 123 123", target: "1" }, function(index, count) {
302     endCounts.push(count);
303   });
304   t.deepEqual(endCounts, [ 1, 2, 3 ]);
305   t.end();
306 });
307
308 test("finds parentheses", function(t) {
309   t.deepEqual(styleSearchResults({
310     source: "a { color: rgb(0,0,0); }",
311     target: "(",
312   }), [14]);
313   t.deepEqual(styleSearchResults({
314     source: "a { color: rgb(0,0,0); }",
315     target: ")",
316   }), [20]);
317   t.end();
318 });
319
320 test("functionNames: 'check'", function(t) {
321   t.deepEqual(styleSearchResults({
322     source: "a { color: rgb(0,0,0); }",
323     target: "rgb",
324   }), []);
325   t.deepEqual(styleSearchResults({
326     source: "a { color: rgb(0,0,0); }",
327     target: "rgb",
328     functionNames: "check"
329   }), [11]);
330   t.end();
331 });
332
333 test("non-single-character target", function(t) {
334   t.deepEqual(styleSearchResults({
335     source: "abc cba",
336     target: "abc",
337   }), [0]);
338   t.deepEqual(styleSearchResults({
339     source: "abc cba",
340     target: "cb",
341   }), [4]);
342   t.deepEqual(styleSearchResults({
343     source: "abc cba",
344     target: "c c",
345   }), [2]);
346   t.deepEqual(styleSearchResults({
347     source: "abc cba abc",
348     target: "abc",
349   }), [ 0, 8 ]);
350   t.deepEqual(styleSearchResults({
351     source: "abc cba 'abc'",
352     target: "abc",
353   }), [0]);
354   t.deepEqual(styleSearchResults({
355     source: "abc cb",
356     target: "aa",
357   }), []);
358   t.end();
359 });
360
361 test("array target", function(t) {
362   t.deepEqual(styleSearchResults({
363     source: "abc cba",
364     target: [ "a", "b" ],
365   }), [ 0, 1, 5, 6 ]);
366   t.deepEqual(styleSearchResults({
367     source: "abc cba",
368     target: [ "c", "b" ],
369   }), [ 1, 2, 4, 5 ]);
370   t.deepEqual(styleSearchResults({
371     source: "abc cba",
372     target: [ "bc", "a" ],
373   }), [ 0, 1, 6 ]);
374   t.deepEqual(styleSearchResults({
375     source: "abc cba",
376     target: [ "abc", "f" ],
377   }), [0]);
378   t.deepEqual(styleSearchResults({
379     source: "abc cba",
380     target: [ 0, 1, 2 ],
381   }), []);
382   t.end();
383 });
384
385 test("match object", function(t) {
386   styleSearch({ source: "abc", target: "bc" }, function(match) {
387     t.equal(match.startIndex, 1);
388     t.equal(match.endIndex, 3);
389     t.equal(match.target, "bc");
390     t.equal(match.insideFunctionArguments, false);
391     t.equal(match.insideComment, false);
392   });
393
394   const twoMatches = []
395   styleSearch({ source: "abc bca", target: [ "bc ", "ca" ] }, function(match) {
396     twoMatches.push(match);
397   });
398   const firstMatch = twoMatches[0]
399   const secondMatch = twoMatches[1]
400   t.equal(firstMatch.startIndex, 1);
401   t.equal(firstMatch.endIndex, 4);
402   t.equal(firstMatch.target, "bc ");
403   t.equal(firstMatch.insideFunctionArguments, false);
404   t.equal(firstMatch.insideComment, false);
405   t.equal(secondMatch.startIndex, 5);
406   t.equal(secondMatch.endIndex, 7);
407   t.equal(secondMatch.target, "ca");
408   t.equal(secondMatch.insideFunctionArguments, false);
409   t.equal(secondMatch.insideComment, false);
410   t.end();
411 });
412
413 test("match inside a function", function(t) {
414   styleSearch({ source: "a { color: rgb(0, 0, 1); }", target: "1" }, function(match) {
415     t.equal(match.insideFunctionArguments, true);
416     t.equal(match.insideComment, false);
417     t.end();
418   });
419 });
420
421 test("match inside a comment", function(t) {
422   styleSearch({
423     source: "a { color: /* 1 */ pink; }",
424     target: "1",
425     comments: "check"
426   }, function(match) {
427     t.equal(match.insideFunctionArguments, false);
428     t.equal(match.insideComment, true);
429     t.end();
430   });
431 });
432
433 test("match inside a block comment", function(t) {
434   styleSearch({
435     source: "a { color:\n/**\n * 0\n * 1\n */\npink; }",
436     target: "1",
437     comments: "check"
438   }, function(match) {
439     t.equal(match.insideFunctionArguments, false);
440     t.equal(match.insideComment, true);
441     t.end();
442   });
443 });
444
445 test("match inside a comment inside function", function(t) {
446   styleSearch({
447     source: "a { color: rgb(0, 0, 0 /* 1 */); }",
448     target: "1",
449     comments: "check"
450   }, function(match) {
451     t.equal(match.insideFunctionArguments, true);
452     t.equal(match.insideComment, true);
453     t.end();
454   });
455 });
456
457 test("error on multiple 'only' options", function(t) {
458   t.throws(function() {
459     styleSearch({
460       source: "a {}",
461       target: "a",
462       comments: "only",
463       strings: "only",
464     }, function(match) {});
465   }, /Only one syntax/);
466   t.end();
467 });