Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent case: from implicit object literal #357

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ NestedImplicitPropertyDefinitions
return defs.flat()

NestedImplicitPropertyDefinition
Nested:ws NamedProperty:prop ObjectPropertyDelimiter:delimiter ->
Nested:ws ImplicitNamedProperty:prop ObjectPropertyDelimiter:delimiter ->
return {
...prop,
children: [...ws, ...prop.children, delimiter],
Expand All @@ -2000,7 +2000,7 @@ NestedPropertyDefinition
}))

InlineObjectLiteral
InsertInlineOpenBrace:open SnugNamedProperty ImplicitInlineObjectPropertyDelimiter ( ( Samedent / _* ) NamedProperty ImplicitInlineObjectPropertyDelimiter )* InsertCloseBrace:close ->
InsertInlineOpenBrace:open SnugNamedProperty ImplicitInlineObjectPropertyDelimiter ( ( Samedent / _* ) ImplicitNamedProperty ImplicitInlineObjectPropertyDelimiter )* InsertCloseBrace:close ->
return {
type: "ObjectExpression",
children: [open, $2, $3, ...$4, close],
Expand Down Expand Up @@ -2135,10 +2135,23 @@ NamedProperty
value: exp,
}

# NOTE: Variation on NamedProperty that forbids newlines after colon
# when forbidMultiLineImplicitObjectLiteral is set
ImplicitNamedProperty
PropertyName:name __ Colon ( MultiLineImplicitObjectLiteralAllowed / !EOS ) ExtendedExpression:exp ->
return {
type: "Property",
children: $0,
name: name,
names: exp.names || [],
value: exp,
}

# Named property but doesn't allow any space between name and colon
# used to distinguish between braceless inline objects and ternary expression conditions
# Only used by implicit object literals, so forbid newlines after colon
SnugNamedProperty
PropertyName Colon ExtendedExpression:exp ->
PropertyName Colon ( MultiLineImplicitObjectLiteralAllowed / !EOS ) ExtendedExpression:exp ->
return {
type: "Property",
children: $0,
Expand Down Expand Up @@ -3250,15 +3263,16 @@ CaseClause
}

CaseExpressionList
( _* ExpressionWithIndentedApplicationForbidden ImpliedColon ) ( __ Comma ExpressionWithIndentedApplicationForbidden ImpliedColon )* ->
ForbidMultiLineImplicitObjectLiteral ( _* ExpressionWithIndentedApplicationForbidden ImpliedColon )?:first ( __ Comma ExpressionWithIndentedApplicationForbidden ImpliedColon )*:rest RestoreMultiLineImplicitObjectLiteral ->
if (!first) return $skip
// Convert comma separated expression list to `case <exp>:`
const result = $2.map(([ws, _comma, exp, col]) => {
const result = rest.map(([ws, _comma, exp, col]) => {
exp = module.insertTrimmingSpace(exp, "")

if (ws.length) return [module.insertTrimmingSpace("case ", ws), exp, col]
return ["case ", exp, col]
})
result./**/unshift($1)
result./**/unshift(first)

return result

Expand Down Expand Up @@ -3341,7 +3355,7 @@ RestoreIndentedApplication
IndentedApplicationAllowed
"" ->
if (module.config.verbose) {
console.log("IndentedApplicationAllowed:", module.ForbidIndentedApplication)
console.log("forbidIndentedApplication:", module.forbidIndentedApplication)
}
if (module.indentedApplicationForbidden) return $skip
return
Expand All @@ -3358,19 +3372,38 @@ RestoreTrailingMemberProperty
"" ->
module.forbidTrailingMemberProperty.pop()

AllowAll
AllowTrailingMemberProperty AllowIndentedApplication

RestoreAll
RestoreTrailingMemberProperty RestoreIndentedApplication

TrailingMemberPropertyAllowed
"" ->
if (module.config.verbose) {
console.log("IndentedApplicationAllowed:", module.forbidTrailingMemberProperty)
console.log("forbidTrailingMemberProperty:", module.forbidTrailingMemberProperty)
}
if (module.trailingMemberPropertyForbidden) return $skip

ForbidMultiLineImplicitObjectLiteral
"" ->
module.forbidMultiLineImplicitObjectLiteral.push(true)

AllowMultiLineImplicitObjectLiteral
"" ->
module.forbidMultiLineImplicitObjectLiteral.push(false)

RestoreMultiLineImplicitObjectLiteral
"" ->
module.forbidMultiLineImplicitObjectLiteral.pop()

MultiLineImplicitObjectLiteralAllowed
"" ->
if (module.config.verbose) {
console.log("forbidMultiLineImplicitObjectLiteral:", module.forbidMultiLineImplicitObjectLiteral)
}
if (module.multiLineImplicitObjectLiteralForbidden) return $skip

AllowAll
AllowTrailingMemberProperty AllowIndentedApplication AllowMultiLineImplicitObjectLiteral

RestoreAll
RestoreTrailingMemberProperty RestoreIndentedApplication RestoreMultiLineImplicitObjectLiteral

# https://262.ecma-international.org/#prod-ExpressionStatement
ExpressionStatement
# NOTE: semi-colons are being handled elsewhere
Expand Down Expand Up @@ -5578,8 +5611,9 @@ Reset
token: "",
}]

module.forbidTrailingMemberProperty = [false]
module.forbidIndentedApplication = [false]
module.forbidTrailingMemberProperty = [false]
module.forbidMultiLineImplicitObjectLiteral = [false]
module.JSXTagStack = []

module.operators = new Set
Expand All @@ -5605,6 +5639,12 @@ Reset
return s[s.length-1]
},
},
multiLineImplicitObjectLiteralForbidden: {
get() {
const {forbidMultiLineImplicitObjectLiteral: s} = module
return s[s.length-1]
},
},
currentJSXTag: {
get() {
const {JSXTagStack: s} = module
Expand Down
13 changes: 13 additions & 0 deletions test/switch.civet
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ describe "switch", ->
}
"""

testCase """
case doesn't allow object literal
---
switch x
case 1:
2
---
switch(x) {
case 1:
2
}
"""

testCase """
multiple cases
---
Expand Down