Merge pull request #37727 from Kingwl/logical_assignment

Add logical assignment operator
This commit is contained in:
Daniel Rosenwasser
2020-06-09 14:44:08 -07:00
committed by GitHub
108 changed files with 6398 additions and 594 deletions

View File

@@ -0,0 +1,27 @@
// @strict: true
// @target: esnext, es2020, es2015
declare let a: string | undefined
declare let b: string | undefined
declare let c: string | undefined
declare let d: number | undefined
declare let e: number | undefined
declare let f: number | undefined
declare let g: 0 | 1 | 42
declare let h: 0 | 1 | 42
declare let i: 0 | 1 | 42
a &&= "foo"
b ||= "foo"
c ??= "foo"
d &&= 42
e ||= 42
f ??= 42
g &&= 42
h ||= 42
i ??= 42

View File

@@ -0,0 +1,29 @@
// @strict: true
// @target: esnext, es2020, es2015
interface A {
foo: {
bar(): {
baz: 0 | 1 | 42 | undefined | ''
}
baz: 0 | 1 | 42 | undefined | ''
}
baz: 0 | 1 | 42 | undefined | ''
}
declare const result: A
declare const a: A
declare const b: A
declare const c: A
a.baz &&= result.baz
b.baz ||= result.baz
c.baz ??= result.baz
a.foo["baz"] &&= result.foo.baz
b.foo["baz"] ||= result.foo.baz
c.foo["baz"] ??= result.foo.baz
a.foo.bar().baz &&= result.foo.bar().baz
b.foo.bar().baz ||= result.foo.bar().baz
c.foo.bar().baz ??= result.foo.bar().baz

View File

@@ -0,0 +1,15 @@
// @strict: true
// @target: esnext, es2020, es2015
interface A {
baz: 0 | 1 | 42 | undefined | ''
}
declare const result: A;
declare const a: A;
declare const b: A;
declare const c: A;
(a.baz) &&= result.baz;
(b.baz) ||= result.baz;
(c.baz) ??= result.baz;

View File

@@ -0,0 +1,52 @@
// @strict: true
// @target: esnext, es2020, es2015
// @allowUnreachableCode: false
function foo1(results: number[] | undefined) {
(results ||= []).push(100);
}
function foo2(results: number[] | undefined) {
(results ??= []).push(100);
}
function foo3(results: number[] | undefined) {
results ||= [];
results.push(100);
}
function foo4(results: number[] | undefined) {
results ??= [];
results.push(100);
}
interface ThingWithOriginal {
name: string;
original?: ThingWithOriginal
}
declare const v: number
function doSomethingWithAlias(thing: ThingWithOriginal | undefined, defaultValue: ThingWithOriginal | undefined) {
if (v === 1) {
if (thing &&= thing.original) {
thing.name;
}
}
else if (v === 2) {
if (thing &&= defaultValue) {
thing.name;
defaultValue.name
}
}
else if (v === 3) {
if (thing ||= defaultValue) {
thing.name;
defaultValue.name;
}
}
else {
if (thing ??= defaultValue) {
thing.name;
defaultValue.name;
}
}
}

View File

@@ -0,0 +1,32 @@
// @strict: true
// @target: esnext, es2020, es2015
function foo1 (f?: (a: number) => void) {
f ??= (a => a)
f(42)
}
function foo2 (f?: (a: number) => void) {
f ||= (a => a)
f(42)
}
function foo3 (f?: (a: number) => void) {
f &&= (a => a)
f(42)
}
function bar1 (f?: (a: number) => void) {
f ??= (f.toString(), (a => a))
f(42)
}
function bar2 (f?: (a: number) => void) {
f ||= (f.toString(), (a => a))
f(42)
}
function bar3 (f?: (a: number) => void) {
f &&= (f.toString(), (a => a))
f(42)
}

View File

@@ -0,0 +1,14 @@
// @strict: true
// @target: esnext, es2020, es2015
function foo1(results: number[] | undefined, results1: number[] | undefined) {
(results ||= (results1 ||= [])).push(100);
}
function foo2(results: number[] | undefined, results1: number[] | undefined) {
(results ??= (results1 ??= [])).push(100);
}
function foo3(results: number[] | undefined, results1: number[] | undefined) {
(results &&= (results1 &&= [])).push(100);
}

View File

@@ -0,0 +1,14 @@
// @strict: true
// @target: esnext, es2020, es2015
function foo1(results: number[] | undefined, results1: number[] | undefined) {
(results ||= results1 ||= []).push(100);
}
function foo2(results: number[] | undefined, results1: number[] | undefined) {
(results ??= results1 ??= []).push(100);
}
function foo3(results: number[] | undefined, results1: number[] | undefined) {
(results &&= results1 &&= []).push(100);
}

View File

@@ -0,0 +1,16 @@
// @strict: true
// @target: esnext, es2020, es2015
declare const bar: { value?: number[] } | undefined
function foo1(results: number[] | undefined) {
(results ||= bar?.value ?? []).push(100);
}
function foo2(results: number[] | undefined) {
(results ??= bar?.value ?? []).push(100);
}
function foo3(results: number[] | undefined) {
(results &&= bar?.value ?? []).push(100);
}