All files / mframejs/binding/ast traverseAst.ts

81.29% Statements 126/155
84.21% Branches 80/95
83.33% Functions 15/18
81.29% Lines 126/155

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 50829x 29x   29x                               118x 6x 11x 14x 2x 1x 1x 1x 10x 5x 1x 5x 2x 2x 2x 29x 1x 1x   2x                         3179x       5279x       623x       29x       41x       67x     9307x                                 7x     6x                 14x     6x           5246x   4894x         5245x                   4882x   4882x       5245x     5245x             5246x                                       6x   3x             3x       24x 24x 24x     45x   1x       24x 24x   22x     24x 24x   16x     8x 8x                       30x     13x 13x 15x   13x 13x         36x         36x     36x   36x       376x   24x 24x   352x     376x   376x     203x 203x 203x 203x         37x 37x 29x     37x     7x   30x       37x   29x       36x   3x   33x         1x         695x                         41x 41x 41x   41x   41x                       67x   67x 72x       67x         67x   67x               67x                     4955x 4955x 4955x 4955x 7828x 7828x       1432x   3523x                                                                                                         16x 16x 16x 16x   16x                                           16x                         3392x 3392x   3392x                                     3392x               29x 4926x       29x 16x       29x 3392x    
import { ContainerValueConverters, ContainerClasses } from '../../container/exported';
import { getCorrectContext } from '../contextOfObject';
import { IBindingContext } from '../../interface/exported';
import { setValue } from '../setValue';
 
 
 
let fromView: any;
 
const Traverser = class {
 
 
 
    /**
     * operator calls
     *
     */
    public static operator = function (val: string, a: any, b: any) {
        switch (val) {
            case '+': return a + b;
            case '-': return b ? a - b : -a;
            case '*': return a * b;
            case '!': return !a;
            case '%': return a % b;
            case '>': return a > b;
            case '<': return a < b;
            case '>=': return a >= b;
            case '&&': return a && b;
            case '||': return a || b;
            case '<=': return a <= b;
            case '+=': return a += b;
            case '-=': return a -= b;
            case '*=': return a *= b;
            case '/=': return a /= b;
            case '===': return a === b;
            case '!==': return a !== b;
            case '/': return a / b;
            // tslint:disable-next-line:no-bitwise
            case '^': return a ^ b;
        }
    };
 
 
 
    /**
     * finds out what arity it is
     *
     */
    public static checkArity = function (ast: any, ctx: any, mainctx: any) {
        let value;
        if (ast && ast.arity === 'literal') {
            value = ast.value;
        }
 
        if (ast && ast.arity === 'variable') {
            value = Traverser.variable(ast, ctx, mainctx);
        }
 
        if (ast && ast.arity === 'binary') {
            value = Traverser.binary(ast, ctx, mainctx);
        }
 
        if (ast && ast.arity === 'unary') {
            value = Traverser.binary(ast, ctx, mainctx);
        }
 
        if (ast && ast.arity === 'ternary') {
            value = Traverser.ternary(ast, ctx, mainctx);
        }
 
        if (ast && ast.arity === 'valueConverter') {
            value = Traverser.valueConverter(ast, ctx, mainctx);
        }
 
        return value;
    };
 
 
 
    /**
     * called if arity is variable
     * todo: this one is kinda messy atm
     *
     */
    public static variable = function (ast: any, ctx: any, _mainctx: IBindingContext) {
        let result;
        try {
 
 
            switch (true) {
                case ast.value === 'null' && ast.arity === 'variable':
                    return null;
 
                case ast.value === 'undefined' && ast.arity === 'variable':
                    return undefined;
 
                case ast.value === '$this' && ast.arity === 'variable' && ast.root:
                    return ctx.$context;
 
                case ast.value === '$event' && ast.arity === 'variable' && ast.root:
                    return ctx.$event;
 
                case ast.arity === 'variable' && typeof ast.value === 'string' && ast.value.toUpperCase() === 'TRUE':
                    return true;
 
                case ast.arity === 'variable' && typeof ast.value === 'string' && ast.value.toUpperCase() === 'FALSE':
                    return false;
 
                default:
            }
 
            let curObj: any;
            let curCtx = ctx;
            if (ctx.__bindingContext) {
                curCtx = ctx.$context;
            }
 
 
            // check if it varibale starts with $
            let localVariable = false;
            if (ast.value && ast.value[0] === 'string' && ast.value[0][0] !== '$') {
                localVariable = true;
            }
 
            if (localVariable) {
                curObj = curCtx[ast.value];
            } else {
 
                if (ast.root) {
                    curCtx = getCorrectContext(ast.value, ctx);
                    if (curCtx.__bindingContext) {
                        curCtx = curCtx.$context;
                    }
                }
 
                curObj = curCtx[ast.value];
            }
 
            result = curObj;
        } catch (e) {
            // we dont want to display this just nothing if there is no var
            // console.warn('failed in variable');
            // nothing
        }
 
        return result;
    };
 
 
 
    /**
     * called if arity is binary
     *
     */
    public static binary = function (ast: any, ctx: any, mainctx: any) {
        let second: any;
        let value: any;
        let first: any;
 
        switch (true) {
            case ast.assignment === true:
 
                // maybe a bit weird, but works and is dynamic for simple expression assignments
                try {
                    if (ast.first.arity === 'variable') {
                        const $ctx = ctx.__bindingContext ? ctx.$context : ctx;
                        if (ast.value === '+=' || ast.value === '-=') {
                            $ctx[ast.first.value] =
                                Traverser.operator(
                                    ast.value,
                                    $ctx[ast.first.value],
                                    Traverser.checkArity(ast.second, ctx, mainctx)
                                );
                        } else {
                            $ctx[ast.first.value] = Traverser.checkArity(ast.second, ctx, mainctx);
                        }
                    } else {
                        // so it will return object
                        let done = false;
                        let astFirst = ast.first;
                        const x: any = [];
                        while (!done) {
                            if (astFirst.second.arity !== 'binary') {
                                x.push(astFirst.second.value);
                            } else {
                                x.push(`[${Traverser.checkArity(astFirst.second, ctx, mainctx)}]`);
                            }
 
                            if (astFirst.first.arity === 'variable') {
                                x.push(astFirst.first.value);
                                done = true;
                            } else {
                                astFirst = astFirst.first;
                            }
                        }
                        x.reverse();
                        const objectString = x.join('.').split('.[').join('['); // obj.something.cool[5]
                        if (ast.value === '=') {
                            setValue(ctx, objectString, Traverser.checkArity(ast.second, ctx, mainctx));
                        } else {
 
                            first = Traverser.checkArity(ast.first, ctx, mainctx);
                            setValue(ctx, objectString, Traverser.operator(
                                ast.value,
                                first,
                                Traverser.checkArity(ast.second, ctx, mainctx)
                            ));
 
                        }
                    }
 
                } catch (x) {
                    console.error('only simple assigments are supported, sorry', ast);
                }
                break;
            case ast.value === '{':
 
                const x = {};
                ast.first.forEach((y: any) => {
                    x[y.key] = Traverser.checkArity(y, ctx, mainctx);
                });
                value = x;
                break;
 
 
            case ast.checkArray:
 
                first = Traverser.checkArity(ast.first, ctx, mainctx);
                if (first && ast.contextReset) {
                    second = Traverser.checkArity(ast.second, mainctx, mainctx);
                    second = first[second];
                } else {
                    second = Traverser.checkArity(ast.second, first, mainctx);
                }
 
                value = second ? second : first;
 
                break;
 
            case ast.isObject:
 
                first = Traverser.checkArity(ast.first, ctx, mainctx);
                if (first && ast.contextReset) {
                    second = Traverser.checkArity(ast.second, mainctx, mainctx);
                    second = first[second];
                } else {
                    second = Traverser.checkArity(ast.second, first, mainctx);
                }
 
                value = second;
 
                break;
            case !ast.isFunction:
 
                first = Traverser.checkArity(ast.first, ctx, mainctx);
                second = Traverser.checkArity(ast.second, ctx, mainctx);
                value = Traverser.operator(ast.value, first, second);
                break;
 
            default:
 
                // function calls
                const results: any[] = [];
                ast.second.forEach(function (res: any) {
                    results.push(Traverser.evaluate(res, mainctx));
                });
 
                let curCtx = ctx.$context;
                let valueTrigger: any;
                if (ast.first.arity === 'binary') {
                    valueTrigger = Traverser.binary(ast.first, ctx, mainctx);
                } else {
                    valueTrigger = curCtx[ast.first.value];
                }
 
 
                curCtx = getCorrectContext(ast.first.value, ctx);
                if (curCtx && curCtx.$context && typeof curCtx.$context[ast.first.value] === 'function') {
                    valueTrigger = curCtx.$context[ast.first.value];
                }
 
                if (valueTrigger) {
                    const typeObj = Traverser.binary({ first: ast.first.first, checkArray: true }, ctx, mainctx);
                    if (Array.isArray(typeObj)) {
                        value = valueTrigger.apply(typeObj, results);
                    } else {
                        value = valueTrigger.apply(curCtx.$context, results);
                    }
 
 
                } else {
                    console.warn(`method does not exist:${ast.first.value}`);
                }
 
        }
 
        return value;
 
    };
 
 
 
    /**
     * ternary
     * called if arity is ternary (conditional)
     * {condition ? expr1 : expr2}'
     */
    public static ternary = function (ast: any, ctx: any, mainctx: any) {
 
        const first = Traverser.checkArity(ast.first, ctx, mainctx);
        const second = Traverser.checkArity(ast.second, ctx, mainctx);
        const third = Traverser.checkArity(ast.third, ctx, mainctx);
 
        const value: any = first ? second : third;
 
        return value;
    };
 
 
 
    /**
     * called if arity is value converter
     *
     */
    public static valueConverter = function (ast: any, ctx: any, mainctx: any) {
        let result;
 
        const args: any[] = [];
        // loop args
        ast.args.forEach((arg: any) => {
            args.push(Traverser.checkArity(arg, ctx, mainctx));
        });
 
        // find value converter
        const valueConverterExist = ContainerValueConverters.findConverter(ast.value);
        let _class: any;
 
        // if it exist, then we execute it
        if (valueConverterExist) {
            _class = ContainerClasses.get(valueConverterExist);
            try {
                result = fromView ? _class.fromView.apply(ctx, args) : _class.toView.apply(ctx, args);
            } catch (e) {
                console.warn('value converter failed:' + ast.value);
            }
        } else {
            console.warn('value converter does not exist:' + ast.value);
        }
 
        return result;
    };
 
 
 
    /**
     * called to start evaluating ast (getting result)
     *
     */
    public static evaluate = function (astInput: any, ctx: any) {
 
        fromView = false;
        const astArray = Array.isArray(astInput) ? astInput : [astInput];
        const returnArray: any[] = [];
        for (let i = 0; i < astArray.length; i++) {
            const result = Traverser.checkArity(astArray[i], ctx, ctx);
            returnArray.push(result);
        }
 
        if (returnArray.length > 1) {
            return returnArray.join('');
        } else {
            return returnArray[0];
        }
 
    };
 
 
 
    /**
     * handle value converter fromView only
     *
     */
    public static traverseOnlyValueConverter = function (value: any, ast: any, ctx: any, mainctx: any) {
        let result;
 
        let args: any[] = [];
        // loop args
        ast.args.forEach((arg: any, i: number) => {
            if (i > 0) {
                args.push(Traverser.checkArity(arg, ctx, mainctx));
            }
 
        });
 
        args = [value, ...args];
 
        // find value converter
        const valueConverterExist = ContainerValueConverters.findConverter(ast.value);
        let _class: any;
 
        // if it exist, then we execute it
        if (valueConverterExist) {
            _class = ContainerClasses.get(valueConverterExist);
            try {
                result = fromView ? _class.fromView.apply(ctx, args) : _class.toView.apply(ctx, args);
            } catch (e) {
                console.warn('value converter failed:' + ast.value);
            }
        } else {
            console.warn('value converter does not exist:' + ast.value);
        }
 
        return result;
    };
 
 
 
    /**
     * evaluate ast
     *
     */
    public static evaluateConverterFromViewOnly = function (astInput: any, value: any, ctx: any, mainctx: any) {
 
        // if more then its
        const astArray = Array.isArray(astInput) ? astInput : [astInput];
        const returnArray: any[] = [];
        fromView = true;
        let valueConverted = false;
 
        for (let i = 0; i < astArray.length; i++) {
 
            let result;
            switch (true) {
                case astArray[i].arity === 'valueConverter':
                    // resolve args
                    valueConverted = true;
                    result = Traverser.traverseOnlyValueConverter(value, astArray[i], ctx, mainctx);
                    break;
            }
            if (result) {
                returnArray.push(result);
            }
 
        }
 
        if (returnArray.length > 1) {
            return returnArray.join('');
        } else {
            if (valueConverted) {
                return returnArray[0];
            } else {
                return value;
            }
        }
 
    };
 
 
 
    /**
     * gets the behaviors from ast
     *
     */
    public static getBehaviorFunctions = function (ast: any) {
        const astArray = Array.isArray(ast) ? ast : [ast];
        const returnArray: any[] = [];
 
        for (let i = 0; i < astArray.length; i++) {
 
            let result;
            switch (true) {
                case astArray[i].arity === 'behavior':
                    result = {
                        name: astArray[i].value,
                        args: astArray[i].args.map((a: any) => a.value)
                    };
                    // this will be used outside this scope for now
                    break;
            }
            if (result) {
                returnArray.push(result);
            }
 
 
        }
 
        return returnArray;
 
    };
 
};
 
 
// default for getting result
export const traverseAST = function (ast: any, ctx: IBindingContext) {
    return Traverser.evaluate(ast, ctx);
};
 
// used for getting valueConverter
export const valueConvert = function (ast: any, ctx: IBindingContext, value: any) {
    return Traverser.evaluateConverterFromViewOnly(ast, value, ctx, ctx);
};
 
// return behavior
export const getBehavior = function (ast: any) {
    return Traverser.getBehaviorFunctions(ast);
};