-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Migrate strict mode check #2684
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
Conversation
// class C { | ||
// foo(x: public){} // Error. | ||
// } | ||
if (node.typeName.kind === SyntaxKind.Identifier && (<Identifier>node.typeName).strictModeKind) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this reads oddly. Perhaps if this was .isKeywordInStrictMode instead?
} | ||
|
||
function checkGrammarDeclarationNameInStrictMode(node: Declaration): boolean { | ||
let name = node.name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work for modules. For example, if you have:
"use strict";
module public.whatever {
}
(make sure to add a test for this).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also won't work for a variable with a binding pattern. For example:
"use strict";
var { public, private };
@@ -8069,6 +8070,8 @@ module ts { | |||
// DECLARATION AND STATEMENT TYPE CHECKING | |||
|
|||
function checkTypeParameter(node: TypeParameterDeclaration) { | |||
checkGrammarDeclarationNameInStrictMode(node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch remembering to check this. Is there a test for this as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep
// GRAMMAR CHECKING | ||
function isReservedwordInStrictMode(node: Identifier): boolean { | ||
// Check that originalStrictModeSyntaxKind is less than LastFurtureReservedWord to see if an Identifier is a strict-mode reserved word | ||
if ((node.parserContextFlags & ParserContextFlags.StrictMode) && node.originalStrictModeSyntaxKind <= SyntaxKind.LastFutureReservedWord) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you simplify this to "return (node.parseContextFlags & ParserContextFlags.StrictMode) !== 0 && ..."
let name = element.name; | ||
if (name.originalStrictModeSyntaxKind) { | ||
let nameText = declarationNameToString(name); | ||
reportError = grammarErrorOnNode(name, Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably be |=
With this change, Such code compiles with 1.5 alpha with no issue. |
Move strict mode check into type checker so we can give better error messages. This will be useful particularly when class declaration and ES6 module become strict mode code. One thing worth pointing out is that strict mode is a grammar check which means that if there is any parsing error, strict mode error will not get reported.