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

Function isBlockScopedNameDeclaredBeforeUse

test/fixtures/snapshot/typescript.js:49394–49574  ·  view source on GitHub ↗
(declaration, usage)

Source from the content-addressed store, hash-verified

49392 return ts.Debug.fail("There should exist two symbols, one as property declaration and one as parameter declaration");
49393 }
49394 function isBlockScopedNameDeclaredBeforeUse(declaration, usage) {
49395 var declarationFile = ts.getSourceFileOfNode(declaration);
49396 var useFile = ts.getSourceFileOfNode(usage);
49397 var declContainer = ts.getEnclosingBlockScopeContainer(declaration);
49398 if (declarationFile !== useFile) {
49399 if ((moduleKind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) ||
49400 (!ts.outFile(compilerOptions)) ||
49401 isInTypeQuery(usage) ||
49402 declaration.flags & 16777216 /* NodeFlags.Ambient */) {
49403 // nodes are in different files and order cannot be determined
49404 return true;
49405 }
49406 // declaration is after usage
49407 // can be legal if usage is deferred (i.e. inside function or in initializer of instance property)
49408 if (isUsedInFunctionOrInstanceProperty(usage, declaration)) {
49409 return true;
49410 }
49411 var sourceFiles = host.getSourceFiles();
49412 return sourceFiles.indexOf(declarationFile) <= sourceFiles.indexOf(useFile);
49413 }
49414 if (declaration.pos <= usage.pos && !(ts.isPropertyDeclaration(declaration) && ts.isThisProperty(usage.parent) && !declaration.initializer && !declaration.exclamationToken)) {
49415 // declaration is before usage
49416 if (declaration.kind === 203 /* SyntaxKind.BindingElement */) {
49417 // still might be illegal if declaration and usage are both binding elements (eg var [a = b, b = b] = [1, 2])
49418 var errorBindingElement = ts.getAncestor(usage, 203 /* SyntaxKind.BindingElement */);
49419 if (errorBindingElement) {
49420 return ts.findAncestor(errorBindingElement, ts.isBindingElement) !== ts.findAncestor(declaration, ts.isBindingElement) ||
49421 declaration.pos < errorBindingElement.pos;
49422 }
49423 // or it might be illegal if usage happens before parent variable is declared (eg var [a] = a)
49424 return isBlockScopedNameDeclaredBeforeUse(ts.getAncestor(declaration, 254 /* SyntaxKind.VariableDeclaration */), usage);
49425 }
49426 else if (declaration.kind === 254 /* SyntaxKind.VariableDeclaration */) {
49427 // still might be illegal if usage is in the initializer of the variable declaration (eg var a = a)
49428 return !isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage);
49429 }
49430 else if (ts.isClassDeclaration(declaration)) {
49431 // still might be illegal if the usage is within a computed property name in the class (eg class A { static p = "a"; [A.p]() {} })
49432 return !ts.findAncestor(usage, function (n) { return ts.isComputedPropertyName(n) && n.parent.parent === declaration; });
49433 }
49434 else if (ts.isPropertyDeclaration(declaration)) {
49435 // still might be illegal if a self-referencing property initializer (eg private x = this.x)
49436 return !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ false);
49437 }
49438 else if (ts.isParameterPropertyDeclaration(declaration, declaration.parent)) {
49439 // foo = this.bar is illegal in esnext+useDefineForClassFields when bar is a parameter property
49440 return !(ts.getEmitScriptTarget(compilerOptions) === 99 /* ScriptTarget.ESNext */ && useDefineForClassFields
49441 && ts.getContainingClass(declaration) === ts.getContainingClass(usage)
49442 && isUsedInFunctionOrInstanceProperty(usage, declaration));
49443 }
49444 return true;
49445 }
49446 // declaration is after usage, but it can still be legal if usage is deferred:
49447 // 1. inside an export specifier
49448 // 2. inside a function
49449 // 3. inside an instance property initializer, a reference to a non-instance property
49450 // (except when target: "esnext" and useDefineForClassFields: true and the reference is to a parameter property)
49451 // 4. inside a static property initializer, a reference to a static method in the same class

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…