MCPcopy Index your code
hub / github.com/nodejs/node / checkThisExpression

Function checkThisExpression

test/fixtures/snapshot/typescript.js:71941–71997  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

71939 }
71940 }
71941 function checkThisExpression(node) {
71942 var isNodeInTypeQuery = isInTypeQuery(node);
71943 // Stop at the first arrow function so that we can
71944 // tell whether 'this' needs to be captured.
71945 var container = ts.getThisContainer(node, /* includeArrowFunctions */ true);
71946 var capturedByArrowFunction = false;
71947 if (container.kind === 171 /* SyntaxKind.Constructor */) {
71948 checkThisBeforeSuper(node, container, ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class);
71949 }
71950 // Now skip arrow functions to get the "real" owner of 'this'.
71951 if (container.kind === 214 /* SyntaxKind.ArrowFunction */) {
71952 container = ts.getThisContainer(container, /* includeArrowFunctions */ false);
71953 capturedByArrowFunction = true;
71954 }
71955 checkThisInStaticClassFieldInitializerInDecoratedClass(node, container);
71956 switch (container.kind) {
71957 case 261 /* SyntaxKind.ModuleDeclaration */:
71958 error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_module_or_namespace_body);
71959 // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
71960 break;
71961 case 260 /* SyntaxKind.EnumDeclaration */:
71962 error(node, ts.Diagnostics.this_cannot_be_referenced_in_current_location);
71963 // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
71964 break;
71965 case 171 /* SyntaxKind.Constructor */:
71966 if (isInConstructorArgumentInitializer(node, container)) {
71967 error(node, ts.Diagnostics.this_cannot_be_referenced_in_constructor_arguments);
71968 // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
71969 }
71970 break;
71971 case 162 /* SyntaxKind.ComputedPropertyName */:
71972 error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_computed_property_name);
71973 break;
71974 }
71975 // When targeting es6, mark that we'll need to capture `this` in its lexically bound scope.
71976 if (!isNodeInTypeQuery && capturedByArrowFunction && languageVersion < 2 /* ScriptTarget.ES2015 */) {
71977 captureLexicalThis(node, container);
71978 }
71979 var type = tryGetThisTypeAt(node, /*includeGlobalThis*/ true, container);
71980 if (noImplicitThis) {
71981 var globalThisType_1 = getTypeOfSymbol(globalThisSymbol);
71982 if (type === globalThisType_1 && capturedByArrowFunction) {
71983 error(node, ts.Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this);
71984 }
71985 else if (!type) {
71986 // With noImplicitThis, functions may not reference 'this' if it has type 'any'
71987 var diag = error(node, ts.Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
71988 if (!ts.isSourceFile(container)) {
71989 var outsideThis = tryGetThisTypeAt(container);
71990 if (outsideThis && outsideThis !== globalThisType_1) {
71991 ts.addRelatedInfo(diag, ts.createDiagnosticForNode(container, ts.Diagnostics.An_outer_value_of_this_is_shadowed_by_this_container));
71992 }
71993 }
71994 }
71995 }
71996 return type || anyType;
71997 }
71998 function tryGetThisTypeAt(node, includeGlobalThis, container) {

Callers 5

checkIdentifierFunction · 0.85
checkQualifiedNameFunction · 0.85
checkExpressionWorkerFunction · 0.85

Calls 8

isInTypeQueryFunction · 0.85
checkThisBeforeSuperFunction · 0.85
captureLexicalThisFunction · 0.85
tryGetThisTypeAtFunction · 0.85
getTypeOfSymbolFunction · 0.85
errorFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…