MCPcopy Create free account
hub / github.com/nodejs/node / getSymbolScope

Function getSymbolScope

test/fixtures/snapshot/typescript.js:137913–137975  ·  view source on GitHub ↗

* Determines the smallest scope in which a symbol may have named references. * Note that not every construct has been accounted for. This function can * probably be improved. * * @returns undefined if the scope cannot be determined, implying that

(symbol)

Source from the content-addressed store, hash-verified

137911 * a reference to a symbol can occur anywhere.
137912 */
137913 function getSymbolScope(symbol) {
137914 // If this is the symbol of a named function expression or named class expression,
137915 // then named references are limited to its own scope.
137916 var declarations = symbol.declarations, flags = symbol.flags, parent = symbol.parent, valueDeclaration = symbol.valueDeclaration;
137917 if (valueDeclaration && (valueDeclaration.kind === 213 /* SyntaxKind.FunctionExpression */ || valueDeclaration.kind === 226 /* SyntaxKind.ClassExpression */)) {
137918 return valueDeclaration;
137919 }
137920 if (!declarations) {
137921 return undefined;
137922 }
137923 // If this is private property or method, the scope is the containing class
137924 if (flags & (4 /* SymbolFlags.Property */ | 8192 /* SymbolFlags.Method */)) {
137925 var privateDeclaration = ts.find(declarations, function (d) { return ts.hasEffectiveModifier(d, 8 /* ModifierFlags.Private */) || ts.isPrivateIdentifierClassElementDeclaration(d); });
137926 if (privateDeclaration) {
137927 return ts.getAncestor(privateDeclaration, 257 /* SyntaxKind.ClassDeclaration */);
137928 }
137929 // Else this is a public property and could be accessed from anywhere.
137930 return undefined;
137931 }
137932 // If symbol is of object binding pattern element without property name we would want to
137933 // look for property too and that could be anywhere
137934 if (declarations.some(ts.isObjectBindingElementWithoutPropertyName)) {
137935 return undefined;
137936 }
137937 /*
137938 If the symbol has a parent, it's globally visible unless:
137939 - It's a private property (handled above).
137940 - It's a type parameter.
137941 - The parent is an external module: then we should only search in the module (and recurse on the export later).
137942 - But if the parent has `export as namespace`, the symbol is globally visible through that namespace.
137943 */
137944 var exposedByParent = parent && !(symbol.flags & 262144 /* SymbolFlags.TypeParameter */);
137945 if (exposedByParent && !(ts.isExternalModuleSymbol(parent) && !parent.globalExports)) {
137946 return undefined;
137947 }
137948 var scope;
137949 for (var _i = 0, declarations_1 = declarations; _i < declarations_1.length; _i++) {
137950 var declaration = declarations_1[_i];
137951 var container = ts.getContainerNode(declaration);
137952 if (scope && scope !== container) {
137953 // Different declarations have different containers, bail out
137954 return undefined;
137955 }
137956 if (!container || container.kind === 305 /* SyntaxKind.SourceFile */ && !ts.isExternalOrCommonJsModule(container)) {
137957 // This is a global variable and not an external module, any declaration defined
137958 // within this scope is visible outside the file
137959 return undefined;
137960 }
137961 scope = container;
137962 if (ts.isFunctionExpression(scope)) {
137963 var next = void 0;
137964 while (next = ts.getNextJSDocCommentLocation(scope)) {
137965 scope = next;
137966 }
137967 }
137968 }
137969 // If symbol.parent, this means we are in an export of an external module. (Otherwise we would have returned `undefined` above.)
137970 // For an export of a module, we may be in a declaration file, and it may be accessed elsewhere. E.g.:

Callers 1

Calls 2

someMethod · 0.80
findMethod · 0.65

Tested by

no test coverage detected