-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
As operator #3201
As operator #3201
Changes from 6 commits
20f5987
634cc0e
8f78848
8b03911
3aa1caa
e1e577d
1e6bd1c
d5bf689
c1e02c4
d127775
9e6e265
225e695
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,9 @@ module ts { | |
return visitNode(cbNode, (<BinaryExpression>node).left) || | ||
visitNode(cbNode, (<BinaryExpression>node).operatorToken) || | ||
visitNode(cbNode, (<BinaryExpression>node).right); | ||
case SyntaxKind.AsExpression: | ||
return visitNode(cbNode, (<AsExpression>node).expression) || | ||
visitNode(cbNode, (<AsExpression>node).type); | ||
case SyntaxKind.ConditionalExpression: | ||
return visitNode(cbNode, (<ConditionalExpression>node).condition) || | ||
visitNode(cbNode, (<ConditionalExpression>node).questionToken) || | ||
|
@@ -2818,7 +2821,12 @@ module ts { | |
break; | ||
} | ||
|
||
leftOperand = makeBinaryExpression(leftOperand, parseTokenNode(), parseBinaryExpressionOrHigher(newPrecedence)); | ||
if (token === SyntaxKind.AsKeyword) { | ||
parseTokenNode(); | ||
leftOperand = makeAsExpression(leftOperand, parseType()); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
leftOperand = makeBinaryExpression(leftOperand, parseTokenNode(), parseBinaryExpressionOrHigher(newPrecedence)); | ||
} | ||
} | ||
|
||
return leftOperand; | ||
|
@@ -2855,6 +2863,7 @@ module ts { | |
case SyntaxKind.GreaterThanEqualsToken: | ||
case SyntaxKind.InstanceOfKeyword: | ||
case SyntaxKind.InKeyword: | ||
case SyntaxKind.AsKeyword: | ||
return 7; | ||
case SyntaxKind.LessThanLessThanToken: | ||
case SyntaxKind.GreaterThanGreaterThanToken: | ||
|
@@ -2882,6 +2891,13 @@ module ts { | |
return finishNode(node); | ||
} | ||
|
||
function makeAsExpression(left: Expression, right: TypeNode): AsExpression { | ||
let node = <AsExpression>createNode(SyntaxKind.AsExpression, left.pos); | ||
node.expression = left; | ||
node.type = right; | ||
return finishNode(node); | ||
} | ||
|
||
function parsePrefixUnaryExpression() { | ||
let node = <PrefixUnaryExpression>createNode(SyntaxKind.PrefixUnaryExpression); | ||
node.operator = token; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,6 +207,7 @@ module ts { | |
ClassExpression, | ||
OmittedExpression, | ||
ExpressionWithTypeArguments, | ||
AsExpression, | ||
// Misc | ||
TemplateSpan, | ||
SemicolonClassElement, | ||
|
@@ -404,6 +405,8 @@ module ts { | |
|
||
export type DeclarationName = Identifier | LiteralExpression | ComputedPropertyName | BindingPattern; | ||
|
||
export type AssertionExpression = TypeAssertion | AsExpression; | ||
|
||
export interface Declaration extends Node { | ||
_declarationBrand: any; | ||
name?: DeclarationName; | ||
|
@@ -669,6 +672,12 @@ module ts { | |
right: Expression; | ||
} | ||
|
||
export interface AsExpression extends Expression { | ||
expression: Expression; | ||
asToken: Node; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you need this token? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think we need to store the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove. |
||
type: TypeNode; | ||
} | ||
|
||
export interface ConditionalExpression extends Expression { | ||
condition: Expression; | ||
questionToken: Node; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//// [asOperator1.ts] | ||
var as = 43; | ||
var x = undefined as number; | ||
var y = (null as string).length; | ||
var z = Date as any as string; | ||
|
||
|
||
//// [asOperator1.js] | ||
var as = 43; | ||
var x = undefined; | ||
var y = null.length; | ||
var z = Date; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
=== tests/cases/conformance/expressions/asOperator/asOperator1.ts === | ||
var as = 43; | ||
>as : Symbol(as, Decl(asOperator1.ts, 0, 3)) | ||
|
||
var x = undefined as number; | ||
>x : Symbol(x, Decl(asOperator1.ts, 1, 3)) | ||
|
||
var y = (null as string).length; | ||
>y : Symbol(y, Decl(asOperator1.ts, 2, 3)) | ||
>(null as string).length : Symbol(String.length, Decl(lib.d.ts, 414, 19)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19)) | ||
|
||
var z = Date as any as string; | ||
>z : Symbol(z, Decl(asOperator1.ts, 3, 3)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
=== tests/cases/conformance/expressions/asOperator/asOperator1.ts === | ||
var as = 43; | ||
>as : number | ||
>43 : number | ||
|
||
var x = undefined as number; | ||
>x : number | ||
>undefined : any | ||
|
||
var y = (null as string).length; | ||
>y : number | ||
>(null as string).length : number | ||
>(null as string) : string | ||
>null : null | ||
>length : number | ||
|
||
var z = Date as any as string; | ||
>z : string | ||
>Date : any | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
tests/cases/conformance/expressions/asOperator/asOperator2.ts(1,9): error TS2352: Neither type 'number' nor type 'string' is assignable to the other. | ||
|
||
|
||
==== tests/cases/conformance/expressions/asOperator/asOperator2.ts (1 errors) ==== | ||
var x = 23 as string; | ||
~~~~~~~~~~~~ | ||
!!! error TS2352: Neither type 'number' nor type 'string' is assignable to the other. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//// [asOperator2.ts] | ||
var x = 23 as string; | ||
|
||
|
||
//// [asOperator2.js] | ||
var x = 23; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//// [asOperator3.ts] | ||
declare function tag(...x: any[]): any; | ||
|
||
var a = `${123 + 456 as number}`; | ||
var b = `leading ${123 + 456 as number}`; | ||
var c = `${123 + 456 as number} trailing`; | ||
var d = `Hello ${123} World` as string; | ||
var e = `Hello` as string; | ||
var f = 1 + `${1} end of string` as string; | ||
var g = tag `Hello ${123} World` as string; | ||
var h = tag `Hello` as string; | ||
|
||
//// [asOperator3.js] | ||
var a = "" + 123 + 456; | ||
var b = "leading " + 123 + 456; | ||
var c = 123 + 456 + " trailing"; | ||
var d = "Hello " + 123 + " World"; | ||
var e = "Hello"; | ||
var f = 1 + (1 + " end of string"); | ||
var g = (_a = ["Hello ", " World"], _a.raw = ["Hello ", " World"], tag(_a, 123)); | ||
var h = (_b = ["Hello"], _b.raw = ["Hello"], tag(_b)); | ||
var _a, _b; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
=== tests/cases/conformance/expressions/asOperator/asOperator3.ts === | ||
declare function tag(...x: any[]): any; | ||
>tag : Symbol(tag, Decl(asOperator3.ts, 0, 0)) | ||
>x : Symbol(x, Decl(asOperator3.ts, 0, 21)) | ||
|
||
var a = `${123 + 456 as number}`; | ||
>a : Symbol(a, Decl(asOperator3.ts, 2, 3)) | ||
|
||
var b = `leading ${123 + 456 as number}`; | ||
>b : Symbol(b, Decl(asOperator3.ts, 3, 3)) | ||
|
||
var c = `${123 + 456 as number} trailing`; | ||
>c : Symbol(c, Decl(asOperator3.ts, 4, 3)) | ||
|
||
var d = `Hello ${123} World` as string; | ||
>d : Symbol(d, Decl(asOperator3.ts, 5, 3)) | ||
|
||
var e = `Hello` as string; | ||
>e : Symbol(e, Decl(asOperator3.ts, 6, 3)) | ||
|
||
var f = 1 + `${1} end of string` as string; | ||
>f : Symbol(f, Decl(asOperator3.ts, 7, 3)) | ||
|
||
var g = tag `Hello ${123} World` as string; | ||
>g : Symbol(g, Decl(asOperator3.ts, 8, 3)) | ||
>tag : Symbol(tag, Decl(asOperator3.ts, 0, 0)) | ||
|
||
var h = tag `Hello` as string; | ||
>h : Symbol(h, Decl(asOperator3.ts, 9, 3)) | ||
>tag : Symbol(tag, Decl(asOperator3.ts, 0, 0)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
=== tests/cases/conformance/expressions/asOperator/asOperator3.ts === | ||
declare function tag(...x: any[]): any; | ||
>tag : (...x: any[]) => any | ||
>x : any[] | ||
|
||
var a = `${123 + 456 as number}`; | ||
>a : string | ||
>`${123 + 456 as number}` : string | ||
>123 + 456 : number | ||
>123 : number | ||
>456 : number | ||
|
||
var b = `leading ${123 + 456 as number}`; | ||
>b : string | ||
>`leading ${123 + 456 as number}` : string | ||
>123 + 456 : number | ||
>123 : number | ||
>456 : number | ||
|
||
var c = `${123 + 456 as number} trailing`; | ||
>c : string | ||
>`${123 + 456 as number} trailing` : string | ||
>123 + 456 : number | ||
>123 : number | ||
>456 : number | ||
|
||
var d = `Hello ${123} World` as string; | ||
>d : string | ||
>`Hello ${123} World` : string | ||
>123 : number | ||
|
||
var e = `Hello` as string; | ||
>e : string | ||
>`Hello` : string | ||
|
||
var f = 1 + `${1} end of string` as string; | ||
>f : string | ||
>1 + `${1} end of string` : string | ||
>1 : number | ||
>`${1} end of string` : string | ||
>1 : number | ||
|
||
var g = tag `Hello ${123} World` as string; | ||
>g : string | ||
>tag `Hello ${123} World` : any | ||
>tag : (...x: any[]) => any | ||
>`Hello ${123} World` : string | ||
>123 : number | ||
|
||
var h = tag `Hello` as string; | ||
>h : string | ||
>tag `Hello` : any | ||
>tag : (...x: any[]) => any | ||
>`Hello` : string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var as = 43; | ||
var x = undefined as number; | ||
var y = (null as string).length; | ||
var z = Date as any as string; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var x = 23 as string; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
declare function tag(...x: any[]): any; | ||
|
||
var a = `${123 + 456 as number}`; | ||
var b = `leading ${123 + 456 as number}`; | ||
var c = `${123 + 456 as number} trailing`; | ||
var d = `Hello ${123} World` as string; | ||
var e = `Hello` as string; | ||
var f = 1 + `${1} end of string` as string; | ||
var g = tag `Hello ${123} World` as string; | ||
var h = tag `Hello` as string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to update parenthesizeForAccess as well.