Skip to content

Commit

Permalink
Add not keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Mar 6, 2021
1 parent 7675ebb commit 3e45f7e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { equalKeyword, notEqualKeyword } from './utils/equality';
import { hasBlockKeyword } from './utils/has-block';
import { ifUnlessInlineKeyword } from './utils/if-unless';
import { logKeyword } from './utils/log';
import { andKeyword, orKeyword } from './utils/logical';
import { andKeyword, notKeyword, orKeyword } from './utils/logical';

export const CALL_KEYWORDS = keywords('Call')
.kw('has-block', hasBlockKeyword('has-block'))
Expand All @@ -23,6 +23,7 @@ export const CALL_KEYWORDS = keywords('Call')
.kw('gte', comparisonKeyword('gte'))
.kw('and', andKeyword)
.kw('or', orKeyword)
.kw('not', notKeyword)
.kw('if', ifUnlessInlineKeyword('if'))
.kw('unless', ifUnlessInlineKeyword('unless'))
.kw('component', curryKeyword(CurriedType.Component))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,43 @@ export const orKeyword: KeywordDelegate<
assert: assertOrKeyword,
translate: translateOrKeyword,
};

function assertNotKeyword(node: GenericKeywordNode): Result<ASTv2.ExpressionNode> {
let {
args: { named, positional },
} = node;

if (named && !named.isEmpty()) {
return Err(generateSyntaxError(`(not) does not take any named arguments`, node.loc));
}

if (positional?.size > 1) {
return Err(generateSyntaxError(`(not) only accepts 1 argument`, node.loc));
}

let value = positional?.nth(0);

if (!value) {
return Err(generateSyntaxError(`(not) requires 1 argument`, node.loc));
}

return Ok(value);
}

function translateNotKeyword(
{ node, state }: { node: ASTv2.CallExpression; state: NormalizationState },
value: ASTv2.ExpressionNode
): Result<mir.Not> {
return VISIT_EXPRS.visit(value, state).mapOk(
(result) => new mir.Not({ value: result, loc: node.loc })
);
}

export const notKeyword: KeywordDelegate<
ASTv2.CallExpression | ASTv2.AppendContent,
ASTv2.ExpressionNode,
mir.Not
> = {
assert: assertNotKeyword,
translate: translateNotKeyword,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderTest, test, jitSuite } from '../..';
import { RenderTest, test, jitSuite, syntaxErrorFor } from '../..';

class AndTest extends RenderTest {
static suiteName = '{{and}} keyword';
Expand Down Expand Up @@ -119,3 +119,30 @@ class OrTest extends RenderTest {
}

jitSuite(OrTest);

class NotTest extends RenderTest {
static suiteName = '{{not}} keyword';

@test
['it works']() {
this.render(`{{not 1}}`);

this.assertHTML('false');
}

@test
['it works falsey']() {
this.render(`{{not 0}}`);

this.assertHTML('true');
}

@test
['it errors with too many arguments']() {
this.assert.throws(() => {
this.render(`{{not 1 2}}`);
}, syntaxErrorFor('(not) only accepts 1 argument', '{{not 1 2}}', 'an unknown module', 1, 0));
}
}

jitSuite(NotTest);

0 comments on commit 3e45f7e

Please sign in to comment.