Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

JetBrains.Annotations.cs 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. #if UNITY_DOTSRUNTIME
  2. /* MIT License
  3. Copyright (c) 2016 JetBrains http://www.jetbrains.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE. */
  19. using System;
  20. #pragma warning disable 1591
  21. // ReSharper disable UnusedMember.Global
  22. // ReSharper disable MemberCanBePrivate.Global
  23. // ReSharper disable UnusedAutoPropertyAccessor.Global
  24. // ReSharper disable IntroduceOptionalParameters.Global
  25. // ReSharper disable MemberCanBeProtected.Global
  26. // ReSharper disable InconsistentNaming
  27. namespace JetBrains.Annotations
  28. {
  29. /// <summary>
  30. /// Indicates that the value of the marked element could be <c>null</c> sometimes,
  31. /// so the check for <c>null</c> is necessary before its usage.
  32. /// </summary>
  33. /// <example><code>
  34. /// [CanBeNull] object Test() => null;
  35. ///
  36. /// void UseTest() {
  37. /// var p = Test();
  38. /// var s = p.ToString(); // Warning: Possible 'System.NullReferenceException'
  39. /// }
  40. /// </code></example>
  41. [AttributeUsage(
  42. AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property |
  43. AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Event |
  44. AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.GenericParameter)]
  45. internal sealed class CanBeNullAttribute : Attribute {}
  46. /// <summary>
  47. /// Indicates that the value of the marked element could never be <c>null</c>.
  48. /// </summary>
  49. /// <example><code>
  50. /// [NotNull] object Foo() {
  51. /// return null; // Warning: Possible 'null' assignment
  52. /// }
  53. /// </code></example>
  54. [AttributeUsage(
  55. AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property |
  56. AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Event |
  57. AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.GenericParameter)]
  58. internal sealed class NotNullAttribute : Attribute {}
  59. /// <summary>
  60. /// Can be appplied to symbols of types derived from IEnumerable as well as to symbols of Task
  61. /// and Lazy classes to indicate that the value of a collection item, of the Task.Result property
  62. /// or of the Lazy.Value property can never be null.
  63. /// </summary>
  64. [AttributeUsage(
  65. AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property |
  66. AttributeTargets.Delegate | AttributeTargets.Field)]
  67. internal sealed class ItemNotNullAttribute : Attribute {}
  68. /// <summary>
  69. /// Can be appplied to symbols of types derived from IEnumerable as well as to symbols of Task
  70. /// and Lazy classes to indicate that the value of a collection item, of the Task.Result property
  71. /// or of the Lazy.Value property can be null.
  72. /// </summary>
  73. [AttributeUsage(
  74. AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property |
  75. AttributeTargets.Delegate | AttributeTargets.Field)]
  76. internal sealed class ItemCanBeNullAttribute : Attribute {}
  77. /// <summary>
  78. /// Indicates that the marked method builds string by format pattern and (optional) arguments.
  79. /// Parameter, which contains format string, should be given in constructor. The format string
  80. /// should be in <see cref="string.Format(IFormatProvider,string,object[])"/>-like form.
  81. /// </summary>
  82. /// <example><code>
  83. /// [StringFormatMethod("message")]
  84. /// void ShowError(string message, params object[] args) { /* do something */ }
  85. ///
  86. /// void Foo() {
  87. /// ShowError("Failed: {0}"); // Warning: Non-existing argument in format string
  88. /// }
  89. /// </code></example>
  90. [AttributeUsage(
  91. AttributeTargets.Constructor | AttributeTargets.Method |
  92. AttributeTargets.Property | AttributeTargets.Delegate)]
  93. internal sealed class StringFormatMethodAttribute : Attribute
  94. {
  95. /// <param name="formatParameterName">
  96. /// Specifies which parameter of an annotated method should be treated as format-string
  97. /// </param>
  98. public StringFormatMethodAttribute([NotNull] string formatParameterName)
  99. {
  100. FormatParameterName = formatParameterName;
  101. }
  102. [NotNull] public string FormatParameterName { get; private set; }
  103. }
  104. /// <summary>
  105. /// For a parameter that is expected to be one of the limited set of values.
  106. /// Specify fields of which type should be used as values for this parameter.
  107. /// </summary>
  108. [AttributeUsage(
  109. AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field,
  110. AllowMultiple = true)]
  111. internal sealed class ValueProviderAttribute : Attribute
  112. {
  113. public ValueProviderAttribute([NotNull] string name)
  114. {
  115. Name = name;
  116. }
  117. [NotNull] public string Name { get; private set; }
  118. }
  119. /// <summary>
  120. /// Indicates that the function argument should be string literal and match one
  121. /// of the parameters of the caller function. For example, ReSharper annotates
  122. /// the parameter of <see cref="System.ArgumentNullException"/>.
  123. /// </summary>
  124. /// <example><code>
  125. /// void Foo(string param) {
  126. /// if (param == null)
  127. /// throw new ArgumentNullException("par"); // Warning: Cannot resolve symbol
  128. /// }
  129. /// </code></example>
  130. [AttributeUsage(AttributeTargets.Parameter)]
  131. internal sealed class InvokerParameterNameAttribute : Attribute {}
  132. /// <summary>
  133. /// Indicates that the method is contained in a type that implements
  134. /// <c>System.ComponentModel.INotifyPropertyChanged</c> interface and this method
  135. /// is used to notify that some property value changed.
  136. /// </summary>
  137. /// <remarks>
  138. /// The method should be non-static and conform to one of the supported signatures:
  139. /// <list>
  140. /// <item><c>NotifyChanged(string)</c></item>
  141. /// <item><c>NotifyChanged(params string[])</c></item>
  142. /// <item><c>NotifyChanged{T}(Expression{Func{T}})</c></item>
  143. /// <item><c>NotifyChanged{T,U}(Expression{Func{T,U}})</c></item>
  144. /// <item><c>SetProperty{T}(ref T, T, string)</c></item>
  145. /// </list>
  146. /// </remarks>
  147. /// <example><code>
  148. /// public class Foo : INotifyPropertyChanged {
  149. /// public event PropertyChangedEventHandler PropertyChanged;
  150. ///
  151. /// [NotifyPropertyChangedInvocator]
  152. /// protected virtual void NotifyChanged(string propertyName) { ... }
  153. ///
  154. /// string _name;
  155. ///
  156. /// public string Name {
  157. /// get { return _name; }
  158. /// set { _name = value; NotifyChanged("LastName"); /* Warning */ }
  159. /// }
  160. /// }
  161. /// </code>
  162. /// Examples of generated notifications:
  163. /// <list>
  164. /// <item><c>NotifyChanged("Property")</c></item>
  165. /// <item><c>NotifyChanged(() =&gt; Property)</c></item>
  166. /// <item><c>NotifyChanged((VM x) =&gt; x.Property)</c></item>
  167. /// <item><c>SetProperty(ref myField, value, "Property")</c></item>
  168. /// </list>
  169. /// </example>
  170. [AttributeUsage(AttributeTargets.Method)]
  171. internal sealed class NotifyPropertyChangedInvocatorAttribute : Attribute
  172. {
  173. public NotifyPropertyChangedInvocatorAttribute() {}
  174. public NotifyPropertyChangedInvocatorAttribute([NotNull] string parameterName)
  175. {
  176. ParameterName = parameterName;
  177. }
  178. [CanBeNull] public string ParameterName { get; private set; }
  179. }
  180. /// <summary>
  181. /// Describes dependency between method input and output.
  182. /// </summary>
  183. /// <syntax>
  184. /// <p>Function Definition Table syntax:</p>
  185. /// <list>
  186. /// <item>FDT ::= FDTRow [;FDTRow]*</item>
  187. /// <item>FDTRow ::= Input =&gt; Output | Output &lt;= Input</item>
  188. /// <item>Input ::= ParameterName: Value [, Input]*</item>
  189. /// <item>Output ::= [ParameterName: Value]* {halt|stop|void|nothing|Value}</item>
  190. /// <item>Value ::= true | false | null | notnull | canbenull</item>
  191. /// </list>
  192. /// If method has single input parameter, it's name could be omitted.<br/>
  193. /// Using <c>halt</c> (or <c>void</c>/<c>nothing</c>, which is the same) for method output
  194. /// means that the methos doesn't return normally (throws or terminates the process).<br/>
  195. /// Value <c>canbenull</c> is only applicable for output parameters.<br/>
  196. /// You can use multiple <c>[ContractAnnotation]</c> for each FDT row, or use single attribute
  197. /// with rows separated by semicolon. There is no notion of order rows, all rows are checked
  198. /// for applicability and applied per each program state tracked by R# analysis.<br/>
  199. /// </syntax>
  200. /// <examples><list>
  201. /// <item><code>
  202. /// [ContractAnnotation("=&gt; halt")]
  203. /// public void TerminationMethod()
  204. /// </code></item>
  205. /// <item><code>
  206. /// [ContractAnnotation("halt &lt;= condition: false")]
  207. /// public void Assert(bool condition, string text) // regular assertion method
  208. /// </code></item>
  209. /// <item><code>
  210. /// [ContractAnnotation("s:null =&gt; true")]
  211. /// public bool IsNullOrEmpty(string s) // string.IsNullOrEmpty()
  212. /// </code></item>
  213. /// <item><code>
  214. /// // A method that returns null if the parameter is null,
  215. /// // and not null if the parameter is not null
  216. /// [ContractAnnotation("null =&gt; null; notnull =&gt; notnull")]
  217. /// public object Transform(object data)
  218. /// </code></item>
  219. /// <item><code>
  220. /// [ContractAnnotation("=&gt; true, result: notnull; =&gt; false, result: null")]
  221. /// public bool TryParse(string s, out Person result)
  222. /// </code></item>
  223. /// </list></examples>
  224. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  225. internal sealed class ContractAnnotationAttribute : Attribute
  226. {
  227. public ContractAnnotationAttribute([NotNull] string contract)
  228. : this(contract, false) {}
  229. public ContractAnnotationAttribute([NotNull] string contract, bool forceFullStates)
  230. {
  231. Contract = contract;
  232. ForceFullStates = forceFullStates;
  233. }
  234. [NotNull] public string Contract { get; private set; }
  235. public bool ForceFullStates { get; private set; }
  236. }
  237. /// <summary>
  238. /// Indicates that marked element should be localized or not.
  239. /// </summary>
  240. /// <example><code>
  241. /// [LocalizationRequiredAttribute(true)]
  242. /// class Foo {
  243. /// string str = "my string"; // Warning: Localizable string
  244. /// }
  245. /// </code></example>
  246. [AttributeUsage(AttributeTargets.All)]
  247. internal sealed class LocalizationRequiredAttribute : Attribute
  248. {
  249. public LocalizationRequiredAttribute() : this(true) {}
  250. public LocalizationRequiredAttribute(bool required)
  251. {
  252. Required = required;
  253. }
  254. public bool Required { get; private set; }
  255. }
  256. /// <summary>
  257. /// Indicates that the value of the marked type (or its derivatives)
  258. /// cannot be compared using '==' or '!=' operators and <c>Equals()</c>
  259. /// should be used instead. However, using '==' or '!=' for comparison
  260. /// with <c>null</c> is always permitted.
  261. /// </summary>
  262. /// <example><code>
  263. /// [CannotApplyEqualityOperator]
  264. /// class NoEquality { }
  265. ///
  266. /// class UsesNoEquality {
  267. /// void Test() {
  268. /// var ca1 = new NoEquality();
  269. /// var ca2 = new NoEquality();
  270. /// if (ca1 != null) { // OK
  271. /// bool condition = ca1 == ca2; // Warning
  272. /// }
  273. /// }
  274. /// }
  275. /// </code></example>
  276. [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct)]
  277. internal sealed class CannotApplyEqualityOperatorAttribute : Attribute {}
  278. /// <summary>
  279. /// When applied to a target attribute, specifies a requirement for any type marked
  280. /// with the target attribute to implement or inherit specific type or types.
  281. /// </summary>
  282. /// <example><code>
  283. /// [BaseTypeRequired(typeof(IComponent)] // Specify requirement
  284. /// class ComponentAttribute : Attribute { }
  285. ///
  286. /// [Component] // ComponentAttribute requires implementing IComponent interface
  287. /// class MyComponent : IComponent { }
  288. /// </code></example>
  289. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  290. [BaseTypeRequired(typeof(Attribute))]
  291. internal sealed class BaseTypeRequiredAttribute : Attribute
  292. {
  293. public BaseTypeRequiredAttribute([NotNull] Type baseType)
  294. {
  295. BaseType = baseType;
  296. }
  297. [NotNull] public Type BaseType { get; private set; }
  298. }
  299. /// <summary>
  300. /// Indicates that the marked symbol is used implicitly (e.g. via reflection, in external library),
  301. /// so this symbol will not be marked as unused (as well as by other usage inspections).
  302. /// </summary>
  303. [AttributeUsage(AttributeTargets.All)]
  304. internal sealed class UsedImplicitlyAttribute : Attribute
  305. {
  306. public UsedImplicitlyAttribute()
  307. : this(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.Default) {}
  308. public UsedImplicitlyAttribute(ImplicitUseKindFlags useKindFlags)
  309. : this(useKindFlags, ImplicitUseTargetFlags.Default) {}
  310. public UsedImplicitlyAttribute(ImplicitUseTargetFlags targetFlags)
  311. : this(ImplicitUseKindFlags.Default, targetFlags) {}
  312. public UsedImplicitlyAttribute(ImplicitUseKindFlags useKindFlags, ImplicitUseTargetFlags targetFlags)
  313. {
  314. UseKindFlags = useKindFlags;
  315. TargetFlags = targetFlags;
  316. }
  317. public ImplicitUseKindFlags UseKindFlags { get; private set; }
  318. public ImplicitUseTargetFlags TargetFlags { get; private set; }
  319. }
  320. /// <summary>
  321. /// Should be used on attributes and causes ReSharper to not mark symbols marked with such attributes
  322. /// as unused (as well as by other usage inspections)
  323. /// </summary>
  324. [AttributeUsage(AttributeTargets.Class | AttributeTargets.GenericParameter)]
  325. internal sealed class MeansImplicitUseAttribute : Attribute
  326. {
  327. public MeansImplicitUseAttribute()
  328. : this(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.Default) {}
  329. public MeansImplicitUseAttribute(ImplicitUseKindFlags useKindFlags)
  330. : this(useKindFlags, ImplicitUseTargetFlags.Default) {}
  331. public MeansImplicitUseAttribute(ImplicitUseTargetFlags targetFlags)
  332. : this(ImplicitUseKindFlags.Default, targetFlags) {}
  333. public MeansImplicitUseAttribute(ImplicitUseKindFlags useKindFlags, ImplicitUseTargetFlags targetFlags)
  334. {
  335. UseKindFlags = useKindFlags;
  336. TargetFlags = targetFlags;
  337. }
  338. [UsedImplicitly] public ImplicitUseKindFlags UseKindFlags { get; private set; }
  339. [UsedImplicitly] public ImplicitUseTargetFlags TargetFlags { get; private set; }
  340. }
  341. [Flags]
  342. internal enum ImplicitUseKindFlags
  343. {
  344. Default = Access | Assign | InstantiatedWithFixedConstructorSignature,
  345. /// <summary>Only entity marked with attribute considered used.</summary>
  346. Access = 1,
  347. /// <summary>Indicates implicit assignment to a member.</summary>
  348. Assign = 2,
  349. /// <summary>
  350. /// Indicates implicit instantiation of a type with fixed constructor signature.
  351. /// That means any unused constructor parameters won't be reported as such.
  352. /// </summary>
  353. InstantiatedWithFixedConstructorSignature = 4,
  354. /// <summary>Indicates implicit instantiation of a type.</summary>
  355. InstantiatedNoFixedConstructorSignature = 8,
  356. }
  357. /// <summary>
  358. /// Specify what is considered used implicitly when marked
  359. /// with <see cref="MeansImplicitUseAttribute"/> or <see cref="UsedImplicitlyAttribute"/>.
  360. /// </summary>
  361. [Flags]
  362. internal enum ImplicitUseTargetFlags
  363. {
  364. Default = Itself,
  365. Itself = 1,
  366. /// <summary>Members of entity marked with attribute are considered used.</summary>
  367. Members = 2,
  368. /// <summary>Entity marked with attribute and all its members considered used.</summary>
  369. WithMembers = Itself | Members
  370. }
  371. /// <summary>
  372. /// This attribute is intended to mark publicly available API
  373. /// which should not be removed and so is treated as used.
  374. /// </summary>
  375. [MeansImplicitUse(ImplicitUseTargetFlags.WithMembers)]
  376. internal sealed class PublicAPIAttribute : Attribute
  377. {
  378. public PublicAPIAttribute() {}
  379. public PublicAPIAttribute([NotNull] string comment)
  380. {
  381. Comment = comment;
  382. }
  383. [CanBeNull] public string Comment { get; private set; }
  384. }
  385. /// <summary>
  386. /// Tells code analysis engine if the parameter is completely handled when the invoked method is on stack.
  387. /// If the parameter is a delegate, indicates that delegate is executed while the method is executed.
  388. /// If the parameter is an enumerable, indicates that it is enumerated while the method is executed.
  389. /// </summary>
  390. [AttributeUsage(AttributeTargets.Parameter)]
  391. internal sealed class InstantHandleAttribute : Attribute {}
  392. /// <summary>
  393. /// Indicates that a method does not make any observable state changes.
  394. /// The same as <c>System.Diagnostics.Contracts.PureAttribute</c>.
  395. /// </summary>
  396. /// <example><code>
  397. /// [Pure] int Multiply(int x, int y) => x * y;
  398. ///
  399. /// void M() {
  400. /// Multiply(123, 42); // Waring: Return value of pure method is not used
  401. /// }
  402. /// </code></example>
  403. [AttributeUsage(AttributeTargets.Method)]
  404. internal sealed class PureAttribute : Attribute {}
  405. /// <summary>
  406. /// Indicates that the return value of method invocation must be used.
  407. /// </summary>
  408. [AttributeUsage(AttributeTargets.Method)]
  409. internal sealed class MustUseReturnValueAttribute : Attribute
  410. {
  411. public MustUseReturnValueAttribute() {}
  412. public MustUseReturnValueAttribute([NotNull] string justification)
  413. {
  414. Justification = justification;
  415. }
  416. [CanBeNull] public string Justification { get; private set; }
  417. }
  418. /// <summary>
  419. /// Indicates the type member or parameter of some type, that should be used instead of all other ways
  420. /// to get the value that type. This annotation is useful when you have some "context" value evaluated
  421. /// and stored somewhere, meaning that all other ways to get this value must be consolidated with existing one.
  422. /// </summary>
  423. /// <example><code>
  424. /// class Foo {
  425. /// [ProvidesContext] IBarService _barService = ...;
  426. ///
  427. /// void ProcessNode(INode node) {
  428. /// DoSomething(node, node.GetGlobalServices().Bar);
  429. /// // ^ Warning: use value of '_barService' field
  430. /// }
  431. /// }
  432. /// </code></example>
  433. [AttributeUsage(
  434. AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.Method |
  435. AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.GenericParameter)]
  436. internal sealed class ProvidesContextAttribute : Attribute {}
  437. /// <summary>
  438. /// Indicates that a parameter is a path to a file or a folder within a web project.
  439. /// Path can be relative or absolute, starting from web root (~).
  440. /// </summary>
  441. [AttributeUsage(AttributeTargets.Parameter)]
  442. internal sealed class PathReferenceAttribute : Attribute
  443. {
  444. public PathReferenceAttribute() {}
  445. public PathReferenceAttribute([NotNull, PathReference] string basePath)
  446. {
  447. BasePath = basePath;
  448. }
  449. [CanBeNull] public string BasePath { get; private set; }
  450. }
  451. /// <summary>
  452. /// An extension method marked with this attribute is processed by ReSharper code completion
  453. /// as a 'Source Template'. When extension method is completed over some expression, it's source code
  454. /// is automatically expanded like a template at call site.
  455. /// </summary>
  456. /// <remarks>
  457. /// Template method body can contain valid source code and/or special comments starting with '$'.
  458. /// Text inside these comments is added as source code when the template is applied. Template parameters
  459. /// can be used either as additional method parameters or as identifiers wrapped in two '$' signs.
  460. /// Use the <see cref="MacroAttribute"/> attribute to specify macros for parameters.
  461. /// </remarks>
  462. /// <example>
  463. /// In this example, the 'forEach' method is a source template available over all values
  464. /// of enumerable types, producing ordinary C# 'foreach' statement and placing caret inside block:
  465. /// <code>
  466. /// [SourceTemplate]
  467. /// public static void forEach&lt;T&gt;(this IEnumerable&lt;T&gt; xs) {
  468. /// foreach (var x in xs) {
  469. /// //$ $END$
  470. /// }
  471. /// }
  472. /// </code>
  473. /// </example>
  474. [AttributeUsage(AttributeTargets.Method)]
  475. internal sealed class SourceTemplateAttribute : Attribute {}
  476. /// <summary>
  477. /// Allows specifying a macro for a parameter of a <see cref="SourceTemplateAttribute">source template</see>.
  478. /// </summary>
  479. /// <remarks>
  480. /// You can apply the attribute on the whole method or on any of its additional parameters. The macro expression
  481. /// is defined in the <see cref="MacroAttribute.Expression"/> property. When applied on a method, the target
  482. /// template parameter is defined in the <see cref="MacroAttribute.Target"/> property. To apply the macro silently
  483. /// for the parameter, set the <see cref="MacroAttribute.Editable"/> property value = -1.
  484. /// </remarks>
  485. /// <example>
  486. /// Applying the attribute on a source template method:
  487. /// <code>
  488. /// [SourceTemplate, Macro(Target = "item", Expression = "suggestVariableName()")]
  489. /// public static void forEach&lt;T&gt;(this IEnumerable&lt;T&gt; collection) {
  490. /// foreach (var item in collection) {
  491. /// //$ $END$
  492. /// }
  493. /// }
  494. /// </code>
  495. /// Applying the attribute on a template method parameter:
  496. /// <code>
  497. /// [SourceTemplate]
  498. /// public static void something(this Entity x, [Macro(Expression = "guid()", Editable = -1)] string newguid) {
  499. /// /*$ var $x$Id = "$newguid$" + x.ToString();
  500. /// x.DoSomething($x$Id); */
  501. /// }
  502. /// </code>
  503. /// </example>
  504. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method, AllowMultiple = true)]
  505. internal sealed class MacroAttribute : Attribute
  506. {
  507. /// <summary>
  508. /// Allows specifying a macro that will be executed for a <see cref="SourceTemplateAttribute">source template</see>
  509. /// parameter when the template is expanded.
  510. /// </summary>
  511. [CanBeNull] public string Expression { get; set; }
  512. /// <summary>
  513. /// Allows specifying which occurrence of the target parameter becomes editable when the template is deployed.
  514. /// </summary>
  515. /// <remarks>
  516. /// If the target parameter is used several times in the template, only one occurrence becomes editable;
  517. /// other occurrences are changed synchronously. To specify the zero-based index of the editable occurrence,
  518. /// use values >= 0. To make the parameter non-editable when the template is expanded, use -1.
  519. /// </remarks>>
  520. public int Editable { get; set; }
  521. /// <summary>
  522. /// Identifies the target parameter of a <see cref="SourceTemplateAttribute">source template</see> if the
  523. /// <see cref="MacroAttribute"/> is applied on a template method.
  524. /// </summary>
  525. [CanBeNull] public string Target { get; set; }
  526. }
  527. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
  528. internal sealed class AspMvcAreaMasterLocationFormatAttribute : Attribute
  529. {
  530. public AspMvcAreaMasterLocationFormatAttribute([NotNull] string format)
  531. {
  532. Format = format;
  533. }
  534. [NotNull] public string Format { get; private set; }
  535. }
  536. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
  537. internal sealed class AspMvcAreaPartialViewLocationFormatAttribute : Attribute
  538. {
  539. public AspMvcAreaPartialViewLocationFormatAttribute([NotNull] string format)
  540. {
  541. Format = format;
  542. }
  543. [NotNull] public string Format { get; private set; }
  544. }
  545. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
  546. internal sealed class AspMvcAreaViewLocationFormatAttribute : Attribute
  547. {
  548. public AspMvcAreaViewLocationFormatAttribute([NotNull] string format)
  549. {
  550. Format = format;
  551. }
  552. [NotNull] public string Format { get; private set; }
  553. }
  554. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
  555. internal sealed class AspMvcMasterLocationFormatAttribute : Attribute
  556. {
  557. public AspMvcMasterLocationFormatAttribute([NotNull] string format)
  558. {
  559. Format = format;
  560. }
  561. [NotNull] public string Format { get; private set; }
  562. }
  563. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
  564. internal sealed class AspMvcPartialViewLocationFormatAttribute : Attribute
  565. {
  566. public AspMvcPartialViewLocationFormatAttribute([NotNull] string format)
  567. {
  568. Format = format;
  569. }
  570. [NotNull] public string Format { get; private set; }
  571. }
  572. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
  573. internal sealed class AspMvcViewLocationFormatAttribute : Attribute
  574. {
  575. public AspMvcViewLocationFormatAttribute([NotNull] string format)
  576. {
  577. Format = format;
  578. }
  579. [NotNull] public string Format { get; private set; }
  580. }
  581. /// <summary>
  582. /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter
  583. /// is an MVC action. If applied to a method, the MVC action name is calculated
  584. /// implicitly from the context. Use this attribute for custom wrappers similar to
  585. /// <c>System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper, String)</c>.
  586. /// </summary>
  587. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)]
  588. internal sealed class AspMvcActionAttribute : Attribute
  589. {
  590. public AspMvcActionAttribute() {}
  591. public AspMvcActionAttribute([NotNull] string anonymousProperty)
  592. {
  593. AnonymousProperty = anonymousProperty;
  594. }
  595. [CanBeNull] public string AnonymousProperty { get; private set; }
  596. }
  597. /// <summary>
  598. /// ASP.NET MVC attribute. Indicates that a parameter is an MVC area.
  599. /// Use this attribute for custom wrappers similar to
  600. /// <c>System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper, String)</c>.
  601. /// </summary>
  602. [AttributeUsage(AttributeTargets.Parameter)]
  603. internal sealed class AspMvcAreaAttribute : Attribute
  604. {
  605. public AspMvcAreaAttribute() {}
  606. public AspMvcAreaAttribute([NotNull] string anonymousProperty)
  607. {
  608. AnonymousProperty = anonymousProperty;
  609. }
  610. [CanBeNull] public string AnonymousProperty { get; private set; }
  611. }
  612. /// <summary>
  613. /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter is
  614. /// an MVC controller. If applied to a method, the MVC controller name is calculated
  615. /// implicitly from the context. Use this attribute for custom wrappers similar to
  616. /// <c>System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper, String, String)</c>.
  617. /// </summary>
  618. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)]
  619. internal sealed class AspMvcControllerAttribute : Attribute
  620. {
  621. public AspMvcControllerAttribute() {}
  622. public AspMvcControllerAttribute([NotNull] string anonymousProperty)
  623. {
  624. AnonymousProperty = anonymousProperty;
  625. }
  626. [CanBeNull] public string AnonymousProperty { get; private set; }
  627. }
  628. /// <summary>
  629. /// ASP.NET MVC attribute. Indicates that a parameter is an MVC Master. Use this attribute
  630. /// for custom wrappers similar to <c>System.Web.Mvc.Controller.View(String, String)</c>.
  631. /// </summary>
  632. [AttributeUsage(AttributeTargets.Parameter)]
  633. internal sealed class AspMvcMasterAttribute : Attribute {}
  634. /// <summary>
  635. /// ASP.NET MVC attribute. Indicates that a parameter is an MVC model type. Use this attribute
  636. /// for custom wrappers similar to <c>System.Web.Mvc.Controller.View(String, Object)</c>.
  637. /// </summary>
  638. [AttributeUsage(AttributeTargets.Parameter)]
  639. internal sealed class AspMvcModelTypeAttribute : Attribute {}
  640. /// <summary>
  641. /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter is an MVC
  642. /// partial view. If applied to a method, the MVC partial view name is calculated implicitly
  643. /// from the context. Use this attribute for custom wrappers similar to
  644. /// <c>System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(HtmlHelper, String)</c>.
  645. /// </summary>
  646. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)]
  647. internal sealed class AspMvcPartialViewAttribute : Attribute {}
  648. /// <summary>
  649. /// ASP.NET MVC attribute. Allows disabling inspections for MVC views within a class or a method.
  650. /// </summary>
  651. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
  652. internal sealed class AspMvcSuppressViewErrorAttribute : Attribute {}
  653. /// <summary>
  654. /// ASP.NET MVC attribute. Indicates that a parameter is an MVC display template.
  655. /// Use this attribute for custom wrappers similar to
  656. /// <c>System.Web.Mvc.Html.DisplayExtensions.DisplayForModel(HtmlHelper, String)</c>.
  657. /// </summary>
  658. [AttributeUsage(AttributeTargets.Parameter)]
  659. internal sealed class AspMvcDisplayTemplateAttribute : Attribute {}
  660. /// <summary>
  661. /// ASP.NET MVC attribute. Indicates that a parameter is an MVC editor template.
  662. /// Use this attribute for custom wrappers similar to
  663. /// <c>System.Web.Mvc.Html.EditorExtensions.EditorForModel(HtmlHelper, String)</c>.
  664. /// </summary>
  665. [AttributeUsage(AttributeTargets.Parameter)]
  666. internal sealed class AspMvcEditorTemplateAttribute : Attribute {}
  667. /// <summary>
  668. /// ASP.NET MVC attribute. Indicates that a parameter is an MVC template.
  669. /// Use this attribute for custom wrappers similar to
  670. /// <c>System.ComponentModel.DataAnnotations.UIHintAttribute(System.String)</c>.
  671. /// </summary>
  672. [AttributeUsage(AttributeTargets.Parameter)]
  673. internal sealed class AspMvcTemplateAttribute : Attribute {}
  674. /// <summary>
  675. /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter
  676. /// is an MVC view component. If applied to a method, the MVC view name is calculated implicitly
  677. /// from the context. Use this attribute for custom wrappers similar to
  678. /// <c>System.Web.Mvc.Controller.View(Object)</c>.
  679. /// </summary>
  680. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)]
  681. internal sealed class AspMvcViewAttribute : Attribute {}
  682. /// <summary>
  683. /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter
  684. /// is an MVC view component name.
  685. /// </summary>
  686. [AttributeUsage(AttributeTargets.Parameter)]
  687. internal sealed class AspMvcViewComponentAttribute : Attribute {}
  688. /// <summary>
  689. /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter
  690. /// is an MVC view component view. If applied to a method, the MVC view component view name is default.
  691. /// </summary>
  692. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)]
  693. internal sealed class AspMvcViewComponentViewAttribute : Attribute {}
  694. /// <summary>
  695. /// ASP.NET MVC attribute. When applied to a parameter of an attribute,
  696. /// indicates that this parameter is an MVC action name.
  697. /// </summary>
  698. /// <example><code>
  699. /// [ActionName("Foo")]
  700. /// public ActionResult Login(string returnUrl) {
  701. /// ViewBag.ReturnUrl = Url.Action("Foo"); // OK
  702. /// return RedirectToAction("Bar"); // Error: Cannot resolve action
  703. /// }
  704. /// </code></example>
  705. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
  706. internal sealed class AspMvcActionSelectorAttribute : Attribute {}
  707. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field)]
  708. internal sealed class HtmlElementAttributesAttribute : Attribute
  709. {
  710. public HtmlElementAttributesAttribute() {}
  711. public HtmlElementAttributesAttribute([NotNull] string name)
  712. {
  713. Name = name;
  714. }
  715. [CanBeNull] public string Name { get; private set; }
  716. }
  717. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property)]
  718. internal sealed class HtmlAttributeValueAttribute : Attribute
  719. {
  720. public HtmlAttributeValueAttribute([NotNull] string name)
  721. {
  722. Name = name;
  723. }
  724. [NotNull] public string Name { get; private set; }
  725. }
  726. /// <summary>
  727. /// Razor attribute. Indicates that a parameter or a method is a Razor section.
  728. /// Use this attribute for custom wrappers similar to
  729. /// <c>System.Web.WebPages.WebPageBase.RenderSection(String)</c>.
  730. /// </summary>
  731. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)]
  732. internal sealed class RazorSectionAttribute : Attribute {}
  733. /// <summary>
  734. /// Indicates how method, constructor invocation or property access
  735. /// over collection type affects content of the collection.
  736. /// </summary>
  737. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Property)]
  738. internal sealed class CollectionAccessAttribute : Attribute
  739. {
  740. public CollectionAccessAttribute(CollectionAccessType collectionAccessType)
  741. {
  742. CollectionAccessType = collectionAccessType;
  743. }
  744. public CollectionAccessType CollectionAccessType { get; private set; }
  745. }
  746. [Flags]
  747. internal enum CollectionAccessType
  748. {
  749. /// <summary>Method does not use or modify content of the collection.</summary>
  750. None = 0,
  751. /// <summary>Method only reads content of the collection but does not modify it.</summary>
  752. Read = 1,
  753. /// <summary>Method can change content of the collection but does not add new elements.</summary>
  754. ModifyExistingContent = 2,
  755. /// <summary>Method can add new elements to the collection.</summary>
  756. UpdatedContent = ModifyExistingContent | 4
  757. }
  758. /// <summary>
  759. /// Indicates that the marked method is assertion method, i.e. it halts control flow if
  760. /// one of the conditions is satisfied. To set the condition, mark one of the parameters with
  761. /// <see cref="AssertionConditionAttribute"/> attribute.
  762. /// </summary>
  763. [AttributeUsage(AttributeTargets.Method)]
  764. internal sealed class AssertionMethodAttribute : Attribute {}
  765. /// <summary>
  766. /// Indicates the condition parameter of the assertion method. The method itself should be
  767. /// marked by <see cref="AssertionMethodAttribute"/> attribute. The mandatory argument of
  768. /// the attribute is the assertion type.
  769. /// </summary>
  770. [AttributeUsage(AttributeTargets.Parameter)]
  771. internal sealed class AssertionConditionAttribute : Attribute
  772. {
  773. public AssertionConditionAttribute(AssertionConditionType conditionType)
  774. {
  775. ConditionType = conditionType;
  776. }
  777. public AssertionConditionType ConditionType { get; private set; }
  778. }
  779. /// <summary>
  780. /// Specifies assertion type. If the assertion method argument satisfies the condition,
  781. /// then the execution continues. Otherwise, execution is assumed to be halted.
  782. /// </summary>
  783. internal enum AssertionConditionType
  784. {
  785. /// <summary>Marked parameter should be evaluated to true.</summary>
  786. IS_TRUE = 0,
  787. /// <summary>Marked parameter should be evaluated to false.</summary>
  788. IS_FALSE = 1,
  789. /// <summary>Marked parameter should be evaluated to null value.</summary>
  790. IS_NULL = 2,
  791. /// <summary>Marked parameter should be evaluated to not null value.</summary>
  792. IS_NOT_NULL = 3,
  793. }
  794. /// <summary>
  795. /// Indicates that method is pure LINQ method, with postponed enumeration (like Enumerable.Select,
  796. /// .Where). This annotation allows inference of [InstantHandle] annotation for parameters
  797. /// of delegate type by analyzing LINQ method chains.
  798. /// </summary>
  799. [AttributeUsage(AttributeTargets.Method)]
  800. internal sealed class LinqTunnelAttribute : Attribute {}
  801. /// <summary>
  802. /// Indicates that IEnumerable, passed as parameter, is not enumerated.
  803. /// </summary>
  804. [AttributeUsage(AttributeTargets.Parameter)]
  805. internal sealed class NoEnumerationAttribute : Attribute {}
  806. /// <summary>
  807. /// Indicates that parameter is regular expression pattern.
  808. /// </summary>
  809. [AttributeUsage(AttributeTargets.Parameter)]
  810. internal sealed class RegexPatternAttribute : Attribute {}
  811. /// <summary>
  812. /// Prevents the Member Reordering feature from tossing members of the marked class.
  813. /// </summary>
  814. /// <remarks>
  815. /// The attribute must be mentioned in your member reordering patterns
  816. /// </remarks>
  817. [AttributeUsage(
  818. AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enum)]
  819. internal sealed class NoReorderAttribute : Attribute {}
  820. /// <summary>
  821. /// XAML attribute. Indicates the type that has <c>ItemsSource</c> property and should be treated
  822. /// as <c>ItemsControl</c>-derived type, to enable inner items <c>DataContext</c> type resolve.
  823. /// </summary>
  824. [AttributeUsage(AttributeTargets.Class)]
  825. internal sealed class XamlItemsControlAttribute : Attribute {}
  826. /// <summary>
  827. /// XAML attribute. Indicates the property of some <c>BindingBase</c>-derived type, that
  828. /// is used to bind some item of <c>ItemsControl</c>-derived type. This annotation will
  829. /// enable the <c>DataContext</c> type resolve for XAML bindings for such properties.
  830. /// </summary>
  831. /// <remarks>
  832. /// Property should have the tree ancestor of the <c>ItemsControl</c> type or
  833. /// marked with the <see cref="XamlItemsControlAttribute"/> attribute.
  834. /// </remarks>
  835. [AttributeUsage(AttributeTargets.Property)]
  836. internal sealed class XamlItemBindingOfItemsControlAttribute : Attribute {}
  837. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  838. internal sealed class AspChildControlTypeAttribute : Attribute
  839. {
  840. public AspChildControlTypeAttribute([NotNull] string tagName, [NotNull] Type controlType)
  841. {
  842. TagName = tagName;
  843. ControlType = controlType;
  844. }
  845. [NotNull] public string TagName { get; private set; }
  846. [NotNull] public Type ControlType { get; private set; }
  847. }
  848. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)]
  849. internal sealed class AspDataFieldAttribute : Attribute {}
  850. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)]
  851. internal sealed class AspDataFieldsAttribute : Attribute {}
  852. [AttributeUsage(AttributeTargets.Property)]
  853. internal sealed class AspMethodPropertyAttribute : Attribute {}
  854. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  855. internal sealed class AspRequiredAttributeAttribute : Attribute
  856. {
  857. public AspRequiredAttributeAttribute([NotNull] string attribute)
  858. {
  859. Attribute = attribute;
  860. }
  861. [NotNull] public string Attribute { get; private set; }
  862. }
  863. [AttributeUsage(AttributeTargets.Property)]
  864. internal sealed class AspTypePropertyAttribute : Attribute
  865. {
  866. public bool CreateConstructorReferences { get; private set; }
  867. public AspTypePropertyAttribute(bool createConstructorReferences)
  868. {
  869. CreateConstructorReferences = createConstructorReferences;
  870. }
  871. }
  872. [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
  873. internal sealed class RazorImportNamespaceAttribute : Attribute
  874. {
  875. public RazorImportNamespaceAttribute([NotNull] string name)
  876. {
  877. Name = name;
  878. }
  879. [NotNull] public string Name { get; private set; }
  880. }
  881. [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
  882. internal sealed class RazorInjectionAttribute : Attribute
  883. {
  884. public RazorInjectionAttribute([NotNull] string type, [NotNull] string fieldName)
  885. {
  886. Type = type;
  887. FieldName = fieldName;
  888. }
  889. [NotNull] public string Type { get; private set; }
  890. [NotNull] public string FieldName { get; private set; }
  891. }
  892. [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
  893. internal sealed class RazorDirectiveAttribute : Attribute
  894. {
  895. public RazorDirectiveAttribute([NotNull] string directive)
  896. {
  897. Directive = directive;
  898. }
  899. [NotNull] public string Directive { get; private set; }
  900. }
  901. [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
  902. internal sealed class RazorPageBaseTypeAttribute : Attribute
  903. {
  904. public RazorPageBaseTypeAttribute([NotNull] string baseType)
  905. {
  906. BaseType = baseType;
  907. }
  908. public RazorPageBaseTypeAttribute([NotNull] string baseType, string pageName)
  909. {
  910. BaseType = baseType;
  911. PageName = pageName;
  912. }
  913. [NotNull] public string BaseType { get; private set; }
  914. [CanBeNull] public string PageName { get; private set; }
  915. }
  916. [AttributeUsage(AttributeTargets.Method)]
  917. internal sealed class RazorHelperCommonAttribute : Attribute {}
  918. [AttributeUsage(AttributeTargets.Property)]
  919. internal sealed class RazorLayoutAttribute : Attribute {}
  920. [AttributeUsage(AttributeTargets.Method)]
  921. internal sealed class RazorWriteLiteralMethodAttribute : Attribute {}
  922. [AttributeUsage(AttributeTargets.Method)]
  923. internal sealed class RazorWriteMethodAttribute : Attribute {}
  924. [AttributeUsage(AttributeTargets.Parameter)]
  925. internal sealed class RazorWriteMethodParameterAttribute : Attribute {}
  926. }
  927. #endif