/
validatePrimaryOption.js
85 lines (70 loc) · 1.82 KB
/
validatePrimaryOption.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
import { isObject, isString } from '../../utils/validateType.js';
export function validatePrimaryOption(actualOptions) {
// Otherwise, begin checking array options
if (!Array.isArray(actualOptions)) {
return false;
}
// Every item in the array must be a certain string or an object
// with a "type" property
if (
!actualOptions.every((item) => {
if (isString(item)) {
return [
'custom-properties',
'dollar-variables',
'at-variables',
'declarations',
'rules',
'at-rules',
'less-mixins',
].includes(item);
}
return isObject(item) && item.type !== undefined;
})
) {
return false;
}
const objectItems = actualOptions.filter(isObject);
if (
!objectItems.every((item) => {
let result = true;
if (item.type !== 'at-rule' && item.type !== 'rule') {
return false;
}
if (item.type === 'at-rule') {
// if parameter is specified, name should be specified also
if (item.parameter !== undefined && item.name === undefined) {
return false;
}
if (item.hasBlock !== undefined) {
result = item.hasBlock === true || item.hasBlock === false;
}
if (item.name !== undefined) {
result = isString(item.name) && item.name.length;
}
if (item.parameter !== undefined) {
result =
(isString(item.parameter) && item.parameter.length) ||
isRegExp(item.parameter);
}
}
if (item.type === 'rule') {
if (item.selector !== undefined) {
result =
(isString(item.selector) && item.selector.length) ||
isRegExp(item.selector);
}
if (result && item.name !== undefined) {
result = isString(item.name) && item.name.length;
}
}
return result;
})
) {
return false;
}
return true;
}
function isRegExp(value) {
return Object.prototype.toString.call(value) === '[object RegExp]';
}