New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@feedloop/expression-parser

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@feedloop/expression-parser - npm Package Compare versions

Comparing version 0.1.14 to 0.1.15

3

lib/suggestion.d.ts

@@ -31,2 +31,3 @@ import { FunctionConfig, OperatorConfig } from ".";

value: string;
score: number;
start: number;

@@ -37,2 +38,3 @@ end: number;

value: string;
score: number;
start: number;

@@ -43,2 +45,3 @@ end: number;

value: string;
score: number;
start: number;

@@ -45,0 +48,0 @@ end: number;

41

lib/suggestion.js

@@ -15,2 +15,22 @@ "use strict";

exports.getContextAtPosition = exports.getSuggestionsAtPosition = void 0;
var fuzzyMatch = function (rawPattern, suggestion) {
var pattern = rawPattern.replace(/\s/g, "").toLowerCase();
var name = suggestion.name.replace(/\s/g, "").toLowerCase();
if (pattern === name) {
return Infinity;
}
var totalScore = pattern.split("").reduce(function (_a, patternChar) {
var totalScore = _a.totalScore, consecutiveScore = _a.consecutiveScore, seq = _a.seq;
var patternIndex = seq.indexOf(patternChar);
if (patternIndex === -1)
return { totalScore: -Infinity, consecutiveScore: 0, seq: "" };
var score = patternIndex === 0 ? consecutiveScore * 2 + 1 : 1;
return {
totalScore: totalScore + score,
consecutiveScore: score,
seq: seq.slice(patternIndex + 1),
};
}, { totalScore: 0, consecutiveScore: 0, seq: name }).totalScore;
return totalScore;
};
var getSuggestionsAtPosition = function (parserOptions, expression, cursorPosition) {

@@ -34,2 +54,3 @@ var getFunctionSuggestions = function (leftType) {

examples: config.examples,
score: config.return.type === "any" ? 0 : 1,
start: cursorPosition,

@@ -61,2 +82,3 @@ end: cursorPosition,

examples: config.examples,
score: config.return.type === "any" ? 0 : 1,
start: cursorPosition,

@@ -80,2 +102,3 @@ end: cursorPosition,

type: config.type,
score: config.type === "any" ? 0 : 1,
start: cursorPosition,

@@ -96,2 +119,3 @@ end: cursorPosition,

type: "boolean",
score: 1,
start: cursorPosition,

@@ -133,9 +157,8 @@ end: cursorPosition,

return getTermSuggestions(expression.type, cursorPosition)
.filter(function (suggestion) {
return suggestion.name !== expression.name &&
suggestion.name
.toLowerCase()
.startsWith(expression.name.toLowerCase());
})
.map(function (suggestion) { return (__assign(__assign({}, suggestion), { start: expression.start })); });
.map(function (suggestion) { return (__assign(__assign({}, suggestion), { start: expression.start, score: fuzzyMatch(expression.name, suggestion) })); })
.map(function (suggestion) { return suggestion; })
.filter(function (_a) {
var score = _a.score;
return score >= 0;
});
}

@@ -164,3 +187,3 @@ case "boolean":

.sort(function (a, b) { return a.name.localeCompare(b.name); })
.sort(function (a, b) { return (a === b ? 0 : a.type === "any" ? 1 : -1); }));
.sort(function (a, b) { return b.score - a.score; }));
};

@@ -181,3 +204,3 @@ exports.getSuggestionsAtPosition = getSuggestionsAtPosition;

return fn.name === expression.name &&
fn.parameters.every(function (param, i) { return param.type === expression.arguments[i].type; });
fn.parameters.every(function (param, i) { var _a; return param.type === ((_a = expression.arguments[i]) === null || _a === void 0 ? void 0 : _a.type); });
}) ||

@@ -184,0 +207,0 @@ parserOptions.functions.find(function (fn) { return fn.name === expression.name; });

@@ -50,3 +50,4 @@ "use strict";

return config.parameters.find(function (param, i) {
return checkType(expression.arguments[i], param.type)[1].length === 0;
return !!expression.arguments[i] &&
checkType(expression.arguments[i], param.type)[1].length === 0;
});

@@ -78,3 +79,14 @@ }) || configs[0];

var _b = __read(_a, 2), sumArgs = _b[0], sumErrs = _b[1];
var _c = __read(checkType(expression.arguments[i], param.type), 2), typedArg = _c[0], errors = _c[1];
var argument = expression.arguments[i];
if (!argument)
return [
sumArgs,
sumErrs.concat({
start: expression.end - 1,
end: expression.end - 1,
id: (0, parser_utils_1.generateErrorId)(),
message: "missing parameter ".concat(param.name, " of type ").concat(param.type),
}),
];
var _c = __read(checkType(argument, param.type), 2), typedArg = _c[0], errors = _c[1];
return [sumArgs.concat(typedArg), sumErrs.concat(errors)];

@@ -81,0 +93,0 @@ }, [[], []]), 2), args = _a[0], argumentErrors = _a[1];

{
"name": "@feedloop/expression-parser",
"version": "0.1.14",
"version": "0.1.15",
"main": "lib/index.js",

@@ -5,0 +5,0 @@ "types": "lib/index.d.ts",

import { FunctionConfig, OperatorConfig } from ".";
import { ExpressionSyntax, ParserOptions } from "./parser";
import { ParserOptions } from "./parser";
import { TypedExpression } from "./type-checker";

@@ -13,3 +13,2 @@

export type FunctionExpressionInfo = BaseExpressionInfo & {

@@ -44,2 +43,3 @@ kind: "function";

value: string;
score: number;
start: number;

@@ -51,2 +51,3 @@ end: number;

value: string;
score: number;
start: number;

@@ -58,2 +59,3 @@ end: number;

value: string;
score: number;
start: number;

@@ -68,2 +70,25 @@ end: number;

const fuzzyMatch = (rawPattern: string, suggestion: Suggestion): number => {
const pattern = rawPattern.replace(/\s/g, "").toLowerCase();
const name = suggestion.name.replace(/\s/g, "").toLowerCase();
if (pattern === name) {
return Infinity;
}
const { totalScore } = pattern.split("").reduce(
({ totalScore, consecutiveScore, seq }, patternChar) => {
const patternIndex = seq.indexOf(patternChar);
if (patternIndex === -1)
return { totalScore: -Infinity, consecutiveScore: 0, seq: "" };
const score = patternIndex === 0 ? consecutiveScore * 2 + 1 : 1;
return {
totalScore: totalScore + score,
consecutiveScore: score,
seq: seq.slice(patternIndex + 1),
};
},
{ totalScore: 0, consecutiveScore: 0, seq: name }
);
return totalScore;
};
export const getSuggestionsAtPosition = (

@@ -93,2 +118,3 @@ parserOptions: ParserOptions,

examples: config.examples,
score: config.return.type === "any" ? 0 : 1,
start: cursorPosition,

@@ -120,2 +146,3 @@ end: cursorPosition,

examples: config.examples,
score: config.return.type === "any" ? 0 : 1,
start: cursorPosition,

@@ -140,2 +167,3 @@ end: cursorPosition,

type: config.type,
score: config.type === "any" ? 0 : 1,
start: cursorPosition,

@@ -160,2 +188,3 @@ end: cursorPosition,

type: "boolean",
score: 1,
start: cursorPosition,

@@ -215,10 +244,9 @@ end: cursorPosition,

return getTermSuggestions(expression.type, cursorPosition)
.filter(
(suggestion) =>
suggestion.name !== expression.name &&
suggestion.name
.toLowerCase()
.startsWith(expression.name.toLowerCase())
)
.map((suggestion) => ({ ...suggestion, start: expression.start }));
.map((suggestion) => ({
...suggestion,
start: expression.start,
score: fuzzyMatch(expression.name, suggestion),
}))
.map((suggestion) => suggestion)
.filter(({ score }) => score >= 0);
}

@@ -254,3 +282,3 @@

.sort((a, b) => a.name.localeCompare(b.name))
.sort((a, b) => (a === b ? 0 : a.type === "any" ? 1 : -1))
.sort((a, b) => b.score - a.score)
);

@@ -286,3 +314,3 @@ };

fn.parameters.every(
(param, i) => param.type === expression.arguments[i].type
(param, i) => param.type === expression.arguments[i]?.type
)

@@ -289,0 +317,0 @@ ) ||

@@ -84,2 +84,3 @@ import {

(param, i) =>
!!expression.arguments[i] &&
checkType(expression.arguments[i], param.type)[1].length === 0

@@ -112,6 +113,14 @@ )

([sumArgs, sumErrs], param, i) => {
const [typedArg, errors] = checkType(
expression.arguments[i],
param.type
);
const argument = expression.arguments[i];
if (!argument)
return [
sumArgs,
sumErrs.concat({
start: expression.end - 1,
end: expression.end - 1,
id: generateErrorId(),
message: `missing parameter ${param.name} of type ${param.type}`,
}),
];
const [typedArg, errors] = checkType(argument, param.type);
return [sumArgs.concat(typedArg), sumErrs.concat(errors)];

@@ -118,0 +127,0 @@ },

@@ -5,4 +5,4 @@ import { parseExpressionWithoutSuggestions } from "./parseHelpers";

it("should parse non-primitive value", () => {
expect(parseExpressionWithoutSuggestions("format_date(NOW())")).
toMatchInlineSnapshot(`
expect(parseExpressionWithoutSuggestions("format_date(NOW())"))
.toMatchInlineSnapshot(`
Object {

@@ -32,4 +32,4 @@ "ast": Object {

it("should not parse mismatched argument type", () => {
expect(parseExpressionWithoutSuggestions('add("23", 12)')).
toMatchInlineSnapshot(`
expect(parseExpressionWithoutSuggestions('add("23", 12)'))
.toMatchInlineSnapshot(`
Object {

@@ -79,4 +79,4 @@ "ast": Object {

it("should not parse mismatched argument type from function return", () => {
expect(parseExpressionWithoutSuggestions('add(lower("23"), 12)')).
toMatchInlineSnapshot(`
expect(parseExpressionWithoutSuggestions('add(lower("23"), 12)'))
.toMatchInlineSnapshot(`
Object {

@@ -135,4 +135,4 @@ "ast": Object {

it("should parse nested function argument", () => {
expect(parseExpressionWithoutSuggestions("add(add(23, 12), 15)")).
toMatchInlineSnapshot(`
expect(parseExpressionWithoutSuggestions("add(add(23, 12), 15)"))
.toMatchInlineSnapshot(`
Object {

@@ -184,4 +184,4 @@ "ast": Object {

it("should parse operator in argument", () => {
expect(parseExpressionWithoutSuggestions("add(12 + 23, 15)")).
toMatchInlineSnapshot(`
expect(parseExpressionWithoutSuggestions("add(12 + 23, 15)"))
.toMatchInlineSnapshot(`
Object {

@@ -231,5 +231,90 @@ "ast": Object {

it("should parse a function with arguments less than defined config", () => {
expect(parseExpressionWithoutSuggestions('Concatenate("Hello")'))
.toMatchInlineSnapshot(`
Object {
"ast": Object {
"arguments": Array [
Object {
"end": 19,
"expressions": Array [],
"kind": "string",
"parts": Array [
Object {
"end": 18,
"start": 13,
"value": "Hello",
},
],
"start": 12,
"type": "string",
},
],
"end": 20,
"kind": "function",
"name": "Concatenate",
"start": 0,
"type": "string",
},
"errors": Array [
Object {
"end": 19,
"id": 4,
"message": "missing parameter part of type string",
"start": 19,
},
],
}
`);
});
it("should parse a function with arguments more than defined config", () => {
expect(
parseExpressionWithoutSuggestions('Concatenate("Hello", ",", "World")')
).toMatchInlineSnapshot(`
Object {
"ast": Object {
"arguments": Array [
Object {
"end": 19,
"expressions": Array [],
"kind": "string",
"parts": Array [
Object {
"end": 18,
"start": 13,
"value": "Hello",
},
],
"start": 12,
"type": "string",
},
Object {
"end": 24,
"expressions": Array [],
"kind": "string",
"parts": Array [
Object {
"end": 23,
"start": 22,
"value": ",",
},
],
"start": 21,
"type": "string",
},
],
"end": 34,
"kind": "function",
"name": "Concatenate",
"start": 0,
"type": "string",
},
"errors": Array [],
}
`);
});
it("should parse a placeholder", () => {
expect(parseExpressionWithoutSuggestions("add(12 + 23,, 5)")).
toMatchInlineSnapshot(`
expect(parseExpressionWithoutSuggestions("add(12 + 23,, 5)"))
.toMatchInlineSnapshot(`
Object {

@@ -262,3 +347,3 @@ "ast": Object {

"end": 12,
"errorID": 4,
"errorID": 5,
"kind": "placeholder",

@@ -278,3 +363,3 @@ "start": 12,

"end": 12,
"id": 5,
"id": 6,
"message": "expected type of number",

@@ -281,0 +366,0 @@ "start": 12,

@@ -133,2 +133,24 @@ import {

},
{
name: "Cncatenate",
description: "concatenate 2 strings together used to test fuzzy search",
parameters: [
{
name: "part",
description: "part of string",
type: "string",
},
{
name: "part",
description: "part of string",
type: "string",
},
],
return: {
type: "string",
},
examples: [],
evaluate: (str1: string, str2: string) => str1.concat(str2),
generate: (str1: string, str2: string) => `(${str1} || ${str2})`,
},
];

@@ -135,0 +157,0 @@

@@ -5,26 +5,32 @@ import { parseExpression } from "./parseHelpers";

it("can generate suggestion", () => {
expect(parseExpression("1 + 1").suggestions).toMatchInlineSnapshot(`Array []`);
expect(parseExpression("1 + 1").suggestions).toMatchInlineSnapshot(
`Array []`
);
expect(parseExpression("true &").suggestions).toMatchInlineSnapshot(`
Array [
Object {
"end": 6,
"kind": "variable",
"name": "true",
"start": 6,
"type": "boolean",
"value": "true",
},
Object {
"end": 6,
"kind": "variable",
"name": "false",
"start": 6,
"type": "boolean",
"value": "false",
},
]
`);
Array [
Object {
"end": 6,
"kind": "variable",
"name": "false",
"score": 1,
"start": 6,
"type": "boolean",
"value": "false",
},
Object {
"end": 6,
"kind": "variable",
"name": "true",
"score": 1,
"start": 6,
"type": "boolean",
"value": "true",
},
]
`);
expect(parseExpression("add(5,4)").suggestions).toMatchInlineSnapshot(`Array []`);
expect(parseExpression("add(5,4)").suggestions).toMatchInlineSnapshot(
`Array []`
);
});

@@ -34,65 +40,65 @@

expect(parseExpression("(1 + add(5, ))", 7).context).toMatchInlineSnapshot(`
Object {
"description": "add two numbers",
"examples": Array [],
"kind": "function",
"name": "add",
"output": undefined,
"parameters": Array [
Object {
"description": "Number",
"name": "number",
"description": "add two numbers",
"examples": Array [],
"kind": "function",
"name": "add",
"output": undefined,
"parameters": Array [
Object {
"description": "Number",
"name": "number",
"type": "number",
},
Object {
"description": "Number",
"name": "number",
"type": "number",
},
],
"type": "number",
},
Object {
"description": "Number",
"name": "number",
"type": "number",
},
],
"type": "number",
}
`);
}
`);
expect(parseExpression("(1 + add(5, ))", 11).context)
.toMatchInlineSnapshot(`
Object {
"description": "add two numbers",
"examples": Array [],
"kind": "function",
"name": "add",
"output": undefined,
"parameters": Array [
Object {
"description": "Number",
"name": "number",
"description": "add two numbers",
"examples": Array [],
"kind": "function",
"name": "add",
"output": undefined,
"parameters": Array [
Object {
"description": "Number",
"name": "number",
"type": "number",
},
Object {
"description": "Number",
"name": "number",
"type": "number",
},
],
"type": "number",
},
Object {
"description": "Number",
"name": "number",
"type": "number",
},
],
"type": "number",
}
`);
}
`);
expect(parseExpression("lower(upper())", 9).context).toMatchInlineSnapshot(`
Object {
"description": "convert text to uppercase",
"examples": Array [],
"kind": "function",
"name": "upper",
"output": undefined,
"parameters": Array [
Object {
"description": "Text",
"name": "text",
"description": "convert text to uppercase",
"examples": Array [],
"kind": "function",
"name": "upper",
"output": undefined,
"parameters": Array [
Object {
"description": "Text",
"name": "text",
"type": "string",
},
],
"type": "string",
},
],
"type": "string",
}
`);
}
`);

@@ -106,18 +112,18 @@ expect(parseExpression("lower() + 1").context).toMatchInlineSnapshot(

).toMatchInlineSnapshot(`
Object {
"description": "convert text to uppercase",
"examples": Array [],
"kind": "function",
"name": "upper",
"output": undefined,
"parameters": Array [
Object {
"description": "Text",
"name": "text",
"description": "convert text to uppercase",
"examples": Array [],
"kind": "function",
"name": "upper",
"output": undefined,
"parameters": Array [
Object {
"description": "Text",
"name": "text",
"type": "string",
},
],
"type": "string",
},
],
"type": "string",
}
`);
}
`);
});

@@ -128,54 +134,6 @@

.toMatchInlineSnapshot(`
Array [
Object {
"description": undefined,
"end": 13,
"kind": "variable",
"name": "ID",
"start": 13,
"type": "number",
"value": "ID",
},
Object {
"description": "add two numbers",
"end": 13,
"examples": Array [],
"kind": "function",
"name": "add",
"output": undefined,
"parameters": Array [
Object {
"description": "Number",
"name": "number",
"type": "number",
},
Object {
"description": "Number",
"name": "number",
"type": "number",
},
],
"start": 13,
"type": "number",
"value": "add(,)",
},
]
`);
});
it("can generate suggestion for placeholder", () => {
expect(parseExpression("add(5, )", 7).suggestions).toMatchInlineSnapshot(`
Array [
Object {
"description": undefined,
"end": 7,
"kind": "variable",
"name": "ID",
"start": 7,
"type": "number",
"value": "ID",
},
Object {
"description": "add two numbers",
"end": 7,
"end": 13,
"examples": Array [],

@@ -197,6 +155,17 @@ "kind": "function",

],
"start": 7,
"score": 1,
"start": 13,
"type": "number",
"value": "add(,)",
},
Object {
"description": undefined,
"end": 13,
"kind": "variable",
"name": "ID",
"score": 1,
"start": 13,
"type": "number",
"value": "ID",
},
]

@@ -206,8 +175,8 @@ `);

it("can search function and variable", () => {
expect(parseExpression("1 + a").suggestions).toMatchInlineSnapshot(`
it("can generate suggestion for placeholder", () => {
expect(parseExpression("add(5, )", 7).suggestions).toMatchInlineSnapshot(`
Array [
Object {
"description": "add two numbers",
"end": 5,
"end": 7,
"examples": Array [],

@@ -229,9 +198,75 @@ "kind": "function",

],
"start": 4,
"score": 1,
"start": 7,
"type": "number",
"value": "add(,)",
},
Object {
"description": undefined,
"end": 7,
"kind": "variable",
"name": "ID",
"score": 1,
"start": 7,
"type": "number",
"value": "ID",
},
]
`);
});
it("can search function and variable", () => {
expect(parseExpression("lower(cnt)", 9).suggestions).toMatchInlineSnapshot(`
Array [
Object {
"description": "concatenate 2 strings together used to test fuzzy search",
"end": 9,
"examples": Array [],
"kind": "function",
"name": "Cncatenate",
"output": undefined,
"parameters": Array [
Object {
"description": "part of string",
"name": "part",
"type": "string",
},
Object {
"description": "part of string",
"name": "part",
"type": "string",
},
],
"score": 5,
"start": 6,
"type": "string",
"value": "Cncatenate(,)",
},
Object {
"description": "concatenate 2 strings together",
"end": 9,
"examples": Array [],
"kind": "function",
"name": "Concatenate",
"output": undefined,
"parameters": Array [
Object {
"description": "part of string",
"name": "part",
"type": "string",
},
Object {
"description": "part of string",
"name": "part",
"type": "string",
},
],
"score": 3,
"start": 6,
"type": "string",
"value": "Concatenate(,)",
},
]
`);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc