Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate simple template expressions #53907

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29289,6 +29289,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.NumericLiteral:
case SyntaxKind.BigIntLiteral:
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.TemplateExpression:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
case SyntaxKind.NullKeyword:
Expand Down Expand Up @@ -37301,7 +37302,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
texts.push(span.literal.text);
types.push(isTypeAssignableTo(type, templateConstraintType) ? type : stringType);
}
return isConstContext(node) || isTemplateLiteralContext(node) || someType(getContextualType(node, /*contextFlags*/ undefined) || unknownType, isTemplateLiteralContextualType) ? getTemplateLiteralType(texts, types) : stringType;
if (isConstContext(node) || isTemplateLiteralContext(node) || someType(getContextualType(node, /*contextFlags*/ undefined) || unknownType, isTemplateLiteralContextualType)) {
return getTemplateLiteralType(texts, types);
}
const evaluated = node.parent.kind !== SyntaxKind.TaggedTemplateExpression && evaluateTemplateExpression(node);
return evaluated ? getFreshTypeOfLiteralType(getStringLiteralType(evaluated)) : stringType;
}

function isTemplateLiteralContextualType(type: Type): boolean {
Expand Down Expand Up @@ -43578,7 +43583,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return value;
}

function evaluate(expr: Expression, location: Declaration): string | number | undefined {
function evaluate(expr: Expression, location?: Declaration): string | number | undefined {
switch (expr.kind) {
case SyntaxKind.PrefixUnaryExpression:
const value = evaluate((expr as PrefixUnaryExpression).operand, location);
Expand Down Expand Up @@ -43635,11 +43640,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const symbol = resolveEntityName(expr, SymbolFlags.Value, /*ignoreErrors*/ true);
if (symbol) {
if (symbol.flags & SymbolFlags.EnumMember) {
return evaluateEnumMember(expr, symbol, location);
return location ? evaluateEnumMember(expr, symbol, location) : getEnumMemberValue(symbol.valueDeclaration as EnumMember);
}
if (isConstVariable(symbol)) {
const declaration = symbol.valueDeclaration as VariableDeclaration | undefined;
if (declaration && !declaration.type && declaration.initializer && declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location)) {
if (declaration && !declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) {
return evaluate(declaration.initializer, declaration);
}
}
Expand All @@ -43654,7 +43659,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const name = escapeLeadingUnderscores(((expr as ElementAccessExpression).argumentExpression as StringLiteralLike).text);
const member = rootSymbol.exports!.get(name);
if (member) {
return evaluateEnumMember(expr, member, location);
return location ? evaluateEnumMember(expr, member, location) : getEnumMemberValue(member.valueDeclaration as EnumMember);
}
}
}
Expand All @@ -43676,7 +43681,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getEnumMemberValue(declaration as EnumMember);
}

function evaluateTemplateExpression(expr: TemplateExpression, location: Declaration) {
function evaluateTemplateExpression(expr: TemplateExpression, location?: Declaration) {
let result = expr.head.text;
for (const span of expr.templateSpans) {
const value = evaluate(span.expression, location);
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asOperator3.types
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var c = `${123 + 456 as number} trailing`;
var d = `Hello ${123} World` as string;
>d : string
>`Hello ${123} World` as string : string
>`Hello ${123} World` : string
>`Hello ${123} World` : "Hello 123 World"
>123 : 123

var e = `Hello` as string;
Expand All @@ -43,7 +43,7 @@ var f = 1 + `${1} end of string` as string;
>1 + `${1} end of string` as string : string
>1 + `${1} end of string` : string
>1 : 1
>`${1} end of string` : string
>`${1} end of string` : "1 end of string"
>1 : 1

var g = tag `Hello ${123} World` as string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ class MyClass {
>`foo` : "foo"

this.property2 = `foo-${variable}`; // Causes an error
>this.property2 = `foo-${variable}` : string
>this.property2 = `foo-${variable}` : "foo-something"
>this.property2 : string
>this : this
>property2 : string
>`foo-${variable}` : string
>`foo-${variable}` : "foo-something"
>variable : "something"

const localProperty = `foo-${variable}`; // Correctly inferred as `string`
>localProperty : string
>`foo-${variable}` : string
>localProperty : "foo-something"
>`foo-${variable}` : "foo-something"
>variable : "something"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ class MyClass {
>`foo` : "foo"

this.property2 = `foo-${variable}`; // Causes an error
>this.property2 = `foo-${variable}` : string
>this.property2 = `foo-${variable}` : "foo-something"
>this.property2 : string
>this : this
>property2 : string
>`foo-${variable}` : string
>`foo-${variable}` : "foo-something"
>variable : "something"

const localProperty = `foo-${variable}`; // Correctly inferred as `string`
>localProperty : string
>`foo-${variable}` : string
>localProperty : "foo-something"
>`foo-${variable}` : "foo-something"
>variable : "something"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/compiler/discriminantUsingEvaluatableTemplateExpression.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/53888

type S = { d: "s"; cb: (x: string) => void };
>S : Symbol(S, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 0, 0))
>d : Symbol(d, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 2, 10))
>cb : Symbol(cb, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 2, 18))
>x : Symbol(x, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 2, 24))

type N = { d: "n"; cb: (x: number) => void };
>N : Symbol(N, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 2, 45))
>d : Symbol(d, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 3, 10))
>cb : Symbol(cb, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 3, 18))
>x : Symbol(x, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 3, 24))

declare function foo(foo: S | N): void;
>foo : Symbol(foo, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 3, 45))
>foo : Symbol(foo, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 5, 21))
>S : Symbol(S, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 0, 0))
>N : Symbol(N, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 2, 45))

foo({
>foo : Symbol(foo, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 3, 45))

d: `${"s"}`,
>d : Symbol(d, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 7, 5))

cb: (x) => {
>cb : Symbol(cb, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 8, 14))
>x : Symbol(x, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 9, 7))

x; // string
>x : Symbol(x, Decl(discriminantUsingEvaluatableTemplateExpression.ts, 9, 7))

},
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/discriminantUsingEvaluatableTemplateExpression.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/53888

type S = { d: "s"; cb: (x: string) => void };
>S : { d: "s"; cb: (x: string) => void; }
>d : "s"
>cb : (x: string) => void
>x : string

type N = { d: "n"; cb: (x: number) => void };
>N : { d: "n"; cb: (x: number) => void; }
>d : "n"
>cb : (x: number) => void
>x : number

declare function foo(foo: S | N): void;
>foo : (foo: S | N) => void
>foo : S | N

foo({
>foo({ d: `${"s"}`, cb: (x) => { x; // string },}) : void
>foo : (foo: S | N) => void
>{ d: `${"s"}`, cb: (x) => { x; // string },} : { d: "s"; cb: (x: string) => void; }

d: `${"s"}`,
>d : "s"
>`${"s"}` : "s"
>"s" : "s"

cb: (x) => {
>cb : (x: string) => void
>(x) => { x; // string } : (x: string) => void
>x : string

x; // string
>x : string

},
});

Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ enum T5 {

g = `1${"2"}3`,
>g : T5.c
>`1${"2"}3` : string
>`1${"2"}3` : "123"
>"2" : "2"

h = `1`.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,55 @@ var a = 1 ** `${ 3 }`;
>a : number
>1 ** `${ 3 }` : number
>1 : 1
>`${ 3 }` : string
>`${ 3 }` : "3"
>3 : 3

var b = 1 ** `2${ 3 }`;
>b : number
>1 ** `2${ 3 }` : number
>1 : 1
>`2${ 3 }` : string
>`2${ 3 }` : "23"
>3 : 3

var c = 1 ** `${ 3 }4`;
>c : number
>1 ** `${ 3 }4` : number
>1 : 1
>`${ 3 }4` : string
>`${ 3 }4` : "34"
>3 : 3

var d = 1 ** `2${ 3 }4`;
>d : number
>1 ** `2${ 3 }4` : number
>1 : 1
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3

var e = `${ 3 }` ** 5;
>e : number
>`${ 3 }` ** 5 : number
>`${ 3 }` : string
>`${ 3 }` : "3"
>3 : 3
>5 : 5

var f = `2${ 3 }` ** 5;
>f : number
>`2${ 3 }` ** 5 : number
>`2${ 3 }` : string
>`2${ 3 }` : "23"
>3 : 3
>5 : 5

var g = `${ 3 }4` ** 5;
>g : number
>`${ 3 }4` ** 5 : number
>`${ 3 }4` : string
>`${ 3 }4` : "34"
>3 : 3
>5 : 5

var h = `2${ 3 }4` ** 5;
>h : number
>`2${ 3 }4` ** 5 : number
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3
>5 : 5

Expand All @@ -62,25 +62,25 @@ var k = 10;
k **= `${ 3 }`;
>k **= `${ 3 }` : number
>k : number
>`${ 3 }` : string
>`${ 3 }` : "3"
>3 : 3

k **= `2${ 3 }`;
>k **= `2${ 3 }` : number
>k : number
>`2${ 3 }` : string
>`2${ 3 }` : "23"
>3 : 3

k **= `2${ 3 }4`;
>k **= `2${ 3 }4` : number
>k : number
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3

k **= `2${ 3 }4`;
>k **= `2${ 3 }4` : number
>k : number
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,55 @@ var a = 1 ** `${ 3 }`;
>a : number
>1 ** `${ 3 }` : number
>1 : 1
>`${ 3 }` : string
>`${ 3 }` : "3"
>3 : 3

var b = 1 ** `2${ 3 }`;
>b : number
>1 ** `2${ 3 }` : number
>1 : 1
>`2${ 3 }` : string
>`2${ 3 }` : "23"
>3 : 3

var c = 1 ** `${ 3 }4`;
>c : number
>1 ** `${ 3 }4` : number
>1 : 1
>`${ 3 }4` : string
>`${ 3 }4` : "34"
>3 : 3

var d = 1 ** `2${ 3 }4`;
>d : number
>1 ** `2${ 3 }4` : number
>1 : 1
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3

var e = `${ 3 }` ** 5;
>e : number
>`${ 3 }` ** 5 : number
>`${ 3 }` : string
>`${ 3 }` : "3"
>3 : 3
>5 : 5

var f = `2${ 3 }` ** 5;
>f : number
>`2${ 3 }` ** 5 : number
>`2${ 3 }` : string
>`2${ 3 }` : "23"
>3 : 3
>5 : 5

var g = `${ 3 }4` ** 5;
>g : number
>`${ 3 }4` ** 5 : number
>`${ 3 }4` : string
>`${ 3 }4` : "34"
>3 : 3
>5 : 5

var h = `2${ 3 }4` ** 5;
>h : number
>`2${ 3 }4` ** 5 : number
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3
>5 : 5

Expand All @@ -62,24 +62,24 @@ var k = 10;
k **= `${ 3 }`;
>k **= `${ 3 }` : number
>k : number
>`${ 3 }` : string
>`${ 3 }` : "3"
>3 : 3

k **= `2${ 3 }`;
>k **= `2${ 3 }` : number
>k : number
>`2${ 3 }` : string
>`2${ 3 }` : "23"
>3 : 3

k **= `2${ 3 }4`;
>k **= `2${ 3 }4` : number
>k : number
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3

kj **= `2${ 3 }4`;
>kj **= `2${ 3 }4` : number
>kj : any
>`2${ 3 }4` : string
>`2${ 3 }4` : "234"
>3 : 3

Loading