Skip to content

Commit 52d1ddc

Browse files
authored
Implement rest params in abstract closures (#573)
1 parent 03981a8 commit 52d1ddc

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

src/lint/rules/variable-use-def.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,21 @@ function walkAlgorithm(
265265
continue;
266266
}
267267
// TODO this kind of parsing should really be factored out
268+
let varName = true;
268269
for (let i = 0; i < paramList.items.length; ++i) {
269270
const item = paramList.items[i];
270-
if (i % 2 === 0) {
271+
if (varName) {
272+
if (item.name === 'text' && item.contents === '...') {
273+
if (i < paramList.items.length - 2) {
274+
report({
275+
ruleId: 'bad-ac',
276+
message: `expected rest param to come last`,
277+
...offsetToLineAndColumn(algorithmSource, item.location.start.offset),
278+
});
279+
continue stepLoop;
280+
}
281+
continue;
282+
}
271283
if (item.name !== 'underscore') {
272284
report({
273285
ruleId: 'bad-ac',
@@ -287,6 +299,7 @@ function walkAlgorithm(
287299
continue stepLoop;
288300
}
289301
}
302+
varName = !varName;
290303
}
291304
}
292305

test/lint-variable-use-def.js

+96
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,102 @@ describe('variables are declared and used appropriately', () => {
401401
);
402402
});
403403

404+
it('abstract closure rest parameters must be last', async () => {
405+
await assertLint(
406+
positioned`
407+
<emu-clause id="sec-object.fromentries">
408+
<h1>Object.fromEntries ( _obj_ )</h1>
409+
<emu-alg>
410+
1. Let _closure_ be a new Abstract Closure with parameters (${M}..._x_, _y_) that captures nothing and performs the following steps when called:
411+
1. Do something with _x_.
412+
1. Return *undefined*.
413+
1. Return _closure_.
414+
</emu-alg>
415+
</emu-clause>
416+
`,
417+
{
418+
ruleId: 'bad-ac',
419+
nodeType: 'emu-alg',
420+
message: 'expected rest param to come last',
421+
},
422+
);
423+
424+
await assertLint(
425+
positioned`
426+
<emu-clause id="sec-object.fromentries">
427+
<h1>Object.fromEntries ( _obj_ )</h1>
428+
<emu-alg>
429+
1. Let _closure_ be a new Abstract Closure with parameters (${M}..._x_,) that captures nothing and performs the following steps when called:
430+
1. Do something with _x_.
431+
1. Return *undefined*.
432+
1. Return _closure_.
433+
</emu-alg>
434+
</emu-clause>
435+
`,
436+
{
437+
ruleId: 'bad-ac',
438+
nodeType: 'emu-alg',
439+
message: 'expected rest param to come last',
440+
},
441+
);
442+
});
443+
444+
it('abstract closure rest parameters must be variables', async () => {
445+
await assertLint(
446+
positioned`
447+
<emu-clause id="sec-object.fromentries">
448+
<h1>Object.fromEntries ( _obj_ )</h1>
449+
<emu-alg>
450+
1. Let _closure_ be a new Abstract Closure with parameters (${M}...x) that captures nothing and performs the following steps when called:
451+
1. Do something with _x_.
452+
1. Return *undefined*.
453+
1. Return _closure_.
454+
</emu-alg>
455+
</emu-clause>
456+
`,
457+
{
458+
ruleId: 'bad-ac',
459+
nodeType: 'emu-alg',
460+
message: 'expected to find a parameter name here',
461+
},
462+
);
463+
464+
await assertLint(
465+
positioned`
466+
<emu-clause id="sec-object.fromentries">
467+
<h1>Object.fromEntries ( _obj_ )</h1>
468+
<emu-alg>
469+
1. Let _closure_ be a new Abstract Closure with parameters (${M}..._x_,) that captures nothing and performs the following steps when called:
470+
1. Do something with _x_.
471+
1. Return *undefined*.
472+
1. Return _closure_.
473+
</emu-alg>
474+
</emu-clause>
475+
`,
476+
{
477+
ruleId: 'bad-ac',
478+
nodeType: 'emu-alg',
479+
message: 'expected rest param to come last',
480+
},
481+
);
482+
});
483+
484+
it('abstract closure rest parameters are visible', async () => {
485+
await assertLintFree(
486+
`
487+
<emu-clause id="sec-object.fromentries">
488+
<h1>Object.fromEntries ( _obj_ )</h1>
489+
<emu-alg>
490+
1. Let _closure_ be a new Abstract Closure with parameters (..._x_) that captures nothing and performs the following steps when called:
491+
1. Do something with _x_.
492+
1. Return *undefined*.
493+
1. Return _closure_.
494+
</emu-alg>
495+
</emu-clause>
496+
`,
497+
);
498+
});
499+
404500
it('multiple declarations are visible', async () => {
405501
await assertLintFree(
406502
`

0 commit comments

Comments
 (0)