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

fix(lex): Allow JS object properties as identifiers #614

Merged
merged 1 commit into from
Jan 21, 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
15 changes: 8 additions & 7 deletions src/lexer/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,10 @@ export class Lexer {
advance();
} // read the next word

let twoWords = source.slice(start, current);
//replace all of the whitespace with a single space character so we can properly match keyword token types
twoWords = twoWords.replace(whitespace, " ");
let maybeTokenType = KeyWords[twoWords.toLowerCase()];
if (maybeTokenType) {
addToken(maybeTokenType);
// replace all of the whitespace with a single space character so we can properly match keyword token types
let twoWords = source.slice(start, current).replace(whitespace, " ").toLowerCase();
if (KeyWords.hasOwnProperty(twoWords)) {
addToken(KeyWords[twoWords]);
return;
} else {
// reset if the last word and the current word didn't form a multi-word Lexeme
Expand All @@ -639,7 +637,10 @@ export class Lexer {
advance();
}

let tokenType = KeyWords[text.toLowerCase()] || Lexeme.Identifier;
let textLower = text.toLowerCase();
let tokenType = KeyWords.hasOwnProperty(textLower)
? KeyWords[textLower]
: Lexeme.Identifier;
if (tokenType === KeyWords.rem) {
//the rem keyword can be used as an identifier on objects,
//so do a quick look-behind to see if there's a preceeding dot
Expand Down
12 changes: 12 additions & 0 deletions test/lexer/Lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,18 @@ describe("lexer", () => {
"amet&",
]);
});

it("allows JS keywords as identifiers", () => {
let { tokens } = Lexer.scan("constructor valueOf toString __proto__");
let identifiers = tokens.filter((t) => t.kind !== Lexeme.Eof);
expect(identifiers.every((t) => t.kind === Lexeme.Identifier)).toBe(true);
expect(identifiers.map((t) => t.text)).toEqual([
"constructor",
"valueOf",
"toString",
"__proto__",
]);
});
});

describe("conditional compilation", () => {
Expand Down