Skip to content

JS: allow repeated parameter names in non-strict functions - #140

Closed
bardrop wants to merge 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-duplicate-sloppy-params
Closed

bardrop wants to merge 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-duplicate-sloppy-params

Conversation

@bardrop

@bardrop bardrop commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Repeated parameter names in a non-strict function failed to parse, so minify refused valid sloppy-mode scripts:

function f(a, a) { return a }   // identifier a has already been declared
(function (x, x) {})            // same

The spec allows this when the function is not strict and its parameter list is simple (no defaults, destructuring or rest). The last parameter with the name wins: f(1, 2) returns 2. Older or generated scripts still contain this.

What changed in parseFuncParams:

  • For function declarations and expressions (also generators and async functions), a repeated plain identifier reuses the Var of the earlier parameter instead of failing. Both bindings point to one variable, so the renamer gives them the same name and the last one still gets the value.
  • If the list has a repeat and also a default, a pattern or a rest element, it is still an error, in any order (function f(a,a=1){}, function f(a=1,a){}, function f(a,a,...b){}).
  • Arrow functions and methods still reject repeats (they always use unique parameters).

Strict code must still reject repeats, so the parser now keeps a small strict flag. It is set by a "use strict" directive (script, function, method and arrow bodies) and inside classes, and it is restored when the body ends. A directive in the function's own body is checked after the body, because it applies to the parameters too (function f(a,a){"use strict"} is an error). The flag is only used for this check. Module code (import/export) is not treated as strict, so export function f(a,a){} is still accepted; I left that out to keep the change small.

Found with tc39/test262-parser-tests: pass/784a059faa166072.js and pass/b7d99c0034be0ce1.js failed before and parse now. No fail/ or early/ results change. (Without the strict flag, 10 early/ strict-mode files would have parsed, so I added it.)

Tests: 8 new TestParse cases and 2 TestParseScope cases fail before the change. 10 new TestParseError cases cover non-simple lists, methods, classes and "use strict" before and after the parameters. go test ./... passes, and minify's go test ./... passes against this branch. With it:

function f(first,first){return first}console.log(f(1,2))
=> function f(e,e){return e}console.log(f(1,2))      // node prints 2, same as the input

`function f(a,a){}` failed with "identifier a has already been
declared", so minify rejected valid sloppy-mode scripts. Duplicate
parameter names are allowed in non-strict functions with a simple
parameter list (no defaults, destructuring or rest). The repeated
parameters now share one variable, so renaming keeps them equal and
the last one still gets the argument value.

They stay an error for arrow functions, methods, non-simple lists and
strict mode code. The parser now tracks strict mode from "use strict"
directives (script and function bodies, also when the directive comes
after the parameters) and class bodies.
@bardrop bardrop closed this Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant