From 5c1d4295ba5fb306d09d6a320307bc85d2e2fe22 Mon Sep 17 00:00:00 2001 From: Joel Mut <62260472+sw-joelmut@users.noreply.github.com> Date: Wed, 13 Jul 2022 15:25:37 -0300 Subject: [PATCH] fix: [#4204] Fix remaining eslint warnings - botbuilder-lg (#4235) * Fix eslint issues in botbuilder-lg * Update botbuilder-lg compat * Fix EvaluationOptions nullSubstitution * Missing dots. * Revert "Update botbuilder-lg compat" This reverts commit 2c88f64b18be4e41bc7b6f493ac24f3035a0ed0a. * Add missing documentation for the Templates class * Uppercase instead of lowercase in method definitions. * ammend Co-authored-by: Emiliano Quiroga Co-authored-by: Cecilia Avila <44245136+ceciliaavila@users.noreply.github.com> --- libraries/botbuilder-lg/src/analyzer.ts | 12 +- libraries/botbuilder-lg/src/analyzerResult.ts | 2 + .../botbuilder-lg/src/customizedMemory.ts | 4 + libraries/botbuilder-lg/src/diagnostic.ts | 2 + libraries/botbuilder-lg/src/errorListener.ts | 2 + .../botbuilder-lg/src/evaluationOptions.ts | 3 + .../botbuilder-lg/src/evaluationTarget.ts | 2 + libraries/botbuilder-lg/src/evaluator.ts | 28 ++- libraries/botbuilder-lg/src/expander.ts | 26 ++- libraries/botbuilder-lg/src/extractor.ts | 9 +- libraries/botbuilder-lg/src/lgResource.ts | 1 + .../botbuilder-lg/src/multiLanguageLG.ts | 5 +- libraries/botbuilder-lg/src/position.ts | 1 + libraries/botbuilder-lg/src/range.ts | 3 + libraries/botbuilder-lg/src/sourceRange.ts | 3 + libraries/botbuilder-lg/src/staticChecker.ts | 17 +- libraries/botbuilder-lg/src/template.ts | 2 + libraries/botbuilder-lg/src/templateErrors.ts | 76 +++++--- .../botbuilder-lg/src/templateException.ts | 3 + .../botbuilder-lg/src/templateExtensions.ts | 17 +- libraries/botbuilder-lg/src/templateImport.ts | 1 + libraries/botbuilder-lg/src/templates.ts | 49 ++++- .../botbuilder-lg/src/templatesParser.ts | 23 ++- libraries/botbuilder-lg/tests/lg.test.js | 8 +- .../botbuilder-lg/tests/lgDiagnostic.test.js | 176 +++++++++--------- 25 files changed, 318 insertions(+), 157 deletions(-) diff --git a/libraries/botbuilder-lg/src/analyzer.ts b/libraries/botbuilder-lg/src/analyzer.ts index 0f6db46515..46775a9c47 100644 --- a/libraries/botbuilder-lg/src/analyzer.ts +++ b/libraries/botbuilder-lg/src/analyzer.ts @@ -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, @@ -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. */ @@ -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. */ @@ -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. */ @@ -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. */ @@ -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. */ @@ -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. */ @@ -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. */ @@ -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. */ @@ -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 { diff --git a/libraries/botbuilder-lg/src/analyzerResult.ts b/libraries/botbuilder-lg/src/analyzerResult.ts index 0365121035..1c6f240b70 100644 --- a/libraries/botbuilder-lg/src/analyzerResult.ts +++ b/libraries/botbuilder-lg/src/analyzerResult.ts @@ -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. */ @@ -32,6 +33,7 @@ export class AnalyzerResult { /** * Combine two analyzer results. + * * @param outputItem Another analyzer result. * @returns Combined analyzer result. */ diff --git a/libraries/botbuilder-lg/src/customizedMemory.ts b/libraries/botbuilder-lg/src/customizedMemory.ts index 974197e8cb..58430f14f5 100644 --- a/libraries/botbuilder-lg/src/customizedMemory.ts +++ b/libraries/botbuilder-lg/src/customizedMemory.ts @@ -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. */ @@ -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. */ @@ -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. */ @@ -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 { diff --git a/libraries/botbuilder-lg/src/diagnostic.ts b/libraries/botbuilder-lg/src/diagnostic.ts index 315080a81d..0c2aa691c1 100644 --- a/libraries/botbuilder-lg/src/diagnostic.ts +++ b/libraries/botbuilder-lg/src/diagnostic.ts @@ -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. @@ -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 { diff --git a/libraries/botbuilder-lg/src/errorListener.ts b/libraries/botbuilder-lg/src/errorListener.ts index adabb17e59..96c394b4ec 100644 --- a/libraries/botbuilder-lg/src/errorListener.ts +++ b/libraries/botbuilder-lg/src/errorListener.ts @@ -21,6 +21,7 @@ export class ErrorListener implements ANTLRErrorListener { /** * 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. */ @@ -34,6 +35,7 @@ export class ErrorListener implements ANTLRErrorListener { /** * 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. diff --git a/libraries/botbuilder-lg/src/evaluationOptions.ts b/libraries/botbuilder-lg/src/evaluationOptions.ts index bcf3a1c278..a4782109e4 100644 --- a/libraries/botbuilder-lg/src/evaluationOptions.ts +++ b/libraries/botbuilder-lg/src/evaluationOptions.ts @@ -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[]) { @@ -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}') + '`'); @@ -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. */ diff --git a/libraries/botbuilder-lg/src/evaluationTarget.ts b/libraries/botbuilder-lg/src/evaluationTarget.ts index d987604c47..53b08169d4 100644 --- a/libraries/botbuilder-lg/src/evaluationTarget.ts +++ b/libraries/botbuilder-lg/src/evaluationTarget.ts @@ -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. */ @@ -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 { diff --git a/libraries/botbuilder-lg/src/evaluator.ts b/libraries/botbuilder-lg/src/evaluator.ts index bb8a09e0a6..2c1110a1d4 100644 --- a/libraries/botbuilder-lg/src/evaluator.ts +++ b/libraries/botbuilder-lg/src/evaluator.ts @@ -1,4 +1,5 @@ /* eslint-disable security/detect-object-injection */ +/* eslint-disable security/detect-non-literal-fs-filename */ /** * @module botbuilder-lg */ @@ -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, @@ -104,6 +105,7 @@ export class Evaluator extends AbstractParseTreeVisitor implements LGTe /** * Creates a new instance of the [Evaluator](xref:botbuilder-lg.Evaluator) class. + * * @param templates Templates. * @param opt Options for LG. */ @@ -122,6 +124,7 @@ export class Evaluator extends AbstractParseTreeVisitor implements LGTe /** * Evaluate a template with given name and scope. + * * @param inputTemplateName Template name. * @param scope Scope. * @returns Evaluate result. @@ -193,6 +196,7 @@ export class Evaluator extends AbstractParseTreeVisitor implements LGTe /** * Visit a parse tree produced by LGTemplateParser.structuredTemplateBody. + * * @param ctx The parse tree. * @returns The result of visiting the structured template body. */ @@ -222,7 +226,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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]; } } @@ -249,7 +253,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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; @@ -273,6 +277,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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. */ @@ -282,6 +287,7 @@ export class Evaluator extends AbstractParseTreeVisitor implements LGTe /** * Visit a parse tree produced by LGTemplateParser.normalTemplateBody. + * * @param ctx The parse tree. * @returns The result of visiting the normal template body. */ @@ -294,7 +300,9 @@ export class Evaluator extends AbstractParseTreeVisitor 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(); @@ -309,6 +317,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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. */ @@ -354,6 +363,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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. @@ -388,6 +398,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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. */ @@ -396,7 +407,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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], @@ -419,7 +430,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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], @@ -438,8 +449,10 @@ export class Evaluator extends AbstractParseTreeVisitor 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 @@ -456,6 +469,7 @@ export class Evaluator extends AbstractParseTreeVisitor implements LGTe /** * Gets the default value returned by visitor methods. + * * @returns Empty string. */ protected defaultResult(): string { @@ -464,6 +478,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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. @@ -482,6 +497,7 @@ export class Evaluator extends AbstractParseTreeVisitor implements LGTe /** * Checks an expression result and throws the corresponding error. + * * @param exp Expression text. * @param error Error message. * @param result Result. @@ -536,7 +552,7 @@ export class Evaluator extends AbstractParseTreeVisitor 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; } diff --git a/libraries/botbuilder-lg/src/expander.ts b/libraries/botbuilder-lg/src/expander.ts index 382e1dbc71..4f79bcb03b 100644 --- a/libraries/botbuilder-lg/src/expander.ts +++ b/libraries/botbuilder-lg/src/expander.ts @@ -1,4 +1,5 @@ /* eslint-disable security/detect-object-injection */ +/* eslint-disable security/detect-non-literal-fs-filename */ /** * @module botbuilder-lg */ @@ -19,7 +20,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, @@ -83,8 +84,8 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Creates a new instance of the Expander class. + * * @param templates Template list. - * @param expressionParser Given expression parser. * @param opt Options for LG. */ constructor(templates: Templates, opt?: EvaluationOptions) { @@ -93,7 +94,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT this.templateMap = keyBy(templates.allTemplates, (t: Template): string => t.name); this.lgOptions = opt; - // generate a new customzied expression parser by injecting the template as functions + // Generate a new customized expression parser by injecting the template as functions. this.expanderExpressionParser = new ExpressionParser( this.customizedEvaluatorLookup(templates.expressionParser.EvaluatorLookup, true) ); @@ -104,6 +105,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Expand the results of a template with given name and scope. + * * @param templateName Given template name. * @param scope Given scope. * @returns All possiable results. @@ -136,6 +138,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * 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. */ @@ -145,6 +148,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Visit a parse tree produced by LGTemplateParser.normalTemplateBody. + * * @param ctx The parse tree. * @returns The result of visiting the normal template body. */ @@ -160,7 +164,9 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Visit a parse tree produced by the ifElseBody labeled alternative in LGTemplateParser.body. + * * @param ctx The parse tree. + * @returns The result of visiting if-else body. */ visitIfElseBody(ctx: IfElseBodyContext): unknown[] { const ifRules: IfConditionRuleContext[] = ctx.ifElseTemplateBody().ifConditionRule(); @@ -175,6 +181,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Visit a parse tree produced by LGTemplateParser.structuredBody. + * * @param ctx The parse tree. * @returns The result of visiting the structured body. */ @@ -236,7 +243,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT propertyObject[Evaluator.LGType].toString() === typeName ) { for (const key of Object.keys(propertyObject)) { - if (propertyObject.hasOwnProperty(key) && !(key in tempRes)) { + if (Object.prototype.hasOwnProperty.call(propertyObject, key) && !(key in tempRes)) { tempRes[key] = propertyObject[key]; } } @@ -316,6 +323,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Visit a parse tree produced by the switchCaseBody labeled alternative in LGTemplateParser.body. + * * @param ctx The parse tree. * @returns The result of visiting the switch case body. */ @@ -367,7 +375,9 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Visit a parse tree produced by LGTemplateParser.normalTemplateString. + * * @param ctx The parse tree. + * @returns The result of visiting NormalTemplateString. */ visitNormalTemplateString(ctx: NormalTemplateStringContext): unknown[] { const prefixErrorMsg = TemplateExtensions.getPrefixErrorMessage(ctx); @@ -401,10 +411,11 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Constructs the scope for mapping the values of arguments to the parameters of the template. - * @param templateName The template name to evaluate. + * + * @param inputTemplateName The template name to evaluate. * @param args Arguments to map to the template parameters. * @param allTemplates All templates. - * @returns The current scope if the number of arguments is 0, otherwise, returns a CustomizedMemory + * @returns The current scope if the number of arguments is 0, otherwise, returns a CustomizedMemory. * with the mapping of the parameter name to the argument value added to the scope. */ constructScope(inputTemplateName: string, args: unknown[], allTemplates: Template[]): MemoryInterface { @@ -435,6 +446,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT /** * Gets the default value returned by visitor methods. + * * @returns Empty string array. */ protected defaultResult(): string[] { @@ -457,7 +469,7 @@ export class Expander extends AbstractParseTreeVisitor implements LGT 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; } diff --git a/libraries/botbuilder-lg/src/extractor.ts b/libraries/botbuilder-lg/src/extractor.ts index 2e50b24a81..22543f43d0 100644 --- a/libraries/botbuilder-lg/src/extractor.ts +++ b/libraries/botbuilder-lg/src/extractor.ts @@ -6,7 +6,7 @@ * Licensed under the MIT License. */ import { AbstractParseTreeVisitor, TerminalNode } from 'antlr4ts/tree'; -import { keyBy } from 'lodash'; +import keyBy = require('lodash/keyBy'); import { LGTemplateParserVisitor } from './generated/LGTemplateParserVisitor'; import { Template } from './template'; @@ -33,6 +33,7 @@ export class Extractor /** * Creates a new instance of the [Extractor](xref:botbuilder-lg.Extractor) class. + * * @param templates Template list. */ constructor(templates: Template[]) { @@ -43,6 +44,7 @@ export class Extractor /** * Extracts the templates and returns a map with their names and bodies. + * * @returns Map object with template names and bodies. */ extract(): Map>[] { @@ -74,6 +76,7 @@ export class Extractor /** * Visit a parse tree produced by LGTemplateParser.normalTemplateBody. + * * @param context The parse tree. * @returns The result of visiting the normal template body. */ @@ -88,6 +91,7 @@ export class Extractor /** * Visit a parse tree produced by the structuredBody labeled alternative in LGTemplateParser.body. + * * @param context The parse tree. * @returns The result of visiting the structured body. */ @@ -108,6 +112,7 @@ export class Extractor /** * Visit a parse tree produced by the ifElseBody labeled alternative in LGTemplateParser.body. + * * @param context The parse tree. * @returns The result of visiting the if else body. */ @@ -146,6 +151,7 @@ export class Extractor /** * Visit a parse tree produced by the switchCaseBody labeled alternative in LGTemplateParser.body. + * * @param context The parse tree. * @returns The result of visiting the switch case body. */ @@ -185,6 +191,7 @@ export class Extractor /** * Gets the default value returned by visitor methods. + * * @returns Empty Map. */ protected defaultResult(): Map { diff --git a/libraries/botbuilder-lg/src/lgResource.ts b/libraries/botbuilder-lg/src/lgResource.ts index 251fabb4d9..61d97e80c3 100644 --- a/libraries/botbuilder-lg/src/lgResource.ts +++ b/libraries/botbuilder-lg/src/lgResource.ts @@ -28,6 +28,7 @@ export class LGResource { /** * Creates a new instance of the [LGResource](xref:botbuilder-lg.LGResource) class. + * * @param id Resource id. * @param fullName The full path to the resource on disk. * @param content Resource content. diff --git a/libraries/botbuilder-lg/src/multiLanguageLG.ts b/libraries/botbuilder-lg/src/multiLanguageLG.ts index 7910288648..c29562a06d 100644 --- a/libraries/botbuilder-lg/src/multiLanguageLG.ts +++ b/libraries/botbuilder-lg/src/multiLanguageLG.ts @@ -869,6 +869,7 @@ export class MultiLanguageLG { /** * Initializes a new instance of the MultiLanguageLG class. + * * @param templatesPerLocale A map of LG file templates per locale. * @param filePerLocale A map of locale and LG file. * @param defaultLanguage Default language. @@ -881,7 +882,7 @@ export class MultiLanguageLG { if (templatesPerLocale !== undefined) { this.lgPerLocale = templatesPerLocale; } else if (filePerLocale === undefined) { - throw new Error(`input is empty`); + throw new Error('input is empty'); } else { this.lgPerLocale = new Map(); for (const item of filePerLocale.entries()) { @@ -895,9 +896,11 @@ export class MultiLanguageLG { /** * Generate template evaluate result. + * * @param template Template name. * @param data Scope data. * @param locale Locale info. + * @returns The evaluated template result. */ generate(template: string, data?: object, locale?: string): any { if (!template) { diff --git a/libraries/botbuilder-lg/src/position.ts b/libraries/botbuilder-lg/src/position.ts index cd1f9a187e..692ced159f 100644 --- a/libraries/botbuilder-lg/src/position.ts +++ b/libraries/botbuilder-lg/src/position.ts @@ -15,6 +15,7 @@ export class Position { /** * Creates a new instance of the [Position](xref:botbuilder-lg.Position) class. + * * @param line Line number of the current position. * @param character Character number of the current line. */ diff --git a/libraries/botbuilder-lg/src/range.ts b/libraries/botbuilder-lg/src/range.ts index 57799e22ce..3aeac19198 100644 --- a/libraries/botbuilder-lg/src/range.ts +++ b/libraries/botbuilder-lg/src/range.ts @@ -18,6 +18,7 @@ export class Range { /** * Creates a new instance of the [Range](xref:botbuilder-lg.Range) class. + * * @param start Starting [Position](xref:botbuilder-lg.Position). * @param end Ending [Position](xref:botbuilder-lg.Position). */ @@ -25,6 +26,7 @@ export class Range { /** * Creates a new instance of the [Range](xref:botbuilder-lg.Range) class. + * * @param x Starting line number in a file. * @param y Starting character number in a file. * @param endLine Ending line number in a file. @@ -34,6 +36,7 @@ export class Range { /** * Creates a new instance of the [Range](xref:botbuilder-lg.Range) class. + * * @param x Starting line number in a file or [Position](xref:botbuilder-lg.Position). * @param y Starting character number in a file or [Position](xref:botbuilder-lg.Position). * @param endLine Optional. Ending line number in a file. diff --git a/libraries/botbuilder-lg/src/sourceRange.ts b/libraries/botbuilder-lg/src/sourceRange.ts index c4fb23f2a6..24c7eba6d5 100644 --- a/libraries/botbuilder-lg/src/sourceRange.ts +++ b/libraries/botbuilder-lg/src/sourceRange.ts @@ -26,6 +26,7 @@ export class SourceRange { /** * Creates a new instance of the [SourceRange](xref:botbuilder-lg.SourceRange) class. + * * @param parseTree `ParserRuleContext`. Rule invocation record for parsing. * @param source Optional. Source, used as the lg file path. * @param offset Optional. Offset in the parse tree. @@ -34,6 +35,7 @@ export class SourceRange { /** * Creates a new instance of the [SourceRange](xref:botbuilder-lg.SourceRange) class. + * * @param range [Range](xref:botbuilder-lg.Range) of block. * @param source Optional. Source, used as the lg file path. */ @@ -41,6 +43,7 @@ export class SourceRange { /** * Creates a new instance of the [SourceRange](xref:botbuilder-lg.SourceRange) class. + * * @param x [Range](xref:botbuilder-lg.Range) of block or `ParserRuleContext`, rule invocation record for parsing. * @param source Optional. Source, used as the lg file path. * @param offset Optional. Offset in the parse tree. diff --git a/libraries/botbuilder-lg/src/staticChecker.ts b/libraries/botbuilder-lg/src/staticChecker.ts index 1d254338df..fe7cc100ba 100644 --- a/libraries/botbuilder-lg/src/staticChecker.ts +++ b/libraries/botbuilder-lg/src/staticChecker.ts @@ -42,6 +42,7 @@ export class StaticChecker /** * Creates a new instance of the [StaticChecker](xref:botbuilder-lg.StaticChecker) class. + * * @param templates [Templates](xref:botbuilder-lg.Templates) to be checked. */ constructor(templates: Templates) { @@ -65,6 +66,7 @@ export class StaticChecker /** * Return error messages list. + * * @returns Report result. */ check(): Diagnostic[] { @@ -115,7 +117,9 @@ export class StaticChecker /** * Visit a parse tree produced by `LGTemplateParser.normalTemplateBody`. + * * @param context The parse tree. + * @returns The result of visiting normal template body. */ visitNormalTemplateBody(context: NormalTemplateBodyContext): Diagnostic[] { let result: Diagnostic[] = []; @@ -134,7 +138,9 @@ export class StaticChecker /** * Visit a parse tree produced by `LGTemplateParser.structuredTemplateBody`. + * * @param context The parse tree. + * @returns The result of visiting structured template body. */ visitStructuredTemplateBody(context: StructuredTemplateBodyContext): Diagnostic[] { let result: Diagnostic[] = []; @@ -169,7 +175,7 @@ export class StaticChecker result = result.concat(this.checkExpression(body.expressionInStructure())); } else { const structureValues = body.keyValueStructureLine().keyValueStructureValue(); - const errorPrefix = `Property '` + body.keyValueStructureLine().text + `':`; + const errorPrefix = "Property '" + body.keyValueStructureLine().text + "':"; for (const structureValue of structureValues) { for (const expr of structureValue.expressionInStructure()) { result = result.concat(this.checkExpression(expr, errorPrefix)); @@ -185,7 +191,9 @@ export class StaticChecker /** * Visit a parse tree produced by the `ifElseBody` labeled alternative in `LGTemplateParser.body`. + * * @param context The parse tree. + * @returns The result of visiting if-else body. */ visitIfElseBody(context: IfElseBodyContext): Diagnostic[] { let result: Diagnostic[] = []; @@ -244,7 +252,7 @@ export class StaticChecker this.buildLGDiagnostic(TemplateErrors.invalidExpressionInCondition, undefined, conditionNode) ); } else { - const errorPrefix = `Condition '` + conditionNode.expression(0).text + `': `; + const errorPrefix = "Condition '" + conditionNode.expression(0).text + "': "; result = result.concat(this.checkExpression(conditionNode.expression(0), errorPrefix)); } } else { @@ -270,7 +278,9 @@ export class StaticChecker /** * Visit a parse tree produced by the `switchCaseBody` labeled alternative in `LGTemplateParser.body`. + * * @param context The parse tree. + * @returns The result of visiting switch-case body. */ visitSwitchCaseBody(context: SwitchCaseBodyContext): Diagnostic[] { let result: Diagnostic[] = []; @@ -375,7 +385,9 @@ export class StaticChecker /** * Visit a parse tree produced by `LGTemplateParser.normalTemplateString`. + * * @param context The parse tree. + * @returns The result of visiting normal template string. */ visitNormalTemplateString(context: NormalTemplateStringContext): Diagnostic[] { const prefixErrorMsg = TemplateExtensions.getPrefixErrorMessage(context); @@ -396,6 +408,7 @@ export class StaticChecker /** * Gets the default value returned by visitor methods. + * * @returns Empty [Diagnostic](xref:botbuilder-lg.Diagnostic) array. */ protected defaultResult(): Diagnostic[] { diff --git a/libraries/botbuilder-lg/src/template.ts b/libraries/botbuilder-lg/src/template.ts index 9f36a74ebf..cfd3cd1630 100644 --- a/libraries/botbuilder-lg/src/template.ts +++ b/libraries/botbuilder-lg/src/template.ts @@ -45,6 +45,7 @@ export class Template { /** * Creates a new instance of the [Template](xref:botbuilder-lg.Template) class. + * * @param templatename Template name without parameters. * @param parameters Parameter list. * @param templatebody Template content. @@ -59,6 +60,7 @@ export class Template { /** * Returns a string representing the current [Template](xref:botbuilder-lg.Template) object. + * * @returns A string representing the [Template](xref:botbuilder-lg.Template). */ toString(): string { diff --git a/libraries/botbuilder-lg/src/templateErrors.ts b/libraries/botbuilder-lg/src/templateErrors.ts index 5027c37274..8a6dacf6c9 100644 --- a/libraries/botbuilder-lg/src/templateErrors.ts +++ b/libraries/botbuilder-lg/src/templateErrors.ts @@ -10,62 +10,78 @@ * Centralized LG errors. */ export class TemplateErrors { - static readonly noTemplate: string = `LG file must have at least one template definition.`; + static readonly noTemplate: string = 'LG file must have at least one template definition.'; - static readonly invalidTemplateBody: string = `Invalid template body. Expecting '-' prefix.`; + static readonly invalidTemplateBody: string = "Invalid template body. Expecting '-' prefix."; - static readonly missingStrucEnd: string = `Invalid structure body. Expecting ']' at the end of the body.`; + static readonly missingStrucEnd: string = "Invalid structure body. Expecting ']' at the end of the body."; - static readonly emptyStrucContent: string = `Invalid structure body. Body cannot be empty.`; + public static readonly emptyStrucContent: string = 'Invalid structure body. Body cannot be empty.'; - static readonly invalidWhitespaceInCondition: string = `Invalid condition: At most 1 whitespace allowed between 'IF/ELSEIF/ELSE' and ':'.`; + static readonly invalidWhitespaceInCondition: string = + "Invalid condition: At most 1 whitespace allowed between 'IF/ELSEIF/ELSE' and ':'."; - static readonly notStartWithIfInCondition: string = `Invalid condition: Conditions must start with 'IF/ELSEIF/ELSE' prefix.`; + static readonly notStartWithIfInCondition: string = + "Invalid condition: Conditions must start with 'IF/ELSEIF/ELSE' prefix."; - static readonly multipleIfInCondition: string = `Invalid template body. There cannot be more than one 'IF' condition. Expecting 'IFELSE' or 'ELSE' statement.`; + static readonly multipleIfInCondition: string = + "Invalid template body. There cannot be more than one 'IF' condition. Expecting 'IFELSE' or 'ELSE' statement."; - static readonly notEndWithElseInCondition: string = `Conditional response template does not end with 'ELSE' condition.`; + static readonly notEndWithElseInCondition: string = + "Conditional response template does not end with 'ELSE' condition."; - static readonly invalidMiddleInCondition: string = `Invalid template body. Expecting 'ELSEIF'.`; + static readonly invalidMiddleInCondition: string = "Invalid template body. Expecting 'ELSEIF'."; - static readonly invalidExpressionInCondition: string = `Invalid condition. 'IF', 'ELSEIF' definitions must include a valid expression.`; + static readonly invalidExpressionInCondition: string = + "Invalid condition. 'IF', 'ELSEIF' definitions must include a valid expression."; - static readonly extraExpressionInCondition: string = `Invalid condition. 'ELSE' definition cannot include an expression.`; + static readonly extraExpressionInCondition: string = + "Invalid condition. 'ELSE' definition cannot include an expression."; - static readonly missingTemplateBodyInCondition: string = `Invalid condition body. Conditions must include a valid body.`; + static readonly missingTemplateBodyInCondition: string = + 'Invalid condition body. Conditions must include a valid body.'; - static readonly invalidWhitespaceInSwitchCase: string = `Invalid condition: At most 1 whitespace allowed between 'SWITCH/CASE/DEFAULT' and ':'.`; + static readonly invalidWhitespaceInSwitchCase: string = + "Invalid condition: At most 1 whitespace allowed between 'SWITCH/CASE/DEFAULT' and ':'."; - static readonly notStartWithSwitchInSwitchCase: string = `Invalid conditional response template. Expecting a 'SWITCH' statement?`; + static readonly notStartWithSwitchInSwitchCase: string = + "Invalid conditional response template. Expecting a 'SWITCH' statement?"; - static readonly multipleSwithStatementInSwitchCase: string = `Invalid template body. There cannot be more than one 'SWITCH' statement. Expecting 'CASE' or 'DEFAULT' statement.`; + static readonly multipleSwithStatementInSwitchCase: string = + "Invalid template body. There cannot be more than one 'SWITCH' statement. Expecting 'CASE' or 'DEFAULT' statement."; - static readonly invalidStatementInMiddlerOfSwitchCase: string = `Invalid template body. Expecting a 'CASE' statement.`; + static readonly invalidStatementInMiddlerOfSwitchCase: string = + "Invalid template body. Expecting a 'CASE' statement."; - static readonly notEndWithDefaultInSwitchCase: string = `Conditional response template does not end with 'DEFAULT' condition.`; + static readonly notEndWithDefaultInSwitchCase: string = + "Conditional response template does not end with 'DEFAULT' condition."; - static readonly missingCaseInSwitchCase: string = `Invalid template body. Expecting at least one 'CASE' statement.`; + static readonly missingCaseInSwitchCase: string = + "Invalid template body. Expecting at least one 'CASE' statement."; - static readonly invalidExpressionInSwiathCase: string = `Invalid condition. 'SWITCH' and 'CASE' statements must include a valid expression.`; + static readonly invalidExpressionInSwiathCase: string = + "Invalid condition. 'SWITCH' and 'CASE' statements must include a valid expression."; - static readonly extraExpressionInSwitchCase: string = `Invalid condition. 'DEFAULT' statement cannot include an expression.`; + static readonly extraExpressionInSwitchCase: string = + "Invalid condition. 'DEFAULT' statement cannot include an expression."; - static readonly missingTemplateBodyInSwitchCase: string = `Invalid condition body. Expecing valid body inside a 'CASE' or 'DEFAULT' block.`; + static readonly missingTemplateBodyInSwitchCase: string = + "Invalid condition body. Expecing valid body inside a 'CASE' or 'DEFAULT' block."; static readonly noEndingInMultiline: string = 'Expecting "```" to close the multi-line block.'; - static readonly noCloseBracket: string = `Close } is missing in Expression.`; + static readonly noCloseBracket: string = 'Close } is missing in Expression.'; - static readonly loopDetected: string = `Loop detected:`; + static readonly loopDetected: string = 'Loop detected:'; - static readonly invalidMemory: string = `Scope is not a LG customized memory.`; + static readonly invalidMemory: string = 'Scope is not a LG customized memory.'; - static readonly staticFailure: string = `Static failure with the following error.`; + static readonly staticFailure: string = 'Static failure with the following error.'; static readonly invalidTemplateNameType: string = 'Expected string type for the parameter of template function.'; - static readonly importFormatError: string = `Import format should follow '[x](y)' or '[x](y) as z'.`; + static readonly importFormatError: string = "Import format should follow '[x](y)' or '[x](y) as z'."; static readonly invalidStrucBody = (invalidBody: string): string => `Invalid structure body: '${invalidBody}'. Body can include = pairs or \${reference()} template reference.`; @@ -103,13 +119,13 @@ export class TemplateErrors { expectedCount: number, actualCount: number ): string => - `arguments mismatch for template '` + + "arguments mismatch for template '" + `${templateName}` + - `'. Expecting '` + + "'. Expecting '" + `${expectedCount}` + - `' arguments, actual '` + + "' arguments, actual '" + `${actualCount}` + - `'.`; + "'."; static readonly templateExist = (templateName: string): string => `template '${templateName}' already exists.`; diff --git a/libraries/botbuilder-lg/src/templateException.ts b/libraries/botbuilder-lg/src/templateException.ts index 1bde10ad96..8bbab4ec19 100644 --- a/libraries/botbuilder-lg/src/templateException.ts +++ b/libraries/botbuilder-lg/src/templateException.ts @@ -15,6 +15,7 @@ export class TemplateException extends Error { /** * Creates a new instance of the [TemplateException](xref:botbuilder-lg.TemplateException) class. + * * @param m Error message. * @param diagnostics List of [Diagnostic](xref:botbuilder-lg.Diagnostic) to throw. */ @@ -26,6 +27,8 @@ export class TemplateException extends Error { /** * Diagnostics. + * + * @returns A diagnostic of the error or warning (range, message, severity, source, code). */ getDiagnostic(): Diagnostic[] { return this.diagnostics; diff --git a/libraries/botbuilder-lg/src/templateExtensions.ts b/libraries/botbuilder-lg/src/templateExtensions.ts index 7d03114ea5..0d1c49995b 100644 --- a/libraries/botbuilder-lg/src/templateExtensions.ts +++ b/libraries/botbuilder-lg/src/templateExtensions.ts @@ -26,6 +26,7 @@ import { export class TemplateExtensions { /** * Trim expression. ${abc} => abc, ${a == {}} => a == {}. + * * @param expression Input expression string. * @returns Pure expression string. */ @@ -49,6 +50,7 @@ export class TemplateExtensions { * path is from authored content which doesn't know what OS it is running on. * This method treats / and \ both as seperators regardless of OS, for windows that means / -> \ and for linux/mac \ -> /. * This allows author to use ../foo.lg or ..\foo.lg as equivelents for importing. + * * @param ambiguousPath AuthoredPath. * @returns Path expressed as OS path. */ @@ -64,6 +66,7 @@ export class TemplateExtensions { /** * Get prefix error message from normal template sting context. + * * @param context Normal template sting context. * @returns Prefix error message. */ @@ -75,14 +78,14 @@ export class TemplateExtensions { let tempMsg = ''; if (conditionContext.ifCondition() && conditionContext.ifCondition().expression().length > 0) { tempMsg = conditionContext.ifCondition().expression(0).text; - errorPrefix = `Condition '` + tempMsg + `': `; + errorPrefix = "Condition '" + tempMsg + "': "; } } else { if (context.parent.parent.parent instanceof SwitchCaseRuleContext) { const switchCaseContext = context.parent.parent.parent; const state = switchCaseContext.switchCaseStat(); if (state && state.DEFAULT()) { - errorPrefix = `Case 'Default':`; + errorPrefix = "Case 'Default':"; } else if (state && state.SWITCH()) { let tempMsg = ''; if (state.expression(0)) { @@ -105,7 +108,9 @@ export class TemplateExtensions { /** * If a value is pure Expression. + * * @param ctx Key value structure value context. + * @returns True if the value is pure Expression, false otherwise. */ static isPureExpression(ctx: KeyValueStructureValueContext): boolean { if (ctx.expressionInStructure() === undefined || ctx.expressionInStructure().length != 1) { @@ -117,6 +122,7 @@ export class TemplateExtensions { /** * Escape \ from text. + * * @param exp Input text. * @returns Escaped text. */ @@ -143,14 +149,18 @@ export class TemplateExtensions { /** * Generate new guid string. + * + * @returns The new guid string. */ static newGuid(): string { return uuidv4(); } /** - * read line from text. + * Read line from text. + * * @param input Text content. + * @returns Split read line. */ static readLine(input: string): string[] { if (!input) { @@ -162,6 +172,7 @@ export class TemplateExtensions { /** * Convert antlr parser into Range. + * * @param context Antlr parse context. * @param [lineOffset] Line offset. * @returns Range object. diff --git a/libraries/botbuilder-lg/src/templateImport.ts b/libraries/botbuilder-lg/src/templateImport.ts index 8056612479..e7b31b244b 100644 --- a/libraries/botbuilder-lg/src/templateImport.ts +++ b/libraries/botbuilder-lg/src/templateImport.ts @@ -34,6 +34,7 @@ export class TemplateImport { /** * Creates a new instance of the [TemplateImport](xref:botbuilder-lg.TemplateImport) class. + * * @param description Import description, which is in []. * @param id Import id, which is a path, in (). * @param sourceRange [SourceRange](xref:botbuilder-lg.SourceRange) of template. diff --git a/libraries/botbuilder-lg/src/templates.ts b/libraries/botbuilder-lg/src/templates.ts index 39b890def1..f4e9c2cd61 100644 --- a/libraries/botbuilder-lg/src/templates.ts +++ b/libraries/botbuilder-lg/src/templates.ts @@ -46,7 +46,8 @@ export class Templates implements Iterable