Typescript Deep Dive by Basarat Ali Syed
Typescript Deep Dive by Basarat Ali Syed
Typescript Deep Dive by Basarat Ali Syed
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
TypeScript <strong>Deep</strong> <strong>Dive</strong><br />
Binder Error Reporting<br />
Binding errors are added to the sourceFile's list of bindDiagnostics .<br />
An example error detected during binding is the use of eval or arguments as a variable name in use strict scenario. The<br />
relevant code is presented in its entirety below ( checkStrictModeEvalOrArguments is called from multiple places, call stacks<br />
originating from bindWorker which calls different functions for different node SyntaxKind ):<br />
function checkStrictModeEvalOrArguments(contextNode: Node, name: Node) {<br />
if (name && name.kind === SyntaxKind.Identifier) {<br />
let identifier = name;<br />
if (isEvalOrArgumentsIdentifier(identifier)) {<br />
// We check first if the name is inside class declaration or class expression; if so give explicit message<br />
// otherwise report generic error message.<br />
let span = getErrorSpanForNode(file, name);<br />
file.bindDiagnostics.push(createFileDiagnostic(file, span.start, span.length,<br />
getStrictModeEvalOrArgumentsMessage(contextNode), identifier.text));<br />
}<br />
}<br />
}<br />
function isEvalOrArgumentsIdentifier(node: Node): boolean {<br />
return node.kind === SyntaxKind.Identifier &&<br />
((node).text === "eval" || (node).text === "arguments");<br />
}<br />
function getStrictModeEvalOrArgumentsMessage(node: Node) {<br />
// Provide specialized messages to help the user understand why we think they're in<br />
// strict mode.<br />
if (getContainingClass(node)) {<br />
return Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode;<br />
}<br />
if (file.externalModuleIndicator) {<br />
return Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode;<br />
}<br />
}<br />
return Diagnostics.Invalid_use_of_0_in_strict_mode;<br />
Binder Error Reporting<br />
91