Skip to content

Commit

Permalink
Merge pull request #1688 from DanielXMoore/trailing-binary
Browse files Browse the repository at this point in the history
Allow binary operators after nested object literals and bulleted arrays, fix `++` with bulleted arrays
  • Loading branch information
edemaine authored Jan 22, 2025
2 parents 058fc28 + 8c32b1c commit 3beb915
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
yarn build
yarn test
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: browser.js
path: dist/browser.js
Expand Down Expand Up @@ -72,8 +72,10 @@ jobs:
cache: yarn
node-version: 20

- name: Install and Test
- name: Install and Build
# `yarn docs:build` includes `yarn build`
run: |
yarn
yarn docs:build
env:
API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32 changes: 19 additions & 13 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ IsLike

# Whitespace followed by RHS
WRHS
NestedBulletedArray -> [undefined, $1]
NestedImplicitObjectLiteral -> [undefined, $1]
PushIndent ( ( Nested _? ) RHS )?:wrhs PopIndent ->
if (!wrhs) return $skip
return wrhs
Expand Down Expand Up @@ -710,12 +712,13 @@ NWTypePostfix

# https://262.ecma-international.org/#prod-UpdateExpression
UpdateExpression
# NOTE: Not allowing whitespace betwen prefix and postfix increment operators and operand
UpdateExpressionSymbol UnaryWithoutParenthesizedAssignment ->
# NOTE: Not allowing whitespace between prefix and postfix increment operators and operand
# This is especially important for ++ to work as binary concat operator
UpdateExpressionSymbol:symbol !Whitespace UnaryWithoutParenthesizedAssignment:assigned ->
return {
type: "UpdateExpression",
assigned: $2,
children: $0,
assigned,
children: [symbol, assigned],
}
LeftHandSideExpression ( UpdateExpressionSymbol /(?!\p{ID_Start}|[_$0-9(\[{])/ )? ->
if (!$2) return $1
Expand Down Expand Up @@ -5639,18 +5642,20 @@ Debugger
return { $loc, token: $1 }

MaybeNestedNonPipelineExpression
NestedBulletedArray
NestedImplicitObjectLiteral
PushIndent ( Nested NonPipelineExpression )?:expression PopIndent AllowedTrailingCallExpressions?:trailing ->
# Leave NestedBulletedArray and NestedImplicitObjectLiteral for
# the second case, where they get checked by PrimaryExpression,
# which then allows for trailing binary operators etc.
!NestedBulletedArray !NestedImplicitObjectLiteral PushIndent ( Nested NonPipelineExpression )?:expression PopIndent AllowedTrailingCallExpressions?:trailing ->
if (!expression) return $skip
if (!trailing) return expression
return [ expression, trailing ]
NonPipelineExpression

MaybeNestedPostfixedExpression
NestedBulletedArray
NestedImplicitObjectLiteral
PushIndent ( Nested PostfixedExpression )?:expression PopIndent AllowedTrailingCallExpressions?:trailing ->
# Leave NestedBulletedArray and NestedImplicitObjectLiteral for
# the second case, where they get checked by PrimaryExpression,
# which then allows for trailing binary operators etc.
!NestedBulletedArray !NestedImplicitObjectLiteral PushIndent ( Nested PostfixedExpression )?:expression PopIndent AllowedTrailingCallExpressions?:trailing ->
if (!expression) return $skip
if (!trailing) return expression
return [ expression, trailing ]
Expand All @@ -5664,9 +5669,10 @@ NestedPostfixedExpressionNoTrailing
return expression

MaybeNestedExpression
NestedBulletedArray
NestedImplicitObjectLiteral
PushIndent ( Nested Expression )?:expression PopIndent AllowedTrailingCallExpressions?:trailing ->
# Leave NestedBulletedArray and NestedImplicitObjectLiteral for
# the second case, where they get checked by PrimaryExpression,
# which then allows for trailing binary operators etc.
!NestedBulletedArray !NestedImplicitObjectLiteral PushIndent ( Nested Expression )?:expression PopIndent AllowedTrailingCallExpressions?:trailing ->
if (!expression) return $skip
if (!trailing) return expression
return [ expression, trailing ]
Expand Down
41 changes: 41 additions & 0 deletions test/array.civet
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,47 @@ describe "array", ->
.toString())
"""

testCase """
with trailing type cast
---
x =
. a
. b
as const
---
x = [
a,
b] as const
"""

testCase """
with trailing binary operator
---
x =
. a
. b
++ rest
---
x = [
a,
b]
.concat(rest)
"""

testCase """
with preceding and trailing binary operator
---
x = front ++
. a
. b
++ rest
---
x = front.concat( [
a,
b])
.concat(rest)
"""

describe "[] followed by elements", ->
testCase """
indented
Expand Down
16 changes: 15 additions & 1 deletion test/object.civet
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,21 @@ describe "object", ->
return ({a:(hasA? 1:void 0),
b:(hasB? 2:void 0)})
}
"""; // TODO: thinks next triple quote is a call expression
"""

testCase """
braceless object with trailing type cast
---
x =
a: 1
b: 2
as const
---
x = {
a: 1,
b: 2,
} as const
"""

"""
async
Expand Down

0 comments on commit 3beb915

Please sign in to comment.