Skip to content
Merged
5 changes: 5 additions & 0 deletions libraries/adaptive-expressions/src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ExpressionEvaluator, EvaluateExpressionDelegate } from './expressionEva
import { ExpressionType } from './expressionType';
import { SimpleObjectMemory, MemoryInterface } from './memory';
import { Extensions } from './extensions';
import { ExpressionEngine } from './parser';

/**
* Type expected from evalating an expression.
Expand Down Expand Up @@ -76,6 +77,10 @@ export class Expression {
this.children = children;
}

public static parse(expression: string): Expression {
return new ExpressionEngine().parse(expression);
}

/**
* Make an expression and validate it.
* @param type Type of expression from ExpressionType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SimpleObjectMemory implements MemoryInterface {
}

public getValue(path: string): any {
if (this.memory === undefined) {
if (this.memory === undefined || path.length === 0 || (path[0] !== '[' && !path[0].match(/[a-z]/i))) {
return undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/adaptive-expressions/tests/badExpression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const badExpressions =
'split(one, \'l\')', // split only accept string parameter
'split(hello, 1)', // split only accept string parameter
'substring(hello, 0.5)', // the second parameter of substring must be integer
'substring(one, 0)', // the first parameter of substring must be string
'substring(two, 0)', // the first parameter of substring must be string or null
'substring(hello, 10)', // the start index is out of the range of the string length
'substring(hello, 0, hello)', // length is not integer
'substring(hello, 0, \'hello\')', // length is not integer
Expand Down
13 changes: 6 additions & 7 deletions libraries/adaptive-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable @typescript-eslint/no-var-requires */
const { ExpressionEngine, Extensions, SimpleObjectMemory, ExpressionFunctions } = require('../');
const { Expression, Extensions, SimpleObjectMemory, ExpressionFunctions } = require('../');
const assert = require('assert');
const moment = require('moment');

Expand Down Expand Up @@ -629,7 +629,7 @@ describe('expression functional test', () => {
for (const data of dataSource) {
const input = data[0].toString();
console.log(input);
var parsed = new ExpressionEngine().parse(input);
var parsed = Expression.parse(input);
assert(parsed !== undefined);
var { value: actual, error } = parsed.tryEvaluate(scope);
assert(error === undefined, `input: ${ input }, Has error: ${ error }`);
Expand Down Expand Up @@ -670,28 +670,27 @@ describe('expression functional test', () => {
n: 2
};
const memory = new SimpleObjectMemory(scope);
let parser = new ExpressionEngine();

// normal case, note, we doesn't append a " yet
let exp = parser.parse('a[f].b[n].z');
let exp = Expression.parse('a[f].b[n].z');
let path = undefined;
let left = undefined;
let error = undefined;
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'a[\'foo\'].b[2].z');

// normal case
exp = parser.parse('a[z.z][z.z].y');
exp = Expression.parse('a[z.z][z.z].y');
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'a[\'zar\'][\'zar\'].y');

// normal case
exp = parser.parse('a.b[z.z]');
exp = Expression.parse('a.b[z.z]');
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'a.b[\'zar\']');

// stop evaluate at middle
exp = parser.parse('json(x).b');
exp = Expression.parse('json(x).b');
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'b');

Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-lg/tests/ActivityChecker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { LGParser, ActivityChecker } = require('../lib');
const assert = require('assert');

function getTemplateEngine(){
const filePath = `${ __dirname }/testData/Examples/DiagnosticStructuredLG.lg`;
const filePath = `${ __dirname }/testData/examples/DiagnosticStructuredLG.lg`;
return LGParser.parseFile(filePath);
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-lg/tests/ActivityFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { LGParser, ActivityFactory } = require('../lib');
const assert = require('assert');

function getTemplateEngine(){
const filePath = `${ __dirname }/testData/Examples/NormalStructuredLG.lg`;
const filePath = `${ __dirname }/testData/examples/NormalStructuredLG.lg`;
return LGParser.parseFile(filePath);
}

Expand Down