Skip to content

Commit 78f8077

Browse files
committed
ast-walkers: hide iterator in searchOpts
Now that searchOpts have one required property, we may remove all `opts = opts || {}` checking.
1 parent cc51df1 commit 78f8077

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

lib/ast-walkers.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,59 @@ var TypeIndex = require('./type-index');
55
var walkers = exports;
66

77

8-
walkers.topScan = function (node, nodeIndex, parent, iterator, opts) {
8+
walkers.topScan = function (node, nodeIndex, parent, opts) {
99
if (parent) {
1010
// We would like to avoid spinning an extra loop through the starting
1111
// node's siblings just to count its typeIndex.
1212
throw Error('topScan is supposed to be called from the root node');
1313
}
1414

15-
iterator(node, nodeIndex, parent);
15+
if (!opts.typeIndex) {
16+
opts.iterator(node, nodeIndex, parent);
17+
}
1618
walkers.descendant.apply(this, arguments);
1719
};
1820

1921

20-
walkers.descendant = function (node, nodeIndex, parent, iterator, opts) {
22+
walkers.descendant = function (node, nodeIndex, parent, opts) {
2123
if (!node.children || !node.children.length) {
2224
return;
2325
}
2426

25-
if ((opts = opts || {}).typeIndex) {
27+
if (opts.typeIndex) {
2628
var typeIndex = TypeIndex();
2729
}
2830

2931
node.children.forEach(function (child, childIndex) {
30-
iterator(child, childIndex, node,
31-
opts.typeIndex ? { typeIndex: typeIndex(child) } : undefined);
32-
walkers.descendant(child, childIndex, node, iterator, opts);
32+
opts.iterator(child, childIndex, node,
33+
opts.typeIndex ? { typeIndex: typeIndex(child) } : undefined);
34+
walkers.descendant(child, childIndex, node, opts);
3335
});
3436
};
3537

3638

37-
walkers.child = function (node, nodeIndex, parent, iterator, opts) {
39+
walkers.child = function (node, nodeIndex, parent, opts) {
3840
if (!node.children || !node.children.length) {
3941
return;
4042
}
4143

42-
if ((opts = opts || {}).typeIndex) {
44+
if (opts.typeIndex) {
4345
var typeIndex = TypeIndex();
4446
}
4547

4648
node.children.forEach(function (child, childIndex) {
47-
iterator(child, childIndex, node,
48-
opts.typeIndex ? { typeIndex: typeIndex(child) } : undefined);
49+
opts.iterator(child, childIndex, node,
50+
opts.typeIndex ? { typeIndex: typeIndex(child) } : undefined);
4951
});
5052
};
5153

5254

53-
walkers.adjacentSibling = function (node, nodeIndex, parent, iterator, opts) {
55+
walkers.adjacentSibling = function (node, nodeIndex, parent, opts) {
5456
if (!parent) {
5557
return;
5658
}
5759

58-
if ((opts = opts || {}).typeIndex) {
60+
if (opts.typeIndex) {
5961
var typeIndex = TypeIndex();
6062

6163
// Prefill type indexes with preceding nodes.
@@ -66,18 +68,18 @@ walkers.adjacentSibling = function (node, nodeIndex, parent, iterator, opts) {
6668

6769
if (++nodeIndex < parent.children.length) {
6870
node = parent.children[nodeIndex];
69-
iterator(node, nodeIndex, parent,
70-
opts.typeIndex ? { typeIndex: typeIndex(node) } : undefined);
71+
opts.iterator(node, nodeIndex, parent,
72+
opts.typeIndex ? { typeIndex: typeIndex(node) } : undefined);
7173
}
7274
};
7375

7476

75-
walkers.generalSibling = function (node, nodeIndex, parent, iterator, opts) {
77+
walkers.generalSibling = function (node, nodeIndex, parent, opts) {
7678
if (!parent) {
7779
return;
7880
}
7981

80-
if ((opts = opts || {}).typeIndex) {
82+
if (opts.typeIndex) {
8183
var typeIndex = TypeIndex();
8284

8385
// Prefill type indexes with preceding nodes.
@@ -88,7 +90,7 @@ walkers.generalSibling = function (node, nodeIndex, parent, iterator, opts) {
8890

8991
while (++nodeIndex < parent.children.length) {
9092
node = parent.children[nodeIndex];
91-
iterator(node, nodeIndex, parent,
92-
opts.typeIndex ? { typeIndex: typeIndex(node) } : undefined);
93+
opts.iterator(node, nodeIndex, parent,
94+
opts.typeIndex ? { typeIndex: typeIndex(node) } : undefined);
9395
}
9496
};

lib/select.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ select.rule = function (rule, ast) {
3636
'+': walkers.adjacentSibling,
3737
'~': walkers.generalSibling
3838
})[rule.nestingOperator](
39-
node, nodeIndex, parent, match.bind(null, rule), searchOpts(rule)
39+
node,
40+
nodeIndex,
41+
parent,
42+
searchOpts({ iterator: match.bind(null, rule) }, rule)
4043
);
4144
}
4245

4346
function match (rule, node, nodeIndex, parent, props) {
44-
if (matchNode(rule, node, nodeIndex, parent, props)) {
47+
if (matchNode.apply(this, arguments)) {
4548
if (rule.rule) {
4649
search(rule.rule, node, nodeIndex, parent);
4750
}
@@ -53,15 +56,15 @@ select.rule = function (rule, ast) {
5356
};
5457

5558

56-
function searchOpts (rule) {
57-
var needsTypeIndex = rule.pseudos && rule.pseudos.some(function (pseudo) {
58-
return pseudo.name == 'nth-of-type' ||
59-
pseudo.name == 'nth-last-of-type' ||
60-
pseudo.name == 'first-of-type' ||
61-
pseudo.name == 'last-of-type';
62-
});
63-
64-
if (needsTypeIndex) {
65-
return { typeIndex: true };
59+
function searchOpts (opts, rule) {
60+
if (rule.pseudos && rule.pseudos.some(function (pseudo) {
61+
return (pseudo.name == 'nth-of-type' ||
62+
pseudo.name == 'nth-last-of-type' ||
63+
pseudo.name == 'first-of-type' ||
64+
pseudo.name == 'last-of-type');
65+
})) {
66+
opts.typeIndex = true;
6667
}
68+
69+
return opts;
6770
}

0 commit comments

Comments
 (0)