TypeWriter - Filter classes or properties without

2019-07-26 03:17发布

问题:

Using a Typewriter .tst file it is possible to only include Properties with a certain attribute using a $Properties([MyAttr]) filter.

Like this for example:

export class $Name{
        $Properties([MyAttr])[
        public $name: $Type = $Type[$Default];]
    }

Is it possible to include all properties except those with the given attribute?

Something like this maybe:

export class $Name{
        $Properties(![TsIgnore])[               //but this doesnt work!!
        public $name: $Type = $Type[$Default];]
    }

I've tried what I can think of ![TsIgnore], [!TsIgnore], etc. but none work. Also cannot find anything in the docs

回答1:

I implement an IsIncluded method:

bool IsIncluded(Class c)
{
    // exclude attributes
    if(c.BaseClass?.FullName == "System.Attribute") return false;

    return !ExcludeObjects.Any(ec => c.Name == ec || c.FullName == ec);
}

and somewhere at the top, I have this:

static string[] ExcludeObjects = new string[]
{
    "MyClassToExclude",
    "Full.Namespace.Path.To.MyOtherClassToExclude",
};

and the template has this:

$Classes($IsIncluded)[ ... ]

Thanks



回答2:

You can also use

  bool IsIncluded(Property c)
  {
      if(c.Attributes.Any(a => String.Equals(a.name, "TypeScriptExportExlude", StringComparison.OrdinalIgnoreCase)))
        return false;
      return true;
  }

where "TypeScriptExportExlude" is the attribute on your property that you want to exclude.