Skip to content

Commit

Permalink
Fix suggestions for and, or and ?? operators
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Sep 10, 2024
1 parent 4c7a403 commit f91dc58
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 79 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## next

- Improved suggestions for the ternary operator (`?:`)
- Disallowed duplicate parameter names in function definitions, i.e., `($a, $a) => expr` is now prohibited
- Improved suggestions for the ternary operator (`?:`)
- Fixed suggestions for `and`, `or` and `??` operators
- Fixed a compilation error for the generated code of `function` expressions in stat mode in some cases
- Fixed a runtime error for a `comparator function` in some scenarios
- Fixed support for TypedArrays with slice notation and methods such as `numbers()`, `avg()`, `count()`, `sum()`, `median()`, `variance()`, `stdev()` and `percentile()`
Expand Down
8 changes: 4 additions & 4 deletions src/lang/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function compileInternal(ast, kind, tolerant = false, suggestions = null) {

function createScope(fn, defCurrent, $ref = '$') {
const prevScope = ctx.scope;
const scopeStart = buffer.length;
const scopeStart = buffer.length - 1;

ctx.scope = prevScope.spawn(prevScope.arg1 || kind === 'method', $ref);

Expand All @@ -88,7 +88,7 @@ function compileInternal(ast, kind, tolerant = false, suggestions = null) {
if (ctx.scope.firstCurrent) {
buffer[ctx.scope.firstCurrent] = stat;
} else {
buffer[scopeStart] = defCurrent(buffer[scopeStart], stat);
buffer[scopeStart] += defCurrent(stat);
}
}

Expand Down Expand Up @@ -239,9 +239,9 @@ function compileInternal(ast, kind, tolerant = false, suggestions = null) {

createScope(
() => walk(ast),
(scopeStart, sp) => {
(sp) => {
buffer.push(')');
return '(' + sp + ',' + scopeStart;
return '(' + sp + ',';
},
'data'
);
Expand Down
24 changes: 18 additions & 6 deletions src/lang/nodes/Binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ export function compile(node, ctx) {
ctx.put(`${ctx.buildinFn('bool')}(${tmpVar}=`);
ctx.node(node.left);
ctx.put(`)?${tmpVar}:`);
ctx.scope.captureCurrent.disabled = true;
ctx.node(node.right);
ctx.scope.captureCurrent.disabled = false;
ctx.createScope(
() => ctx.node(node.right),
(sp) => {
ctx.put(')');
return '(' + sp + ',';
},
ctx.scope.$ref
);
break;
}

Expand All @@ -100,12 +105,19 @@ export function compile(node, ctx) {
case '??': {
const tmpVar = ctx.allocateVar();

// TODO: replace for Nullish coalescing operator (??) instead of ternary operator once drop support for Node.js below 14.0
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
ctx.put(`(${tmpVar}=`);
ctx.node(node.left);
ctx.put(`,${tmpVar}!==null&&${tmpVar}!==undefined)?${tmpVar}:`);
ctx.scope.captureCurrent.disabled = true;
ctx.node(node.right);
ctx.scope.captureCurrent.disabled = false;
ctx.createScope(
() => ctx.node(node.right),
(sp) => {
ctx.put(')');
return '(' + sp + ',';
},
ctx.scope.$ref
);
break;
}

Expand Down
8 changes: 3 additions & 5 deletions src/lang/nodes/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ export function suggest(node, ctx) {
}
export function compile(node, ctx) {
if (node.definitions.length) {
ctx.put('(()=>{');
ctx.createScope(
() => {
for (const definition of node.definitions) {
ctx.scope.awaitInit.add(definition.declarator.name);
}

ctx.put('(()=>{');
ctx.list(node.definitions);
ctx.put('return ');
ctx.nodeOrCurrent(node.body);
ctx.put('})()');
},
(scopeStart, sp) => {
return scopeStart + sp + ';';
},
(sp) => sp + ';',
ctx.scope.$ref
);
ctx.put('})()');
} else if (node.body && node.body.type === 'Object') {
ctx.put('(');
ctx.nodeOrCurrent(node.body);
Expand Down
12 changes: 4 additions & 8 deletions src/lang/nodes/Compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ export function compile(node, ctx) {
}

ctx.put(ctx.buildinFn(cmpFn));
ctx.put('((_q=$=>(');
ctx.createScope(
() => {
ctx.put('((_q=$=>(');
ctx.node(node.query);
ctx.put('))(_a),_q(_b))');
},
(scopeStart, sp) => {
return scopeStart + sp + ',';
}
() => ctx.node(node.query),
(sp) => sp + ','
);
ctx.put('))(_a),_q(_b))');
}
export function walk(node, ctx) {
ctx.node(node.query);
Expand Down
16 changes: 7 additions & 9 deletions src/lang/nodes/Conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ export function compile(node, ctx) {
ctx.put(ctx.buildinFn('bool'));
ctx.put('(');
ctx.nodeOrCurrent(node.test);
ctx.put(')?');
ctx.createScope(
() => {
ctx.put(')?');
ctx.nodeOrCurrent(node.consequent);
},
(scopeStart, sp) => {
() => ctx.nodeOrCurrent(node.consequent),
(sp) => {
ctx.put(')');
return scopeStart + '(' + sp + ',';
return '(' + sp + ',';
},
ctx.scope.$ref
);
ctx.put(':');
ctx.createScope(
() => {
ctx.put(':');
if (node.alternate) {
if (node.alternate.type === 'Placeholder') {
ctx.put('(');
Expand All @@ -28,9 +26,9 @@ export function compile(node, ctx) {
ctx.put('undefined');
}
},
(scopeStart, sp) => {
(sp) => {
ctx.put(')');
return scopeStart + '(' + sp + ',';
return '(' + sp + ',';
},
ctx.scope.$ref
);
Expand Down
10 changes: 4 additions & 6 deletions src/lang/nodes/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ export function compile(node, ctx) {
ctx.put(ctx.buildinFn('filter'));
ctx.put('(');
ctx.nodeOrCurrent(node.value);
ctx.put(',$=>');
ctx.createScope(
() => {
ctx.put(',$=>');
ctx.node(node.query);
},
(scopeStart, sp) => {
() => ctx.node(node.query),
(sp) => {
ctx.put(')');
return scopeStart + '(' + sp + ',';
return '(' + sp + ',';
}
);
ctx.put(')');
Expand Down
8 changes: 2 additions & 6 deletions src/lang/nodes/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function compile(node, ctx) {
// but Function doesn't create 2nd argument implicitly to prevent function arity changes
ctx.put('function(');
ctx.put(String(args) || '$');

ctx.put('){return ');
ctx.createScope(
() => {
ctx.scope.arg1 = true;
Expand All @@ -22,14 +22,10 @@ export function compile(node, ctx) {
ctx.scope.add(arg.name);
}

ctx.put('){return ');
ctx.node(node.body);
},
(scopeStart, sp) => {
return scopeStart + sp + ',';
}
(sp) => sp + ','
);

ctx.put('}');
}
export function walk(node, ctx) {
Expand Down
10 changes: 4 additions & 6 deletions src/lang/nodes/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ export function compile(node, ctx) {
ctx.put(ctx.buildinFn('map'));
ctx.put('(');
ctx.nodeOrCurrent(node.value);
ctx.put(',$=>');
ctx.createScope(
() => {
ctx.put(',$=>');
ctx.node(node.query);
},
(scopeStart, sp) => {
() => ctx.node(node.query),
(sp) => {
ctx.put(')');
return scopeStart + '(' + sp + ',';
return '(' + sp + ',';
}
);
ctx.put(')');
Expand Down
10 changes: 4 additions & 6 deletions src/lang/nodes/MapRecursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ export function compile(node, ctx) {
ctx.put(ctx.buildinFn('mapRecursive'));
ctx.put('(');
ctx.nodeOrCurrent(node.value);
ctx.put(',$=>');
ctx.createScope(
() => {
ctx.put(',$=>');
ctx.node(node.query);
},
(scopeStart, sp) => {
() => ctx.node(node.query),
(sp) => {
ctx.put(')');
return scopeStart + '(' + sp + ',';
return '(' + sp + ',';
}
);
ctx.put(')');
Expand Down
13 changes: 4 additions & 9 deletions src/lang/nodes/Pipeline.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
export function compile(node, ctx) {
ctx.put('($=>(');
ctx.createScope(
() => {
ctx.put('($=>(');
ctx.node(node.right);
ctx.put('))');
},
(scopeStart, sp) => {
return scopeStart + sp + ',';
}
() => ctx.node(node.right),
(sp) => sp + ','
);

ctx.put('))');
ctx.put('(');
ctx.nodeOrCurrent(node.left);
ctx.put(')');
Expand Down
18 changes: 5 additions & 13 deletions src/lang/nodes/Postfix.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
export function compile(node, ctx) {
if (node.operator && node.operator.type) {
ctx.put('($=>(');
ctx.createScope(
() => {
ctx.put('($=>(');
ctx.node(node.operator);
ctx.put('))');
},
(scopeStart, sp) => {
return scopeStart + sp + ',';
}
() => ctx.node(node.operator),
(sp) => sp + ','
);
ctx.put('))');
ctx.put('(');
ctx.node(node.argument);
ctx.put(')');
return;
}

switch (node.operator) {
default: {
ctx.error('Unknown operator "' + node.operator + '"', node);
}
}
ctx.error('Unknown operator "' + node.operator + '"', node);
}
export function walk(node, ctx) {
ctx.node(node.argument);
Expand Down
12 changes: 12 additions & 0 deletions test/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,18 @@ describe('query/suggestions', () => {
suggestion('a', ['"foo":value', '"bar":value', 'foo', 'bar'], 3, 4),
suggestion('a', ['"foo":value', '"bar":value', 'foo', 'bar'], 3, 4),
null
],
'[{a:1},{a:2,b:3}].((b?true:[]) and |x|)': [
suggestion('x', ['a', 'b'], 35, 36),
suggestion('x', ['a', 'b'], 35, 36)
],
'[{a:1},{a:2,b:3}].((b?true:[]) or |x|)': [
suggestion('x', ['a'], 34, 35),
suggestion('x', ['a'], 34, 35)
],
'[{a:1},{a:2,b:3}].(b ?? |x|)': [
suggestion('x', ['a'], 24, 25),
suggestion('x', ['a'], 24, 25)
]
});
});
Expand Down

0 comments on commit f91dc58

Please sign in to comment.