Skip to content

Adding capability for ternary expression as a separate scope #129

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

Merged
merged 3 commits into from
Jun 8, 2016
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
34 changes: 34 additions & 0 deletions TypeScript.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,45 @@ repository:
'1': { name: variable.ts }
end: (?=$|[;,])
patterns:
- include: '#ternary-expression'
- include: '#type-annotation'
- include: '#string'
- include: '#comment'
- include: '#expression'

ternary-expression:
begin: (?=\?)
end: (?=$|[;,])
patterns:
- include: '#ternary-operator'
- include: '#ternary-expression-type'

ternary-operator:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are ternary expressions, not operators

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make that change. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

('?' and ':') together are known as the ternary operator, not sure if we should call it expression or operator. Also note that we end at ':', which can be followed by an expression (since expressions can exist anywhere.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe just to end there? I know an expression can exist anywhere, but not any production can come after the ":". For example, is this now parsed as a code block (with a labeled statement) after the ":" instead of an expression:

var x = true ? {a: 42} : {label: "use strict"};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it is not safe to end here since even in other scenarios like the following, highlighting shows no issues, though it should.

var x = true ? {a: 42} : {b: 43};
var y = true ? {a: 42} : let var2 = 8;

I will add a bigger scope called ternary-expression that only allows this ternary-operator followed by an expression, and see if that works. Thanks.

begin: (\?)
end: (:)
patterns:
- include: '#ternary-expression-type'

ternary-expression-type:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document (in comments) exactly what this is supposed to capture? For instance, why do you need types covered here?

name: meta.expression.ts
patterns:
- include: '#string'
- include: '#regex'
- include: '#template'
- include: '#comment'
- include: '#literal'
- include: '#paren-expression'
- include: '#ternary-expression'
- include: '#import-operator'
- include: '#expression-operator'
- include: '#imply-operator'
- include: '#relational-operator'
- include: '#arithmetic-operator'
- include: '#logic-operator'
- include: '#assignment-operator'
- include: '#type-primitive'
- include: '#function-call'

control-statement:
name: keyword.control.ts
match: (?<!\.)\b(break|catch|continue|debugger|declare|do|else|finally|for|if|return|switch|throw|try|while|with|super|case|default)\b
Expand Down
108 changes: 108 additions & 0 deletions TypeScript.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,110 @@
</dict>
</array>
</dict>
<key>ternary-expression</key>
<dict>
<key>begin</key>
<string>(?=\?)</string>
<key>end</key>
<string>(?=$|[;,])</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#ternary-operator</string>
</dict>
<dict>
<key>include</key>
<string>#ternary-expression-type</string>
</dict>
</array>
</dict>
<key>ternary-expression-type</key>
<dict>
<key>name</key>
<string>meta.expression.ts</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#string</string>
</dict>
<dict>
<key>include</key>
<string>#regex</string>
</dict>
<dict>
<key>include</key>
<string>#template</string>
</dict>
<dict>
<key>include</key>
<string>#comment</string>
</dict>
<dict>
<key>include</key>
<string>#literal</string>
</dict>
<dict>
<key>include</key>
<string>#paren-expression</string>
</dict>
<dict>
<key>include</key>
<string>#ternary-expression</string>
</dict>
<dict>
<key>include</key>
<string>#import-operator</string>
</dict>
<dict>
<key>include</key>
<string>#expression-operator</string>
</dict>
<dict>
<key>include</key>
<string>#imply-operator</string>
</dict>
<dict>
<key>include</key>
<string>#relational-operator</string>
</dict>
<dict>
<key>include</key>
<string>#arithmetic-operator</string>
</dict>
<dict>
<key>include</key>
<string>#logic-operator</string>
</dict>
<dict>
<key>include</key>
<string>#assignment-operator</string>
</dict>
<dict>
<key>include</key>
<string>#type-primitive</string>
</dict>
<dict>
<key>include</key>
<string>#function-call</string>
</dict>
</array>
</dict>
<key>ternary-operator</key>
<dict>
<key>begin</key>
<string>(\?)</string>
<key>end</key>
<string>(:)</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#ternary-expression-type</string>
</dict>
</array>
</dict>
<key>this-literal</key>
<dict>
<key>match</key>
Expand Down Expand Up @@ -1839,6 +1943,10 @@
<string>meta.var-single-variable.expr.ts</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#ternary-expression</string>
</dict>
<dict>
<key>include</key>
<string>#type-annotation</string>
Expand Down
34 changes: 34 additions & 0 deletions TypeScriptReact.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,45 @@ repository:
'1': { name: variable.tsx }
end: (?=$|[;,])
patterns:
- include: '#ternary-expression'
- include: '#type-annotation'
- include: '#string'
- include: '#comment'
- include: '#expression'

ternary-expression:
begin: (?=\?)
end: (?=$|[;,])
patterns:
- include: '#ternary-operator'
- include: '#ternary-expression-type'

ternary-operator:
begin: (\?)
end: (:)
patterns:
- include: '#ternary-expression-type'

ternary-expression-type:
name: meta.expression.tsx
patterns:
- include: '#string'
- include: '#regex'
- include: '#template'
- include: '#comment'
- include: '#literal'
- include: '#paren-expression'
- include: '#ternary-expression'
- include: '#import-operator'
- include: '#expression-operator'
- include: '#imply-operator'
- include: '#relational-operator'
- include: '#arithmetic-operator'
- include: '#logic-operator'
- include: '#assignment-operator'
- include: '#type-primitive'
- include: '#function-call'

control-statement:
name: keyword.control.tsx
match: (?<!\.)\b(break|catch|continue|debugger|declare|do|else|finally|for|if|return|switch|throw|try|while|with|super|case|default)\b
Expand Down
108 changes: 108 additions & 0 deletions TypeScriptReact.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,110 @@
</dict>
</array>
</dict>
<key>ternary-expression</key>
<dict>
<key>begin</key>
<string>(?=\?)</string>
<key>end</key>
<string>(?=$|[;,])</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#ternary-operator</string>
</dict>
<dict>
<key>include</key>
<string>#ternary-expression-type</string>
</dict>
</array>
</dict>
<key>ternary-expression-type</key>
<dict>
<key>name</key>
<string>meta.expression.tsx</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#string</string>
</dict>
<dict>
<key>include</key>
<string>#regex</string>
</dict>
<dict>
<key>include</key>
<string>#template</string>
</dict>
<dict>
<key>include</key>
<string>#comment</string>
</dict>
<dict>
<key>include</key>
<string>#literal</string>
</dict>
<dict>
<key>include</key>
<string>#paren-expression</string>
</dict>
<dict>
<key>include</key>
<string>#ternary-expression</string>
</dict>
<dict>
<key>include</key>
<string>#import-operator</string>
</dict>
<dict>
<key>include</key>
<string>#expression-operator</string>
</dict>
<dict>
<key>include</key>
<string>#imply-operator</string>
</dict>
<dict>
<key>include</key>
<string>#relational-operator</string>
</dict>
<dict>
<key>include</key>
<string>#arithmetic-operator</string>
</dict>
<dict>
<key>include</key>
<string>#logic-operator</string>
</dict>
<dict>
<key>include</key>
<string>#assignment-operator</string>
</dict>
<dict>
<key>include</key>
<string>#type-primitive</string>
</dict>
<dict>
<key>include</key>
<string>#function-call</string>
</dict>
</array>
</dict>
<key>ternary-operator</key>
<dict>
<key>begin</key>
<string>(\?)</string>
<key>end</key>
<string>(:)</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#ternary-expression-type</string>
</dict>
</array>
</dict>
<key>this-literal</key>
<dict>
<key>match</key>
Expand Down Expand Up @@ -2205,6 +2309,10 @@
<string>meta.var-single-variable.expr.tsx</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#ternary-expression</string>
</dict>
<dict>
<key>include</key>
<string>#type-annotation</string>
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/Issue124.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[6, 9]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts constant.language.boolean.ts
[6, 16]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts constant.language.boolean.ts
[6, 23]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts constant.language.boolean.ts
[7, 9]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts constant.language.boolean.ts
[7, 16]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts constant.language.boolean.ts
[7, 23]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.template.ts string.template.ts
[7, 24]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.template.ts string.template.ts
[7, 32]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.template.ts template.element.ts keyword.others.ts
[8, 9]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts constant.language.boolean.ts
[8, 16]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.template.ts string.template.ts
[8, 17]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.template.ts string.template.ts
[8, 26]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.template.ts string.template.ts
[8, 27]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.template.ts string.template.ts
[10, 5]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts variable.ts
[10, 14]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts
[10, 16]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts
[10, 18]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts meta.brace.paren.ts
[10, 35]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts constant.language.boolean.ts
[12, 5]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts variable.ts
[12, 15]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts
[12, 20]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts keyword.operator.comparison.ts
[12, 24]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts string.single.ts
[12, 30]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts
[12, 32]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts string.single.ts
[12, 40]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts
[12, 42]: source.ts meta.var.expr.ts meta.var-single-variable.expr.ts string.single.ts
[15, 2]: source.ts comment.block.ts
16 changes: 16 additions & 0 deletions tests/cases/Issue124.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Solving Issue #124, #80, #81
Adding capability of ternary strings.
*/

let a = ^^true ? ^^true : ^^true;
let b = ^^true ? ^^true : ^^`^^this is ^^${} highlighted`
let c = ^^true ? ^^`^^hello` : ^^`^^this ${DEPENDENCY_SEPARATOR}${moduleName} is highlighted`;

var ^^newVar = ^^a ^^? ^^(b == `hello`) : ^^true;

var ^^debArch = ^^arch ^^=== ^^'x64' ^^? ^^'amd64' ^^: ^^'i386';

/*
^^Comment
*/