You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ge...@apache.org on 2020/09/03 21:09:24 UTC

[incubator-annotator] 02/03: make refinedBy plugin recurse properly

This is an automated email from the ASF dual-hosted git repository.

gerben pushed a commit to branch simpler-matcher-creation
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit 3f8d00e9a28eae09e1f549c5c37e2180e9ec61b2
Author: Gerben <ge...@treora.com>
AuthorDate: Thu Sep 3 22:31:10 2020 +0200

    make refinedBy plugin recurse properly
    
    One still has to ensure this plugin is listed before the actual matcher
    implementations, because it needs their return value
---
 packages/selector/src/index.ts | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/packages/selector/src/index.ts b/packages/selector/src/index.ts
index 70396c3..8752abf 100644
--- a/packages/selector/src/index.ts
+++ b/packages/selector/src/index.ts
@@ -70,31 +70,18 @@ export function mapSelectorTypes<TScope, TMatch extends TScope>(
 }
 
 // A plugin to support the Selector’s refinedBy field .
-// TODO this just says `supportRefinement = makeRefinable`; which is doing recursion wrong.
 export const supportRefinement: Plugin<any, any> =
   function supportRefinementPlugin<TScope, TMatch extends TScope>(
     next: MatcherCreator<TScope, TMatch>,
     recurse: MatcherCreator<TScope, TMatch>,
   ) {
-    return makeRefinable(next);
-  };
-
-export function makeRefinable<
-  TSelector extends Selector,
-  TScope,
-  // To enable refinement, the implementation’s Match object must be usable as a
-  // Scope object itself.
-  TMatch extends TScope
->(
-  matcherCreator: (selector: TSelector) => Matcher<TScope, TMatch>,
-): (selector: TSelector) => Matcher<TScope, TMatch> {
   return function createMatcherWithRefinement(
-    sourceSelector: TSelector,
+    sourceSelector: Selector,
   ): Matcher<TScope, TMatch> {
-    const matcher = matcherCreator(sourceSelector);
+    const matcher = next(sourceSelector);
 
     if (sourceSelector.refinedBy) {
-      const refiningSelector = createMatcherWithRefinement(
+      const refiningSelector = recurse(
         sourceSelector.refinedBy,
       );
 
@@ -103,8 +90,8 @@ export function makeRefinable<
           yield* refiningSelector(match);
         }
       };
+    } else {
+      return matcher;
     }
-
-    return matcher;
   };
 }