tst-expression icon indicating copy to clipboard operation
tst-expression copied to clipboard

Expressions don't pass through when they are optional properties

Open avin-kavish opened this issue 3 years ago • 1 comments

class SomeA {
  foo(expr?: Expression<() => number>) {}

  bar(expr?: Expression<() => number>) {
    this.foo(expr)
  }
}

new SomeA().bar(() => 10)

foo method gets identifier as the expression. Without the question mark, works as expected.

avin-kavish avatar Aug 23 '22 14:08 avin-kavish

It works as expected. Can you create a repro?

EDIT: Hmm.. I'm wordering... You have both the tst-expression and the tst-reflect installed, right? Maybe they have some conflict.. /EDIT

What I tried:

import {
	Expression,
	assertExpression,
	assertArrowFunctionExpression,
	assertNumericLiteral
} from "tst-expression";

class SomeA {
	foo(expr?: Expression<() => number>) {
		if (!expr) {
			return;
		}
		
		assertExpression(expr);
		assertArrowFunctionExpression(expr.expression);
		
		console.log("# parameters:", expr.expression.parameters.length);
		
		assertNumericLiteral(expr.expression.body);
		console.log("number in body:", Number(expr.expression.body.text));
	}

	bar(expr?: Expression<() => number>) {
		this.foo(expr)
	}
}

new SomeA().bar(() => 10);

Transpiled code:

import {assertExpression, assertArrowFunctionExpression, assertNumericLiteral} from "tst-expression";

class SomeA {
    foo(expr) {
        if (!expr) {
            return;
        }
        assertExpression(expr);
        assertArrowFunctionExpression(expr.expression);
        console.log("# parameters:", expr.expression.parameters.length);
        assertNumericLiteral(expr.expression.body);
        console.log("number in body:", Number(expr.expression.body.text));
    }

    bar(expr) {
        this.foo(expr);
    }
}

new SomeA().bar({
    compiled: () => 10,
    context: {},
    expression: {
        "flags": 256,
        "kind": 212,
        "locals": {},
        "parameters": [],
        "body": {
            "flags": 0,
            "kind": 8,
            "text": "10",
            "numericLiteralFlags": 0
        },
        "equalsGreaterThanToken": {
            "flags": 0,
            "kind": 38
        },
        "endFlowNode": {"flags": 2}
    }
});

Hookyns avatar Aug 24 '22 18:08 Hookyns