Skip to content

Commit

Permalink
Implement rest params in abstract closures (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell authored Feb 23, 2024
1 parent 03981a8 commit 52d1ddc
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
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

0 comments on commit 52d1ddc

Please sign in to comment.