forked from csscomb/csscomb.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
96 lines (76 loc) · 1.95 KB
/
plugin.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
'use strict';
let Errors = require('./errors');
let Plugin = function(methods) {
for (let method in methods) {
this[method] = typeof method === 'function'?
methods[method].bind(this) : methods[method];
}
this.validate();
};
Plugin.prototype = {
/**
* Plugin's name.
* @type {String}
*/
name: null,
/**
* List of supported syntaxes.
* @type {Array}
*/
syntax: null,
/**
* @type {Object}
*/
accepts: null,
/**
* @type {Function}
*/
process: null,
/**
* @type {Function}
*/
lint: null,
value_: null,
get value() {
return this.value_;
},
set value(value) {
let valueType = typeof value;
let pattern = this.accepts && this.accepts[valueType];
if (this.setValue) {
this.value_ = this.setValue(value);
return this.value_;
}
if (!pattern)
throw new Error(Errors.unacceptableValueType(valueType, this.accepts));
if (valueType === 'boolean') {
if (pattern.indexOf(value) < 0)
throw new Error(Errors.unacceptableBoolean(pattern));
this.value_ = value;
return this.value_;
}
if (valueType === 'number') {
if (value !== parseInt(value))
throw new Error(Errors.unacceptableNumber());
this.value_ = new Array(value + 1).join(' ');
return this.value_;
}
if (valueType = 'string') {
if (!value.match(pattern))
throw new Error(Errors.unacceptableString(pattern));
this.value_ = value;
return this.value_;
}
throw new Error(Errors.implementSetValue(valueType));
},
validate() {
if (typeof this.name !== 'string' || !this.name)
throw new Error(Errors.missingName());
if (!Array.isArray(this.syntax) || this.syntax.length === 0)
throw new Error(Errors.missingSyntax());
if (typeof this.accepts !== 'object' &&
typeof this.setValue !== 'function')
throw new Error(Errors.missingSetValue());
}
};
module.exports = Plugin;