|
| 1 | +import { describe, it, before } from 'node:test'; |
| 2 | +import { |
| 3 | + assertLint, |
| 4 | + assertLintFree, |
| 5 | + lintLocationMarker as M, |
| 6 | + positioned, |
| 7 | + getBiblio, |
| 8 | +} from './utils.ts'; |
| 9 | +import type { ExportedBiblio } from '../lib/Biblio.js'; |
| 10 | + |
| 11 | +const nodeType = 'emu-alg'; |
| 12 | + |
| 13 | +describe('prefer-throw-shorthand', () => { |
| 14 | + let throwBiblio: ExportedBiblio; |
| 15 | + before(async () => { |
| 16 | + throwBiblio = await getBiblio(` |
| 17 | + <emu-clause id="sec-throwcompletion" type="abstract operation"> |
| 18 | + <h1> |
| 19 | + ThrowCompletion ( |
| 20 | + _value_: an ECMAScript language value, |
| 21 | + ): a throw completion |
| 22 | + </h1> |
| 23 | + <dl class="header"> |
| 24 | + </dl> |
| 25 | + <emu-alg> |
| 26 | + 1. Return Completion Record { [[Type]]: ~throw~, [[Value]]: _value_, [[Target]]: ~empty~ }. |
| 27 | + </emu-alg> |
| 28 | + </emu-clause> |
| 29 | + `); |
| 30 | + }); |
| 31 | + |
| 32 | + it('return ThrowCompletion', async () => { |
| 33 | + await assertLint( |
| 34 | + positioned`<emu-alg> |
| 35 | + 1. Let _x_ be *"foo"*. |
| 36 | + 1. Return ${M}ThrowCompletion(_x_). |
| 37 | + </emu-alg>`, |
| 38 | + { |
| 39 | + ruleId: 'prefer-throw-shorthand', |
| 40 | + nodeType, |
| 41 | + message: 'prefer "throw _x_" over "return ThrowCompletion(_x_)"', |
| 42 | + }, |
| 43 | + { extraBiblios: [throwBiblio] }, |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('return ThrowCompletion in single-line If', async () => { |
| 48 | + await assertLint( |
| 49 | + positioned`<emu-alg> |
| 50 | + 1. Let _x_ be *"foo"*. |
| 51 | + 1. If _x_ is *"bar"*, return ${M}ThrowCompletion(_x_). |
| 52 | + </emu-alg>`, |
| 53 | + { |
| 54 | + ruleId: 'prefer-throw-shorthand', |
| 55 | + nodeType, |
| 56 | + message: 'prefer "throw _x_" over "return ThrowCompletion(_x_)"', |
| 57 | + }, |
| 58 | + { extraBiblios: [throwBiblio] }, |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('throw shorthand is allowed', async () => { |
| 63 | + await assertLintFree(` |
| 64 | + <emu-alg> |
| 65 | + 1. Let _x_ be *"foo"*. |
| 66 | + 1. Throw _x_. |
| 67 | + </emu-alg> |
| 68 | + `); |
| 69 | + }); |
| 70 | + |
| 71 | + it('ThrowCompletion assigned to alias is allowed', async () => { |
| 72 | + await assertLintFree( |
| 73 | + ` |
| 74 | + <emu-alg> |
| 75 | + 1. Let _x_ be *"foo"*. |
| 76 | + 1. Let _comp_ be ThrowCompletion(_x_). |
| 77 | + 1. Return _comp_. |
| 78 | + </emu-alg> |
| 79 | + `, |
| 80 | + { extraBiblios: [throwBiblio] }, |
| 81 | + ); |
| 82 | + }); |
| 83 | +}); |
0 commit comments