Skip to content

Commit

Permalink
Fix template string concatenation issue (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Apr 9, 2021
1 parent 0b9bbba commit be1c976
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
13 changes: 6 additions & 7 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1948,13 +1948,6 @@ export class Parser {
}
}

//template string
if (this.check(TokenKind.BackTick)) {
return this.templateString(false);
//tagged template string (currently we do not support spaces between the identifier and the backtick
} else if (this.checkAny(TokenKind.Identifier, ...AllowedLocalIdentifiers) && this.checkNext(TokenKind.BackTick)) {
return this.templateString(true);
}
let expr = this.boolean();

if (this.check(TokenKind.Question)) {
Expand Down Expand Up @@ -2234,6 +2227,12 @@ export class Parser {
//capture source literals (LINE_NUM if brightscript, or a bunch of them if brighterscript)
case this.matchAny(TokenKind.LineNumLiteral, ...(this.options.mode === ParseMode.BrightScript ? [] : BrighterScriptSourceLiterals)):
return new SourceLiteralExpression(this.previous());
//template string
case this.check(TokenKind.BackTick):
return this.templateString(false);
//tagged template string (currently we do not support spaces between the identifier and the backtick)
case this.checkAny(TokenKind.Identifier, ...AllowedLocalIdentifiers) && this.checkNext(TokenKind.BackTick):
return this.templateString(true);
case this.matchAny(TokenKind.Identifier, ...this.allowedLocalIdentifiers):
return new VariableExpression(this.previous() as Identifier, this.currentNamespaceName);
case this.match(TokenKind.LeftParen):
Expand Down
18 changes: 16 additions & 2 deletions src/parser/tests/expression/TemplateStringExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Lexer } from '../../../lexer';
import { Parser, ParseMode } from '../../Parser';
import { AssignmentStatement } from '../../Statement';
import { Program } from '../../../Program';
import { getTestTranspile } from '../../../testHelpers.spec';
import { expectZeroDiagnostics, getTestTranspile } from '../../../testHelpers.spec';

describe('TemplateStringExpression', () => {
describe('parser template String', () => {
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('TemplateStringExpression', () => {

let { tokens } = Lexer.scan('a = ["one", "two", `I am a complex example\n${a.isRunning(["a","b","c"])}`]');
let { statements, diagnostics } = Parser.parse(tokens, { mode: ParseMode.BrighterScript });
expect(diagnostics).to.be.lengthOf(0);
expectZeroDiagnostics(diagnostics);
expect(statements[0]).instanceof(AssignmentStatement);
});
});
Expand Down Expand Up @@ -240,6 +240,20 @@ describe('TemplateStringExpression', () => {
end sub
`);
});

it('can be concatenated with regular string', () => {
testTranspile(`
sub main()
thing = "this" + \`that\`
otherThing = \`that\` + "this"
end sub
`, `
sub main()
thing = "this" + "that"
otherThing = "that" + "this"
end sub
`, undefined, 'source/main.bs');
});
});
});
});

0 comments on commit be1c976

Please sign in to comment.