Skip to content

Commit

Permalink
improve regex for completion-returning steps
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Nov 1, 2023
1 parent 11affd1 commit c4106c1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class Algorithm extends Builder {
continue;
}
const completionyThing = part.contents.match(
/\b(ReturnIfAbrupt|throw|Return (Normal|Throw)?Completion|the result of evaluating)\b|(?<=[\s(])\?\s/i
/\b(ReturnIfAbrupt|(^|(?<=, ))[tT]hrow a|[rR]eturn (Normal|Throw|Return)?Completion\(|[rR]eturn( a| a new| the)? Completion Record|the result of evaluating)(\b|$)|(?<=[\s(])\?\s/
);
if (completionyThing != null) {
if (returnType?.kind === 'completion') {
Expand Down
67 changes: 65 additions & 2 deletions test/typecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,28 @@ describe('typechecking completions', () => {
<dl class="header">
</dl>
<emu-alg>
1. ${M}Throw a new TypeError.
1. ${M}Throw a *TypeError* exception.
</emu-alg>
</emu-clause>
`,
{
ruleId: 'completiony-thing-in-non-completion-algorithm',
nodeType: 'emu-alg',
message:
'this would return a Completion Record, but the containing AO is declared not to return a Completion Record',
}
);

await assertLint(
positioned`
<emu-clause id="example" type="abstract operation">
<h1>
ExampleAlg (): a Number
</h1>
<dl class="header">
</dl>
<emu-alg>
1. If some condition is met, ${M}throw a *TypeError* exception.
</emu-alg>
</emu-clause>
`,
Expand Down Expand Up @@ -290,6 +311,28 @@ describe('typechecking completions', () => {
extraBiblios: [biblio],
}
);

await assertLint(
positioned`
<emu-clause id="example" type="abstract operation">
<h1>
ExampleAlg (): a Number
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _x_ be 0.
1. ${M}Return Completion Record { [[Type]]: ~return~, [[Value]]: _x_, [[Target]]: ~empty~ }.
</emu-alg>
</emu-clause>
`,
{
ruleId: 'completiony-thing-in-non-completion-algorithm',
nodeType: 'emu-alg',
message:
'this would return a Completion Record, but the containing AO is declared not to return a Completion Record',
}
);
});

it('negative', async () => {
Expand All @@ -314,6 +357,26 @@ describe('typechecking completions', () => {
extraBiblios: [biblio],
}
);

await assertLintFree(
`
<emu-clause id="example" type="abstract operation">
<h1>
ExampleAlg (): a Number
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Do something with Completion(0).
1. NOTE: This will not throw a *TypeError* exception.
1. Consider whether something is a return completion.
</emu-alg>
</emu-clause>
`,
{
extraBiblios: [biblio],
}
);
});
});

Expand Down Expand Up @@ -507,7 +570,7 @@ describe('typechecking completions', () => {
<dl class="header">
</dl>
<emu-alg>
1. Throw something.
1. Throw a *TypeError* exception.
</emu-alg>
</emu-clause>
Expand Down

0 comments on commit c4106c1

Please sign in to comment.