Skip to content

Commit

Permalink
Prefer do...while and "loop" braceless loops
Browse files Browse the repository at this point in the history
Prefer them over "do" and "loop" prop keys
  • Loading branch information
peey committed Nov 7, 2024
1 parent 548bc91 commit ef813cf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 29 deletions.
54 changes: 28 additions & 26 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -3422,6 +3422,34 @@ ObjectPropertyDelimiter
# https://262.ecma-international.org/#prod-PropertyDefinition
# NOTE: Must start on same line
PropertyDefinition
# Safe to prioritize this over others as it's unlikely that a loop is matched unless the user intended to put the loop there. This is also required for giving braceless-do loop priority over interpreting do as a PropertyName
_?:ws InsertDotDotDot:dots IterationExpression:exp ->
debugger
let { statement } = exp

// immutably set exp.statement.object = true
statement = { ...statement, object: true }
exp = {
...exp,
statement,
children: exp.children.map(($) => $ === exp.statement ? statement : $),
}

const children = [ws, dots, exp]
if (statement.reduction) {
children.unshift({
type: "Error",
message: "Reduction loops are forbidden in object literals",
})
}

return {
type: "SpreadProperty",
children,
names: exp.names,
dots,
value: exp,
}
_?:ws NamedProperty:prop ->
return prepend(ws, prop)

Expand Down Expand Up @@ -3539,32 +3567,6 @@ PropertyDefinition
# NOTE: basic identifiers are now part of the rule above
#_?:ws IdentifierReference:id ->
# return prepend(ws, id)
_?:ws InsertDotDotDot:dots IterationExpression:exp ->
let { statement } = exp

// immutably set exp.statement.object = true
statement = { ...statement, object: true }
exp = {
...exp,
statement,
children: exp.children.map(($) => $ === exp.statement ? statement : $),
}

const children = [ws, dots, exp]
if (statement.reduction) {
children.unshift({
type: "Error",
message: "Reduction loops are forbidden in object literals",
})
}

return {
type: "SpreadProperty",
children,
names: exp.names,
dots,
value: exp,
}


NamedProperty
Expand Down
41 changes: 40 additions & 1 deletion test/object-comprehensions.civet
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,52 @@ describe "object comprehensions", ->
"""

testCase """
do while object comprehension
do...while object comprehension
---
o := {do [x]: f(x) while(predicate(x)) }
---
const o = {...(()=>{const results={};do { Object.assign(results,({[x]: f(x)})) } while(predicate(x))return results})() }
"""

testCase """
do...while multi-line
---
i .= 1
squares := {
do {
[i]: i * i
} while i++ < 10
}
---
let i = 1
const squares = {
...(()=>{const results={};do {
Object.assign(results,({[i]: i * i}))
} while (i++ < 10)return results})()
}
"""

testCase """
"loop"
---
o := {
a: "a prop",
loop
break if predicate(i)
i++
[i]: f(i)
}
---
const o = {
a: "a prop",
...(()=>{const results={};while(true) {
if (predicate(i)) { break }
i++
Object.assign(results,({[i]: f(i)}))
}return results})()
}
"""

testCase """
empty loop is allowed
---
Expand Down
4 changes: 2 additions & 2 deletions test/object.civet
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,9 @@ describe "object", ->
testCase """
with reserved word keys
---
state.{and,await,break,case,catch,class,const,continue,debugger,default,delete,do,else,enum,export,extends}
state.{and,await,break,case,catch,class,const,continue,debugger,default,delete,else,enum,export,extends}
---
({and:state.and,await:state.await,break:state.break,case:state.case,catch:state.catch,class:state.class,const:state.const,continue:state.continue,debugger:state.debugger,default:state.default,delete:state.delete,do:state.do,else:state.else,enum:state.enum,export:state.export,extends:state.extends})
({and:state.and,await:state.await,break:state.break,case:state.case,catch:state.catch,class:state.class,const:state.const,continue:state.continue,debugger:state.debugger,default:state.default,delete:state.delete,else:state.else,enum:state.enum,export:state.export,extends:state.extends})
"""

testCase """
Expand Down

0 comments on commit ef813cf

Please sign in to comment.