Skip to content

Commit

Permalink
fix: [#4204] Fix remaining eslint warnings - botbuilder-lg (#4235)
Browse files Browse the repository at this point in the history
* Fix eslint issues in botbuilder-lg

* Update botbuilder-lg compat

* Fix EvaluationOptions nullSubstitution

* Missing dots.

* Revert "Update botbuilder-lg compat"

This reverts commit 2c88f64.

* Add missing documentation for the Templates class

* Uppercase instead of lowercase in method definitions.

* ammend

Co-authored-by: Emiliano Quiroga <[email protected]>
Co-authored-by: Cecilia Avila <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2022
1 parent 8ec3563 commit 5c1d429
Show file tree
Hide file tree
Showing 25 changed files with 318 additions and 157 deletions.
12 changes: 10 additions & 2 deletions libraries/botbuilder-lg/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { LGTemplateParserVisitor } from './generated/LGTemplateParserVisitor';
import { Template } from './template';
import { TemplateExtensions } from './templateExtensions';
import { Templates } from './templates';
import { keyBy } from 'lodash';
import keyBy = require('lodash/keyBy');

import {
IfConditionRuleContext,
Expand Down Expand Up @@ -68,6 +68,7 @@ export class Analyzer

/**
* Analyze a template to get the static analyzer results.
*
* @param templateName Template name.
* @returns Analyze result including variables and template references.
*/
Expand Down Expand Up @@ -108,6 +109,7 @@ export class Analyzer

/**
* Visit a parse tree produced by the normalBody labeled alternative in LGTemplateParser.body.
*
* @param ctx The parse tree.
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
Expand All @@ -117,6 +119,7 @@ export class Analyzer

/**
* Visit a parse tree produced by LGTemplateParser.normalTemplateBody.
*
* @param ctx The parse tree.
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
Expand All @@ -131,6 +134,7 @@ export class Analyzer

/**
* Visit a parse tree produced by LGTemplateParser.structuredTemplateBody.
*
* @param ctx The parse tree.
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
Expand All @@ -152,6 +156,7 @@ export class Analyzer

/**
* Visit a parse tree produced by LGTemplateParser.structuredValue.
*
* @param ctx The parse tree.
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
Expand All @@ -175,6 +180,7 @@ export class Analyzer

/**
* Visit a parse tree produced by the ifElseBody labeled alternative in LGTemplateParser.body.
*
* @param ctx The parse tree.
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
Expand All @@ -197,6 +203,7 @@ export class Analyzer

/**
* Visit a parse tree produced by the switchCaseBody labeled alternative in LGTemplateParser.body.
*
* @param ctx The parse tree.
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
Expand All @@ -218,6 +225,7 @@ export class Analyzer

/**
* Visit a parse tree produced by LGTemplateParser.normalTemplateString.
*
* @param ctx The parse tree.
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
Expand All @@ -233,7 +241,7 @@ export class Analyzer

/**
* Gets the default value returned by visitor methods.
* @returns An instance of the AnalyzerResult class.
*
* @returns The [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) instance.
*/
protected defaultResult(): AnalyzerResult {
Expand Down
2 changes: 2 additions & 0 deletions libraries/botbuilder-lg/src/analyzerResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class AnalyzerResult {

/**
* Creates a new instance of the [AnalyzerResult](xref:botbuilder-lg.AnalyzerResult) class.
*
* @param variables Init varibales.
* @param templateRefNames Init template references.
*/
Expand All @@ -32,6 +33,7 @@ export class AnalyzerResult {

/**
* Combine two analyzer results.
*
* @param outputItem Another analyzer result.
* @returns Combined analyzer result.
*/
Expand Down
4 changes: 4 additions & 0 deletions libraries/botbuilder-lg/src/customizedMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class CustomizedMemory implements MemoryInterface {

/**
* Creates a new instance of the [CustomizedMemory](xref:botbuilder-lg.CustomizedMemory) class.
*
* @param scope Optional. Scope.
* @param localMemory Optional. Local memory.
*/
Expand All @@ -37,6 +38,7 @@ export class CustomizedMemory implements MemoryInterface {
/**
* Try to get the value from a given path. Firstly, get result from global memory,
* if global memory does not contain, get from local memory.
*
* @param path Memory path.
* @returns Resolved value.
*/
Expand All @@ -57,6 +59,7 @@ export class CustomizedMemory implements MemoryInterface {

/**
* Set value to a given path. This method is not implemented.
*
* @param _path Memory path.
* @param _value Value to set.
*/
Expand All @@ -68,6 +71,7 @@ export class CustomizedMemory implements MemoryInterface {
/**
* Used to identify whether a particular memory instance has been updated or not.
* If version is not changed, the caller may choose to use the cached result instead of recomputing everything.
*
* @returns A string indicating the version.
*/
version(): string {
Expand Down
2 changes: 2 additions & 0 deletions libraries/botbuilder-lg/src/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class Diagnostic {

/**
* Creates a new instance of the [Diagnostic](xref:botbuilder-lg.Diagnostic) class.
*
* @param range Range where the error or warning occurred.
* @param message Error message of the error or warning.
* @param severity Severity of the error or warning.
Expand All @@ -53,6 +54,7 @@ export class Diagnostic {

/**
* Returns a string that represents the current [Diagnostic](xref:botbuilder-lg.Diagnostic) object.
*
* @returns A string that represents the current [Diagnostic](xref:botbuilder-lg.Diagnostic).
*/
toString(): string {
Expand Down
2 changes: 2 additions & 0 deletions libraries/botbuilder-lg/src/errorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class ErrorListener implements ANTLRErrorListener<void> {

/**
* Creates a new instance of the [ErrorListener](xref:botbuilder-lg.ErrorListener) class.
*
* @param errorSource String value that represents the source of the error.
* @param lineOffset Offset of the line where the error occurred.
*/
Expand All @@ -34,6 +35,7 @@ export class ErrorListener implements ANTLRErrorListener<void> {

/**
* Notifies any interested parties upon a syntax error.
*
* @param recognizer What parser got the error. From this object, you can access the context as well as the input stream.
* @param offendingSymbol Offending token in the input token stream, unless recognizer is a lexer (then it's null).
* @param line Line number in the input where the error occurred.
Expand Down
3 changes: 3 additions & 0 deletions libraries/botbuilder-lg/src/evaluationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class EvaluationOptions {

/**
* Creates a new instance of the [EvaluationOptions](xref:botbuilder-lg.EvaluationOptions) class.
*
* @param opt Instance to copy initial settings from.
*/
constructor(opt?: EvaluationOptions | string[]) {
Expand Down Expand Up @@ -86,6 +87,7 @@ export class EvaluationOptions {
this.strictMode = true;
}
} else if (key.toLowerCase() === this.replaceNullKey.toLowerCase()) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.nullSubstitution = (path) =>
// eslint-disable-next-line security/detect-eval-with-expression
eval('`' + value.replace(this.nullKeyReplaceStrRegex, '${path}') + '`');
Expand All @@ -112,6 +114,7 @@ export class EvaluationOptions {
/**
* Merges an incoming option to current option. If a property in incoming option is not null while it is null in current
* option, then the value of this property will be overwritten.
*
* @param opt Incoming option for merging.
* @returns Result after merging.
*/
Expand Down
2 changes: 2 additions & 0 deletions libraries/botbuilder-lg/src/evaluationTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class EvaluationTarget {

/**
* Creates a new instance of the [EvaluationTarget](xref:botbuilder-lg.EvaluationTarget) class.
*
* @param templateName Template name.
* @param scope Template scope.
*/
Expand All @@ -40,6 +41,7 @@ export class EvaluationTarget {
/**
* Get current instance id. If two target has the same Id,
* we can say they have the same template evaluation result.
*
* @returns Id.
*/
getId(): string {
Expand Down
28 changes: 22 additions & 6 deletions libraries/botbuilder-lg/src/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable security/detect-object-injection */
/* eslint-disable security/detect-non-literal-fs-filename */
/**
* @module botbuilder-lg
*/
Expand All @@ -18,7 +19,7 @@ import { Template } from './template';
import { TemplateErrors } from './templateErrors';
import { TemplateExtensions } from './templateExtensions';
import { Templates } from './templates';
import { keyBy } from 'lodash';
import keyBy = require('lodash/keyBy');

import {
Constant,
Expand Down Expand Up @@ -104,6 +105,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Creates a new instance of the [Evaluator](xref:botbuilder-lg.Evaluator) class.
*
* @param templates Templates.
* @param opt Options for LG.
*/
Expand All @@ -122,6 +124,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Evaluate a template with given name and scope.
*
* @param inputTemplateName Template name.
* @param scope Scope.
* @returns Evaluate result.
Expand Down Expand Up @@ -193,6 +196,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Visit a parse tree produced by LGTemplateParser.structuredTemplateBody.
*
* @param ctx The parse tree.
* @returns The result of visiting the structured template body.
*/
Expand Down Expand Up @@ -222,7 +226,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe
propertyObject[Evaluator.LGType].toString() === typeName
) {
for (const key of Object.keys(propertyObject)) {
if (propertyObject.hasOwnProperty(key) && !(key in result)) {
if (Object.prototype.hasOwnProperty.call(propertyObject, key) && !(key in result)) {
result[key] = propertyObject[key];
}
}
Expand All @@ -249,7 +253,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe
let itemStringResult = '';
for (const child of item.children) {
if (child instanceof ExpressionInStructureContext) {
const errorPrefix = `Property '` + ctx.STRUCTURE_IDENTIFIER().text + `':`;
const errorPrefix = "Property '" + ctx.STRUCTURE_IDENTIFIER().text + "':";
itemStringResult += this.evalExpression(child.text, child, ctx.text, errorPrefix);
} else {
const node = child as TerminalNode;
Expand All @@ -273,6 +277,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Visit a parse tree produced by the normalBody labeled alternative in LGTemplateParser.body.
*
* @param ctx The parse tree.
* @returns The result of visiting the normal body.
*/
Expand All @@ -282,6 +287,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Visit a parse tree produced by LGTemplateParser.normalTemplateBody.
*
* @param ctx The parse tree.
* @returns The result of visiting the normal template body.
*/
Expand All @@ -294,7 +300,9 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Visit a parse tree produced by the ifElseBody labeled alternative in LGTemplateParser.body.
*
* @param ctx The parse tree.
* @returns The visitor result.
*/
visitIfElseBody(ctx: IfElseBodyContext): unknown {
const ifRules: IfConditionRuleContext[] = ctx.ifElseTemplateBody().ifConditionRule();
Expand All @@ -309,6 +317,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Visit a parse tree produced by LGTemplateParser.normalTemplateString.
*
* @param ctx The parse tree.
* @returns The string result of visiting the normal template string.
*/
Expand Down Expand Up @@ -354,6 +363,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe
/**
* Constructs the scope for mapping the values of arguments to the parameters of the template.
* Throws errors if certain errors detected [TemplateErrors](xref:botbuilder-lg.TemplateErrors).
*
* @param inputTemplateName Template name to evaluate.
* @param args Arguments to map to the template parameters.
* @param allTemplates All templates.
Expand Down Expand Up @@ -388,6 +398,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Visit a parse tree produced by the switchCaseBody labeled alternative in LGTemplateParser.body.
*
* @param ctx The parse tree.
* @returns The string result of visiting the switch case body.
*/
Expand All @@ -396,7 +407,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe
const length: number = switchcaseNodes.length;
const switchNode: SwitchCaseRuleContext = switchcaseNodes[0];
const switchExprs = switchNode.switchCaseStat().expression();
const switchErrorPrefix = `Switch '` + switchExprs[0].text + `': `;
const switchErrorPrefix = "Switch '" + switchExprs[0].text + "': ";
const switchExprResult = this.evalExpression(
switchExprs[0].text,
switchExprs[0],
Expand All @@ -419,7 +430,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe
}

const caseExprs = caseNode.switchCaseStat().expression();
const caseErrorPrefix = `Case '` + caseExprs[0].text + `': `;
const caseErrorPrefix = "Case '" + caseExprs[0].text + "': ";
const caseExprResult = this.evalExpression(
caseExprs[0].text,
caseExprs[0],
Expand All @@ -438,8 +449,10 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Replaces an expression contained in text.
*
* @param exp Expression Text.
* @param regex Regex to select the text to replace.
* @returns Text with expression replaced.
*/
wrappedEvalTextContainsExpression(exp: string, regex: RegExp): string {
return exp
Expand All @@ -456,6 +469,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Gets the default value returned by visitor methods.
*
* @returns Empty string.
*/
protected defaultResult(): string {
Expand All @@ -464,6 +478,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Concatenates two error messages.
*
* @param firstError First error message to concatenate.
* @param secondError Second error message to concatenate.
* @returns The concatenated error messages.
Expand All @@ -482,6 +497,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe

/**
* Checks an expression result and throws the corresponding error.
*
* @param exp Expression text.
* @param error Error message.
* @param result Result.
Expand Down Expand Up @@ -536,7 +552,7 @@ export class Evaluator extends AbstractParseTreeVisitor<unknown> implements LGTe
return true; // no expression means it's else
}

if (this.evalExpressionInCondition(expression, condition.text, `Condition '` + expression.text + `':`)) {
if (this.evalExpressionInCondition(expression, condition.text, "Condition '" + expression.text + "':")) {
return true;
}

Expand Down
Loading

0 comments on commit 5c1d429

Please sign in to comment.