Skip to content

Commit efdd58f

Browse files
authored
Clean LG/Expression (#1873)
* init * update * clean LG/Expression * revert ts-node version
1 parent 1a02dff commit efdd58f

29 files changed

+227
-720
lines changed

libraries/adaptive-expressions/src/constant.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,22 @@ export class Constant extends Expression {
4242
}
4343

4444
public toString(): string {
45+
4546
if (this.value === undefined) {
4647
return 'null';
47-
}
48-
49-
if (typeof this.value === 'string') {
50-
if (this.value.includes('\\')) {
51-
this.value = this.value.replace(/\\/g, '\\\\');
48+
} else if (typeof this.value === 'string') {
49+
let result = this.value;
50+
if (result.includes('\\')) {
51+
result = result.replace(/\\/g, '\\\\');
5252
}
5353

54-
return this.value.includes(`'`) ? `"${ this.value }"` : `'${ this.value }'`;
55-
}
56-
57-
if (typeof this.value === 'number') {
54+
return result.includes(`'`) ? `"${ result }"` : `'${ result }'`;
55+
} else if (typeof this.value === 'number') {
5856
return this.value.toString();
57+
} else if(typeof this.value === 'object') {
58+
return JSON.stringify(this.value);
5959
}
6060

61-
if (Array.isArray(this.value)) {
62-
this.value = '[' + this.value.join(' ') + ']';
63-
}
64-
65-
if(typeof this.value === 'object') {
66-
this.value = JSON.stringify(this.value);
67-
}
68-
69-
return this.value === undefined ? undefined : this.value.toString();
61+
return this.value.toString();
7062
}
7163
}

libraries/adaptive-expressions/src/formatConverter.ts renamed to libraries/adaptive-expressions/src/datetimeFormatConverter.ts

File renamed without changes.

libraries/adaptive-expressions/src/expression.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ import { SimpleObjectMemory, MemoryInterface } from './memory';
1313
import { Extensions } from './extensions';
1414
import { ExpressionParser } from './parser';
1515

16-
/**
17-
* key value pair to add method in FunctionTable
18-
*/
19-
type keyValuePair = {
20-
key: string;
21-
value: ExpressionEvaluator;
22-
}
23-
2416
/**
2517
* Type expected from evalating an expression.
2618
*/
@@ -121,7 +113,7 @@ export class Expression {
121113

122114
}
123115

124-
public add(item: keyValuePair | string, value: ExpressionEvaluator = undefined): void{
116+
public add(item: {key: string; value: ExpressionEvaluator} | string, value: ExpressionEvaluator|undefined): void{
125117
if(arguments.length === 1 && item instanceof Object) {
126118
this.set(item.key, item.value);
127119
} else if (arguments.length == 2 && typeof item === 'string') {
@@ -141,7 +133,8 @@ export class Expression {
141133
return this.customFunctions.delete(key);
142134
}
143135

144-
public forEach(callbackfn: (value: ExpressionEvaluator, key: string, map: Map<string, ExpressionEvaluator>) => void, thisArg?: any): void {
136+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
137+
public forEach(_callbackfn: (value: ExpressionEvaluator, key: string, map: Map<string, ExpressionEvaluator>) => void, thisArg?: any): void {
145138
throw Error(`forEach function not implemented`);
146139
}
147140

@@ -184,8 +177,8 @@ export class Expression {
184177
}
185178
}
186179

187-
public static parse(expression: string, lookup: EvaluatorLookup): Expression {
188-
return new ExpressionParser(lookup? lookup : Expression.lookup).parse(expression);
180+
public static parse(expression: string, lookup?: EvaluatorLookup): Expression {
181+
return new ExpressionParser(lookup || Expression.lookup).parse(expression);
189182
}
190183

191184
/**

libraries/adaptive-expressions/src/expressionEvaluator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ export class ExpressionEvaluator {
5656
this.type = type;
5757
this._evaluator = evaluator;
5858
this.returnType = returnType;
59-
// tslint:disable-next-line: no-empty
6059
// eslint-disable-next-line @typescript-eslint/no-unused-vars
61-
this._validator = validator === undefined ? ((expr: Expression): any => { }) : validator;
60+
this._validator = validator || ((expr: Expression): any => { });
6261
}
6362

6463
/**
@@ -71,6 +70,5 @@ export class ExpressionEvaluator {
7170
* Validate an expression.
7271
* @param expression Expression to validate.
7372
*/
74-
// tslint:disable-next-line: informative-docs
7573
public validateExpression = (expression: Expression): void => this._validator(expression);
7674
}

libraries/adaptive-expressions/src/expressionFunctions.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { EvaluateExpressionDelegate, ExpressionEvaluator, ValidateExpressionDele
1818
import { ExpressionType } from './expressionType';
1919
import { Extensions } from './extensions';
2020
import { TimeZoneConverter } from './timeZoneConverter';
21-
import { convertCSharpDateTimeToMomentJS } from './formatConverter';
21+
import { convertCSharpDateTimeToMomentJS } from './datetimeFormatConverter';
2222
import { MemoryInterface, SimpleObjectMemory, StackedMemory } from './memory';
2323

2424
/**
@@ -531,7 +531,6 @@ export class ExpressionFunctions {
531531
(args: any []): any => {
532532
const binaryArgs: any[] = [undefined, undefined];
533533
let soFar: any = args[0];
534-
// tslint:disable-next-line: prefer-for-of
535534
for (let i = 1; i < args.length; i++) {
536535
binaryArgs[0] = soFar;
537536
binaryArgs[1] = args[i];
@@ -557,7 +556,6 @@ export class ExpressionFunctions {
557556
let soFar: any = args[0];
558557
let value: any;
559558
let error: string;
560-
// tslint:disable-next-line: prefer-for-of
561559
for (let i = 1; i < args.length; i++) {
562560
binaryArgs[0] = soFar;
563561
binaryArgs[1] = args[i];
@@ -602,7 +600,7 @@ export class ExpressionFunctions {
602600
* @param func Function to apply.
603601
*/
604602
public static multivariateNumeric(type: string, func: (arg0: any []) => any, verify?: VerifyExpression): ExpressionEvaluator {
605-
return new ExpressionEvaluator(type, ExpressionFunctions.applySequence(func, verify !== undefined ? verify : ExpressionFunctions.verifyNumber),
603+
return new ExpressionEvaluator(type, ExpressionFunctions.applySequence(func, verify || ExpressionFunctions.verifyNumber),
606604
ReturnType.Number, ExpressionFunctions.validateTwoOrMoreThanTwoNumbers);
607605
}
608606
/**
@@ -692,7 +690,6 @@ export class ExpressionFunctions {
692690
return { value: result, error };
693691
},
694692
ReturnType.String,
695-
// tslint:disable-next-line: no-void-expression
696693
(expr: Expression): void => ExpressionFunctions.validateArityAndAnyType(expr, 2, 3, ReturnType.String, ReturnType.Number));
697694
}
698695

@@ -770,7 +767,6 @@ export class ExpressionFunctions {
770767
private static newGuid(): string {
771768
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c: any): string => {
772769
const r: number = Math.random() * 16 | 0;
773-
// tslint:disable-next-line: no-bitwise
774770
const v: number = c === 'x' ? r : (r & 0x3 | 0x8);
775771

776772
return v.toString(16);
@@ -1006,13 +1002,12 @@ export class ExpressionFunctions {
10061002
}
10071003

10081004
if (!error) {
1009-
// 2nd parameter has been rewrite to $local.item
10101005
const iteratorName = (expression.children[1].children[0] as Constant).value as string;
10111006
let arr = [];
10121007
if (Array.isArray(instance)) {
10131008
arr = instance;
10141009
} else if (typeof instance === 'object') {
1015-
Object.keys(instance).forEach(u => arr.push({key: u, value: instance[u]}));
1010+
Object.keys(instance).forEach((u): number => arr.push({key: u, value: instance[u]}));
10161011
} else {
10171012
error = `${ expression.children[0] } is not a collection or structure object to run foreach`;
10181013
}
@@ -1054,7 +1049,7 @@ export class ExpressionFunctions {
10541049
arr = instance;
10551050
isInstanceArray = true;
10561051
} else if (typeof instance === 'object') {
1057-
Object.keys(instance).forEach(u => arr.push({key: u, value: instance[u]}));
1052+
Object.keys(instance).forEach((u): number => arr.push({key: u, value: instance[u]}));
10581053
} else {
10591054
error = `${ expression.children[0] } is not a collection or structure object to run foreach`;
10601055
}
@@ -1116,8 +1111,7 @@ export class ExpressionFunctions {
11161111

11171112
const second: Expression = expression.children[1];
11181113
if (second.returnType === ReturnType.String && second.type === ExpressionType.Constant) {
1119-
// tslint:disable-next-line: restrict-plus-operands
1120-
CommonRegex.CreateRegex((second as Constant).value + '');
1114+
CommonRegex.CreateRegex((second as Constant).value.toString());
11211115
}
11221116
}
11231117

@@ -1389,7 +1383,7 @@ export class ExpressionFunctions {
13891383
({value: propertyName, error} = expression.children[1].tryEvaluate(state));
13901384

13911385
if (!error) {
1392-
propertyName = propertyName === undefined ? '' : propertyName;
1386+
propertyName = propertyName || '';
13931387
}
13941388
if (isDescending) {
13951389
result = lodash.sortBy(arr, propertyName).reverse();
@@ -1439,7 +1433,6 @@ export class ExpressionFunctions {
14391433
let result = '';
14401434
for (const element of stringToConvert) {
14411435
const binaryElement: string = element.charCodeAt(0).toString(2);
1442-
// tslint:disable-next-line: prefer-array-literal
14431436
result += new Array(9 - binaryElement.length).join('0').concat(binaryElement);
14441437
}
14451438

@@ -1784,9 +1777,8 @@ export class ExpressionFunctions {
17841777
}
17851778

17861779
private static flatten(arr: any[], dept: number): any[]{
1787-
dept = typeof dept === 'undefined' ? 1 : dept;
1788-
if (typeof dept !== 'number') {
1789-
return;
1780+
if (!ExpressionFunctions.isNumber(dept) || dept < 1) {
1781+
dept = 1;
17901782
}
17911783

17921784
let res = JSON.parse(JSON.stringify(arr));
@@ -1802,11 +1794,7 @@ export class ExpressionFunctions {
18021794
return res;
18031795
}
18041796

1805-
1806-
1807-
// tslint:disable-next-line: max-func-body-length
18081797
private static getStandardFunctions(): ReadonlyMap<string, ExpressionEvaluator> {
1809-
// tslint:disable-next-line: no-unnecessary-local-variable
18101798
const functions: ExpressionEvaluator[] = [
18111799
//Math
18121800
new ExpressionEvaluator(ExpressionType.Element, ExpressionFunctions.extractElement, ReturnType.Object, this.validateBinary),
@@ -1961,7 +1949,6 @@ export class ExpressionFunctions {
19611949
error = 'Second paramter must be more than zero';
19621950
}
19631951

1964-
// tslint:disable-next-line: prefer-array-literal
19651952
const result: number[] = [...Array(args[1]).keys()].map((u: number): number => u + Number(args[0]));
19661953

19671954
return { value: result, error };
@@ -2034,7 +2021,7 @@ export class ExpressionFunctions {
20342021
new ExpressionEvaluator(
20352022
ExpressionType.Flatten,
20362023
ExpressionFunctions.apply(
2037-
args => {
2024+
(args: any []): any[] => {
20382025
let array = args[0];
20392026
let depth = args.length > 1 ? args[1] : 100;
20402027
return ExpressionFunctions.flatten(array, depth);
@@ -2044,7 +2031,7 @@ export class ExpressionFunctions {
20442031
),
20452032
new ExpressionEvaluator(
20462033
ExpressionType.Unique,
2047-
ExpressionFunctions.apply(args => [... new Set(args[0])]),
2034+
ExpressionFunctions.apply((args: any []): any[] => [... new Set(args[0])]),
20482035
ReturnType.Object,
20492036
(expression: Expression): void => ExpressionFunctions.validateOrder(expression, [], ReturnType.Object)
20502037
),
@@ -2175,7 +2162,7 @@ export class ExpressionFunctions {
21752162
(expression: Expression): void => ExpressionFunctions.validateArityAndAnyType(expression, 3, 3, ReturnType.String)),
21762163
new ExpressionEvaluator(
21772164
ExpressionType.Split,
2178-
ExpressionFunctions.apply((args: any []): string[] => ExpressionFunctions.parseStringOrNull(args[0]).split(ExpressionFunctions.parseStringOrNull(args[1]? args[1]: '')), ExpressionFunctions.verifyStringOrNull),
2165+
ExpressionFunctions.apply((args: any []): string[] => ExpressionFunctions.parseStringOrNull(args[0]).split(ExpressionFunctions.parseStringOrNull(args[1] || '')), ExpressionFunctions.verifyStringOrNull),
21792166
ReturnType.Object,
21802167
(expression: Expression): void => ExpressionFunctions.validateArityAndAnyType(expression, 1, 2, ReturnType.String)),
21812168
new ExpressionEvaluator(
@@ -2823,7 +2810,6 @@ export class ExpressionFunctions {
28232810
error = `Min value ${ args[0] } cannot be greater than max value ${ args[1] }.`;
28242811
}
28252812

2826-
// tslint:disable-next-line: insecure-random
28272813
const value: any = Math.floor(Math.random() * (Number(args[1]) - Number(args[0])) + Number(args[0]));
28282814

28292815
return { value, error };
@@ -2855,7 +2841,6 @@ export class ExpressionFunctions {
28552841
ExpressionFunctions.validateUnary),
28562842
new ExpressionEvaluator(
28572843
ExpressionType.DataUriToString,
2858-
// tslint:disable-next-line: restrict-plus-operands
28592844
ExpressionFunctions.apply((args: Readonly<any>): string => Buffer.from(args[0].slice(args[0].indexOf(',') + 1), 'base64').toString(), ExpressionFunctions.verifyString),
28602845
ReturnType.String,
28612846
ExpressionFunctions.validateUnary),

libraries/adaptive-expressions/src/expressionType.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ export class ExpressionType {
100100
public static readonly Base64ToBinary: string = 'base64ToBinary';
101101
public static readonly Base64ToString: string = 'base64ToString';
102102
public static readonly UriComponent: string = 'uriComponent';
103-
// TODO
104-
// xml
105103

106104
// Memory
107105
public static readonly Accessor: string = 'Accessor';
@@ -144,7 +142,7 @@ export class ExpressionType {
144142

145143
// TODO
146144
// xPath
147-
// jPath
145+
// xml
148146

149147
// URI parsing functions
150148
public static readonly UriHost: string = 'uriHost';

libraries/adaptive-expressions/src/extensions.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class Extensions {
5555
* @returns Accessor path of expression.
5656
*/
5757
public static referenceWalk(expression: Expression,
58-
extension?: (arg0: Expression) => boolean): {path:string; refs:Set<string>} {
58+
extension?: (arg0: Expression) => boolean): {path: string; refs: Set<string>} {
5959
let path: string;
6060
let refs = new Set<string>();
6161
if (extension === undefined || !extension(expression)) {
@@ -114,23 +114,23 @@ export class Extensions {
114114
}
115115

116116
const iteratorName = (children[1].children[0] as Constant).value as string;
117-
var nonLocalRefs2 = Array.from(refs2).filter(x => !(x === iteratorName || x.startsWith(iteratorName + '.') || x.startsWith(iteratorName + '[')));
117+
var nonLocalRefs2 = Array.from(refs2).filter((x): boolean => !(x === iteratorName || x.startsWith(iteratorName + '.') || x.startsWith(iteratorName + '[')));
118118
refs = new Set([...refs, ...refs0, ...nonLocalRefs2]);
119119

120120
} else {
121121
for (const child of expression.children) {
122122
const result = Extensions.referenceWalk(child, extension);
123123
const childPath = result.path;
124124
const refs0 = result.refs;
125-
refs = new Set([...refs, ...refs0])
125+
refs = new Set([...refs, ...refs0]);
126126
if (childPath !== undefined) {
127127
refs.add(childPath);
128128
}
129129
}
130130
}
131131
}
132132

133-
return {path, refs}
133+
return {path, refs};
134134
}
135135

136136
/**
@@ -146,7 +146,6 @@ export class Extensions {
146146
}
147147

148148
let value: any;
149-
// tslint:disable-next-line: prefer-const
150149
let error: string;
151150
// todo, Is there a better way to access value, or any case is not listed below?
152151
if (instance instanceof Map && instance as Map<string, any>!== undefined) {

libraries/adaptive-expressions/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export * from './parser';
1919
export * from './memory';
2020
export * from './regexErrorListener';
2121
export * from './componentExpressionFunctions';
22-
export * from './formatConverter';
22+
export * from './datetimeFormatConverter';

libraries/adaptive-expressions/src/memory/simpleObjectMemory.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { MemoryInterface } from './memoryInterface';
22
import { Extensions } from '../extensions';
3-
import { Util } from '../parser/util';
43

54
/**
65
* @module adaptive-expressions

libraries/adaptive-expressions/src/parser/expressionParser.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* @module adaptive-expressions
43
*/
@@ -7,9 +6,7 @@
76
* Licensed under the MIT License.
87
*/
98
import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts';
10-
// tslint:disable-next-line: no-submodule-imports
119
import { AbstractParseTreeVisitor, ParseTree, TerminalNode } from 'antlr4ts/tree';
12-
import { ExpressionFunctions } from '../expressionFunctions';
1310
import { Constant } from '../constant';
1411
import { Expression } from '../expression';
1512
import { EvaluatorLookup } from '../expressionEvaluator';
@@ -29,7 +26,6 @@ export class ExpressionParser implements ExpressionParserInterface {
2926
*/
3027
public readonly EvaluatorLookup: EvaluatorLookup;
3128

32-
// tslint:disable-next-line: typedef
3329
private readonly ExpressionTransformer = class extends AbstractParseTreeVisitor<Expression> implements ExpressionAntlrParserVisitor<Expression> {
3430

3531
private readonly _lookupFunction: EvaluatorLookup = undefined;
@@ -209,7 +205,7 @@ export class ExpressionParser implements ExpressionParserInterface {
209205
};
210206

211207
public constructor(lookup?: EvaluatorLookup) {
212-
this.EvaluatorLookup = lookup === undefined ? Expression.lookup : lookup;
208+
this.EvaluatorLookup = lookup || Expression.lookup;
213209
}
214210

215211
protected static antlrParse(expression: string): ParseTree {

0 commit comments

Comments
 (0)