-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add more tests for nullish coalescing option
- Loading branch information
1 parent
8cafc8d
commit 30b050a
Showing
1 changed file
with
171 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,183 @@ | ||
import check from './support/check'; | ||
import baseCheck from './support/check'; | ||
|
||
function checkNullishCoalescing(source: string, expected: string): void { | ||
check(source, expected, { options: { nullishCoalescing: true } }); | ||
function check(source: string, expected: string): void { | ||
baseCheck(source, expected, { options: { nullishCoalescing: true } }); | ||
} | ||
|
||
describe('with nullish coalescing enabled', () => { | ||
it('translates binary existence to nullish coalescing', () => { | ||
checkNullishCoalescing( | ||
describe('binary existence with nullish coalescing enabled', () => { | ||
describe('as a statement', () => { | ||
it('handles a global as the LHS', () => { | ||
check( | ||
` | ||
a ? b | ||
`, | ||
` | ||
if (typeof a === 'undefined' || a === null) { b; } | ||
` | ||
a = 1 | ||
b = a ? 2 | ||
`, | ||
); | ||
}); | ||
|
||
it('handles a safe-to-repeat member expression as the LHS', () => { | ||
check( | ||
` | ||
a.b ? a | ||
`, | ||
` | ||
if (a.b == null) { a; } | ||
` | ||
); | ||
}); | ||
|
||
it('handlesa `this` accesses as the LHS', () => { | ||
check( | ||
` | ||
@a ? @b | ||
`, | ||
` | ||
if (this.a == null) { this.b; } | ||
` | ||
const a = 1; | ||
const b = a ?? 2; | ||
); | ||
}); | ||
|
||
it('handles a `this` access on the RHS', () => { | ||
check( | ||
` | ||
a ? @b | ||
`, | ||
` | ||
if (typeof a === 'undefined' || a === null) { this.b; } | ||
` | ||
); | ||
}); | ||
|
||
it('handles an unsafe-to-repeat member expression as the LHS', () => { | ||
check( | ||
` | ||
a() ? b | ||
`, | ||
` | ||
if (a() == null) { b; } | ||
` | ||
); | ||
}); | ||
|
||
it('handles multiline operator with escaped newline', () => { | ||
check( | ||
` | ||
a ? \\ | ||
b | ||
`, | ||
` | ||
if (typeof a === 'undefined' || a === null) { b; } | ||
` | ||
); | ||
); | ||
}); | ||
}); | ||
|
||
it('falls back to a check if the LHS may be undeclared', () => { | ||
checkNullishCoalescing( | ||
describe('as an expression', () => { | ||
it('translates to nullish coalescing', () => { | ||
check( | ||
` | ||
a = 1 | ||
b = a ? 2 | ||
`, | ||
` | ||
const a = 1; | ||
const b = a ?? 2; | ||
` | ||
b = a ? 2 | ||
`, | ||
); | ||
}); | ||
|
||
it('falls back to a check if the LHS may be undeclared', () => { | ||
check( | ||
` | ||
b = a ? 2 | ||
`, | ||
` | ||
const b = typeof a !== 'undefined' && a !== null ? a : 2; | ||
` | ||
const b = typeof a !== 'undefined' && a !== null ? a : 2; | ||
); | ||
}); | ||
|
||
it('handles a global as the LHS', () => { | ||
check( | ||
` | ||
x = (a ? b) | ||
`, | ||
` | ||
const x = (typeof a !== 'undefined' && a !== null ? a : b); | ||
` | ||
); | ||
}) | ||
); | ||
}); | ||
|
||
it('handles a local as the LHS', () => { | ||
check( | ||
` | ||
a = 1 | ||
x = (a ? b) | ||
`, | ||
` | ||
const a = 1; | ||
const x = (a ?? b); | ||
` | ||
); | ||
}); | ||
|
||
it('handles a safe-to-repeat member expression as the LHS', () => { | ||
check( | ||
` | ||
x = (a.b ? a) | ||
`, | ||
` | ||
const x = (a.b ?? a); | ||
` | ||
); | ||
}); | ||
|
||
it('handles a this-access as the LHS', () => { | ||
check( | ||
` | ||
x = (@a.b ? c) | ||
`, | ||
` | ||
const x = (this.a.b ?? c); | ||
` | ||
); | ||
}); | ||
|
||
it('handles an unsafe-to-repeat member expression as the LHS', () => { | ||
check( | ||
` | ||
x = (a() ? b) | ||
`, | ||
` | ||
const x = (a() ?? b); | ||
` | ||
); | ||
}); | ||
|
||
it('works when used as a negated condition', () => { | ||
check( | ||
` | ||
unless 1 ? 2 then 3 | ||
`, | ||
` | ||
if (!(1 ?? 2)) { 3; } | ||
` | ||
); | ||
}); | ||
|
||
it('works when combined with plus', () => { | ||
check( | ||
` | ||
y = 1 | ||
x = 1 + (y ? 0) | ||
`, | ||
` | ||
const y = 1; | ||
const x = 1 + (y ?? 0); | ||
` | ||
); | ||
}); | ||
}); | ||
}); |