Skip to content

Commit

Permalink
Allow arrow functions to contain 'use strict' for simple parameter li…
Browse files Browse the repository at this point in the history
…sts. Fixes dop251#323.
  • Loading branch information
dop251 committed Aug 10, 2021
1 parent 32956a3 commit acd0507
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
26 changes: 11 additions & 15 deletions compiler_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,12 +830,7 @@ func (c *compiler) compileParameterBindingIdentifier(name unistring.String, offs
c.checkIdentifierName(name, offset)
c.checkIdentifierLName(name, offset)
}
b, unique := c.scope.bindNameShadow(name)
if !unique && c.scope.strict {
c.throwSyntaxError(offset, "Strict mode function may not have duplicate parameter names (%s)", name)
return nil, false
}
return b, unique
return c.scope.bindNameShadow(name)
}

func (c *compiler) compileParameterPatternIdBinding(name unistring.String, offset int) {
Expand Down Expand Up @@ -911,16 +906,17 @@ func (e *compiledFunctionLiteral) emitGetter(putOnStack bool) {
if item.Initializer != nil {
hasInits = true
}
if hasPatterns || hasInits || e.isArrow {
if firstDupIdx >= 0 {
e.c.throwSyntaxError(firstDupIdx, "Duplicate parameter name not allowed in this context")
return
}
if e.strict != nil {
e.c.throwSyntaxError(int(e.strict.Idx)-1, "Illegal 'use strict' directive in function with non-simple parameter list")
return
}

if firstDupIdx >= 0 && (hasPatterns || hasInits || s.strict || e.isArrow) {
e.c.throwSyntaxError(firstDupIdx, "Duplicate parameter name not allowed in this context")
return
}

if (hasPatterns || hasInits) && e.strict != nil {
e.c.throwSyntaxError(int(e.strict.Idx)-1, "Illegal 'use strict' directive in function with non-simple parameter list")
return
}

if !hasInits {
length++
}
Expand Down
13 changes: 13 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4100,6 +4100,19 @@ func TestCatchParamPattern(t *testing.T) {
testScript1(SCRIPT, asciiString("1 2 3"), t)
}

func TestArrowUseStrict(t *testing.T) {
// simple parameter list -- ok
_, err := Compile("", "(a) => {'use strict';}", false)
if err != nil {
t.Fatal(err)
}
// non-simple parameter list -- syntax error
_, err = Compile("", "(a=0) => {'use strict';}", false)
if err == nil {
t.Fatal("expected error")
}
}

/*
func TestBabel(t *testing.T) {
src, err := ioutil.ReadFile("babel7.js")
Expand Down

0 comments on commit acd0507

Please sign in to comment.