Skip to content

Commit

Permalink
feat: add more tests for nullish coalescing option
Browse files Browse the repository at this point in the history
  • Loading branch information
shaylew authored and eventualbuddha committed Jun 4, 2022
1 parent 8cafc8d commit 30b050a
Showing 1 changed file with 171 additions and 19 deletions.
190 changes: 171 additions & 19 deletions test/nullishCoalescing_test.ts
@@ -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);
`
);
});
});
});

0 comments on commit 30b050a

Please sign in to comment.