summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs
blob: a21db5b2cec9d448535f120a1bf244db8bb3301c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//! This diagnostic provides an assist for creating a struct definition from a JSON
//! example.

use hir::{PathResolution, Semantics};
use ide_db::{
    base_db::FileId,
    helpers::mod_path_to_ast,
    imports::insert_use::{insert_use, ImportScope},
    source_change::SourceChangeBuilder,
    RootDatabase,
};
use itertools::Itertools;
use stdx::{format_to, never};
use syntax::{
    ast::{self, make},
    SyntaxKind, SyntaxNode,
};
use text_edit::TextEdit;

use crate::{fix, Diagnostic, DiagnosticsConfig, Severity};

#[derive(Default)]
struct State {
    result: String,
    struct_counts: usize,
    has_serialize: bool,
    has_deserialize: bool,
}

impl State {
    fn generate_new_name(&mut self) -> ast::Name {
        self.struct_counts += 1;
        make::name(&format!("Struct{}", self.struct_counts))
    }

    fn serde_derive(&self) -> String {
        let mut v = vec![];
        if self.has_serialize {
            v.push("Serialize");
        }
        if self.has_deserialize {
            v.push("Deserialize");
        }
        match v.as_slice() {
            [] => "".to_string(),
            [x] => format!("#[derive({x})]\n"),
            [x, y] => format!("#[derive({x}, {y})]\n"),
            _ => {
                never!();
                "".to_string()
            }
        }
    }

    fn build_struct(&mut self, value: &serde_json::Map<String, serde_json::Value>) -> ast::Type {
        let name = self.generate_new_name();
        let ty = make::ty(&name.to_string());
        let strukt = make::struct_(
            None,
            name,
            None,
            make::record_field_list(value.iter().sorted_unstable_by_key(|x| x.0).map(
                |(name, value)| make::record_field(None, make::name(name), self.type_of(value)),
            ))
            .into(),
        );
        format_to!(self.result, "{}{}\n", self.serde_derive(), strukt);
        ty
    }

    fn type_of(&mut self, value: &serde_json::Value) -> ast::Type {
        match value {
            serde_json::Value::Null => make::ty_unit(),
            serde_json::Value::Bool(_) => make::ty("bool"),
            serde_json::Value::Number(it) => make::ty(if it.is_i64() { "i64" } else { "f64" }),
            serde_json::Value::String(_) => make::ty("String"),
            serde_json::Value::Array(it) => {
                let ty = match it.iter().next() {
                    Some(x) => self.type_of(x),
                    None => make::ty_placeholder(),
                };
                make::ty(&format!("Vec<{ty}>"))
            }
            serde_json::Value::Object(x) => self.build_struct(x),
        }
    }
}

pub(crate) fn json_in_items(
    sema: &Semantics<'_, RootDatabase>,
    acc: &mut Vec<Diagnostic>,
    file_id: FileId,
    node: &SyntaxNode,
    config: &DiagnosticsConfig,
) {
    (|| {
        if node.kind() == SyntaxKind::ERROR
            && node.first_token().map(|x| x.kind()) == Some(SyntaxKind::L_CURLY)
            && node.last_token().map(|x| x.kind()) == Some(SyntaxKind::R_CURLY)
        {
            let node_string = node.to_string();
            if let Ok(it) = serde_json::from_str(&node_string) {
                if let serde_json::Value::Object(it) = it {
                    let import_scope = ImportScope::find_insert_use_container(node, sema)?;
                    let range = node.text_range();
                    let mut edit = TextEdit::builder();
                    edit.delete(range);
                    let mut state = State::default();
                    let semantics_scope = sema.scope(node)?;
                    let scope_resolve =
                        |it| semantics_scope.speculative_resolve(&make::path_from_text(it));
                    let scope_has = |it| scope_resolve(it).is_some();
                    let deserialize_resolved = scope_resolve("::serde::Deserialize");
                    let serialize_resolved = scope_resolve("::serde::Serialize");
                    state.has_deserialize = deserialize_resolved.is_some();
                    state.has_serialize = serialize_resolved.is_some();
                    state.build_struct(&it);
                    edit.insert(range.start(), state.result);
                    acc.push(
                        Diagnostic::new(
                            "json-is-not-rust",
                            "JSON syntax is not valid as a Rust item",
                            range,
                        )
                        .severity(Severity::WeakWarning)
                        .with_fixes(Some(vec![{
                            let mut scb = SourceChangeBuilder::new(file_id);
                            let scope = match import_scope.clone() {
                                ImportScope::File(it) => ImportScope::File(scb.make_mut(it)),
                                ImportScope::Module(it) => ImportScope::Module(scb.make_mut(it)),
                                ImportScope::Block(it) => ImportScope::Block(scb.make_mut(it)),
                            };
                            let current_module = semantics_scope.module();
                            if !scope_has("Serialize") {
                                if let Some(PathResolution::Def(it)) = serialize_resolved {
                                    if let Some(it) = current_module.find_use_path_prefixed(
                                        sema.db,
                                        it,
                                        config.insert_use.prefix_kind,
                                    ) {
                                        insert_use(
                                            &scope,
                                            mod_path_to_ast(&it),
                                            &config.insert_use,
                                        );
                                    }
                                }
                            }
                            if !scope_has("Deserialize") {
                                if let Some(PathResolution::Def(it)) = deserialize_resolved {
                                    if let Some(it) = current_module.find_use_path_prefixed(
                                        sema.db,
                                        it,
                                        config.insert_use.prefix_kind,
                                    ) {
                                        insert_use(
                                            &scope,
                                            mod_path_to_ast(&it),
                                            &config.insert_use,
                                        );
                                    }
                                }
                            }
                            let mut sc = scb.finish();
                            sc.insert_source_edit(file_id, edit.finish());
                            fix("convert_json_to_struct", "Convert JSON to struct", sc, range)
                        }])),
                    );
                }
            }
        }
        Some(())
    })();
}

#[cfg(test)]
mod tests {
    use crate::{
        tests::{check_diagnostics_with_config, check_fix, check_no_fix},
        DiagnosticsConfig,
    };

    #[test]
    fn diagnostic_for_simple_case() {
        let mut config = DiagnosticsConfig::test_sample();
        config.disabled.insert("syntax-error".to_string());
        check_diagnostics_with_config(
            config,
            r#"
            { "foo": "bar" }
         // ^^^^^^^^^^^^^^^^ 💡 weak: JSON syntax is not valid as a Rust item
"#,
        );
    }

    #[test]
    fn types_of_primitives() {
        check_fix(
            r#"
            //- /lib.rs crate:lib deps:serde
            use serde::Serialize;

            fn some_garbage() {

            }

            {$0
                "foo": "bar",
                "bar": 2.3,
                "baz": null,
                "bay": 57,
                "box": true
            }
            //- /serde.rs crate:serde

            pub trait Serialize {
                fn serialize() -> u8;
            }
            "#,
            r#"
            use serde::Serialize;

            fn some_garbage() {

            }

            #[derive(Serialize)]
            struct Struct1{ bar: f64, bay: i64, baz: (), r#box: bool, foo: String }

            "#,
        );
    }

    #[test]
    fn nested_structs() {
        check_fix(
            r#"
            {$0
                "foo": "bar",
                "bar": {
                    "kind": "Object",
                    "value": {}
                }
            }
            "#,
            r#"
            struct Struct3{  }
            struct Struct2{ kind: String, value: Struct3 }
            struct Struct1{ bar: Struct2, foo: String }

            "#,
        );
    }

    #[test]
    fn arrays() {
        check_fix(
            r#"
            //- /lib.rs crate:lib deps:serde
            {
                "of_string": ["foo", "2", "x"], $0
                "of_object": [{
                    "x": 10,
                    "y": 20
                }, {
                    "x": 10,
                    "y": 20
                }],
                "nested": [[[2]]],
                "empty": []
            }
            //- /serde.rs crate:serde

            pub trait Serialize {
                fn serialize() -> u8;
            }
            pub trait Deserialize {
                fn deserialize() -> u8;
            }
            "#,
            r#"
            use serde::Serialize;
            use serde::Deserialize;

            #[derive(Serialize, Deserialize)]
            struct Struct2{ x: i64, y: i64 }
            #[derive(Serialize, Deserialize)]
            struct Struct1{ empty: Vec<_>, nested: Vec<Vec<Vec<i64>>>, of_object: Vec<Struct2>, of_string: Vec<String> }

            "#,
        );
    }

    #[test]
    fn no_emit_outside_of_item_position() {
        check_no_fix(
            r#"
            fn foo() {
                let json = {$0
                    "foo": "bar",
                    "bar": {
                        "kind": "Object",
                        "value": {}
                    }
                };
            }
            "#,
        );
    }
}