Skip to content

Commit

Permalink
Fix type resolving on coalescing operators
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
valtzu committed Jun 15, 2024
1 parent 090c53f commit a7fe857
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/syntax.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Call {
}

TernaryExpression {
expression !right '?' expression ':' expression
expression !right o<'?'> expression ':' expression
}

@tokens {
Expand All @@ -80,15 +80,15 @@ TernaryExpression {

space { $[ \t\n\r]+ }

Operator { '===' | '!==' | '||' | '&&' | '==' | '!=' | '>=' | '<=' | '..' | '**' | '!' | '|' | '^' | '&' | '<' | '>' | '+' | '-' | '~' | '*' | '/' | '%' | '??' | '?:' }
Operator { '===' | '!==' | '||' | '&&' | '==' | '!=' | '>=' | '<=' | '..' | '**' | '!' | '|' | '^' | '&' | '<' | '>' | '+' | '-' | '~' | '*' | '/' | '%' | '??' | '?:' | '?' }
MemberOf { '.' }
NullSafeMemberOf { '?.' }

@precedence {
String,
NullSafeMemberOf,
Operator,
MemberOf,
NullSafeMemberOf,
Number,
word
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export function resolveTypes(state: EditorState, node: SyntaxNode | undefined, c
} else if (node.name === 'TernaryExpression' && node.firstChild && node.firstChild.nextSibling && node.firstChild.nextSibling.nextSibling) {
resolveTypes(state, node.firstChild.nextSibling, config, matchExact).forEach(x => types.add(x));
resolveTypes(state, node.firstChild.nextSibling.nextSibling, config, matchExact).forEach(x => types.add(x));
} else if (node.name === 'BinaryExpression' && node.firstChild?.nextSibling?.name == 'Operator' && node.lastChild) {
const operator = state.sliceDoc(node.firstChild.nextSibling.from, node.firstChild.nextSibling.to);
if (operator == '?:' || operator == '??') {
resolveTypes(state, node.firstChild, config, matchExact).forEach(x => types.add(x));
}
if (operator == '?:' || operator == '??' || operator == '?') {
resolveTypes(state, node.lastChild, config, matchExact).forEach(x => types.add(x));
}
}

if (types.size === 0) {
Expand Down
16 changes: 16 additions & 0 deletions test/cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ condition ? "yes" : "no"

Expression(TernaryExpression(Variable, String, String))

# Ternary expression shortcut

condition ? "yes"

==>

Expression(BinaryExpression(Variable, Operator, String))

# Ternary expression shortcut 2

condition ?: "yes"

==>

Expression(BinaryExpression(Variable, Operator, String))

# Strings

["hello", "one\"\\two", 't"h"ree']
Expand Down
16 changes: 16 additions & 0 deletions test/test-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,20 @@ describe("Expression language completion", () => {
ist("property22", c[1].label);
ist("firstMethod()", c[2].label);
});

it("does complete after ternary expression shortcut", () => {
let c = get("(foobar ? obj).‸")?.options;
ist(c?.length, 3);
ist("property11", c[0].label);
ist("property22", c[1].label);
ist("firstMethod()", c[2].label);
});

it("does complete after ternary expression shortcut 2", () => {
let c = get("(foobar ?: obj).‸")?.options;
ist(c?.length, 3);
ist("property11", c[0].label);
ist("property22", c[1].label);
ist("firstMethod()", c[2].label);
});
});

0 comments on commit a7fe857

Please sign in to comment.