Skip to content

Commit

Permalink
fixes #2333: fix prohibition of duplicate object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed May 16, 2012
1 parent 5c66e55 commit c264bf0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
11 changes: 11 additions & 0 deletions lib/coffee-script/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/helpers.coffee
Expand Up @@ -54,3 +54,8 @@ exports.del = (obj, key) ->

# Gets the last item of an array(-like) object.
exports.last = (array, back) -> array[array.length - (back or 0) - 1]

# Typical Array::any
exports.any = (array, fn) ->
return true for e in array when fn e
false
14 changes: 12 additions & 2 deletions src/nodes.coffee
Expand Up @@ -7,7 +7,7 @@
{RESERVED, STRICT_PROSCRIBED} = require './lexer'

# Import the helpers we plan to use.
{compact, flatten, extend, merge, del, starts, ends, last} = require './helpers'
{any, compact, flatten, extend, merge, del, starts, ends, last} = require './helpers'

exports.extend = extend # for parser

Expand Down Expand Up @@ -806,7 +806,17 @@ exports.Obj = class Obj extends Base
prop = prop.variable if prop.isComplex()
if prop?
propName = prop.unwrapAll().value.toString()
if propName in propNames
isDuplicate = (x) ->
p0 = propName[0]
x0 = x[0]
propName is x or +propName is +x or
if p0 is "'" and x0 is '"' or p0 is '"' and x0 is "'"
eval(x) is eval(propName)
else if p0 is "'" then propName is "'#{x}'"
else if p0 is '"' then propName is "\"#{x}\""
else if x0 is "'" then x is "'#{propName}'"
else if x0 is '"' then x is "\"#{propName}\""
if any propNames, isDuplicate
throw SyntaxError "multiple object literal properties named \"#{propName}\""
propNames.push propName
return (if @front then '({})' else '{}') unless props.length
Expand Down
10 changes: 9 additions & 1 deletion test/strict.coffee
Expand Up @@ -57,9 +57,17 @@ test "octal escape sequences prohibited", ->


test "duplicate property definitions in object literals are prohibited", ->
strict 'o = {x:1,x:1}'
strict 'o = {x:1, x:1}'
strict 'x = 1; o = {x, x: 2}'

test "#2333: more duplicate property prohibitions", ->
strict '{a:0, "a":0}'
strict "{'a':0, a:0}"
strict '{\'a\':0, "a":0}'
strict '{0:0, 0x0:0}'
strict 'a = 0; {a, "a":0}'
strictOk '{"a":0, "\'a\'":0}'

test "duplicate formal parameters are prohibited", ->
nonce = {}

Expand Down

0 comments on commit c264bf0

Please sign in to comment.