|
1 | | -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 | 3 | /* eslint-disable max-params */ |
4 | 4 |
|
5 | 5 | /* Expose. */ |
6 | | -module.exports = is; |
| 6 | +module.exports = is |
7 | 7 |
|
8 | 8 | /* Assert if `test` passes for `node`. |
9 | 9 | * When a `parent` node is known the `index` of node */ |
10 | 10 | function is(test, node, index, parent, context) { |
11 | | - var hasParent = parent !== null && parent !== undefined; |
12 | | - var hasIndex = index !== null && index !== undefined; |
13 | | - var check = convert(test); |
| 11 | + var hasParent = parent !== null && parent !== undefined |
| 12 | + var hasIndex = index !== null && index !== undefined |
| 13 | + var check = convert(test) |
14 | 14 |
|
15 | 15 | if ( |
16 | 16 | hasIndex && |
17 | 17 | (typeof index !== 'number' || index < 0 || index === Infinity) |
18 | 18 | ) { |
19 | | - throw new Error('Expected positive finite index or child node'); |
| 19 | + throw new Error('Expected positive finite index or child node') |
20 | 20 | } |
21 | 21 |
|
22 | 22 | if (hasParent && (!is(null, parent) || !parent.children)) { |
23 | | - throw new Error('Expected parent node'); |
| 23 | + throw new Error('Expected parent node') |
24 | 24 | } |
25 | 25 |
|
26 | 26 | if (!node || !node.type || typeof node.type !== 'string') { |
27 | | - return false; |
| 27 | + return false |
28 | 28 | } |
29 | 29 |
|
30 | 30 | if (hasParent !== hasIndex) { |
31 | | - throw new Error('Expected both parent and index'); |
| 31 | + throw new Error('Expected both parent and index') |
32 | 32 | } |
33 | 33 |
|
34 | | - return Boolean(check.call(context, node, index, parent)); |
| 34 | + return Boolean(check.call(context, node, index, parent)) |
35 | 35 | } |
36 | 36 |
|
37 | 37 | function convert(test) { |
38 | 38 | if (typeof test === 'string') { |
39 | | - return typeFactory(test); |
| 39 | + return typeFactory(test) |
40 | 40 | } |
41 | 41 |
|
42 | 42 | if (test === null || test === undefined) { |
43 | | - return ok; |
| 43 | + return ok |
44 | 44 | } |
45 | 45 |
|
46 | 46 | if (typeof test === 'object') { |
47 | | - return ('length' in test ? anyFactory : matchesFactory)(test); |
| 47 | + return ('length' in test ? anyFactory : matchesFactory)(test) |
48 | 48 | } |
49 | 49 |
|
50 | 50 | if (typeof test === 'function') { |
51 | | - return test; |
| 51 | + return test |
52 | 52 | } |
53 | 53 |
|
54 | | - throw new Error('Expected function, string, or object as test'); |
| 54 | + throw new Error('Expected function, string, or object as test') |
55 | 55 | } |
56 | 56 |
|
57 | 57 | function convertAll(tests) { |
58 | | - var results = []; |
59 | | - var length = tests.length; |
60 | | - var index = -1; |
| 58 | + var results = [] |
| 59 | + var length = tests.length |
| 60 | + var index = -1 |
61 | 61 |
|
62 | 62 | while (++index < length) { |
63 | | - results[index] = convert(tests[index]); |
| 63 | + results[index] = convert(tests[index]) |
64 | 64 | } |
65 | 65 |
|
66 | | - return results; |
| 66 | + return results |
67 | 67 | } |
68 | 68 |
|
69 | 69 | /* Utility assert each property in `test` is represented |
70 | 70 | * in `node`, and each values are strictly equal. */ |
71 | 71 | function matchesFactory(test) { |
72 | | - return matches; |
| 72 | + return matches |
73 | 73 |
|
74 | 74 | function matches(node) { |
75 | | - var key; |
| 75 | + var key |
76 | 76 |
|
77 | 77 | for (key in test) { |
78 | 78 | if (node[key] !== test[key]) { |
79 | | - return false; |
| 79 | + return false |
80 | 80 | } |
81 | 81 | } |
82 | 82 |
|
83 | | - return true; |
| 83 | + return true |
84 | 84 | } |
85 | 85 | } |
86 | 86 |
|
87 | 87 | function anyFactory(tests) { |
88 | | - var checks = convertAll(tests); |
89 | | - var length = checks.length; |
| 88 | + var checks = convertAll(tests) |
| 89 | + var length = checks.length |
90 | 90 |
|
91 | | - return matches; |
| 91 | + return matches |
92 | 92 |
|
93 | 93 | function matches() { |
94 | | - var index = -1; |
| 94 | + var index = -1 |
95 | 95 |
|
96 | 96 | while (++index < length) { |
97 | 97 | if (checks[index].apply(this, arguments)) { |
98 | | - return true; |
| 98 | + return true |
99 | 99 | } |
100 | 100 | } |
101 | 101 |
|
102 | | - return false; |
| 102 | + return false |
103 | 103 | } |
104 | 104 | } |
105 | 105 |
|
106 | 106 | /* Utility to convert a string into a function which checks |
107 | 107 | * a given node’s type for said string. */ |
108 | 108 | function typeFactory(test) { |
109 | | - return type; |
| 109 | + return type |
110 | 110 |
|
111 | 111 | function type(node) { |
112 | | - return Boolean(node && node.type === test); |
| 112 | + return Boolean(node && node.type === test) |
113 | 113 | } |
114 | 114 | } |
115 | 115 |
|
116 | 116 | /* Utility to return true. */ |
117 | 117 | function ok() { |
118 | | - return true; |
| 118 | + return true |
119 | 119 | } |
0 commit comments