From ea7f88fe0299fdb2da511e97dbc3eb0e6b40ec3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Mon, 14 Dec 2020 19:14:30 +0100 Subject: [PATCH 1/7] move needsIndex to seprate file Tries to remove cyclical dependency any.js -> psudo.js addressing [#9](https://github.com/syntax-tree/unist-util-select/issues/9) --- lib/any.js | 4 ++-- lib/needs.js | 19 +++++++++++++++++++ lib/pseudo.js | 13 ------------- 3 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 lib/needs.js diff --git a/lib/any.js b/lib/any.js index fef9448..3d8b8c5 100644 --- a/lib/any.js +++ b/lib/any.js @@ -3,7 +3,7 @@ module.exports = match var zwitch = require('zwitch') -var pseudo = require('./pseudo') +var needs = require('./needs') var test = require('./test') var nest = require('./nest') @@ -75,7 +75,7 @@ function configure(query, state) { var index = -1 while (++index < pseudos.length) { - if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) { + if (needs.needsIndex.indexOf(pseudos[index].name) > -1) { state.index = true break } diff --git a/lib/needs.js b/lib/needs.js new file mode 100644 index 0000000..3a5e5ea --- /dev/null +++ b/lib/needs.js @@ -0,0 +1,19 @@ +/* istanbul ignore file - Shouldn’t be invoked */ +'use strict' + +module.exports = match + +function match() {} + +match.needsIndex = [ + 'first-child', + 'first-of-type', + 'last-child', + 'last-of-type', + 'nth-child', + 'nth-last-child', + 'nth-of-type', + 'nth-last-of-type', + 'only-child', + 'only-of-type' +] diff --git a/lib/pseudo.js b/lib/pseudo.js index 855fa74..848b453 100644 --- a/lib/pseudo.js +++ b/lib/pseudo.js @@ -9,19 +9,6 @@ var anything = require('./any') var is = convert() -match.needsIndex = [ - 'first-child', - 'first-of-type', - 'last-child', - 'last-of-type', - 'nth-child', - 'nth-last-child', - 'nth-of-type', - 'nth-last-of-type', - 'only-child', - 'only-of-type' -] - var handle = zwitch('name', { unknown: unknownPseudo, invalid: invalidPseudo, From 50ae64e2a50f5fb82ddcfb0bbf42e4aeb8acd357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Mon, 14 Dec 2020 20:44:49 +0100 Subject: [PATCH 2/7] remove ciruclar dependency passing any via state --- index.js | 2 +- lib/any.js | 3 ++- lib/pseudo.js | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5f1352b..871d5ef 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ var any = require('./lib/any') var parse = require('./lib/parse') function matches(selector, node) { - return Boolean(any(parse(selector), node, {one: true, shallow: true})[0]) + return Boolean(any(parse(selector), node, {one: true, shallow: true, any})[0]) } function select(selector, node) { diff --git a/lib/any.js b/lib/any.js index 3d8b8c5..50b82d7 100644 --- a/lib/any.js +++ b/lib/any.js @@ -52,7 +52,8 @@ function rule(query, tree, state) { scopeNodes: tree.type === 'root' ? tree.children : [tree], iterator: iterator, one: state.one, - shallow: state.shallow + shallow: state.shallow, + any: state.any }) ) diff --git a/lib/pseudo.js b/lib/pseudo.js index 848b453..8c6cb0e 100644 --- a/lib/pseudo.js +++ b/lib/pseudo.js @@ -5,7 +5,6 @@ module.exports = match var zwitch = require('zwitch') var not = require('not') var convert = require('unist-util-is/convert') -var anything = require('./any') var is = convert() @@ -48,6 +47,7 @@ function match(query, node, index, parent, state) { function matches(query, node, index, parent, state) { var shallow = state.shallow var one = state.one + var anything = state.any var result state.one = true @@ -147,6 +147,7 @@ function hasSelector(query, node, index, parent, state) { var one = state.one var scopeNodes = state.scopeNodes var value = appendScope(query.value) + var anything = state.any var result state.shallow = false From 2e09516e19acca71985c406224da72287d83ffc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Mon, 14 Dec 2020 21:03:44 +0100 Subject: [PATCH 3/7] move needsIndex to any --- lib/any.js | 16 ++++++++++++++-- lib/needs.js | 19 ------------------- 2 files changed, 14 insertions(+), 21 deletions(-) delete mode 100644 lib/needs.js diff --git a/lib/any.js b/lib/any.js index 50b82d7..80c3620 100644 --- a/lib/any.js +++ b/lib/any.js @@ -2,8 +2,20 @@ module.exports = match +match.needsIndex = [ + 'first-child', + 'first-of-type', + 'last-child', + 'last-of-type', + 'nth-child', + 'nth-last-child', + 'nth-of-type', + 'nth-last-of-type', + 'only-child', + 'only-of-type' +] + var zwitch = require('zwitch') -var needs = require('./needs') var test = require('./test') var nest = require('./nest') @@ -76,7 +88,7 @@ function configure(query, state) { var index = -1 while (++index < pseudos.length) { - if (needs.needsIndex.indexOf(pseudos[index].name) > -1) { + if (match.needsIndex.indexOf(pseudos[index].name) > -1) { state.index = true break } diff --git a/lib/needs.js b/lib/needs.js deleted file mode 100644 index 3a5e5ea..0000000 --- a/lib/needs.js +++ /dev/null @@ -1,19 +0,0 @@ -/* istanbul ignore file - Shouldn’t be invoked */ -'use strict' - -module.exports = match - -function match() {} - -match.needsIndex = [ - 'first-child', - 'first-of-type', - 'last-child', - 'last-of-type', - 'nth-child', - 'nth-last-child', - 'nth-of-type', - 'nth-last-of-type', - 'only-child', - 'only-of-type' -] From 0f99c5dd1aa4de58bc93ec6451676d8998603d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Mon, 14 Dec 2020 21:35:29 +0100 Subject: [PATCH 4/7] make needsIndex private Co-authored-by: Titus --- lib/any.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/any.js b/lib/any.js index 80c3620..8ff7e90 100644 --- a/lib/any.js +++ b/lib/any.js @@ -2,7 +2,7 @@ module.exports = match -match.needsIndex = [ +var needsIndex = [ 'first-child', 'first-of-type', 'last-child', From 77431f02e574f7d7930b49d3eb80a4374f8f63d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Mon, 14 Dec 2020 21:36:24 +0100 Subject: [PATCH 5/7] make needsIndex private Co-authored-by: Titus --- lib/any.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/any.js b/lib/any.js index 8ff7e90..393e0fb 100644 --- a/lib/any.js +++ b/lib/any.js @@ -88,7 +88,7 @@ function configure(query, state) { var index = -1 while (++index < pseudos.length) { - if (match.needsIndex.indexOf(pseudos[index].name) > -1) { + if (needsIndex.indexOf(pseudos[index].name) > -1) { state.index = true break } From edf20d2c5caa0e2a9a4618a8c29d95661d6fea4a Mon Sep 17 00:00:00 2001 From: Titus Date: Tue, 15 Dec 2020 10:49:45 +0100 Subject: [PATCH 6/7] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 871d5ef..9ec0af1 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ var any = require('./lib/any') var parse = require('./lib/parse') function matches(selector, node) { - return Boolean(any(parse(selector), node, {one: true, shallow: true, any})[0]) + return Boolean(any(parse(selector), node, {one: true, shallow: true, any: any})[0]) } function select(selector, node) { From 89f87356ac213f685d5697446632c073f9e64a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Tue, 15 Dec 2020 11:20:41 +0100 Subject: [PATCH 7/7] move needsIndex into local scope Configure function is the sole consumer of the needsIndex array, it shoould then be in it's local scope. This change further improves code quiality. --- lib/any.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/any.js b/lib/any.js index 393e0fb..b1ecedc 100644 --- a/lib/any.js +++ b/lib/any.js @@ -2,19 +2,6 @@ module.exports = match -var needsIndex = [ - 'first-child', - 'first-of-type', - 'last-child', - 'last-of-type', - 'nth-child', - 'nth-last-child', - 'nth-of-type', - 'nth-last-of-type', - 'only-child', - 'only-of-type' -] - var zwitch = require('zwitch') var test = require('./test') var nest = require('./nest') @@ -84,6 +71,18 @@ function rule(query, tree, state) { } function configure(query, state) { + var needsIndex = [ + 'first-child', + 'first-of-type', + 'last-child', + 'last-of-type', + 'nth-child', + 'nth-last-child', + 'nth-of-type', + 'nth-last-of-type', + 'only-child', + 'only-of-type' + ] var pseudos = query.pseudos || [] var index = -1