Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better completion regex #558

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ 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\b|(^|(?<=, ))[tT]hrow (a\b|the\b|$)|[rR]eturn (Normal|Throw|Return)?Completion\(|[rR]eturn( a| a new| the)? Completion Record\b|the result of evaluating\b)|(?<=[\s(])\?\s/
);
if (completionyThing != null) {
if (returnType?.kind === 'completion') {
containsAnyCompletionyThings = true;
} else if (clause.aoid !== 'GeneratorStart') {
// TODO: remove above exception when the spec is more coherent (https://github.com/tc39/ecma262/pull/2429)
} else {
spec.warn({
type: 'contents',
ruleId: 'completiony-thing-in-non-completion-algorithm',
Expand Down
89 changes: 87 additions & 2 deletions test/typecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,50 @@ 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>
`,
{
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. Let _foo_ be a thing.
1. ${M}Throw _foo_.
</emu-alg>
</emu-clause>
`,
Expand Down Expand Up @@ -290,6 +333,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 +379,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 +592,7 @@ describe('typechecking completions', () => {
<dl class="header">
</dl>
<emu-alg>
1. Throw something.
1. Throw a *TypeError* exception.
</emu-alg>
</emu-clause>

Expand Down
Loading