Skip to content

Commit

Permalink
Fix incorrect quasi location in template string (#921)
Browse files Browse the repository at this point in the history
* Fix incorrect quasi location in template string

* Fix lint issue
  • Loading branch information
TwitchBronBron authored Sep 27, 2023
1 parent db6cf8e commit c4ca7d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lexer/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ export class Lexer {
this.scanToken();
}
if (this.check('}')) {
this.current++;
this.advance();
this.addToken(TokenKind.TemplateStringExpressionEnd);
} else {

Expand Down
36 changes: 36 additions & 0 deletions src/parser/tests/expression/TemplateStringExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Parser, ParseMode } from '../../Parser';
import { AssignmentStatement } from '../../Statement';
import { Program } from '../../../Program';
import { expectZeroDiagnostics, getTestTranspile } from '../../../testHelpers.spec';
import { util } from '../../../util';

describe('TemplateStringExpression', () => {
describe('parser template String', () => {
Expand All @@ -18,6 +19,41 @@ describe('TemplateStringExpression', () => {
});

describe('in assignment', () => {
it('generates correct locations for quasis', () => {
let { tokens } = Lexer.scan('print `0xAAAAAA${"0xBBBBBB"}0xCCCCCC`');
expect(
tokens.filter(x => /"?0x/.test(x.text)).map(x => x.range)
).to.eql([
util.createRange(0, 7, 0, 15), // 0xAAAAAA
util.createRange(0, 17, 0, 27), // "0xBBBBBB"
util.createRange(0, 28, 0, 36) // 0xCCCCCC
]);
});

it('generates correct locations for items', () => {
let { tokens } = Lexer.scan('print `${111}${222}${333}`');
//throw out the `print` token
tokens.shift();
expect(
//compute the length of the token char spread
tokens.filter(x => x.text !== '').map(x => [x.range.end.character - x.range.start.character, x.text])
).to.eql([
'`',
'${',
'111',
'}',
'${',
'222',
'}',
'${',
'333',
'}',
'`'
].map(x => [x.length, x])
);
});


it(`simple case`, () => {
let { tokens } = Lexer.scan(`a = \`hello world\``);
let { statements, diagnostics } = Parser.parse(tokens, { mode: ParseMode.BrighterScript });
Expand Down

0 comments on commit c4ca7d9

Please sign in to comment.