@@ -4,14 +4,9 @@ import { BabelVisitor } from "./BabelVisitor.js";
44import * as bpe from "@babel/types" ;
55import Restructure from "./Restructure.js" ;
66import { NotSupportedError } from "./NotSupportedError.js" ;
7- import TimedCache from "../../common/cache/TimedCache.js" ;
87
98type IQueryFragment = string | { name ?: string , value ?: any } ;
109
11- const parsedCache = new TimedCache < string , bpe . Node > ( ) ;
12-
13- const parameterCacheSymbol = Symbol ( "parameterCacheSymbol" ) ;
14-
1510const defaultObject = { } ;
1611
1712export default class ArrowToExpression extends BabelVisitor < Expression > {
@@ -25,10 +20,8 @@ export default class ArrowToExpression extends BabelVisitor<Expression> {
2520 */
2621 public static transform ( fx : ( p : any ) => ( x : any ) => any , target ?: ParameterExpression , outerParameter ?: ParameterExpression ) {
2722 const key = fx . toString ( ) ;
28- const node = parsedCache . getOrCreate ( key , fx , ( k , f ) => {
29- const rs = new Restructure ( ) ;
30- return rs . visit ( parseExpression ( f . toString ( ) ) ) ;
31- } ) ;
23+ const rs = new Restructure ( ) ;
24+ const node = rs . visit ( parseExpression ( key ) ) ;
3225 return this . transformUncached ( node , target , outerParameter ) ;
3326 }
3427
@@ -40,70 +33,65 @@ export default class ArrowToExpression extends BabelVisitor<Expression> {
4033 * @param target parameter to replace
4134 * @returns transformed node
4235 */
43- private static transformUncached ( node : bpe . Node , tx ?: ParameterExpression , outerParameter ?: ParameterExpression ) : { params : Map < string , any > , body : Expression , target : ParameterExpression } {
36+ private static transformUncached ( node : bpe . Node , target ?: ParameterExpression , outerParameter ?: ParameterExpression ) : { params : ParameterExpression [ ] , body : Expression , target : ParameterExpression } {
4437
45- const cache = node [ parameterCacheSymbol ] ??= new TimedCache < ParameterExpression , bpe . Node > ( ) ;
46-
47- return cache . getOrCreate ( tx ?? defaultObject , tx , ( _ , target ) => {
48-
49- if ( node . type !== "ArrowFunctionExpression" ) {
50- throw new Error ( "Expecting an arrow function" ) ;
51- }
38+ if ( node . type !== "ArrowFunctionExpression" ) {
39+ throw new Error ( "Expecting an arrow function" ) ;
40+ }
5241
53- const paramSet = new Map < string , any > ( ) ;
42+ const paramSet = new Map < string , any > ( ) ;
5443
55- let firstOuterParam = null ;
44+ let firstOuterParam = null ;
5645
57- const params = [ ] ;
46+ const params = [ ] ;
5847
59- for ( const iterator of node . params ) {
60- if ( iterator . type !== "Identifier" ) {
61- throw new Error ( "Expecting an identifier" ) ;
62- }
63- if ( ! firstOuterParam && outerParameter ) {
64- firstOuterParam = iterator . name ;
65- paramSet . set ( iterator . name , outerParameter ) ;
66- params . push ( outerParameter ) ;
67- continue ;
68- }
69- const p1 = ParameterExpression . create ( { name : iterator . name } ) ;
70- paramSet . set ( iterator . name , p1 ) ;
71- params . push ( p1 ) ;
48+ for ( const iterator of node . params ) {
49+ if ( iterator . type !== "Identifier" ) {
50+ throw new Error ( "Expecting an identifier" ) ;
7251 }
73-
74- if ( outerParameter && firstOuterParam ) {
75- paramSet . set ( firstOuterParam , outerParameter ) ;
52+ if ( ! firstOuterParam && outerParameter ) {
53+ firstOuterParam = iterator . name ;
54+ paramSet . set ( iterator . name , outerParameter ) ;
55+ params . push ( outerParameter ) ;
56+ continue ;
7657 }
58+ const p1 = ParameterExpression . create ( { name : iterator . name } ) ;
59+ paramSet . set ( iterator . name , p1 ) ;
60+ params . push ( p1 ) ;
61+ }
7762
78- let body = node . body ;
79- if ( body . type !== "ArrowFunctionExpression" ) {
80- throw new Error ( "Expecting an arrow function" ) ;
81- }
63+ if ( outerParameter && firstOuterParam ) {
64+ paramSet . set ( firstOuterParam , outerParameter ) ;
65+ }
8266
83- const firstTarget = body . params [ 0 ] ;
67+ let body = node . body ;
68+ if ( body . type !== "ArrowFunctionExpression" ) {
69+ throw new Error ( "Expecting an arrow function" ) ;
70+ }
8471
85- let name = "____x" ;
72+ const firstTarget = body . params [ 0 ] ;
8673
87- if ( firstTarget ) {
74+ let name = "____x" ;
8875
89- if ( firstTarget . type !== "Identifier" ) {
90- throw new Error ( "Expecting an identifier" ) ;
91- }
92- name = firstTarget . name ;
76+ if ( firstTarget ) {
77+
78+ if ( firstTarget . type !== "Identifier" ) {
79+ throw new Error ( "Expecting an identifier" ) ;
9380 }
81+ name = firstTarget . name ;
82+ }
9483
9584
96- target ??= ParameterExpression . create ( { name} ) ;
85+ target ??= ParameterExpression . create ( { name} ) ;
9786
98- body = body . body ;
87+ body = body . body ;
9988
100- const visitor = new this ( paramSet , target , name ) ;
101- return {
102- params,
103- target,
104- body : visitor . visit ( body )
105- } ;
106- } ) ;
89+ const visitor = new this ( paramSet , target , name ) ;
90+ return {
91+ params,
92+ target,
93+ body : visitor . visit ( body )
94+ } ;
10795 }
10896
10997 public readonly leftJoins : string [ ] = [ ] ;
0 commit comments