Skip to content

Commit

Permalink
Strict only helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Apr 13, 2021
1 parent fb1dcf3 commit be501f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const CALL_KEYWORDS = keywords('Call')
.kw('has-block-params', hasBlockKeyword('has-block-params'))
.kw('-get-dynamic-var', getDynamicVarKeyword)
.kw('log', logKeyword)
.kw('eq', equalKeyword)
.kw('neq', notEqualKeyword)
.kw('eq', equalKeyword, { strictOnly: true })
.kw('neq', notEqualKeyword, { strictOnly: true })
.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 @@ -34,7 +34,8 @@ class KeywordImpl<
constructor(
protected keyword: S,
type: KeywordType,
private delegate: KeywordDelegate<KeywordMatches[K], Param, Out>
private delegate: KeywordDelegate<KeywordMatches[K], Param, Out>,
private options?: { strictOnly: boolean },
) {
let nodes = new Set<KeywordNode['type']>();
for (let nodeType of KEYWORD_NODES[type]) {
Expand All @@ -44,7 +45,14 @@ class KeywordImpl<
this.types = nodes;
}

protected match(node: KeywordCandidates[K]): node is KeywordMatches[K] {
protected match(node: KeywordCandidates[K], state: NormalizationState): node is KeywordMatches[K] {
// some keywords are enabled only in strict mode. None are planned to be loose mode only
if (this.options?.strictOnly) {
if (state.isStrict === false) {
return false;
}
}

if (!this.types.has(node.type)) {
return false;
}
Expand All @@ -67,7 +75,7 @@ class KeywordImpl<
}

translate(node: KeywordMatches[K], state: NormalizationState): Result<Out> | null {
if (this.match(node)) {
if (this.match(node, state)) {
let path = getCalleeExpression(node);

if (path !== null && path.type === 'Path' && path.tail.length > 0) {
Expand Down Expand Up @@ -136,8 +144,8 @@ export function keyword<
K extends KeywordType,
D extends KeywordDelegate<KeywordMatches[K], unknown, Out>,
Out = unknown
>(keyword: string, type: K, delegate: D): Keyword<K, Out> {
return new KeywordImpl(keyword, type, delegate as KeywordDelegate<KeywordMatch, unknown, Out>);
>(keyword: string, type: K, delegate: D, options?: { strictOnly: boolean }): Keyword<K, Out> {
return new KeywordImpl(keyword, type, delegate as KeywordDelegate<KeywordMatch, unknown, Out>, options);
}

export type PossibleKeyword = KeywordNode;
Expand Down Expand Up @@ -177,9 +185,10 @@ export class Keywords<K extends KeywordType, KeywordList extends Keyword<K> = ne

kw<S extends string = string, Out = unknown>(
name: S,
delegate: KeywordDelegate<KeywordMatches[K], unknown, Out>
delegate: KeywordDelegate<KeywordMatches[K], unknown, Out>,
options?: { strictOnly: boolean }
): Keywords<K, KeywordList | Keyword<K, Out>> {
this.#keywords.push(keyword(name, this.#type, delegate));
this.#keywords.push(keyword(name, this.#type, delegate, options));

return this;
}
Expand Down

0 comments on commit be501f0

Please sign in to comment.