Skip to content

Allow reserved words in type queries. #199

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

Merged
merged 2 commits into from
Jul 23, 2014
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
11 changes: 6 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,12 +865,13 @@ module ts {
return createMissingList<T>();
}

function parseEntityName(): EntityName {
// The allowReservedWords parameter controls whether reserved words are permitted after the first dot
function parseEntityName(allowReservedWords: boolean): EntityName {
var entity: EntityName = parseIdentifier();
while (parseOptional(SyntaxKind.DotToken)) {
var node = <QualifiedName>createNode(SyntaxKind.QualifiedName, entity.pos);
node.left = entity;
node.right = parseIdentifier();
node.right = allowReservedWords ? parseIdentifierName() : parseIdentifier();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on the right of a dot, shouldn't we always allow reservedWords?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, n/m. i see the subtlety here. Could you doc the reasoning behind this. Specifically, that normally an Entity name could never have keywords in it, since it can only refer to module names on the left, and an identifier on the right (and module names can only be identifiers). however, for typeof, you need to allow identifier names on the right.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it parses an EntityName by default but when you specify true for the optional parameter it parses a TypeQueryExpression. I can add a comment to that effect.

entity = finishNode(node);
}
return entity;
Expand Down Expand Up @@ -899,7 +900,7 @@ module ts {

function parseTypeReference(): TypeReferenceNode {
var node = <TypeReferenceNode>createNode(SyntaxKind.TypeReference);
node.typeName = parseEntityName();
node.typeName = parseEntityName(/*allowReservedWords*/ false);
if (!scanner.hasPrecedingLineBreak() && token === SyntaxKind.LessThanToken) {
node.typeArguments = parseTypeArguments();
}
Expand All @@ -909,7 +910,7 @@ module ts {
function parseTypeQuery(): TypeQueryNode {
var node = <TypeQueryNode>createNode(SyntaxKind.TypeQuery);
parseExpected(SyntaxKind.TypeOfKeyword);
node.exprName = parseEntityName();
node.exprName = parseEntityName(/*allowReservedWords*/ true);
return finishNode(node);
}

Expand Down Expand Up @@ -2815,7 +2816,7 @@ module ts {
parseExpected(SyntaxKind.ImportKeyword);
node.name = parseIdentifier();
parseExpected(SyntaxKind.EqualsToken);
var entityName = parseEntityName();
var entityName = parseEntityName(/*allowReservedWords*/ false);
if (entityName.kind === SyntaxKind.Identifier && (<Identifier>entityName).text === "require" && parseOptional(SyntaxKind.OpenParenToken)) {
node.externalModuleName = parseStringLiteral();
parseExpected(SyntaxKind.CloseParenToken);
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/typeQueryWithReservedWords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [typeQueryWithReservedWords.ts]
class Controller {
create() {
}
delete() {
}
var() {
}
}

interface IScope {
create: typeof Controller.prototype.create;
delete: typeof Controller.prototype.delete; // Should not error
var: typeof Controller.prototype.var; // Should not error
}


//// [typeQueryWithReservedWords.js]
var Controller = (function () {
function Controller() {
}
Controller.prototype.create = function () {
};
Controller.prototype.delete = function () {
};
Controller.prototype.var = function () {
};
return Controller;
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Controller {
create() {
}
delete() {
}
var() {
}
}

interface IScope {
create: typeof Controller.prototype.create;
delete: typeof Controller.prototype.delete; // Should not error
var: typeof Controller.prototype.var; // Should not error
}