Skip to content

Commit

Permalink
Fix anonymous template name conflict (#2595)
Browse files Browse the repository at this point in the history
Co-authored-by: Dong Lei <[email protected]>
  • Loading branch information
Danieladu and boydc2014 authored Jul 28, 2020
1 parent f0634b0 commit 68a52a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion libraries/botbuilder-lg/src/staticChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export class StaticChecker extends AbstractParseTreeVisitor<Diagnostic[]> implem
const lineOffset = this.currentTemplate !== undefined ? this.currentTemplate.sourceRange.range.start.line : 0;

let templateNameInfo = '';
if (this.currentTemplate !== undefined && this.currentTemplate.name !== Templates.inlineTemplateId) {
if (this.currentTemplate !== undefined && this.currentTemplate.name.startsWith(Templates.inlineTemplateIdPrefix)) {
templateNameInfo = `[${ this.currentTemplate.name }]`;
}

Expand Down
17 changes: 14 additions & 3 deletions libraries/botbuilder-lg/src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Templates implements Iterable<Template> {
/**
* Temp Template ID for inline content.
*/
public static readonly inlineTemplateId: string = '__temp__';
public static readonly inlineTemplateIdPrefix: string = '__temp__';
private readonly newLineRegex = /(\r?\n)/g;
private readonly newLine: string = '\r\n';
private readonly namespaceKey = '@namespace';
Expand Down Expand Up @@ -246,17 +246,19 @@ export class Templates implements Iterable<Template> {

this.checkErrors();

const inlineTemplateId = `${Templates.inlineTemplateIdPrefix}${this.getramdonTemplateId()}`;

// wrap inline string with "# name and -" to align the evaluation process
const multiLineMark = '```';

inlineStr = !(inlineStr.trim().startsWith(multiLineMark) && inlineStr.includes('\n'))
? `${ multiLineMark }${ inlineStr }${ multiLineMark }` : inlineStr;

const newContent = `#${ Templates.inlineTemplateId } ${ this.newLine } - ${ inlineStr }`;
const newContent = `#${ inlineTemplateId } ${ this.newLine } - ${ inlineStr }`;

const newTemplates = TemplatesParser.parseTextWithRef(newContent, this);
var evalOpt = opt !== undefined ? opt.merge(this.lgOptions) : this.lgOptions;
return newTemplates.evaluate(Templates.inlineTemplateId, scope, evalOpt);
return newTemplates.evaluate(inlineTemplateId, scope, evalOpt);
}

/**
Expand Down Expand Up @@ -361,6 +363,15 @@ export class Templates implements Iterable<Template> {
return this.content;
}

private getramdonTemplateId(): string {
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, (c: any): string => {
const r: number = Math.random() * 16 | 0;
const v: number = c === 'x' ? r : (r & 0x3 | 0x8);

return v.toString(16);
});
}

private appendDiagnosticWithOffset(diagnostics: Diagnostic[], offset: number): void {
if (diagnostics) {
diagnostics.forEach((u): void => {
Expand Down
3 changes: 3 additions & 0 deletions libraries/botbuilder-lg/tests/lg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ describe('LG', function() {

let evaled = templates.evaluate('template', scope);
assert.strictEqual(evaled, 'hello vivian'.length);

evaled = templates.evaluateText('${length(expandText(@answer))}', scope);
assert.strictEqual(evaled, 'hello vivian'.length);
});

it('TestBasicTemplateRefWithParameters', function() {
Expand Down

0 comments on commit 68a52a2

Please sign in to comment.