-
Notifications
You must be signed in to change notification settings - Fork 92
/
index.js
1016 lines (866 loc) · 28.9 KB
/
index.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Processes a string and finds Sass operators in it
*
* @param {Object} args - Named arguments object
* @param {String} args.string - the input string
* @param {Number} args.globalIndex - the position of args.string from the start of the line
* @param {Boolean} args.isAfterColon - pass "true" if the string is
* a variable value, a mixin/function parameter default.
* In such cases + and / tend to be operations more often
* @param {Function} args.callback - will be called on every instance of
* an operator. Accepts parameters:
* • string - the default source string
* • globalIndex - the string's position in the outer input
* • startIndex - index in string, where the operator starts
* • endIndex - index in string, where the operator ends (for `==`, etc.)
*
* @return {Array} array of { symbol, globalIndex, startIndex, endIndex }
* for each operator found within a string
*/
export default function findOperators({
string,
globalIndex,
isAfterColon,
callback
}) {
const mathOperators = ["+", "/", "-", "*", "%"];
// A stack of modes activated for the current char: string, interpolation
// Calculations inside strings are not processed, so spaces are not linted
const modesEntered = [
{
mode: "normal",
isCalculationEnabled: true,
character: null
}
];
const result = [];
let lastModeIndex = 0;
for (let i = 0; i < string.length; i++) {
const character = string[i];
const substringStartingWithIndex = string.substring(i);
const lastMode = modesEntered[lastModeIndex];
// If entering/exiting a string
if (character === '"' || character === "'") {
if (lastMode && lastMode.isCalculationEnabled === true) {
modesEntered.push({
mode: "string",
isCalculationEnabled: false,
character
});
lastModeIndex++;
} else if (
lastMode &&
lastMode.mode === "string" &&
lastMode.character === character &&
string[i - 1] !== "\\"
) {
modesEntered.pop();
lastModeIndex--;
}
}
// If entering/exiting interpolation (may be inside a string)
// Comparing with length-2 because `#{` at the very end doesnt matter
if (
character === "#" &&
i + 1 < string.length - 2 &&
string[i + 1] === "{"
) {
modesEntered.push({
mode: "interpolation",
isCalculationEnabled: true
});
lastModeIndex++;
} else if (character === "}") {
modesEntered.pop();
lastModeIndex--;
}
// Don't lint if inside a string
if (lastMode && lastMode.isCalculationEnabled === false) {
continue;
}
// If it's a math operator
if (
(mathOperators.includes(character) &&
mathOperatorCharType(string, i, isAfterColon) === "op") ||
// or is "<" or ">"
substringStartingWithIndex.search(/^[<>]([^=]|$)/) !== -1
) {
result.push({
symbol: string[i],
globalIndex,
startIndex: i,
endIndex: i
});
if (callback) {
callback(string, globalIndex, i, i);
}
}
// "<=", ">=", "!=", "=="
if (substringStartingWithIndex.search(/^[><=!]=/) !== -1) {
result.push({
symbol: string[i],
globalIndex,
startIndex: i,
endIndex: i + 1
});
if (callback) {
callback(string, globalIndex, i, i + 1);
}
}
}
// result.length > 0 && console.log(string, result)
return result;
}
/**
* Checks if a character is an operator, a sign (+ or -), or part of a string
*
* @param {String} string - the source string
* @param {Number} index - the index of the character in string to check
* @param {Boolean} isAfterColon - if the value string a variable
* value, a mixin/function parameter default. In such cases + and / tend
* to be operations more often
* @return {String|false}
* • "op", if the character is a operator in a math/string operation
* • "sign" if it is a + or - before a numeric,
* • "char" if it is a part of a string,
* • false - if it is none from above (most likely an error)
*/
export function mathOperatorCharType(string, index, isAfterColon) {
// !Checking here to prevent unnecessary calculations and deep recursion
// when calling isPrecedingOperator()
if (!["+", "/", "-", "*", "%"].includes(string[index])) {
return "char";
}
const character = string[index];
const prevCharacter = string[index - 1];
if (prevCharacter !== "\\") {
// ---- Processing + characters
if (character === "+") {
return checkPlus(string, index, isAfterColon);
}
// ---- Processing - characters
if (character === "-") {
return checkMinus(string, index);
}
// ---- Processing * character
if (character === "*") {
return checkMultiplication(string, index);
}
// ---- Processing % character
if (character === "%") {
return checkPercent(string, index);
}
// ---- Processing / character
// https://sass-lang.com/documentation/operators/numeric#slash-separated-values
if (character === "/") {
return checkSlash(string, index, isAfterColon);
}
}
return "char";
}
// --------------------------------------------------------------------------
// Functions for checking particular characters (+, -, /)
// --------------------------------------------------------------------------
/**
* Checks the specified `*` char type: operator, sign (*), part of string
*
* @param {String} string - the source string
* @param {Number} index - the index of the character in string to check
* @return {String|false}
* • "op", if the character is a operator in a math/string operation
* • "sign" if it is a sign before a positive number,
* • "char" if it is a part of a string or identifier,
* • false - if it is none from above (most likely an error)
*/
function checkMultiplication(string, index) {
const insideFn = isInsideFunctionCall(string, index);
if (insideFn.is && insideFn.fn) {
const fnArgsReg = new RegExp(insideFn.fn + "\\(([^)]+)\\)");
const fnArgs = string.match(fnArgsReg);
const isSingleMultiplicationChar =
Array.isArray(fnArgs) && fnArgs[1] === "*";
// e.g. selector(:has(*))
if (isSingleMultiplicationChar) {
return "char";
}
}
return "op";
}
/**
* Checks the specified `+` char type: operator, sign (+ or -), part of string
*
* @param {String} string - the source string
* @param {Number} index - the index of the character in string to check
* @param {Boolean} isAftercolon - if the value string a variable
* value, a mixin/function parameter default. In such cases + is always an
* operator if surrounded by numbers/values with units
* @return {String|false}
* • "op", if the character is a operator in a math/string operation
* • "sign" if it is a sign before a positive number,
* • false - if it is none from above (most likely an error)
*/
function checkPlus(string, index, isAftercolon) {
const before = string.substring(0, index);
const after = string.substring(index + 1);
// If the character is at the beginning of the input
const isAtStart_ = isAtStart(string, index);
// If the character is at the end of the input
const isAtEnd_ = isAtEnd(string, index);
const isWhitespaceBefore = before.search(/\s$/) !== -1;
const isWhitespaceAfter = after.search(/^\s/) !== -1;
const isValueWithUnitAfter_ = isValueWithUnitAfter(after);
const isNumberAfter_ = isNumberAfter(after);
const isInterpolationAfter_ = isInterpolationAfter(after);
// The early check above helps prevent deep recursion here
const isPrecedingOperator_ = isPrecedingOperator(string, index);
if (isAtStart_) {
// console.log("+, `+<sth>` or `+ <sth>`")
return "sign";
}
// E.g. `1+1`, `string+#fff`
if (!isAtStart_ && !isWhitespaceBefore && !isAtEnd_ && !isWhitespaceAfter) {
// E.g. `1-+1`
if (isPrecedingOperator_) {
// console.log('1+1')
return "sign";
}
// console.log("+, no spaces")
return "op";
}
// e.g. `something +something`
if (!isAtEnd_ && !isWhitespaceAfter) {
// e.g. `+something`, ` ... , +something`, etc.
if (isNoOperandBefore(string, index)) {
// console.log("+, nothing before")
return "sign";
}
// e.g. `sth +10px`, `sth +1`
if (
(isValueWithUnitAfter_.is && !isValueWithUnitAfter_.opsBetween) ||
(isNumberAfter_.is && !isNumberAfter_.opsBetween)
) {
if (isAftercolon === true) {
// console.log(": 10px +1")
return "op";
}
// e.g. `(sth +10px)`, `fun(sth +1)`
if (
isInsideParens(string, index) ||
isInsideFunctionCall(string, index).is
) {
// console.log("+10px or +1, inside function or parens")
return "op";
}
// e.g. `#{10px +1}`
if (isInsideInterpolation(string, index)) {
// console.log('+, #{10px +1}')
return "op";
}
// console.log('+, default')
return "sign";
}
// e.g. `sth +#fff`, `sth +string`, `sth +#{...}`, `sth +$var`
if (
isStringAfter(after) ||
isHexColorAfter(after) ||
after[0] === "$" ||
(isInterpolationAfter_.is && !isInterpolationAfter_.opsBefore)
) {
// e.g. `sth+ +string`
if (isPrecedingOperator_) {
// console.log("+10px or +1, before is an operator")
return "sign";
}
// console.log("+#000, +string, +#{sth}, +$var")
return "op";
}
// console.log('sth +sth, default')
return "op";
}
// If the + is after a value, e.g. `$var+`
if (!isAtStart_ && !isWhitespaceBefore) {
// It is always an operator. Prior to Sass 4, `#{...}+` was different,
// but that's not logical and had been fixed.
// console.log('1+ sth')
return "op";
}
// If it has whitespaces on both sides
// console.log('sth + sth')
return "op";
}
/**
* Checks the specified `-` character: operator, sign (+ or -), part of string
*
* @param {String} string - the source string
* @param {Number} index - the index of the character in string to check
* @return {String|false}
* • "op", if the character is a operator in a math/string operation
* • "sign" if it is a sign before a negative number,
* • "char" if it is a part of a string or identifier,
* • false - if it is none from above (most likely an error)
*/
function checkMinus(string, index) {
const before = string.substring(0, index);
const after = string.substring(index + 1);
// If the character is at the beginning of the input
const isAtStart_ = isAtStart(string, index);
// If the character is at the end of the input
const isAtEnd_ = isAtEnd(string, index);
const isWhitespaceBefore = before.search(/\s$/) !== -1;
const isWhitespaceAfter = after.search(/^\s/) !== -1;
const isValueWithUnitAfter_ = isValueWithUnitAfter(after);
const isValueWithUnitBefore_ = isValueWithUnitBefore(before);
const isNumberAfter_ = isNumberAfter(after);
const isNumberBefore_ = isNumberBefore(before);
const isInterpolationAfter_ = isInterpolationAfter(after);
const isParensAfter_ = isParensAfter(after);
const isParensBefore_ = isParensBefore(before);
// The early check above helps prevent deep recursion here
const isPrecedingOperator_ = isPrecedingOperator(string, index);
const isInsideFunctionCall_ = isInsideFunctionCall(string, index);
if (isAtStart_) {
// console.log("-, -<sth> or - <sth>")
return "sign";
}
// `10 - 11`
if (!isAtEnd_ && !isAtStart_ && isWhitespaceBefore && isWhitespaceAfter) {
// console.log("-, Op: 10px - 10px")
return "op";
}
// e.g. `something -10px`
if (!isAtEnd_ && !isAtStart_ && isWhitespaceBefore && !isWhitespaceAfter) {
if (isParensAfter_.is && !isParensAfter_.opsBefore) {
// console.log("-, Op: <sth> -(...)")
return "op";
}
// e.g. `#{10px -1}`, `#{math.acos(-0.5)}`
if (isInsideInterpolation(string, index)) {
// e.g. `url(https://my-url.com/image-#{$i -2}-dark.svg)`
if (isInsideFunctionCall_.fn === "url") {
return "op";
}
if (
isInsideFunctionCall_.is &&
((isValueWithUnitAfter_.is && !isValueWithUnitAfter_.opsBetween) ||
(isNumberAfter_.is && !isNumberAfter_.opsBetween))
) {
return "sign";
}
// e.g. `#{$i * -10}px`
if (isWhitespaceBefore && isNumberAfter_.is && isPrecedingOperator_) {
return "sign";
}
return "op";
}
// e.g. `sth -1px`, `sth -1`.
// Always a sign, even inside parens/function args
if (
(isValueWithUnitAfter_.is && !isValueWithUnitAfter_.opsBetween) ||
(isNumberAfter_.is && !isNumberAfter_.opsBetween)
) {
// console.log("-, sign: -1px or -1")
return "sign";
}
// e.g. `sth --1`, `sth +-2px`
if (
(isValueWithUnitAfter_.is && isValueWithUnitAfter_.opsBetween) ||
(isNumberAfter_.is && isNumberAfter_.opsBetween)
) {
// console.log("-, op: --1px or --1")
return "op";
}
// `<sth> -string`, `<sth> -#{...}`
if (
isStringAfter(after) ||
(isInterpolationAfter_.is && !isInterpolationAfter_.opsBefore)
) {
// console.log("-, char: -#{...}")
return "char";
}
// e.g. `#0af -#f0a`, and edge-cases can take a hike
if (isHexColorAfter(after) && isHexColorBefore(before.trim())) {
// console.log("-, op: #fff-, -#fff")
return "op";
}
// If the - is before a variable, than it's most likely an operator
if (after[0] === "$") {
if (isPrecedingOperator_) {
// console.log("-, sign: -$var, another operator before")
return "sign";
}
// console.log("-, op: -$var, NO other operator before")
return "op";
}
// By default let's make it an sign for now
// console.log('-, sign: default in <sth> -<sth>')
return "sign";
}
// No whitespace before,
// e.g. `10x- something`
if (!isAtEnd_ && !isAtStart_ && !isWhitespaceBefore && isWhitespaceAfter) {
if (isParensBefore_) {
// console.log('-, op: `(...)- <sth>`')
return "op";
}
// e.g. `#{10px- 1}`
if (isInsideInterpolation(string, index)) {
return "op";
}
if (isNumberBefore(before) || isHexColorBefore(before)) {
// console.log('`-, op: 10- <sth>, #aff- <sth>`')
return "op";
}
// console.log('-, char: default in <sth>- <sth>')
return "char";
}
// NO Whitespace,
// e.g. `10px-1`
if (!isAtEnd_ && !isAtStart_ && !isWhitespaceBefore && !isWhitespaceAfter) {
// console.log('no spaces')
// `<something>-1`, `<something>-10px`
if (
(isValueWithUnitAfter_.is && !isValueWithUnitAfter_.opsBetween) ||
(isNumberAfter_.is && !isNumberAfter_.opsBetween)
) {
// `10px-1`, `1-10px`, `1-1`, `1x-1x`
if (isValueWithUnitBefore_ || isNumberBefore_) {
// console.log("-, op: 1-10px")
return "op";
}
// The - could be a "sign" here, but for now "char" does the job
}
// `1-$var`
if (isNumberBefore_ && after[0] === "$") {
// console.log("-, op: 1-$var")
return "op";
}
// `fn()-10px`
if (
isFunctionBefore(before) &&
((isNumberAfter_.is && !isNumberAfter_.opsBetween) ||
(isValueWithUnitAfter_.is && !isValueWithUnitAfter_.opsBetween))
) {
// console.log("-, op: fn()-10px")
return "op";
}
}
// And in all the other cases it's a character inside a string
// console.log("-, default: char")
return "char";
}
/**
* Checks the specified `/` character: operator, sign (+ or -), part of string
*
* @param {String} string - the source string
* @param {Number} index - the index of the character in string to check
* @param {Boolean} isAfterColon - if the value string a variable
* value, a mixin/function parameter default. In such cases / is always an
* operator if surrounded by numbers/values with units
* @return {String|false}
* • "op", if the character is a operator in a math/string operation
* • "char" if it gets compiled as-is, e.g. `font: 10px/1.2;`,
* • false - if it is none from above (most likely an error)
*/
function checkSlash(string, index, isAfterColon) {
// Trimming these, as spaces before/after a slash don't matter
const before = string.substring(0, index).trim();
const after = string.substring(index + 1).trim();
const isValueWithUnitAfter_ = isValueWithUnitAfter(after);
const isValueWithUnitBefore_ = isValueWithUnitBefore(before);
const isNumberAfter_ = isNumberAfter(after);
const isNumberBefore_ = isNumberBefore(before);
const isParensAfter_ = isParensAfter(after);
const isParensBefore_ = isParensBefore(before);
// FIRST OFF. Interpolation on any of the sides is a NO-GO for division op
if (isInterpolationBefore(before).is || isInterpolationAfter(after).is) {
// console.log("/, interpolation")
return "char";
}
// having a dot before probably means a relative path.
// e.g. url(../../image.png)
if (isDotBefore(before)) {
return "char";
}
// e.g. `(1px/1)`, `fn(7 / 15)`, but not `url(8/11)`
const isInsideFn = isInsideFunctionCall(string, index);
if (isInsideFn.is && isInsideFn.fn === "url") {
// e.g. `url(https://my-url.com/image-#{$i /2}-dark.svg)`
if (isInsideInterpolation(string, index)) {
return "op";
}
return "char";
}
// e.g. `10px/normal`
if (isStringBefore(before).is || isStringAfter(after)) {
// console.log("/, string")
return "char";
}
// For all other value options (numbers, value+unit, hex color)
// `$var/1`, `#fff/-$var`
// Here we don't care if there is a sign before the var
if (isVariableBefore(before) || isVariableAfter(after).is) {
// console.log("/, variable")
return "op";
}
if (isFunctionBefore(before) || isFunctionAfter(after).is) {
// console.log("/, function as operand")
return "op";
}
if (isParensBefore_ || isParensAfter_.is) {
// console.log("/, function as operand")
return "op";
}
// `$var: 10px/2; // 5px`
if (
isAfterColon === true &&
(isValueWithUnitAfter_.is || isNumberAfter_.is) &&
(isValueWithUnitBefore_ || isNumberBefore_)
) {
return "op";
}
// Quick check of the following operator symbol - if it is a math operator
if (
// +, *, % count as operators unless after interpolation or at the start
before.search(/[^{,(}\s]\s*[+*%][^(){},]+$/) !== -1 ||
// We consider minus as op only if surrounded by whitespaces (` - `);
before.search(/[^{,(}\s]\s+-\s[^(){},]+$/) !== -1 ||
// `10/2 * 3`, `10/2 % 3`, with or without spaces
after.search(/^[^(){},]+[*%]/) !== -1 ||
// `10px/2px+1`, `10px/2px+ 1`
after.search(/^[^(){},\s]+\+/) !== -1 ||
// Anything but `10px/2px +1`, `10px/2px +1px`
after.search(/^[^(){},\s]+\s+(\+\D)/) !== -1 ||
// Following ` -`: only if `$var` after (`10/10 -$var`)
after.search(/^[^(){},\s]+\s+-(\$|\s)/) !== -1 ||
// Following `-`: only if number after (`10s/10s-10`, `10s/10s-.1`)
after.search(/^[^(){},\s]+-(\.)?\d/) !== -1 ||
// Or if there is a number before anything but string after (not `10s/1-str`,)
after.search(/^(\d*\.)?\d+-\s*[^#a-zA-Z_\s]/) !== -1
) {
// console.log("/, math op around")
return "op";
}
if (
isInsideParens(string, index) ||
(isInsideFn.is && isInsideFn.fn !== "url")
) {
// console.log("/, parens or function arg")
return "op";
}
// console.log("/, default")
return "char";
}
/**
* Checks the specified `%` character: operator or part of value
*
* @param {String} string - the source string
* @param {Number} index - the index of the character in string to check
* @return {String|false}
* • "op", if the character is a operator in a math/string operation
* • "char" if it gets compiled as-is, e.g. `width: 10%`,
* • false - if it is none from above (most likely an error)
*/
function checkPercent(string, index) {
// Trimming these, as spaces before/after a slash don't matter
const before = string.substring(0, index);
const after = string.substring(index + 1);
// If the character is at the beginning of the input
const isAtStart_ = isAtStart(string, index);
// If the character is at the end of the input
const isAtEnd_ = isAtEnd(string, index);
const isWhitespaceBefore = before.search(/\s$/) !== -1;
const isWhitespaceAfter = after.search(/^\s/) !== -1;
const isParensBefore_ = isParensBefore(before);
// FIRST OFF. Interpolation on any of the sides is a NO-GO
if (
isInterpolationBefore(before.trim()).is ||
isInterpolationAfter(after.trim()).is
) {
// console.log("%, interpolation")
return "char";
}
if (isAtStart_ || isAtEnd_) {
// console.log("%, start/end")
return "char";
}
// In `<sth> %<sth>` it's most likely an operator (except for interpolation
// checked above)
if (isWhitespaceBefore && !isWhitespaceAfter) {
// console.log("%, `<sth> %<sth>`")
return "op";
}
// `$var% 1`, `$var%1`, `$var%-1`
if (isVariableBefore(before) || isParensBefore_) {
// console.log("%, after a variable, function or parens")
return "op";
}
// in all other cases in `<sth>% <sth>` it is most likely a unit
if (!isWhitespaceBefore && isWhitespaceAfter) {
// console.log("%, `<sth>% <sth>`")
return "char";
}
// console.log("%, default")
return "char";
}
// --------------------------------------------------------------------------
// Lots of elementary helpers
// --------------------------------------------------------------------------
function isAtStart(string, index) {
const before = string.substring(0, index).trim();
return before.length === 0 || before.search(/[({,]$/) !== -1;
}
function isAtEnd(string, index) {
const after = string.substring(index + 1).trim();
return after.length === 0 || after.search(/^[,)}]/) !== -1;
}
function isInsideParens(string, index) {
const before = string.substring(0, index).trim();
const after = string.substring(index + 1).trim();
return (
before.search(/(?:^|[,{\s])\([^(){},]+$/) !== -1 &&
after.search(/^[^(){},\s]+\s*\)/) !== -1
);
}
function isInsideInterpolation(string, index) {
const before = string.substring(0, index).trim();
return before.search(/#{[^}]*$/) !== -1;
}
/**
* Checks if the character is inside a function arguments
*
* @param {String} string - the input string
* @param {Number} index - current character index
* @return {Object} return
* {Boolean} return.is - if inside a function arguments
* {String} return.fn - function name
*/
export function isInsideFunctionCall(string, index) {
const result = { is: false, fn: null };
const before = string.substring(0, index).trim();
const after = string.substring(index + 1).trim();
const beforeMatch = before.match(
/(?:[a-zA-Z_-][\w-]*\()?(:?[a-zA-Z_-][\w-]*)\(/
);
if (beforeMatch && beforeMatch[0] && after.search(/^[^(,]+\)/) !== -1) {
result.is = true;
result.fn = beforeMatch[1];
}
return result;
}
/**
* Checks if there is a string before the character.
* Also checks if there is a math operator in between
*
* @param {String} before - the input string that preceses the character
* @return {Object} return
* {Boolean} return.is - if there is a string
* {String} return.opsBetween - if there are operators in between
*/
function isStringBefore(before) {
const result = { is: false, opsBetween: false };
const stringOpsClipped = before.replace(/(\s*[+/*%]|\s+-)+$/, "");
if (stringOpsClipped !== before) {
result.opsBetween = true;
}
// If it is quoted
if (
stringOpsClipped[stringOpsClipped.length - 1] === '"' ||
stringOpsClipped[stringOpsClipped.length - 1] === "'"
) {
result.is = true;
} else if (
stringOpsClipped.search(
/(?:^|[/(){},: ])([a-zA-Z_][\w-]*|-+[a-zA-Z_][\w-]*)$/
) !== -1
) {
// First pattern: a1, a1a, a-1,
result.is = true;
}
return result;
}
function isStringAfter(after) {
const stringTrimmed = after.trim();
// If it is quoted
if (stringTrimmed[0] === '"' || stringTrimmed[0] === "'") return true;
// e.g. `a1`, `a1a`, `a-1`, and even `--s323`
return (
stringTrimmed.search(/^([a-zA-Z_][\w-]*|-+[a-zA-Z_][\w-]*)(?:$|[)}, ])/) !==
-1
);
}
function isInterpolationAfter(after) {
const result = { is: false, opsBetween: false };
const matches = after.match(/^\s*([+/*%-]\s*)*#{/);
if (matches) {
if (matches[0]) {
result.is = true;
}
if (matches[1]) {
result.opsBetween = true;
}
}
return result;
}
function isParensAfter(after) {
const result = { is: false, opsBetween: false };
const matches = after.match(/^\s*([+/*%-]\s*)*\(/);
if (matches) {
if (matches[0]) {
result.is = true;
}
if (matches[1]) {
result.opsBetween = true;
}
}
return result;
}
function isParensBefore(before) {
return before.search(/\)\s*$/) !== -1;
}
/**
* Checks if there is an interpolation before the character.
* Also checks if there is a math operator in between
*
* @param {String} before - the input string that preceses the character
* @return {Object} return
* {Boolean} return.is - if there is an interpolation
* {String} return.opsBetween - if there are operators in between
*/
function isInterpolationBefore(before) {
const result = { is: false, opsBetween: false };
// Removing preceding operators if any
const beforeOpsClipped = before.replace(/(\s*[+/*%-])+$/, "");
if (beforeOpsClipped !== before) {
result.opsBetween = true;
}
if (beforeOpsClipped[beforeOpsClipped.length - 1] === "}") {
result.is = true;
}
return result;
}
function isValueWithUnitBefore(before) {
// 1px, 0.1p-x, .2p-, 11.2pdf-df1df_
// Surprisingly, ` d.10px` - .10px is separated from a sequence
// and is considered a value with a unit
return before.trim().search(/(^|[/(, .])\d[\w-]+$/) !== -1;
}
function isValueWithUnitAfter(after) {
const result = { is: false, opsBetween: false };
// 1px, 0.1p-x, .2p-, 11.2pdf-dfd1f_
// Again, ` d.10px` - .10px is separated from a sequence
// and is considered a value with a unit
const matches = after.match(
/^\s*([+/*%-]\s*)*(\d+(\.\d+)?|\.\d+)[\w-%]+(?:$|[)}, ])/
);
if (matches) {
if (matches[0]) {
result.is = true;
}
if (matches[1]) {
result.opsBetween = true;
}
}
return result;
}
function isNumberAfter(after) {
const result = { is: false, opsBetween: false };
const matches = after.match(
/^\s*([+/*%-]\s*)*(\d+(\.\d+)?|\.\d+)(?:$|[)}, ])/
);
if (matches) {
if (matches[0]) {
result.is = true;
}
if (matches[1]) {
result.opsBetween = true;
}
}
return result;
}
function isNumberBefore(before) {
return before.trim().search(/(?:^|[/(){},\s])(\d+(\.\d+)?|\.\d+)$/) !== -1;
}
function isVariableBefore(before) {
return before.trim().search(/\$[\w-]+$/) !== -1;
}
function isVariableAfter(after) {
const result = { is: false, opsBetween: false };
const matches = after.match(/^\s*([+/*%-]\s*)*\$/);
if (matches) {
if (matches[0]) {
result.is = true;
}
if (matches[1]) {
result.opsBetween = true;
}
}
return result;
}
function isDotBefore(before) {
return before.slice(-1) === ".";
}
function isFunctionBefore(before) {
return before.trim().search(/[\w-]\(.*?\)\s*$/) !== -1;
}
function isFunctionAfter(after) {
const result = { is: false, opsBetween: false };
// `-fn()` is a valid function name, so if a - should be a sign/operator,
// it must have a space after
const matches = after.match(/^\s*(-\s+|[+/*%]\s*)*[a-zA-Z_-][\w-]*\(/);
if (matches) {
if (matches[0]) {
result.is = true;
}
if (matches[1]) {
result.opsBetween = true;
}
}
return result;
}
/**
* Checks if the input string is a hex color value
*
* @param {String} string - the input
* @return {Boolean} true, if the input is a hex color
*/
function isHexColor(string) {
return string.trim().search(/^#([\da-fA-F]{3}|[\da-fA-F]{6})$/) !== -1;
}
function isHexColorAfter(after) {
const afterTrimmed = after.match(/(.*?)(?:[)},+/*%\-\s]|$)/)[1].trim();
return isHexColor(afterTrimmed);
}
function isHexColorBefore(before) {
return (
before.search(/(?:[/(){},+*%-\s]|^)#([\da-fA-F]{3}|[\da-fA-F]{6})$/) !== -1
);
}
/**
* Checks if there is no operand before the current char
* In other words, the current char is at the start of a possible operation,
* e.g. at the string start, after the opening paren or after a comma
*
* @param {String} string - the input string
* @param {Number} index - current char's position in string
* @return {Boolean}
*/
function isNoOperandBefore(string, index) {
const before = string.substring(0, index).trim();
return before.length === 0 || before.search(/[({,]&/) !== -1;
}
function isPrecedingOperator(string, index) {
let prevCharIndex = -1;
for (let i = index - 1; i >= 0; i--) {