-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathutils.ts
54 lines (49 loc) · 1.33 KB
/
utils.ts
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
import type { AttributeToken } from 'pug-lexer';
import type {
PugEmptyAttributes,
PugEmptyAttributesForceQuotes,
} from './types';
const EMPTY_VALUES: Set<string | boolean> = new Set([true, '""', "''"]);
/**
* Formats the token's `val` if it's empty, based on the `pugEmptyAttributes` option.
*
* @param token The attribute token.
* @param pugEmptyAttributes The option.
* @param pugEmptyAttributesForceQuotes Array with string-patterns for attribute names that needs empty quotes.
*/
export function formatEmptyAttribute(
token: AttributeToken,
pugEmptyAttributes: PugEmptyAttributes,
pugEmptyAttributesForceQuotes: PugEmptyAttributesForceQuotes,
): void {
const { val, name } = token;
const forceQuotesPatterns: RegExp[] = pugEmptyAttributesForceQuotes.map(
(pattern) => new RegExp(pattern),
);
const isForced: boolean = forceQuotesPatterns.some((pattern) =>
pattern.test(name),
);
if (isForced) {
if (token.val === true) {
token.val = '""';
}
return;
}
if (pugEmptyAttributes === 'as-is' || !EMPTY_VALUES.has(val)) {
return;
}
switch (pugEmptyAttributes) {
case 'all': {
if (token.val === true) {
token.val = '""';
}
break;
}
case 'none': {
if (token.val === '""' || token.val === "''") {
token.val = true;
}
break;
}
}
}