Repository navigation
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Repeated parameter names in a non-strict function failed to parse, so minify refused valid sloppy-mode scripts:
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)returns2. Older or generated scripts still contain this.What changed in
parseFuncParams:functiondeclarations and expressions (also generators and async functions), a repeated plain identifier reuses theVarof 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.function f(a,a=1){},function f(a=1,a){},function f(a,a,...b){}).Strict code must still reject repeats, so the parser now keeps a small
strictflag. 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, soexport function f(a,a){}is still accepted; I left that out to keep the change small.Found with tc39/test262-parser-tests:
pass/784a059faa166072.jsandpass/b7d99c0034be0ce1.jsfailed before and parse now. Nofail/orearly/results change. (Without the strict flag, 10early/strict-mode files would have parsed, so I added it.)Tests: 8 new
TestParsecases and 2TestParseScopecases fail before the change. 10 newTestParseErrorcases cover non-simple lists, methods, classes and"use strict"before and after the parameters.go test ./...passes, and minify'sgo test ./...passes against this branch. With it: