Skip to content

Commit

Permalink
- Prefer for-of
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Apr 30, 2020
1 parent b844fd8 commit 6cf77bb
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions esquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const RIGHT_SIDE = 'RIGHT_SIDE';
*/
function getPath(obj, key) {
const keys = key.split('.');
for (let i = 0; i < keys.length; i++) {
for (const key of keys) {
if (obj == null) { return obj; }
obj = obj[keys[i]];
obj = obj[key];
}
return obj;
}
Expand All @@ -55,8 +55,8 @@ function inPath(node, ancestor, path) {
const field = ancestor[path[0]];
const remainingPath = path.slice(1);
if (Array.isArray(field)) {
for (let i = 0, l = field.length; i < l; ++i) {
if (inPath(node, field[i], remainingPath)) { return true; }
for (const fld of field) {
if (inPath(node, fld, remainingPath)) { return true; }
}
return false;
} else {
Expand Down Expand Up @@ -93,31 +93,31 @@ function matches(node, selector, ancestry) {

}
case 'matches':
for (let i = 0, l = selector.selectors.length; i < l; ++i) {
if (matches(node, selector.selectors[i], ancestry)) { return true; }
for (const sel of selector.selectors) {
if (matches(node, sel, ancestry)) { return true; }
}
return false;

case 'compound':
for (let i = 0, l = selector.selectors.length; i < l; ++i) {
if (!matches(node, selector.selectors[i], ancestry)) { return false; }
for (const sel of selector.selectors) {
if (!matches(node, sel, ancestry)) { return false; }
}
return true;

case 'not':
for (let i = 0, l = selector.selectors.length; i < l; ++i) {
if (matches(node, selector.selectors[i], ancestry)) { return false; }
for (const sel of selector.selectors) {
if (matches(node, sel, ancestry)) { return false; }
}
return true;

case 'has': {
const collector = [];
for (let i = 0, l = selector.selectors.length; i < l; ++i) {
for (const sel of selector.selectors) {
const a = [];
estraverse.traverse(node, {
enter (node, parent) {
if (parent != null) { a.unshift(parent); }
if (matches(node, selector.selectors[i], a)) {
if (matches(node, sel, a)) {
collector.push(node);
}
},
Expand Down Expand Up @@ -237,8 +237,8 @@ function sibling(node, selector, ancestry, side) {
const [parent] = ancestry;
if (!parent) { return false; }
const keys = estraverse.VisitorKeys[parent.type];
for (let i = 0, l = keys.length; i < l; ++i) {
const listProp = parent[keys[i]];
for (const key of keys) {
const listProp = parent[key];
if (Array.isArray(listProp)) {
const startIndex = listProp.indexOf(node);
if (startIndex < 0) { continue; }
Expand Down Expand Up @@ -273,8 +273,8 @@ function adjacent(node, selector, ancestry, side) {
const [parent] = ancestry;
if (!parent) { return false; }
const keys = estraverse.VisitorKeys[parent.type];
for (let i = 0, l = keys.length; i < l; ++i) {
const listProp = parent[keys[i]];
for (const key of keys) {
const listProp = parent[key];
if (Array.isArray(listProp)) {
const idx = listProp.indexOf(node);
if (idx < 0) { continue; }
Expand Down Expand Up @@ -307,8 +307,8 @@ function nthChild(node, ancestry, idxFn) {
const [parent] = ancestry;
if (!parent) { return false; }
const keys = estraverse.VisitorKeys[parent.type];
for (let i = 0, l = keys.length; i < l; ++i) {
const listProp = parent[keys[i]];
for (const key of keys) {
const listProp = parent[key];
if (Array.isArray(listProp)) {
const idx = listProp.indexOf(node);
if (idx >= 0 && idx === idxFn(listProp.length)) { return true; }
Expand Down

0 comments on commit 6cf77bb

Please sign in to comment.