From 6cf401dfcd4a8e4f3b058974f0a1aba0a038b77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 18 Mar 2026 19:32:52 +0100 Subject: [PATCH] Lint against re-declaring parameters --- src/lint/rules/variable-use-def.ts | 3 +- test/lint-variable-use-def.ts | 48 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/lint/rules/variable-use-def.ts b/src/lint/rules/variable-use-def.ts index 3a9e9361..b364a4fe 100644 --- a/src/lint/rules/variable-use-def.ts +++ b/src/lint/rules/variable-use-def.ts @@ -141,9 +141,10 @@ export function checkVariableUsage( preceding.querySelector('emu-alg') == null && preceding.textContent != null ) { + const isParameter = preceding.nodeName === 'H1'; // `__` is for _x__y_, which has textContent `_x__y_` for (const name of preceding.textContent.matchAll(/(?<=\b|_)_([a-zA-Z0-9]+)_(?=\b|_)/g)) { - scope.declare(name[1], null, undefined, true); + scope.declare(name[1], null, isParameter ? 'parameter' : undefined, !isParameter); } } preceding = previousOrParent(preceding, parentClause); diff --git a/test/lint-variable-use-def.ts b/test/lint-variable-use-def.ts index f13a5eac..bb4ced15 100644 --- a/test/lint-variable-use-def.ts +++ b/test/lint-variable-use-def.ts @@ -699,4 +699,52 @@ describe('variables cannot be redeclared', () => { `, ); }); + + it('parameters cannot be redeclared', async () => { + await assertLint( + positioned` + +

+ Example ( + _x_: ~foo~, + ): ~foo~ or ~bar~ +

+
+ + 1. Let ${M}_x_ be ~bar~. + 1. Return _x_. + +
+ `, + { + ruleId: 're-declaration', + nodeType: 'emu-alg', + message: '"x" is already declared', + }, + ); + }); + + it('optional parameters cannot be redeclared', async () => { + await assertLint( + positioned` + +

+ Example ( + optional _x_: ~foo~, + ): ~foo~ or ~bar~ +

+
+ + 1. If _x_ is not present, let ${M}_x_ be ~bar~. + 1. Return _x_. + +
+ `, + { + ruleId: 're-declaration', + nodeType: 'emu-alg', + message: '"x" is already declared', + }, + ); + }); });