Skip to content

Commit

Permalink
Reduce the use of "any" in LG library (#3526)
Browse files Browse the repository at this point in the history
* init

* fix bug

* fix comments

* fix comments
  • Loading branch information
Danieladu authored Apr 7, 2021
1 parent 6bd641e commit c6b01e3
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 119 deletions.
2 changes: 1 addition & 1 deletion libraries/botbuilder-lg/src/customizedMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CustomizedMemory implements MemoryInterface {
* @param scope Optional. Scope.
* @param localMemory Optional. Local memory.
*/
public constructor(scope?: any, localMemory?: MemoryInterface) {
public constructor(scope?: unknown, localMemory?: MemoryInterface) {
this.globalMemory = !scope ? undefined : SimpleObjectMemory.wrap(scope);
this.localMemory = localMemory;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-lg/src/errorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TemplateErrors } from './templateErrors';
/**
* LG parser error listener.
*/
export class ErrorListener implements ANTLRErrorListener<any> {
export class ErrorListener implements ANTLRErrorListener<void> {
private readonly source: string;
private lineOffset: number;

Expand Down
4 changes: 2 additions & 2 deletions libraries/botbuilder-lg/src/evaluationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class EvaluationOptions {

public strictMode: boolean | undefined;

public nullSubstitution: (path: string) => any | undefined;
public nullSubstitution: (path: string) => unknown;

public LineBreakStyle: LGLineBreakStyle | undefined;

Expand Down Expand Up @@ -86,7 +86,7 @@ export class EvaluationOptions {
this.strictMode = true;
}
} else if (key.toLowerCase() === this.replaceNullKey.toLowerCase()) {
this.nullSubstitution = (path): any =>
this.nullSubstitution = (path) =>
// eslint-disable-next-line security/detect-eval-with-expression
eval('`' + value.replace(this.nullKeyReplaceStrRegex, '${path}') + '`');
} else if (key.toLowerCase() === this.lineBreakKey.toLowerCase()) {
Expand Down
4 changes: 2 additions & 2 deletions libraries/botbuilder-lg/src/evaluationTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class EvaluationTarget {
/**
* The children templates that this template has evaluated currently.
*/
public cachedEvaluatedChildren: Map<string, any>;
public cachedEvaluatedChildren: Map<string, unknown>;

/**
* Creates a new instance of the [EvaluationTarget](xref:botbuilder-lg.EvaluationTarget) class.
Expand All @@ -34,7 +34,7 @@ export class EvaluationTarget {
public constructor(templateName: string, scope: MemoryInterface) {
this.templateName = templateName;
this.scope = scope;
this.cachedEvaluatedChildren = new Map<string, any>();
this.cachedEvaluatedChildren = new Map<string, unknown>();
}

/**
Expand Down
62 changes: 32 additions & 30 deletions libraries/botbuilder-lg/src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export enum FileFormat {
/**
* Evaluation runtime engine
*/
export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTemplateParserVisitor<any> {
export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTemplateParserVisitor<unknown> {
/**
* Templates.
*/
Expand All @@ -75,7 +75,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
public readonly templateMap: { [name: string]: Template };
private readonly evaluationTargetStack: EvaluationTarget[] = [];
private readonly lgOptions: EvaluationOptions;
private readonly cacheResult: Map<string, any> = new Map<string, any>();
private readonly cacheResult: Map<string, unknown> = new Map<string, unknown>();

public static readonly LGType = 'lgType';
public static readonly activityAttachmentFunctionName = 'ActivityAttachment';
Expand Down Expand Up @@ -110,7 +110,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
* @param scope Scope.
* @returns Evaluate result.
*/
public evaluateTemplate(inputTemplateName: string, scope: any): any {
public evaluateTemplate(inputTemplateName: string, scope: unknown): unknown {
const memory = scope instanceof CustomizedMemory ? scope : new CustomizedMemory(scope);
const { reExecute, pureTemplateName: templateName } = this.parseTemplateName(inputTemplateName);

Expand All @@ -130,7 +130,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
);
}

let result: any;
let result: unknown;
let hasResult = false;
if (!reExecute) {
if (this.lgOptions.cacheScope === LGCacheScope.Global) {
Expand Down Expand Up @@ -180,8 +180,8 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
* @param ctx The parse tree.
* @returns The result of visiting the structured template body.
*/
public visitStructuredTemplateBody(ctx: lp.StructuredTemplateBodyContext): any {
const result: any = {};
public visitStructuredTemplateBody(ctx: lp.StructuredTemplateBodyContext): unknown {
const result: Record<string, unknown> = {};
const typeName: string = ctx.structuredBodyNameLine().STRUCTURE_NAME().text;
result[Evaluator.LGType] = typeName;

Expand All @@ -193,7 +193,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
const value = this.visitStructureValue(body.keyValueStructureLine());
result[property] = value;
} else {
const propertyObject: any = this.evalExpression(
const propertyObject = this.evalExpression(
body.expressionInStructure().text,
body.expressionInStructure(),
body.text
Expand All @@ -220,7 +220,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
/**
* @private
*/
private visitStructureValue(ctx: lp.KeyValueStructureLineContext): any {
private visitStructureValue(ctx: lp.KeyValueStructureLineContext): unknown {
const values = ctx.keyValueStructureValue();

const result = [];
Expand Down Expand Up @@ -260,7 +260,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
* @param ctx The parse tree.
* @returns The result of visiting the normal body.
*/
public visitNormalBody(ctx: lp.NormalBodyContext): any {
public visitNormalBody(ctx: lp.NormalBodyContext): unknown {
return this.visit(ctx.normalTemplateBody());
}

Expand All @@ -269,7 +269,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
* @param ctx The parse tree.
* @returns The result of visiting the normal template body.
*/
public visitNormalTemplateBody(ctx: lp.NormalTemplateBodyContext): any {
public visitNormalTemplateBody(ctx: lp.NormalTemplateBodyContext): unknown {
const normalTemplateStrs: lp.TemplateStringContext[] = ctx.templateString();
const randomNumber = Extensions.randomNext(this.currentTarget().scope, 0, normalTemplateStrs.length);

Expand All @@ -280,7 +280,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
* Visit a parse tree produced by the ifElseBody labeled alternative in LGTemplateParser.body.
* @param ctx The parse tree.
*/
public visitIfElseBody(ctx: lp.IfElseBodyContext): any {
public visitIfElseBody(ctx: lp.IfElseBodyContext): unknown {
const ifRules: lp.IfConditionRuleContext[] = ctx.ifElseTemplateBody().ifConditionRule();
for (const ifRule of ifRules) {
if (this.evalCondition(ifRule.ifCondition()) && ifRule.normalTemplateBody() !== undefined) {
Expand All @@ -296,9 +296,9 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
* @param ctx The parse tree.
* @returns The string result of visiting the normal template string.
*/
public visitNormalTemplateString(ctx: lp.NormalTemplateStringContext): string {
public visitNormalTemplateString(ctx: lp.NormalTemplateStringContext): unknown {
const prefixErrorMsg = TemplateExtensions.getPrefixErrorMessage(ctx);
const result: any[] = [];
const result: unknown[] = [];
for (const child of ctx.children) {
if (child instanceof lp.ExpressionContext) {
result.push(this.evalExpression(child.text, child, ctx.text, prefixErrorMsg));
Expand All @@ -325,7 +325,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
}

return result
.map((u: any): string => {
.map((u) => {
if (typeof u === 'string') {
return u;
} else {
Expand Down Expand Up @@ -375,7 +375,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
* @param ctx The parse tree.
* @returns The string result of visiting the switch case body.
*/
public visitSwitchCaseBody(ctx: lp.SwitchCaseBodyContext): string {
public visitSwitchCaseBody(ctx: lp.SwitchCaseBodyContext): unknown {
const switchcaseNodes: lp.SwitchCaseRuleContext[] = ctx.switchCaseTemplateBody().switchCaseRule();
const length: number = switchcaseNodes.length;
const switchNode: lp.SwitchCaseRuleContext = switchcaseNodes[0];
Expand Down Expand Up @@ -430,7 +430,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
.split('')
.reverse()
.join('')
.replace(regex, (sub: string): any =>
.replace(regex, (sub: string) =>
this.evalExpression(sub.split('').reverse().join('')).toString().split('').reverse().join('')
)
.split('')
Expand Down Expand Up @@ -476,7 +476,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
public static checkExpressionResult(
exp: string,
error: string,
result: any,
result: unknown,
templateName: string,
inlineContent = '',
errorPrefix = ''
Expand Down Expand Up @@ -561,7 +561,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
expressionContext?: ParserRuleContext,
inlineContent = '',
errorPrefix = ''
): any {
): unknown {
exp = TemplateExtensions.trimExpression(exp);
const { value: result, error: error } = this.evalByAdaptiveExpression(exp, this.currentTarget().scope);

Expand All @@ -581,7 +581,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
/**
* @private
*/
private evalByAdaptiveExpression(exp: string, scope: any): { value: any; error: string } {
private evalByAdaptiveExpression(exp: string, scope: unknown): { value: unknown; error: string } {
const parse: Expression = this.expressionParser.parse(exp);
const opt = new Options();
opt.nullSubstitution = this.lgOptions.nullSubstitution;
Expand All @@ -591,7 +591,9 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
}

// Genearte a new lookup function based on one lookup function
private readonly customizedEvaluatorLookup = (baseLookup: EvaluatorLookup): any => (name: string): any => {
private readonly customizedEvaluatorLookup = (baseLookup: EvaluatorLookup) => (
name: string
): ExpressionEvaluator => {
const standardFunction = baseLookup(name);

if (standardFunction !== undefined) {
Expand Down Expand Up @@ -674,17 +676,17 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
return undefined;
};

private readonly isTemplate = (): any => (args: readonly any[]): boolean => {
private readonly isTemplate = () => (args: readonly unknown[]): boolean => {
const templateName = args[0].toString();
return templateName in this.templateMap;
};

private readonly fromFile = (): any => (args: readonly any[]): any => {
private readonly fromFile = () => (args: readonly unknown[]): unknown => {
const filePath: string = TemplateExtensions.normalizePath(args[0].toString());
const resourcePath: string = this.getResourcePath(filePath);
let format = FileFormat.Evaluated;
if (args.length > 1) {
const expected = args[1].toLowerCase();
const expected = args[1].toString().toLowerCase();
const currentFormat = Object.values(FileFormat).find((f) => f.toLowerCase() === expected);
if (currentFormat != null) {
format = currentFormat;
Expand Down Expand Up @@ -719,7 +721,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
return result;
};

private readonly expandText = (): any => (args: readonly any[]): any => {
private readonly expandText = () => (args: readonly unknown[]): unknown => {
const stringContent = args[0].toString();

const newScope = this.evaluationTargetStack.length > 0 ? this.currentTarget().scope : undefined;
Expand Down Expand Up @@ -765,7 +767,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
return resourcePath;
}

private readonly activityAttachment = (): any => (args: readonly any[]): any => {
private readonly activityAttachment = () => (args: readonly unknown[]): unknown => {
return {
[Evaluator.LGType]: 'attachment',
contenttype: args[1].toString(),
Expand All @@ -781,9 +783,9 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
return templates.evaluate(templateName, newScope);
};

private readonly templateFunction = (): any => (args: readonly any[]): any => {
const templateName: string = args[0];
const newScope: any = this.constructScope(templateName, args.slice(1), this.templates.allTemplates);
private readonly templateFunction = () => (args: readonly unknown[]): unknown => {
const templateName: string = args[0].toString();
const newScope = this.constructScope(templateName, args.slice(1), this.templates.allTemplates);

return this.evaluateTemplate(templateName, newScope);
};
Expand Down Expand Up @@ -821,8 +823,8 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGTempla
}
}

private readonly templateEvaluator = (templateName: string): any => (args: readonly any[]): any => {
const newScope: any = this.constructScope(templateName, Array.from(args), this.templates.allTemplates);
private readonly templateEvaluator = (templateName: string) => (args: readonly unknown[]): unknown => {
const newScope = this.constructScope(templateName, Array.from(args), this.templates.allTemplates);

return this.evaluateTemplate(templateName, newScope);
};
Expand Down
Loading

0 comments on commit c6b01e3

Please sign in to comment.