-
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 option to emit nullish coalescing operator
- Loading branch information
1 parent
c14d689
commit 8cafc8d
Showing
5 changed files
with
64 additions
and
1 deletion.
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
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
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
22 changes: 22 additions & 0 deletions
22
src/stages/main/patchers/NullishCoalescingExistsOpPatcher.ts
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { PatchOptions } from '../../../patchers/types'; | ||
import BinaryOpPatcher from './BinaryOpPatcher'; | ||
import ExistsOpPatcher from './ExistsOpPatcher'; | ||
|
||
export default class NullishCoalescingExistsOpPatcher extends ExistsOpPatcher { | ||
/** | ||
* LEFT '?' RIGHT → `LEFT ?? RIGHT` | ||
*/ | ||
patchAsExpression(options: PatchOptions = {}): void { | ||
const needsTypeofCheck = this.left.mayBeUnboundReference(); | ||
if (needsTypeofCheck) { | ||
super.patchAsExpression(options); | ||
} else { | ||
BinaryOpPatcher.prototype.patchAsExpression.call(this, options); | ||
} | ||
} | ||
|
||
patchOperator(): void { | ||
const opToken = this.getOperatorToken(); | ||
this.overwrite(opToken.start, opToken.end, '??'); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import check from './support/check'; | ||
|
||
function checkNullishCoalescing(source: string, expected: string): void { | ||
check(source, expected, { options: { nullishCoalescing: true } }); | ||
} | ||
|
||
describe('with nullish coalescing enabled', () => { | ||
it('translates binary existence to nullish coalescing', () => { | ||
checkNullishCoalescing( | ||
` | ||
a = 1 | ||
b = a ? 2 | ||
`, | ||
` | ||
const a = 1; | ||
const b = a ?? 2; | ||
` | ||
); | ||
}); | ||
|
||
it('falls back to a check if the LHS may be undeclared', () => { | ||
checkNullishCoalescing( | ||
` | ||
b = a ? 2 | ||
`, | ||
` | ||
const b = typeof a !== 'undefined' && a !== null ? a : 2; | ||
` | ||
); | ||
}) | ||
}); |