metadata.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #[derive(Debug, Clone, PartialEq, Eq)]
  2. /// Information about a hook call or function.
  3. pub struct HookInfo {
  4. /// The name of the hook, e.g. `use_state`.
  5. pub name: String,
  6. /// The span of the hook, e.g. `use_state(cx, || 0)`.
  7. pub span: Span,
  8. /// The span of the name, e.g. `use_state`.
  9. pub name_span: Span,
  10. }
  11. impl HookInfo {
  12. pub const fn new(span: Span, name_span: Span, name: String) -> Self {
  13. Self {
  14. span,
  15. name_span,
  16. name,
  17. }
  18. }
  19. }
  20. #[derive(Debug, Clone, PartialEq, Eq)]
  21. pub enum ConditionalInfo {
  22. If(IfInfo),
  23. Match(MatchInfo),
  24. }
  25. #[derive(Debug, Clone, PartialEq, Eq)]
  26. pub struct IfInfo {
  27. /// The span of the `if` statement, e.g. `if true { ... }`.
  28. pub span: Span,
  29. /// The span of the `if true` part only.
  30. pub head_span: Span,
  31. }
  32. impl IfInfo {
  33. pub const fn new(span: Span, head_span: Span) -> Self {
  34. Self { span, head_span }
  35. }
  36. }
  37. #[derive(Debug, Clone, PartialEq, Eq)]
  38. pub struct MatchInfo {
  39. /// The span of the `match` statement, e.g. `match true { ... }`.
  40. pub span: Span,
  41. /// The span of the `match true` part only.
  42. pub head_span: Span,
  43. }
  44. impl MatchInfo {
  45. pub const fn new(span: Span, head_span: Span) -> Self {
  46. Self { span, head_span }
  47. }
  48. }
  49. #[derive(Debug, Clone, PartialEq, Eq)]
  50. /// Information about one of the possible loop types.
  51. pub enum AnyLoopInfo {
  52. For(ForInfo),
  53. While(WhileInfo),
  54. Loop(LoopInfo),
  55. }
  56. #[derive(Debug, Clone, PartialEq, Eq)]
  57. /// Information about a `for` loop.
  58. pub struct ForInfo {
  59. pub span: Span,
  60. pub head_span: Span,
  61. }
  62. impl ForInfo {
  63. pub const fn new(span: Span, head_span: Span) -> Self {
  64. Self { span, head_span }
  65. }
  66. }
  67. #[derive(Debug, Clone, PartialEq, Eq)]
  68. /// Information about a `while` loop.
  69. pub struct WhileInfo {
  70. pub span: Span,
  71. pub head_span: Span,
  72. }
  73. impl WhileInfo {
  74. pub const fn new(span: Span, head_span: Span) -> Self {
  75. Self { span, head_span }
  76. }
  77. }
  78. #[derive(Debug, Clone, PartialEq, Eq)]
  79. /// Information about a `loop` loop.
  80. pub struct LoopInfo {
  81. pub span: Span,
  82. }
  83. impl LoopInfo {
  84. pub const fn new(span: Span) -> Self {
  85. Self { span }
  86. }
  87. }
  88. #[derive(Debug, Clone, PartialEq, Eq)]
  89. /// Information about a closure.
  90. pub struct ClosureInfo {
  91. pub span: Span,
  92. }
  93. impl ClosureInfo {
  94. pub const fn new(span: Span) -> Self {
  95. Self { span }
  96. }
  97. }
  98. #[derive(Debug, Clone, PartialEq, Eq)]
  99. /// Information about a component function.
  100. pub struct ComponentInfo {
  101. pub span: Span,
  102. pub name: String,
  103. pub name_span: Span,
  104. }
  105. impl ComponentInfo {
  106. pub const fn new(span: Span, name: String, name_span: Span) -> Self {
  107. Self {
  108. span,
  109. name,
  110. name_span,
  111. }
  112. }
  113. }
  114. #[derive(Debug, Clone, PartialEq, Eq)]
  115. /// Information about a non-component, non-hook function.
  116. pub struct FnInfo {
  117. pub span: Span,
  118. pub name: String,
  119. pub name_span: Span,
  120. }
  121. impl FnInfo {
  122. pub const fn new(span: Span, name: String, name_span: Span) -> Self {
  123. Self {
  124. span,
  125. name,
  126. name_span,
  127. }
  128. }
  129. }
  130. #[derive(Debug, Clone, PartialEq, Eq)]
  131. /// A span of text in a source code file.
  132. pub struct Span {
  133. pub source_text: Option<String>,
  134. pub start: LineColumn,
  135. pub end: LineColumn,
  136. }
  137. impl Span {
  138. pub fn new_from_str(source_text: &str, start: LineColumn) -> Self {
  139. let mut lines = source_text.lines();
  140. let first_line = lines.next().unwrap_or_default();
  141. let mut end = LineColumn {
  142. line: start.line,
  143. column: start.column + first_line.len(),
  144. };
  145. for line in lines {
  146. end.line += 1;
  147. end.column = line.len();
  148. }
  149. Self {
  150. source_text: Some(source_text.to_string()),
  151. start,
  152. end,
  153. }
  154. }
  155. }
  156. impl From<proc_macro2::Span> for Span {
  157. fn from(span: proc_macro2::Span) -> Self {
  158. Self {
  159. source_text: span.source_text(),
  160. start: span.start().into(),
  161. end: span.end().into(),
  162. }
  163. }
  164. }
  165. #[derive(Debug, Clone, PartialEq, Eq)]
  166. /// A location in a source code file.
  167. pub struct LineColumn {
  168. pub line: usize,
  169. pub column: usize,
  170. }
  171. impl From<proc_macro2::LineColumn> for LineColumn {
  172. fn from(lc: proc_macro2::LineColumn) -> Self {
  173. Self {
  174. line: lc.line,
  175. column: lc.column,
  176. }
  177. }
  178. }