Skip to main content

griz_core/
definitions.rs

1//! Named-definition diff: which functions, types, and other declarations a
2//! before/after pair added, removed, or changed, per enabled language.
3//!
4//! Fact only. A definition present in both with the same body is left out;
5//! a rename shows up as one removed and one added.
6
7mod compare;
8mod identity;
9
10use ast_grep_language::SupportLang;
11pub use compare::diff_items;
12use schemars::JsonSchema;
13use serde::{Deserialize, Serialize};
14
15/// What happened to one named definition between before and after.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
17#[serde(rename_all = "snake_case")]
18pub enum ItemChange {
19    /// Present after, not before.
20    Added,
21    /// Present before, not after.
22    Removed,
23    /// Present in both, with a different body.
24    Changed,
25}
26
27/// One named definition a diff added, removed, or changed.
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
29pub struct DiffItem {
30    /// Kind of definition: function, method, struct, class, enum, trait,
31    /// `type_alias`, or const.
32    pub kind: String,
33    /// The definition's name.
34    pub name: String,
35    /// Enclosing named scopes, outermost first; omitted at file scope.
36    #[serde(default, skip_serializing_if = "Vec::is_empty")]
37    pub scope: Vec<String>,
38    /// What happened to it.
39    pub change: ItemChange,
40}
41
42struct DefKind {
43    node_kind: &'static str,
44    label: &'static str,
45    name_field: &'static str,
46}
47
48const RUST: &[DefKind] = &[
49    DefKind {
50        node_kind: "function_item",
51        label: "function",
52        name_field: "name",
53    },
54    DefKind {
55        node_kind: "struct_item",
56        label: "struct",
57        name_field: "name",
58    },
59    DefKind {
60        node_kind: "enum_item",
61        label: "enum",
62        name_field: "name",
63    },
64    DefKind {
65        node_kind: "trait_item",
66        label: "trait",
67        name_field: "name",
68    },
69    DefKind {
70        node_kind: "type_item",
71        label: "type_alias",
72        name_field: "name",
73    },
74    DefKind {
75        node_kind: "const_item",
76        label: "const",
77        name_field: "name",
78    },
79];
80
81const TS: &[DefKind] = &[
82    DefKind {
83        node_kind: "function_declaration",
84        label: "function",
85        name_field: "name",
86    },
87    DefKind {
88        node_kind: "method_definition",
89        label: "method",
90        name_field: "name",
91    },
92    DefKind {
93        node_kind: "class_declaration",
94        label: "class",
95        name_field: "name",
96    },
97    DefKind {
98        node_kind: "interface_declaration",
99        label: "trait",
100        name_field: "name",
101    },
102    DefKind {
103        node_kind: "type_alias_declaration",
104        label: "type_alias",
105        name_field: "name",
106    },
107    DefKind {
108        node_kind: "enum_declaration",
109        label: "enum",
110        name_field: "name",
111    },
112];
113
114const JS: &[DefKind] = &[
115    DefKind {
116        node_kind: "function_declaration",
117        label: "function",
118        name_field: "name",
119    },
120    DefKind {
121        node_kind: "method_definition",
122        label: "method",
123        name_field: "name",
124    },
125    DefKind {
126        node_kind: "class_declaration",
127        label: "class",
128        name_field: "name",
129    },
130];
131
132const PYTHON: &[DefKind] = &[
133    DefKind {
134        node_kind: "function_definition",
135        label: "function",
136        name_field: "name",
137    },
138    DefKind {
139        node_kind: "class_definition",
140        label: "class",
141        name_field: "name",
142    },
143];
144
145const GO: &[DefKind] = &[
146    DefKind {
147        node_kind: "function_declaration",
148        label: "function",
149        name_field: "name",
150    },
151    DefKind {
152        node_kind: "method_declaration",
153        label: "method",
154        name_field: "name",
155    },
156    DefKind {
157        node_kind: "type_spec",
158        label: "type_alias",
159        name_field: "name",
160    },
161];
162
163const SWIFT: &[DefKind] = &[
164    DefKind {
165        node_kind: "function_declaration",
166        label: "function",
167        name_field: "name",
168    },
169    DefKind {
170        node_kind: "class_declaration",
171        label: "class",
172        name_field: "name",
173    },
174    DefKind {
175        node_kind: "protocol_declaration",
176        label: "trait",
177        name_field: "name",
178    },
179    DefKind {
180        node_kind: "enum_declaration",
181        label: "enum",
182        name_field: "name",
183    },
184];
185
186fn definitions_for(language: SupportLang) -> &'static [DefKind] {
187    match language {
188        SupportLang::Rust => RUST,
189        SupportLang::TypeScript | SupportLang::Tsx => TS,
190        SupportLang::JavaScript => JS,
191        SupportLang::Python => PYTHON,
192        SupportLang::Go => GO,
193        SupportLang::Swift => SWIFT,
194        _ => &[],
195    }
196}