Repository: microsoft/TypeScript
File: src/services/completions.ts (TypeScript)

Code before:
            isRightOfOpenTag,
            includeSymbol,
        );
        if (!entry) {
            continue;
        }

        /** True for locals; false for globals, module exports from other files, `this.` completions. */
        const shouldShadowLaterSymbols = (!origin || originIsTypeOnlyAlias(origin)) && !(symbol.parent === undefined && !some(symbol.declarations, d => d.getSourceFile() === location.getSourceFile()));
        uniques.set(name, shouldShadowLaterSymbols);
        insertSorted(entries, entry, compareCompletionEntries, /*equalityComparer*/ undefined, /*allowDuplicates*/ true);
    }

    log("getCompletionsAtPosition: getCompletionEntriesFromSymbols: " + (timestamp() - start));

    // Prevent consumers of this map from having to worry about
    // the boolean value. Externally, it should be seen as the
    // set of all names.
    return {
        has: name => uniques.has(name),
        add: name => uniques.set(name, true),
    };

    function shouldIncludeSymbol(symbol: Symbol, symbolToSortTextMap: SymbolSortTextMap): boolean {
        let allFlags = symbol.flags;
        if (!isSourceFile(location)) {
            // export = /**/ here we want to get all meanings, so any symbol is ok
            if (isExportAssignment(location.parent)) {
                return true;
            }
            // Filter out variables from their own initializers
            // `const a = /* no 'a' here */`
            if (closestSymbolDeclaration && tryCast(closestSymbolDeclaration, isVariableDeclaration)) {
                if (symbol.valueDeclaration === closestSymbolDeclaration) {
                    return false;
                }
                // const { a } = /* no 'a' here */;
                if (isBindingPattern(closestSymbolDeclaration.name) && closestSymbolDeclaration.name.elements.some(e => e === symbol.valueDeclaration)) {
                    return false;
                }
            }

            // Filter out current and latter parameters from defaults
            // `function f(a = /* no 'a' and 'b' here */, b) { }` or
            // `function f<T = /* no 'T' and 'T2' here */>(a: T, b: T2) { }`
            const symbolDeclaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
            if (closestSymbolDeclaration && symbolDeclaration) {
                if (isParameter(closestSymbolDeclaration) && isParameter(symbolDeclaration)) {
                    const parameters = closestSymbolDeclaration.parent.parameters;
                    if (symbolDeclaration.pos >= closestSymbolDeclaration.pos && symbolDeclaration.pos < parameters.end) {
                        return false;

Review comment (suggestion):
All of this is simply outdented because I removed the wrapping `if` statement. Within the outdented codepaths I had to guard 2 places against missing `location.parent` and that's it.

cc @gabritto as you have a fresh context on all of this and related topics

Code after:
            isRightOfOpenTag,
            includeSymbol,
        );
        if (!entry) {
            continue;
        }

        /** True for locals; false for globals, module exports from other files, `this.` completions. */
        const shouldShadowLaterSymbols = (!origin || originIsTypeOnlyAlias(origin)) && !(symbol.parent === undefined && !some(symbol.declarations, d => d.getSourceFile() === location.getSourceFile()));
        uniques.set(name, shouldShadowLaterSymbols);
        insertSorted(entries, entry, compareCompletionEntries, /*equalityComparer*/ undefined, /*allowDuplicates*/ true);
    }

    log("getCompletionsAtPosition: getCompletionEntriesFromSymbols: " + (timestamp() - start));

    // Prevent consumers of this map from having to worry about
    // the boolean value. Externally, it should be seen as the
    // set of all names.
    return {
        has: name => uniques.has(name),
        add: name => uniques.set(name, true),
    };

    function shouldIncludeSymbol(symbol: Symbol, symbolToSortTextMap: SymbolSortTextMap): boolean {
        let allFlags = symbol.flags;
        // export = /**/ here we want to get all meanings, so any symbol is ok
        if (location.parent && isExportAssignment(location.parent)) {
            return true;
        }
        // Filter out variables from their own initializers
        // `const a = /* no 'a' here */`
        if (closestSymbolDeclaration && tryCast(closestSymbolDeclaration, isVariableDeclaration)) {
            if (symbol.valueDeclaration === closestSymbolDeclaration) {
                return false;
            }
            // const { a } = /* no 'a' here */;
            if (isBindingPattern(closestSymbolDeclaration.name) && closestSymbolDeclaration.name.elements.some(e => e === symbol.valueDeclaration)) {
                return false;
            }
        }

        // Filter out current and latter parameters from defaults
        // `function f(a = /* no 'a' and 'b' here */, b) { }` or
        // `function f<T = /* no 'T' and 'T2' here */>(a: T, b: T2) { }`
        const symbolDeclaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
        if (closestSymbolDeclaration && symbolDeclaration) {
            if (isParameter(closestSymbolDeclaration) && isParameter(symbolDeclaration)) {
                const parameters = closestSymbolDeclaration.parent.parameters;
                if (symbolDeclaration.pos >= closestSymbolDeclaration.pos && symbolDeclaration.pos < parameters.end) {
                    return false;
                }