import classes when class declaration in another file - typewriter

600 Views Asked by At

I'm using typerwriter extension to create ts file corresponding to c# file, but there is some variables from type that declared in another file so I have to add import statement.

to do so, I want to get all the classes name that in this file and add import only if class name isnt in this list.

in this example BDTO in another file.

public class ADTO
{
    public uint id { get; set; }
    public uint editionId { get; set; }
    public BDTO mask { get; set; }
}

tst code:

 string Imports(Class c, File f)
{
    IEnumerable<String> classes = f.Classes.Select(p=> p.name);

    IEnumerable<Type> types = c.Properties
        .Select(p => p.Type)
        .Where(t =>((!t.IsPrimitive  || t.IsEnum) && !classes.Contains(t.name)))
        .Select(t => t.IsGeneric ? t.TypeArguments.First() : t)
        .Where(t => t.Name != c.Name && t.Name != "DbGeography")
        .Distinct();
    return string.Join(Environment.NewLine, types.Select(t => $"import {{ {t.Name} }} from './misc';").Distinct());
}

$Classes(*DTO)[
$Imports

export interface $Name {
    $Properties[
    // $LoudName
    $name: $Type;]
}]

this adding: $Imports instead of: import {BDTO} from './misc'

1

There are 1 best solutions below

0
Dan Soper On

I had the same issue. I think it is because the method can only take the one parameter, of type Class.

This example shows another way of solving the problem: https://github.com/frhagn/Typewriter/issues/270