|
| 1 | +// Code largely based on https://github.com/ajafff/tsutils |
| 2 | +// Original license: https://github.com/ajafff/tsutils/blob/26b195358ec36d59f00333115aa3ffd9611ca78b/LICENSE |
| 3 | + |
| 4 | +import ts from "typescript"; |
| 5 | + |
| 6 | +import { isFunctionScopeBoundary } from "../scopes"; |
| 7 | +import { DeclarationDomain } from "./declarations"; |
| 8 | +import type { EnumScope, NamespaceScope } from "./scopes"; |
| 9 | +import { InternalUsageInfo, Usage, UsageInfoCallback } from "./usage"; |
| 10 | + |
| 11 | +export enum ScopeBoundary { |
| 12 | + None = 0, |
| 13 | + Function = 1, |
| 14 | + Block = 2, |
| 15 | + Type = 4, |
| 16 | + ConditionalType = 8, |
| 17 | +} |
| 18 | + |
| 19 | +export enum ScopeBoundarySelector { |
| 20 | + Function = ScopeBoundary.Function, |
| 21 | + Block = ScopeBoundarySelector.Function | ScopeBoundary.Block, |
| 22 | + Type = ScopeBoundarySelector.Block | ScopeBoundary.Type, |
| 23 | + InferType = ScopeBoundary.ConditionalType, |
| 24 | +} |
| 25 | + |
| 26 | +export interface Scope { |
| 27 | + addUse(use: Usage, scope?: Scope): void; |
| 28 | + addVariable( |
| 29 | + identifier: string, |
| 30 | + name: ts.PropertyName, |
| 31 | + selector: ScopeBoundarySelector, |
| 32 | + exported: boolean, |
| 33 | + domain: DeclarationDomain, |
| 34 | + ): void; |
| 35 | + createOrReuseEnumScope(name: string, exported: boolean): EnumScope; |
| 36 | + createOrReuseNamespaceScope( |
| 37 | + name: string, |
| 38 | + exported: boolean, |
| 39 | + ambient: boolean, |
| 40 | + hasExportStatement: boolean, |
| 41 | + ): NamespaceScope; |
| 42 | + end(cb: UsageInfoCallback): void; |
| 43 | + getDestinationScope(selector: ScopeBoundarySelector): Scope; |
| 44 | + getFunctionScope(): Scope; |
| 45 | + getVariables(): Map<string, InternalUsageInfo>; |
| 46 | + markExported(name: ts.Identifier, as?: ts.Identifier): void; |
| 47 | +} |
| 48 | + |
| 49 | +export function isBlockScopeBoundary(node: ts.Node): ScopeBoundary { |
| 50 | + switch (node.kind) { |
| 51 | + case ts.SyntaxKind.Block: { |
| 52 | + const parent = node.parent; |
| 53 | + return parent.kind !== ts.SyntaxKind.CatchClause && |
| 54 | + // blocks inside SourceFile are block scope boundaries |
| 55 | + (parent.kind === ts.SyntaxKind.SourceFile || |
| 56 | + // blocks that are direct children of a function scope boundary are no scope boundary |
| 57 | + // for example the FunctionBlock is part of the function scope of the containing function |
| 58 | + !isFunctionScopeBoundary(parent)) |
| 59 | + ? ScopeBoundary.Block |
| 60 | + : ScopeBoundary.None; |
| 61 | + } |
| 62 | + case ts.SyntaxKind.ForStatement: |
| 63 | + case ts.SyntaxKind.ForInStatement: |
| 64 | + case ts.SyntaxKind.ForOfStatement: |
| 65 | + case ts.SyntaxKind.CaseBlock: |
| 66 | + case ts.SyntaxKind.CatchClause: |
| 67 | + case ts.SyntaxKind.WithStatement: |
| 68 | + return ScopeBoundary.Block; |
| 69 | + default: |
| 70 | + return ScopeBoundary.None; |
| 71 | + } |
| 72 | +} |
0 commit comments