Skip to content

Commit

Permalink
Merge pull request jashkenas#2334 from michaelficarra/issue2333
Browse files Browse the repository at this point in the history
fixes jashkenas#2333: fix prohibition of duplicate object properties
  • Loading branch information
michaelficarra committed May 16, 2012
2 parents 5c66e55 + f31ff77 commit da22989
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
13 changes: 12 additions & 1 deletion lib/coffee-script/helpers.js

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

41 changes: 37 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::some
exports.some = Array::some ? (fn) ->
return true for e in this when fn e
false
19 changes: 17 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'
{compact, flatten, extend, merge, del, starts, ends, last, some} = require './helpers'

exports.extend = extend # for parser

Expand Down Expand Up @@ -802,11 +802,26 @@ exports.Obj = class Obj extends Base
compileNode: (o) ->
props = @properties
propNames = []
normalise = (s) -> switch s[0]
when '"' then s[1...-1].replace /\\"/g, '"'
when "'" then s[1...-1].replace /\\'/g, "'"
isDuplicate = (x) ->
mx = x.match /^['"]/
(y) ->
return true if y is x or +y is +x
my = y.match /^['"]/
if mx and my
return true if normalise(x) is normalise y
else if mx
return true if y is x[1...-1]
else if my
return true if x is y[1...-1]
false
for prop in @properties
prop = prop.variable if prop.isComplex()
if prop?
propName = prop.unwrapAll().value.toString()
if propName in propNames
if some.call propNames, isDuplicate propName
throw SyntaxError "multiple object literal properties named \"#{propName}\""
propNames.push propName
return (if @front then '({})' else '{}') unless props.length
Expand Down
16 changes: 15 additions & 1 deletion test/strict.coffee
Expand Up @@ -57,9 +57,23 @@ 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 '{0:0, "\\x30":0}'
strict '{.1:0, 0.1:0}'
strict '{.1:0, 1e-1:0}'
strict '{100:0, 1e2:0}'
strict '{"\\0":0, "\\x00":0}'
strict 'a = 0; {a, "a":0}'
strictOk '{0:0, "0x0":0}'
strictOk '{"a":0, "\'a\'":0}'

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

Expand Down

0 comments on commit da22989

Please sign in to comment.