TypeWriter - Filter classes or properties without a given attribute

試著忘記壹切 提交于 2019-12-11 07:29:29

问题


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.



来源:https://stackoverflow.com/questions/42142538/typewriter-filter-classes-or-properties-without-a-given-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!