All files ScopedSymbol.ts

84.36% Statements 491/582
82.25% Branches 51/62
78.26% Functions 18/23
84.36% Lines 491/582

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 5831x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 41054x 41054x 1x 1x 1x 1x 1x 1x 1x 1x 1x 587704x 587704x 1x 1x 32x 32x 32x       1x 1x 27x 27x 27x       1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 96335x 96335x 96335x 96335x 96335x 96335x 10201484x 2x 2x     2x 2x 2x 96335x 96335x 96333x 96333x 96333x 96333x 1x 1x 19421x 19421x 19421x 19421x 19421x 19421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 222984x 222984x 222984x 222984x 418682x 99927x 99927x 418682x 418682x 222974x 222974x 222984x 222984x 222984x 222974x 222984x 222984x 222984x 222984x 1x 1x 1x 1x 1x 1x 1x 1x 1x                             1x 1x 1x 1x 1x 1x 1x 36644x 36644x 36644x 36644x 68694x 68694x 68694x 68694x 68694x 36642x 36642x 36644x 36644x 36644x 36642x 36644x 36644x 36644x 36644x 1x 1x 1x 1x 1x 1x 1x                             1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 11x 7x 7x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 7712x 6892x 6892x 7712x 7712x 14x 14x 14x 7712x 31x 31x 17x       17x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 48x 7x 7x 7x 7x 48x 10x 10x 17x 6x 6x 6x 6x 6x 6x 4x 4x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x     4x 4x 4x 4x     4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 1x 1x 1x 1x 1x 1x 1x 4x 4x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x     2x 2x     2x 2x 1x 1x 1x 1x 2x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5579x 5579x 1x  
/*
 * This file is released under the MIT license.
 * Copyright (c) 2023, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import { SymbolConstructor } from "./types";
 
import { BaseSymbol, type IBaseSymbol } from "./BaseSymbol";
import { DuplicateSymbolError } from "./DuplicateSymbolError";
 
export interface IScopedSymbol extends IBaseSymbol {
    /**
     * @returns A promise resolving to all direct child symbols with a scope (e.g. classes in a module).
     */
    directScopes: Promise<ScopedSymbol[]>;
 
    children: BaseSymbol[];
    firstChild: BaseSymbol | undefined;
    lastChild: BaseSymbol | undefined;
 
    clear(): void;
 
    /**
     * Adds the given symbol to this scope. If it belongs already to a different scope
     * it is removed from that before adding it here.
     *
     * @param symbol The symbol to add as a child.
     */
    addSymbol(symbol: BaseSymbol): void;
 
    removeSymbol(symbol: BaseSymbol): void;
 
    /**
     * Asynchronously retrieves child symbols of a given type from this symbol.
     *
     * @param t The type of of the objects to return.
     *
     * @returns A promise resolving to all (nested) children of the given type.
     */
    getNestedSymbolsOfType<T extends BaseSymbol>(t: SymbolConstructor<T>): Promise<T[]>;
 
    /**
     * Synchronously retrieves child symbols of a given type from this symbol.
     *
     * @param t The type of of the objects to return.
     *
     * @returns A list of all (nested) children of the given type.
     */
    getNestedSymbolsOfTypeSync<T extends BaseSymbol>(t: SymbolConstructor<T>): T[];
 
    /**
     * @param name If given only returns symbols with that name.
     *
     * @returns A promise resolving to symbols from this and all nested scopes in the order they were defined.
     */
    getAllNestedSymbols(name?: string): Promise<BaseSymbol[]>;
 
    /**
     * @param name If given only returns symbols with that name.
     *
     * @returns A list of all symbols from this and all nested scopes in the order they were defined.
     */
    getAllNestedSymbolsSync(name?: string): BaseSymbol[];
 
    /**
     * @param t The type of of the objects to return.
     *
     * @returns A promise resolving to direct children of a given type.
     */
    getSymbolsOfType<T extends BaseSymbol>(t: SymbolConstructor<T>): Promise<T[]>;
 
    /**
     * TODO: add optional position dependency (only symbols defined before a given caret pos are viable).
     *
     * @param t The type of the objects to return.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns A promise resolving to all symbols of the the given type, accessible from this scope (if localOnly is
     *          false), within the owning symbol table.
     */
    getAllSymbols<T extends BaseSymbol>(t: SymbolConstructor<T>, localOnly?: boolean): Promise<T[]>;
 
    /**
     * TODO: add optional position dependency (only symbols defined before a given caret pos are viable).
     *
     * @param t The type of the objects to return.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns A list with all symbols of the the given type, accessible from this scope (if localOnly is
     *          false), within the owning symbol table.
     */
    getAllSymbolsSync<T extends BaseSymbol>(t: SymbolConstructor<T>, localOnly?: boolean): T[];
 
    /**
     * @param name The name of the symbol to resolve.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns A promise resolving to the first symbol with a given name, in the order of appearance in this scope
     *          or any of the parent scopes (conditionally).
     */
    resolve(name: string, localOnly?: boolean): Promise<BaseSymbol | undefined>;
 
    /**
     * @param name The name of the symbol to resolve.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns the first symbol with a given name, in the order of appearance in this scope
     *          or any of the parent scopes (conditionally).
     */
    resolveSync(name: string, localOnly?: boolean): BaseSymbol | undefined;
 
    /**
     * @param path The path consisting of symbol names separator by `separator`.
     * @param separator The character to separate path segments.
     *
     * @returns the symbol located at the given path through the symbol hierarchy.
     */
    symbolFromPath(path: string, separator: string): BaseSymbol | undefined;
 
    /**
     * @param child The child to search for.
     *
     * @returns the index of the given child symbol in the child list or -1 if it couldn't be found.
     */
    indexOfChild(child: BaseSymbol): number;
 
    /**
     * @param child The reference node.
     *
     * @returns the sibling symbol after the given child symbol, if one exists.
     */
    nextSiblingOf(child: BaseSymbol): BaseSymbol | undefined;
 
    /**
     * @param child The reference node.
     *
     * @returns the sibling symbol before the given child symbol, if one exists.
     */
    previousSiblingOf(child: BaseSymbol): BaseSymbol | undefined;
 
    /**
     * @param child The reference node.
     *
     * @returns the next symbol in definition order, regardless of the scope.
     */
    nextOf(child: BaseSymbol): BaseSymbol | undefined;
}
 
/** A symbol with a scope (so it can have child symbols). */
export class ScopedSymbol extends BaseSymbol implements IScopedSymbol {
 
    /** All child symbols in definition order. */
    private childSymbols: BaseSymbol[] = [];
 
    public constructor(name = "") {
        super(name);
    }
 
    /**
     * @returns A promise resolving to all direct child symbols with a scope (e.g. classes in a module).
     */
    public get directScopes(): Promise<ScopedSymbol[]> {
        return this.getSymbolsOfType(ScopedSymbol);
    }
 
    public get children(): BaseSymbol[] {
        return this.childSymbols;
    }
 
    public get firstChild(): BaseSymbol | undefined {
        if (this.children.length > 0) {
            return this.children[0];
        }

        return undefined;
    }
 
    public get lastChild(): BaseSymbol | undefined {
        if (this.children.length > 0) {
            return this.children[this.children.length - 1];
        }

        return undefined;
    }
 
    public clear(): void {
        this.childSymbols = [];
    }
 
    /**
     * Adds the given symbol to this scope. If it belongs already to a different scope
     * it is removed from that before adding it here.
     *
     * @param symbol The symbol to add as a child.
     */
    public addSymbol(symbol: BaseSymbol): void {
        symbol.removeFromParent();
 
        // Check for duplicates first.
        const symbolTable = this.symbolTable;
        if (!symbolTable || !symbolTable.options.allowDuplicateSymbols) {
            this.children.forEach((child) => {
                if (child === symbol || (symbol.name.length > 0 && child.name === symbol.name)) {
                    let name = symbol.name;
                    if (name.length === 0) {
                        name = "<anonymous>";
                    }
 
                    throw new DuplicateSymbolError("Attempt to add duplicate symbol '" + name + "'");
                }
            });
        }
 
        this.children.push(symbol);
        symbol.setParent(this);
    }
 
    public removeSymbol(symbol: BaseSymbol): void {
        const index = this.children.indexOf(symbol);
        if (index > -1) {
            this.children.splice(index, 1);
            symbol.setParent(undefined);
        }
    }
 
    /**
     * Asynchronously retrieves child symbols of a given type from this symbol.
     *
     * @param t The type of of the objects to return.
     *
     * @returns A promise resolving to all (nested) children of the given type.
     */
    public async getNestedSymbolsOfType<T extends BaseSymbol>(t: SymbolConstructor<T>): Promise<T[]> {
        const result: T[] = [];
 
        const childPromises: Array<Promise<T[]>> = [];
        this.children.forEach((child) => {
            if (child instanceof t) {
                result.push(child);
            }
 
            if (child instanceof ScopedSymbol) {
                childPromises.push(child.getNestedSymbolsOfType(t));
            }
        });
        const childSymbols = await Promise.all(childPromises);
        childSymbols.forEach((entry) => {
            result.push(...entry);
        });
 
        return result;
    }
 
    /**
     * Synchronously retrieves child symbols of a given type from this symbol.
     *
     * @param t The type of of the objects to return.
     *
     * @returns A list of all (nested) children of the given type.
     */
    public getNestedSymbolsOfTypeSync<T extends BaseSymbol>(t: SymbolConstructor<T>): T[] {
        const result: T[] = [];

        this.children.forEach((child) => {
            if (child instanceof t) {
                result.push(child);
            }

            if (child instanceof ScopedSymbol) {
                result.push(...child.getNestedSymbolsOfTypeSync(t));
            }
        });

        return result;
    }
 
    /**
     * @param name If given only returns symbols with that name.
     *
     * @returns A promise resolving to symbols from this and all nested scopes in the order they were defined.
     */
    public async getAllNestedSymbols(name?: string): Promise<BaseSymbol[]> {
        const result: BaseSymbol[] = [];
 
        const childPromises: Array<Promise<BaseSymbol[]>> = [];
        this.children.forEach((child) => {
            if (!name || child.name === name) {
                result.push(child);
            }
 
            if (child instanceof ScopedSymbol) {
                childPromises.push(child.getAllNestedSymbols(name));
            }
        });
        const childSymbols = await Promise.all(childPromises);
        childSymbols.forEach((entry) => {
            result.push(...entry);
        });
 
        return result;
    }
 
    /**
     * @param name If given only returns symbols with that name.
     *
     * @returns A list of all symbols from this and all nested scopes in the order they were defined.
     */
    public getAllNestedSymbolsSync(name?: string): BaseSymbol[] {
        const result: BaseSymbol[] = [];

        this.children.forEach((child) => {
            if (!name || child.name === name) {
                result.push(child);
            }

            if (child instanceof ScopedSymbol) {
                result.push(...child.getAllNestedSymbolsSync(name));
            }
        });

        return result;
    }
 
    /**
     * @param t The type of of the objects to return.
     *
     * @returns A promise resolving to direct children of a given type.
     */
    public getSymbolsOfType<T extends BaseSymbol>(t: SymbolConstructor<T>): Promise<T[]> {
        return new Promise((resolve) => {
            const result: T[] = [];
            this.children.forEach((child) => {
                if (child instanceof t) {
                    result.push(child);
                }
            });
 
            resolve(result);
        });
    }
 
    /**
     * TODO: add optional position dependency (only symbols defined before a given caret pos are viable).
     *
     * @param t The type of the objects to return.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns A promise resolving to all symbols of the the given type, accessible from this scope (if localOnly is
     *          false), within the owning symbol table.
     */
    public async getAllSymbols<T extends BaseSymbol>(t: SymbolConstructor<T>, localOnly = false): Promise<T[]> {
        const result: T[] = [];
 
        // Special handling for namespaces, which act like grouping symbols in this scope,
        // so we show them as available in this scope.
        for (const child of this.children) {
            if (child instanceof t) {
                result.push(child);
            }
 
            if (child instanceof ScopedSymbol && child.isNamespace()) {
                const childSymbols = await child.getAllSymbols(t, true);
                result.push(...childSymbols);
            }
        }
 
        if (!localOnly) {
            if (this.parent) {
                const childSymbols = await this.getAllSymbols(t, true);
                result.push(...childSymbols);
            }
        }
 
        return result;
    }
 
    /**
     * TODO: add optional position dependency (only symbols defined before a given caret pos are viable).
     *
     * @param t The type of the objects to return.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns A list with all symbols of the the given type, accessible from this scope (if localOnly is
     *          false), within the owning symbol table.
     */
    public getAllSymbolsSync<T extends BaseSymbol>(t: SymbolConstructor<T>, localOnly = false): T[] {
        const result: T[] = [];

        // Special handling for namespaces, which act like grouping symbols in this scope,
        // so we show them as available in this scope.
        for (const child of this.children) {
            if (child instanceof t) {
                result.push(child);
            }

            if (child instanceof ScopedSymbol && child.isNamespace()) {
                const childSymbols = child.getAllSymbolsSync(t, true);
                result.push(...childSymbols);
            }
        }

        if (!localOnly) {
            if (this.parent) {
                const childSymbols = this.getAllSymbolsSync(t, true);
                result.push(...childSymbols);
            }
        }

        return result;
    }
 
    /**
     * @param name The name of the symbol to resolve.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns A promise resolving to the first symbol with a given name, in the order of appearance in this scope
     *          or any of the parent scopes (conditionally).
     */
    public override async resolve(name: string, localOnly = false): Promise<BaseSymbol | undefined> {
        return new Promise((resolve, reject) => {
            for (const child of this.children) {
                if (child.name === name) {
                    resolve(child);
 
                    return;
                }
            }
 
            // Nothing found locally. Let the parent continue.
            if (!localOnly) {
                if (this.parent) {
                    resolve(this.parent.resolve(name, false));
 
                    return;
                }
            }
 
            resolve(undefined);
        });
    }
 
    /**
     * @param name The name of the symbol to resolve.
     * @param localOnly If true only child symbols are returned, otherwise also symbols from the parent of this symbol
     *                  (recursively).
     *
     * @returns the first symbol with a given name, in the order of appearance in this scope
     *          or any of the parent scopes (conditionally).
     */
    public override resolveSync(name: string, localOnly = false): BaseSymbol | undefined {
        for (const child of this.children) {
            if (child.name === name) {
                return child;
            }
        }

        // Nothing found locally. Let the parent continue.
        if (!localOnly) {
            if (this.parent) {
                return this.parent.resolveSync(name, false);

            }
        }

        return undefined;
    }
 
    /**
     * @param path The path consisting of symbol names separator by `separator`.
     * @param separator The character to separate path segments.
     *
     * @returns the symbol located at the given path through the symbol hierarchy.
     */
    public symbolFromPath(path: string, separator = "."): BaseSymbol | undefined {
        const elements = path.split(separator);
        let index = 0;
        if (elements[0] === this.name || elements[0].length === 0) {
            ++index;
        }
 
        // eslint-disable-next-line @typescript-eslint/no-this-alias
        let result: BaseSymbol = this;
        while (index < elements.length) {
            if (!(result instanceof ScopedSymbol)) {
                return undefined;
            }
 
            // eslint-disable-next-line no-loop-func
            const child = result.children.find((candidate) => { return candidate.name === elements[index]; });
            if (!child) {
                return undefined;
            }
 
            result = child;
            ++index;
        }
 
        return result;
    }
 
    /**
     * @param child The child to search for.
     *
     * @returns the index of the given child symbol in the child list or -1 if it couldn't be found.
     */
    public indexOfChild(child: BaseSymbol): number {
        return this.children.findIndex((value: BaseSymbol, index: number) => { return value === child; });
    }
 
    /**
     * @param child The reference node.
     *
     * @returns the sibling symbol after the given child symbol, if one exists.
     */
    public nextSiblingOf(child: BaseSymbol): BaseSymbol | undefined {
        const index = this.indexOfChild(child);
        if (index === -1 || index >= this.children.length - 1) {
            return undefined;
        }
 
        return this.children[index + 1];
    }
 
    /**
     * @param child The reference node.
     *
     * @returns the sibling symbol before the given child symbol, if one exists.
     */
    public previousSiblingOf(child: BaseSymbol): BaseSymbol | undefined {
        const index = this.indexOfChild(child);
        if (index < 1) {
            return undefined;
        }
 
        return this.children[index - 1];
    }
 
    /**
     * @param child The reference node.
     *
     * @returns the next symbol in definition order, regardless of the scope.
     */
    public nextOf(child: BaseSymbol): BaseSymbol | undefined {
        if (!(child.parent)) {
            return undefined;
        }
 
        if (child.parent !== this) {
            return child.parent.nextOf(child);
        }
 
        if (child instanceof ScopedSymbol && child.children.length > 0) {
            return child.children[0];
        }
 
        const sibling = this.nextSiblingOf(child);
        if (sibling) {
            return sibling;
        }
 
        return (this.parent as ScopedSymbol).nextOf(this);
    }
 
    /**
     * To avoid having to import NamespaceSymbol (which creates a circular dependency) this method is used
     * to check if the symbol is a namespace. Only NamespaceSymbol overrides this method.
     *
     * @returns false by default.
     */
    protected isNamespace(): boolean {
        return false;
    }
}