How to use the @ts-morph/common.ArrayUtils.flatten function in @ts-morph/common

To help you get started, we’ve selected a few @ts-morph/common examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dsherret / ts-morph / packages / ts-morph / scripts / inspectors / tsMorph / Mixin.ts View on Github external
getCoveredTsNodePropertyNames(): string[] {
        // this is done just to be fast... there's definitely a more correct way of doing this
        const sourceFile = this.node.getSourceFile();
        const propertyAccessExpressions = sourceFile.getDescendantsOfKind(tsMorph.SyntaxKind.PropertyAccessExpression);
        const names: string[] = [];

        for (const expr of propertyAccessExpressions) {
            if (expr.getText() !== "this.compilerNode")
                continue;
            const parent = expr.getParentIfKind(tsMorph.SyntaxKind.PropertyAccessExpression);
            if (parent == null)
                continue;
            names.push(parent.getName());
        }

        return [...names, ...ArrayUtils.flatten(this.getMixins().map(m => m.getCoveredTsNodePropertyNames()))];
    }
}
github dsherret / ts-morph / packages / ts-morph / scripts / inspectors / TsMorphInspector.ts View on Github external
getPublicDeclarations(): tsMorph.ExportedDeclarations[] {
        const entries = Array.from(this.project.getSourceFileOrThrow("src/main.ts").getExportedDeclarations().entries());
        return ArrayUtils.flatten(entries.filter(([name]) => name !== "ts").map(([_, value]) => value));
    }
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / class / ClassDeclaration.ts View on Github external
function getExtractedClassDetails(classDec: ClassDeclaration, isStatic: boolean) {
    const constructors = ArrayUtils.flatten(classDec.getConstructors().map(c => c.getOverloads().length > 0 ? c.getOverloads() : [c]));
    const properties = classDec.getProperties().filter(p => p.isStatic() === isStatic && p.getScope() === Scope.Public);
    const methods = ArrayUtils.flatten(classDec.getMethods()
        .filter(p => p.isStatic() === isStatic && p.getScope() === Scope.Public)
        .map(m => m.getOverloads().length > 0 ? m.getOverloads() : [m]));

    return { constructors, properties, methods, accessors: getAccessors() };

    function getAccessors() {
        type GetOrSetArray = (GetAccessorDeclaration | SetAccessorDeclaration)[];
        const result = new KeyValueCache();

        for (const accessor of [...classDec.getGetAccessors(), ...classDec.getSetAccessors()]) {
            if (accessor.isStatic() === isStatic && accessor.getScope() === Scope.Public)
                result.getOrCreate(accessor.getName(), () => []).push(accessor);
        }

        return result.getValuesAsArray();
    }
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / class / ClassDeclaration.ts View on Github external
function getExtractedClassDetails(classDec: ClassDeclaration, isStatic: boolean) {
    const constructors = ArrayUtils.flatten(classDec.getConstructors().map(c => c.getOverloads().length > 0 ? c.getOverloads() : [c]));
    const properties = classDec.getProperties().filter(p => p.isStatic() === isStatic && p.getScope() === Scope.Public);
    const methods = ArrayUtils.flatten(classDec.getMethods()
        .filter(p => p.isStatic() === isStatic && p.getScope() === Scope.Public)
        .map(m => m.getOverloads().length > 0 ? m.getOverloads() : [m]));

    return { constructors, properties, methods, accessors: getAccessors() };

    function getAccessors() {
        type GetOrSetArray = (GetAccessorDeclaration | SetAccessorDeclaration)[];
        const result = new KeyValueCache();

        for (const accessor of [...classDec.getGetAccessors(), ...classDec.getSetAccessors()]) {
            if (accessor.isStatic() === isStatic && accessor.getScope() === Scope.Public)
                result.getOrCreate(accessor.getName(), () => []).push(accessor);
        }
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / class / ClassDeclaration.ts View on Github external
extractInterface(name?: string): InterfaceDeclarationStructure {
        const { constructors, properties, methods, accessors } = getExtractedClassDetails(this, false);
        const parameterProperties = ArrayUtils.flatten(constructors.map(c => c.getParameters().filter(p => p.isParameterProperty())))
            .filter(p => p.getName() != null && p.getScope() === Scope.Public);

        return {
            kind: StructureKind.Interface,
            name: getDefaultExtractedName(name, this),
            docs: this.getJsDocs().map(d => d.getStructure()),
            typeParameters: this.getTypeParameters().map(p => p.getStructure()),
            properties: [
                ...parameterProperties.map(p => {
                    const jsDocComment = ArrayUtils.flatten((p.getParentOrThrow() as ConstructorDeclaration).getJsDocs().map(j => j.getTags()))
                        .filter(TypeGuards.isJSDocParameterTag)
                        .filter(t => t.getTagName() === "param" && t.getName() === p.getName() && t.getComment() != null)
                        .map(t => t.getComment()!.trim())[0];
                    return {
                        kind: StructureKind.PropertySignature as StructureKind.PropertySignature,
                        docs: jsDocComment == null ? [] : [{ kind: StructureKind.JSDoc, description: jsDocComment }] as JSDocStructure[],
github dsherret / ts-morph / packages / ts-morph / scripts / inspectors / TsMorphInspector.ts View on Github external
getWrappedNodes(): WrappedNode[] {
        const compilerSourceFiles = this.project.getSourceFiles("src/compiler/**/*.ts");
        const classes = ArrayUtils.flatten(compilerSourceFiles.map(f => f.getClasses()));

        return classes.filter(c => isNodeClass(c)).map(c => this.wrapperFactory.getWrappedNode(c));
    }
github dsherret / ts-morph / packages / ts-morph / scripts / inspectors / ts / TsNodeProperty.ts View on Github external
findReferencedNodes(): tsMorph.Node[] {
        const referencedNodes: tsMorph.Node[] = [];
        const references = (this.node.getNameNode() as tsMorph.Identifier).findReferences();

        for (const reference of ArrayUtils.flatten(references.map(r => r.getReferences()))) {
            const sourceFile = reference.getSourceFile();
            if (sourceFile.getFilePath().indexOf("compiler") === -1)
                continue;

            const node = reference.getNode();
            referencedNodes.push(node);
        }

        return referencedNodes;
    }
}
github dsherret / ts-morph / packages / ts-morph / scripts / generation / createStructurePrinterFactory.ts View on Github external
function getMethods() {
        const structurePrinters = ArrayUtils.flatten(Array.from(project.getSourceFileOrThrow("./src/structurePrinters/index.ts")
            .getExportedDeclarations()
            .values()))
            .filter(tsMorph.Node.isClassDeclaration)
            .filter(c => isAllowedStructurePrinter(c.getNameOrThrow()));
        const methods: tsMorph.MethodDeclarationStructure[] = [];

        for (const structurePrinter of structurePrinters) {
            const ctor = structurePrinter.getConstructors()[0] ?? structurePrinter.getBaseClassOrThrow().getConstructors()[0];
            const ctorParams = ctor?.getParameters() ?? [];
            const exposedCtorParams = ctorParams.filter(exposeCtorParam);
            const name = structurePrinter.getNameOrThrow();
            methods.push({
                kind: tsMorph.StructureKind.Method,
                decorators: [{ name: "Memoize" }],
                name: `for${name.replace(/StructurePrinter$/, "")}`,
                returnType: `structurePrinters.${name}`,