-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1240 from glimmerjs/make-inline-if-and-unless-key…
…words [FEATURE] Adds inline if and unless keywords
- Loading branch information
Showing
17 changed files
with
591 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/@glimmer/compiler/lib/passes/1-normalization/keywords/utils/if-unless.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ASTv2, generateSyntaxError } from '@glimmer/syntax'; | ||
|
||
import { Err, Ok, Result } from '../../../../shared/result'; | ||
|
||
export function assertValidIfUnlessInlineUsage(type: string, inverted: boolean) { | ||
return ( | ||
originalNode: ASTv2.AppendContent | ASTv2.ExpressionNode | ||
): Result<{ | ||
condition: ASTv2.ExpressionNode; | ||
truthy: ASTv2.ExpressionNode; | ||
falsy: ASTv2.ExpressionNode | null; | ||
}> => { | ||
let node = originalNode.type === 'AppendContent' ? originalNode.value : originalNode; | ||
let named = node.type === 'Call' ? node.args.named : null; | ||
let positional = node.type === 'Call' ? node.args.positional : null; | ||
|
||
if (named && !named.isEmpty()) { | ||
return Err( | ||
generateSyntaxError( | ||
`${type} cannot receive named parameters, received ${named.entries | ||
.map((e) => e.name.chars) | ||
.join(', ')}`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
let condition = positional?.nth(0); | ||
|
||
if (!positional || !condition) { | ||
return Err( | ||
generateSyntaxError( | ||
`When used inline, ${type} requires at least two parameters 1. the condition that determines the state of the ${type}, and 2. the value to return if the condition is ${ | ||
inverted ? 'false' : 'true' | ||
}. Did not receive any parameters`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
let truthy = positional.nth(1); | ||
let falsy = positional.nth(2); | ||
|
||
if (truthy === null) { | ||
return Err( | ||
generateSyntaxError( | ||
`When used inline, ${type} requires at least two parameters 1. the condition that determines the state of the ${type}, and 2. the value to return if the condition is ${ | ||
inverted ? 'false' : 'true' | ||
}. Received only one parameter, the condition`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
if (positional.size > 3) { | ||
return Err( | ||
generateSyntaxError( | ||
`When used inline, ${type} can receive a maximum of three positional parameters 1. the condition that determines the state of the ${type}, 2. the value to return if the condition is ${ | ||
inverted ? 'false' : 'true' | ||
}, and 3. the value to return if the condition is ${ | ||
inverted ? 'true' : 'false' | ||
}. Received ${positional?.size ?? 0} parameters`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
return Ok({ condition, truthy, falsy }); | ||
}; | ||
} |
Oops, something went wrong.