Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2c87d0

Browse files
committed
add support for the new assign expression in TypeScript 4
1 parent 2f34990 commit d2c87d0

5 files changed

Lines changed: 54 additions & 3 deletions

File tree

javascript/extractor/src/com/semmle/js/extractor/ExprKinds.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public class ExprKinds {
7777
binOpKinds.put("**", 87);
7878
binOpKinds.put("**=", 88);
7979
binOpKinds.put("??", 107);
80+
binOpKinds.put("&&=", 116);
81+
binOpKinds.put("||=", 117);
82+
binOpKinds.put("??=", 118);
8083
}
8184

8285
private static final Map<String, Integer> unOpKinds = new LinkedHashMap<String, Integer>();

javascript/ql/src/semmle/javascript/Expr.qll

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ class AssignExpr extends @assignexpr, Assignment {
18511851
private class TCompoundAssignExpr =
18521852
@assignaddexpr or @assignsubexpr or @assignmulexpr or @assigndivexpr or @assignmodexpr or
18531853
@assignexpexpr or @assignlshiftexpr or @assignrshiftexpr or @assignurshiftexpr or
1854-
@assignorexpr or @assignxorexpr or @assignandexpr;
1854+
@assignorexpr or @assignxorexpr or @assignandexpr or @assignlogandexpr or @assignlogorexpr or @assignnullishcoalescingexpr;
18551855

18561856
/**
18571857
* A compound assign expression.
@@ -1997,6 +1997,39 @@ class AssignXOrExpr extends @assignxorexpr, CompoundAssignExpr { }
19971997
*/
19981998
class AssignAndExpr extends @assignandexpr, CompoundAssignExpr { }
19991999

2000+
/**
2001+
* A logical-'or'-assign expression.
2002+
*
2003+
* Example:
2004+
*
2005+
* ```
2006+
* x ||= y
2007+
* ```
2008+
*/
2009+
class AssignLogOrExpr extends @assignlogandexpr, CompoundAssignExpr { }
2010+
2011+
/**
2012+
* A logical-'and'-assign expression.
2013+
*
2014+
* Example:
2015+
*
2016+
* ```
2017+
* x &&= y
2018+
* ```
2019+
*/
2020+
class AssignLogAndExpr extends @assignlogorexpr, CompoundAssignExpr { }
2021+
2022+
/**
2023+
* A 'nullish-coalescing'-assign expression.
2024+
*
2025+
* Example:
2026+
*
2027+
* ```
2028+
* x ??= y
2029+
* ```
2030+
*/
2031+
class AssignNullishCoalescingExpr extends @assignnullishcoalescingexpr, CompoundAssignExpr { }
2032+
20002033
/**
20012034
* An update expression, that is, an increment or decrement expression.
20022035
*

javascript/ql/src/semmlecode.javascript.dbscheme

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ case @expr.kind of
351351
| 113 = @e4x_xml_dynamic_qualident
352352
| 114 = @e4x_xml_dotdotexpr
353353
| 115 = @importmetaexpr
354+
| 116 = @assignlogandexpr
355+
| 117 = @assignlogorexpr
356+
| 118 = @assignnullishcoalescingexpr
354357
;
355358

356359
@varaccess = @proper_varaccess | @export_varaccess;
@@ -372,7 +375,7 @@ case @expr.kind of
372375

373376
@binaryexpr = @comparison | @lshiftexpr | @rshiftexpr | @urshiftexpr | @addexpr | @subexpr | @mulexpr | @divexpr | @modexpr | @expexpr | @bitorexpr | @xorexpr | @bitandexpr | @inexpr | @instanceofexpr | @logandexpr | @logorexpr | @nullishcoalescingexpr;
374377

375-
@assignment = @assignexpr | @assignaddexpr | @assignsubexpr | @assignmulexpr | @assigndivexpr | @assignmodexpr | @assignexpexpr | @assignlshiftexpr | @assignrshiftexpr | @assignurshiftexpr | @assignorexpr | @assignxorexpr | @assignandexpr;
378+
@assignment = @assignexpr | @assignaddexpr | @assignsubexpr | @assignmulexpr | @assigndivexpr | @assignmodexpr | @assignexpexpr | @assignlshiftexpr | @assignrshiftexpr | @assignurshiftexpr | @assignorexpr | @assignxorexpr | @assignandexpr | @assignlogandexpr | @assignlogorexpr | @assignnullishcoalescingexpr;
376379

377380
@updateexpr = @preincexpr | @postincexpr | @predecexpr | @postdecexpr;
378381

javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tests.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ test_VariableTypes
113113
| tst.ts:164:56:164:59 | arr2 | arr2 | tst.ts:164:62:164:62 | U |
114114
| tst.ts:169:31:169:31 | x | x | tst.ts:169:34:169:64 | [first: ... number] |
115115
| tst.ts:180:5:180:7 | foo | foo | tst.ts:180:10:180:21 | StrStrNumNum |
116+
| tst.ts:184:7:184:8 | a1 | a1 | tst.ts:184:12:184:17 | number |
117+
| tst.ts:185:7:185:8 | a2 | a2 | tst.ts:185:12:185:17 | number |
118+
| tst.ts:186:7:186:8 | a3 | a3 | tst.ts:186:12:186:17 | number |
116119
test_QualifiedTypeAccess
117120
| tst.ts:63:19:63:21 | N.I | tst.ts:63:19:63:19 | N | tst.ts:63:21:63:21 | I |
118121
| tst.ts:64:20:64:24 | N.M.I | tst.ts:64:20:64:22 | N.M | tst.ts:64:24:64:24 | I |

javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tst.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,13 @@ type Numbers = [number, number];
177177

178178
// [string, string, number, number]
179179
type StrStrNumNum = [...Strings, ...Numbers];
180-
var foo: StrStrNumNum;
180+
var foo: StrStrNumNum;
181+
182+
// Short-Circuiting Assignment Operators
183+
function shortAssignment() {
184+
let a1 : number = parseInt("foo");
185+
let a2 : number = parseInt("bar");
186+
let a3 : number = a1 ||= a2;
187+
let a4 = a2 &&= a3;
188+
let a5 = a3 ??= a4;
189+
}

0 commit comments

Comments
 (0)