-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathno-hook-class.js
68 lines (50 loc) · 1.58 KB
/
no-hook-class.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
/**
* @file rule: no-hook-class
* @author chris<[email protected]>
*/
module.exports = {
name: 'no-hook-class',
desc: 'Classes should be not hooked for script.',
target: 'parser',
lint: function (getCfg, parser, reporter) {
var config = getCfg();
if (!config) {
return;
}
var regTest = /^\/.*\/$/;
var keys = build(config.keys);
function build(keys) {
return (Array.isArray(keys) ? keys : [keys]).map(function (key) {
return regTest.test(key) ? new RegExp(key.slice(1, -1)) : key;
});
}
function isHook(name) {
return keys.some(function (key) {
return key === name || name.match(key);
});
}
function report(name, pos) {
reporter.warn(pos, '047', 'Class name(' + name + ') should be not hooked for script.');
}
parser.tokenizer.on('attribdata', function (value) {
var attribute = parser._attribname.toLowerCase();
if (attribute !== 'class') {
return;
}
var newConfig = getCfg();
if (!newConfig) {
return;
}
if (newConfig.keys !== config.keys) {
config = newConfig;
keys = build(newConfig.keys);
}
var pos = this._sectionStart;
value.split(/\s+/).forEach(function (name) {
if (isHook(name)) {
report(name, pos);
}
});
});
}
};