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

Implement rest params in abstract closures #573

Merged
merged 1 commit into from
Feb 23, 2024
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
15 changes: 14 additions & 1 deletion src/lint/rules/variable-use-def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,21 @@ function walkAlgorithm(
continue;
}
// TODO this kind of parsing should really be factored out
let varName = true;
for (let i = 0; i < paramList.items.length; ++i) {
const item = paramList.items[i];
if (i % 2 === 0) {
if (varName) {
if (item.name === 'text' && item.contents === '...') {
if (i < paramList.items.length - 2) {
report({
ruleId: 'bad-ac',
message: `expected rest param to come last`,
...offsetToLineAndColumn(algorithmSource, item.location.start.offset),
});
continue stepLoop;
}
continue;
}
if (item.name !== 'underscore') {
report({
ruleId: 'bad-ac',
Expand All @@ -287,6 +299,7 @@ function walkAlgorithm(
continue stepLoop;
}
}
varName = !varName;
}
}

Expand Down
96 changes: 96 additions & 0 deletions test/lint-variable-use-def.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,102 @@ describe('variables are declared and used appropriately', () => {
);
});

it('abstract closure rest parameters must be last', async () => {
await assertLint(
positioned`
<emu-clause id="sec-object.fromentries">
<h1>Object.fromEntries ( _obj_ )</h1>
<emu-alg>
1. Let _closure_ be a new Abstract Closure with parameters (${M}..._x_, _y_) that captures nothing and performs the following steps when called:
1. Do something with _x_.
1. Return *undefined*.
1. Return _closure_.
</emu-alg>
</emu-clause>
`,
{
ruleId: 'bad-ac',
nodeType: 'emu-alg',
message: 'expected rest param to come last',
},
);

await assertLint(
positioned`
<emu-clause id="sec-object.fromentries">
<h1>Object.fromEntries ( _obj_ )</h1>
<emu-alg>
1. Let _closure_ be a new Abstract Closure with parameters (${M}..._x_,) that captures nothing and performs the following steps when called:
1. Do something with _x_.
1. Return *undefined*.
1. Return _closure_.
</emu-alg>
</emu-clause>
`,
{
ruleId: 'bad-ac',
nodeType: 'emu-alg',
message: 'expected rest param to come last',
},
);
});

it('abstract closure rest parameters must be variables', async () => {
await assertLint(
positioned`
<emu-clause id="sec-object.fromentries">
<h1>Object.fromEntries ( _obj_ )</h1>
<emu-alg>
1. Let _closure_ be a new Abstract Closure with parameters (${M}...x) that captures nothing and performs the following steps when called:
1. Do something with _x_.
1. Return *undefined*.
1. Return _closure_.
</emu-alg>
</emu-clause>
`,
{
ruleId: 'bad-ac',
nodeType: 'emu-alg',
message: 'expected to find a parameter name here',
},
);

await assertLint(
positioned`
<emu-clause id="sec-object.fromentries">
<h1>Object.fromEntries ( _obj_ )</h1>
<emu-alg>
1. Let _closure_ be a new Abstract Closure with parameters (${M}..._x_,) that captures nothing and performs the following steps when called:
1. Do something with _x_.
1. Return *undefined*.
1. Return _closure_.
</emu-alg>
</emu-clause>
`,
{
ruleId: 'bad-ac',
nodeType: 'emu-alg',
message: 'expected rest param to come last',
},
);
});

it('abstract closure rest parameters are visible', async () => {
await assertLintFree(
`
<emu-clause id="sec-object.fromentries">
<h1>Object.fromEntries ( _obj_ )</h1>
<emu-alg>
1. Let _closure_ be a new Abstract Closure with parameters (..._x_) that captures nothing and performs the following steps when called:
1. Do something with _x_.
1. Return *undefined*.
1. Return _closure_.
</emu-alg>
</emu-clause>
`,
);
});

it('multiple declarations are visible', async () => {
await assertLintFree(
`
Expand Down
Loading