Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling for createStringLiteral #292

Merged
merged 3 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/astUtils/creators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai';
import { createStringLiteral } from './creators';

describe('creators', () => {

describe('createStringLiteral', () => {
it('wraps the value in quotes', () => {
expect(createStringLiteral('hello world').token.text).to.equal('"hello world"');
});
it('does not wrap already-quoted value in extra quotes', () => {
expect(createStringLiteral('"hello world"').token.text).to.equal('"hello world"');
});

it('does not wrap badly quoted value in additional quotes', () => {
//leading
expect(createStringLiteral('"hello world').token.text).to.equal('"hello world');
//trailing
expect(createStringLiteral('hello world"').token.text).to.equal('hello world"');
});
});
});
9 changes: 9 additions & 0 deletions src/astUtils/creators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ export function createDottedIdentifier(path: string[], range?: Range, namespaceN
return new DottedGetExpression(obj, createToken(TokenKind.Identifier, ident, range), createToken(TokenKind.Dot, '.', range));
}

/**
* Create a StringLiteralExpression. The TokenKind.StringLiteral token value includes the leading and trailing doublequote during lexing.
* Since brightscript doesn't support strings with quotes in them, we can safely auto-detect and wrap the value in quotes in this function.
* @param value - the string value. (value will be wrapped in quotes if they are missing)
*/
export function createStringLiteral(value: string, range?: Range) {
//wrap the value in double quotes
if (!value.startsWith('"') && !value.endsWith('"')) {
value = '"' + value + '"';
}
return new LiteralExpression(createToken(TokenKind.StringLiteral, value, range));
}
export function createIntegerLiteral(value: string, range?: Range) {
Expand Down