dprint 0.55.0

Binary for dprint code formatter—a pluggable and configurable code formatting platform.
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use anyhow::Result;
use anyhow::bail;
use jsonc_parser::JsonArray;
use jsonc_parser::JsonObject;
use jsonc_parser::JsonValue;
use jsonc_parser::parse_to_value;
use url::Url;

use crate::environment::Environment;

// note: these don't derive `Eq` because `serde_json::Value` isn't `Eq`

#[derive(PartialEq, Debug)]
pub struct InfoFile {
  pub plugin_system_schema_version: u32,
  pub latest_plugins: Vec<InfoFilePluginInfo>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct InfoFilePluginInfo {
  pub name: String,
  pub version: String,
  pub url: String,
  pub config_key: Option<String>,
  pub file_extensions: Vec<String>,
  pub file_names: Vec<String>,
  pub config_excludes: Vec<String>,
  pub checksum: Option<String>,
  /// Config to insert into the plugin's config block on `dprint init`.
  pub default_config: Option<serde_json::Value>,
  /// Config fragments that `dprint init` merges into the plugin's config block
  /// when their files are found in the current directory (ex. wiring up a
  /// `dprint-plugin-exec` command for a matched file type).
  pub config_items: Vec<InfoFileConfigItem>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct InfoFileConfigItem {
  pub file_extensions: Vec<String>,
  pub file_names: Vec<String>,
  pub config: serde_json::Value,
}

impl InfoFilePluginInfo {
  pub fn is_wasm(&self) -> bool {
    self.url.to_lowercase().ends_with(".wasm")
  }

  pub fn is_process_plugin(&self) -> bool {
    !self.is_wasm()
  }

  pub fn full_url(&self) -> String {
    if let Some(checksum) = &self.checksum {
      return format!("{}@{}", self.url, checksum);
    }
    self.url.to_string()
  }

  pub fn full_url_no_wasm_checksum(&self) -> String {
    if self.is_wasm() { self.url.to_string() } else { self.full_url() }
  }
}

const SCHEMA_VERSION: u8 = 4;
pub const REMOTE_INFO_URL: &str = "https://plugins.dprint.dev/info.json";

pub async fn read_info_file(environment: &impl Environment) -> Result<InfoFile> {
  let (_, info_file) = environment.download_file_err_404(&Url::parse(REMOTE_INFO_URL)?, None).await?;
  let info_text = String::from_utf8(info_file.content)?;
  let json_value = parse_to_value(&info_text, &Default::default())?;
  let mut obj = match json_value {
    Some(JsonValue::Object(obj)) => obj,
    _ => bail!("Expected object in root element."),
  };

  // check schema version
  let schema_version = match obj.take_number("schemaVersion") {
    Some(value) => value.parse::<u32>()?,
    _ => bail!("Could not find schema version."),
  };
  if schema_version != SCHEMA_VERSION as u32 {
    bail!(
      "Cannot handle schema version {}. Expected {}. This might mean your dprint CLI version is old and isn't able to get the latest information.",
      schema_version,
      SCHEMA_VERSION
    );
  }

  // get plugin system version
  let plugin_system_schema_version = match obj.take_number("pluginSystemSchemaVersion") {
    Some(value) => value.parse::<u32>()?,
    _ => bail!("Could not find plugin system schema version."),
  };

  let latest_plugins = match obj.take_array("latest") {
    Some(arr) => {
      let mut plugins = Vec::new();
      for value in arr.into_iter() {
        plugins.push(get_latest_plugin(value)?);
      }
      plugins
    }
    _ => bail!("Could not find latest plugins array."),
  };

  Ok(InfoFile {
    plugin_system_schema_version,
    latest_plugins,
  })
}

fn get_latest_plugin(value: JsonValue) -> Result<InfoFilePluginInfo> {
  let mut obj = match value {
    JsonValue::Object(obj) => obj,
    _ => bail!("Expected an object in the latest array."),
  };
  let name = get_string(&mut obj, "name")?;
  let version = get_string(&mut obj, "version")?;
  let url = get_string(&mut obj, "url")?;
  let config_key = obj.take_string("configKey").map(|k| k.into_owned());
  let file_extensions = get_string_array(&mut obj, "fileExtensions")?;
  let file_names = get_string_array(&mut obj, "fileNames").unwrap_or_default(); // compatible with old configuration
  let config_excludes = get_string_array(&mut obj, "configExcludes")?;
  let checksum = obj.take_string("checksum").map(|s| s.into_owned());
  // these are only used by `dprint init`, so parse them leniently rather than
  // failing the whole info file when a single entry is malformed
  let default_config = obj.take_object("defaultConfig").map(|o| jsonc_to_serde(JsonValue::Object(o)));
  let config_items = obj.take_array("configItems").map(parse_config_items).unwrap_or_default();

  Ok(InfoFilePluginInfo {
    name,
    version,
    url,
    config_key,
    file_extensions,
    file_names,
    config_excludes,
    checksum,
    default_config,
    config_items,
  })
}

fn parse_config_items(arr: JsonArray) -> Vec<InfoFileConfigItem> {
  let mut items = Vec::new();
  for value in arr.into_iter() {
    let JsonValue::Object(mut obj) = value else {
      continue;
    };
    let (file_extensions, file_names) = match obj.take_object("match") {
      Some(mut match_obj) => (
        take_string_array(&mut match_obj, "fileExtensions"),
        take_string_array(&mut match_obj, "fileNames"),
      ),
      None => (Vec::new(), Vec::new()),
    };
    let config = obj
      .take_object("config")
      .map(|o| jsonc_to_serde(JsonValue::Object(o)))
      .unwrap_or_else(|| serde_json::Value::Object(Default::default()));
    items.push(InfoFileConfigItem {
      file_extensions,
      file_names,
      config,
    });
  }
  items
}

/// Converts a parsed jsonc value into an owned `serde_json::Value`.
fn jsonc_to_serde(value: JsonValue) -> serde_json::Value {
  use serde_json::Value as Json;
  match value {
    JsonValue::Null => Json::Null,
    JsonValue::Boolean(value) => Json::Bool(value),
    JsonValue::Number(value) => serde_json::from_str(value).unwrap_or(Json::Null),
    JsonValue::String(value) => Json::String(value.into_owned()),
    JsonValue::Array(arr) => Json::Array(arr.into_iter().map(jsonc_to_serde).collect()),
    JsonValue::Object(obj) => Json::Object(obj.into_iter().map(|(key, value)| (key, jsonc_to_serde(value))).collect()),
  }
}

/// Gets a string array, ignoring the key when it's missing or any non-string entries.
fn take_string_array(obj: &mut JsonObject, key: &str) -> Vec<String> {
  match obj.take_array(key) {
    Some(arr) => arr
      .into_iter()
      .filter_map(|value| match value {
        JsonValue::String(value) => Some(value.into_owned()),
        _ => None,
      })
      .collect(),
    None => Vec::new(),
  }
}

fn get_string_array(value: &mut JsonObject, key: &str) -> Result<Vec<String>> {
  let mut result = Vec::new();
  for item in get_array(value, key)? {
    match item {
      JsonValue::String(item) => result.push(item.into_owned()),
      _ => bail!("Unexpected non-string in {} array.", key),
    }
  }
  Ok(result)
}

fn get_string(value: &mut JsonObject, name: &str) -> Result<String> {
  match value.take_string(name) {
    Some(text) => Ok(text.into_owned()),
    _ => bail!("Could not find string: {}", name),
  }
}

fn get_array<'a>(value: &mut JsonObject<'a>, name: &str) -> Result<JsonArray<'a>> {
  match value.take_array(name) {
    Some(arr) => Ok(arr),
    _ => bail!("Could not find array: {}", name),
  }
}

#[cfg(test)]
mod test {
  use super::*;
  use crate::environment::TestEnvironment;
  use crate::environment::TestEnvironmentBuilder;
  use crate::environment::TestInfoFileConfigItem;
  use crate::environment::TestInfoFileMatch;
  use crate::environment::TestInfoFilePlugin;
  use pretty_assertions::assert_eq;

  #[test]
  fn should_get_info() {
    let environment = TestEnvironmentBuilder::new()
      .with_info_file(|info| {
        info
          .add_plugin(TestInfoFilePlugin {
            name: "dprint-plugin-typescript".to_string(),
            version: "0.17.2".to_string(),
            url: "https://plugins.dprint.dev/typescript-0.17.2.wasm".to_string(),
            config_key: Some("typescript".to_string()),
            file_extensions: vec!["ts".to_string(), "tsx".to_string()],
            config_excludes: vec!["**/node_modules".to_string()],
            ..Default::default()
          })
          .add_plugin(TestInfoFilePlugin {
            name: "dprint-plugin-jsonc".to_string(),
            version: "0.2.3".to_string(),
            url: "https://plugins.dprint.dev/json-0.2.3.wasm".to_string(),
            config_key: None,
            file_extensions: vec!["json".to_string()],
            file_names: Some(vec!["test-file".to_string()]),
            config_excludes: vec!["**/*-lock.json".to_string()],
            checksum: Some("test-checksum".to_string()),
            ..Default::default()
          });
      })
      .build();
    environment.clone().run_in_runtime(async move {
      let info_file = read_info_file(&environment).await.unwrap();
      assert_eq!(
        info_file,
        InfoFile {
          plugin_system_schema_version: 4,
          latest_plugins: vec![
            InfoFilePluginInfo {
              name: "dprint-plugin-typescript".to_string(),
              version: "0.17.2".to_string(),
              url: "https://plugins.dprint.dev/typescript-0.17.2.wasm".to_string(),
              config_key: Some("typescript".to_string()),
              file_extensions: vec!["ts".to_string(), "tsx".to_string()],
              file_names: vec![],
              config_excludes: vec!["**/node_modules".to_string()],
              checksum: None,
              default_config: None,
              config_items: vec![],
            },
            InfoFilePluginInfo {
              name: "dprint-plugin-jsonc".to_string(),
              version: "0.2.3".to_string(),
              url: "https://plugins.dprint.dev/json-0.2.3.wasm".to_string(),
              config_key: None,
              file_extensions: vec!["json".to_string()],
              file_names: vec!["test-file".to_string()],
              config_excludes: vec!["**/*-lock.json".to_string()],
              checksum: Some("test-checksum".to_string()),
              default_config: None,
              config_items: vec![],
            }
          ],
        }
      )
    });
  }

  #[test]
  fn should_parse_default_config_and_config_items() {
    let environment = TestEnvironmentBuilder::new()
      .with_info_file(|info| {
        info.add_plugin(TestInfoFilePlugin {
          name: "dprint-plugin-exec".to_string(),
          version: "0.5.0".to_string(),
          url: "https://plugins.dprint.dev/exec-0.5.0.json".to_string(),
          config_key: Some("exec".to_string()),
          file_extensions: vec![],
          config_excludes: vec![],
          checksum: Some("checksum".to_string()),
          default_config: Some(serde_json::json!({ "cwd": "${configDir}" })),
          config_items: vec![TestInfoFileConfigItem {
            file_match: TestInfoFileMatch {
              file_extensions: vec!["rs".to_string()],
              file_names: vec![],
            },
            config: serde_json::json!({ "commands": [{ "command": "rustfmt", "exts": ["rs"] }] }),
          }],
          ..Default::default()
        });
      })
      .build();
    environment.clone().run_in_runtime(async move {
      let info_file = read_info_file(&environment).await.unwrap();
      let plugin = &info_file.latest_plugins[0];
      assert_eq!(plugin.default_config, Some(serde_json::json!({ "cwd": "${configDir}" })));
      assert_eq!(
        plugin.config_items,
        vec![InfoFileConfigItem {
          file_extensions: vec!["rs".to_string()],
          file_names: vec![],
          config: serde_json::json!({ "commands": [{ "command": "rustfmt", "exts": ["rs"] }] }),
        }]
      );
    });
  }

  #[test]
  fn should_parse_init_fields_leniently() {
    let environment = TestEnvironment::new();
    environment.add_remote_file(
      REMOTE_INFO_URL,
      r#"{
  "schemaVersion": 4,
  "pluginSystemSchemaVersion": 4,
  "latest": [{
    "name": "p",
    "version": "1.0.0",
    "url": "https://plugins.dprint.dev/p.wasm",
    "fileExtensions": ["x"],
    "configExcludes": [],
    "defaultConfig": "not-an-object",
    "configItems": [
      "not-an-object",
      { "config": { "a": 1 } },
      { "match": { "fileExtensions": ["y", 5] }, "config": { "b": 2 } }
    ]
  }]
}"#
        .as_bytes(),
    );
    environment.clone().run_in_runtime(async move {
      let info_file = read_info_file(&environment).await.unwrap();
      let plugin = &info_file.latest_plugins[0];
      // a non-object defaultConfig is ignored rather than failing the whole info file
      assert_eq!(plugin.default_config, None);
      assert_eq!(
        plugin.config_items,
        vec![
          // the bare string entry is skipped; a missing `match` defaults to no matchers
          InfoFileConfigItem {
            file_extensions: vec![],
            file_names: vec![],
            config: serde_json::json!({ "a": 1 }),
          },
          // the non-string extension is dropped
          InfoFileConfigItem {
            file_extensions: vec!["y".to_string()],
            file_names: vec![],
            config: serde_json::json!({ "b": 2 }),
          },
        ]
      );
    });
  }

  #[test]
  fn should_error_if_schema_version_is_different() {
    let environment = TestEnvironment::new();
    environment.add_remote_file(
      REMOTE_INFO_URL,
      r#"{
    "schemaVersion": 1,
}"#
        .as_bytes(),
    );
    environment.clone().run_in_runtime(async move {
      let message = read_info_file(&environment).await.err().unwrap();
      assert_eq!(
        message.to_string(),
        "Cannot handle schema version 1. Expected 4. This might mean your dprint CLI version is old and isn't able to get the latest information."
      );
    });
  }

  #[test]
  fn should_error_if_no_plugin_system_set() {
    let environment = TestEnvironment::new();
    environment.add_remote_file(
      REMOTE_INFO_URL,
      r#"{
    "schemaVersion": 4,
}"#
        .as_bytes(),
    );
    environment.clone().run_in_runtime(async move {
      let message = read_info_file(&environment).await.err().unwrap();
      assert_eq!(message.to_string(), "Could not find plugin system schema version.");
    });
  }

  #[test]
  fn should_error_when_info_file_not_exists() {
    let environment = TestEnvironment::new();
    environment.clone().run_in_runtime(async move {
      let message = read_info_file(&environment).await.err().unwrap();
      assert_eq!(message.to_string(), "Error downloading https://plugins.dprint.dev/info.json - 404 Not Found");
    });
  }

  #[test]
  fn should_error_when_info_file_errors() {
    let environment = TestEnvironment::new();
    environment.add_remote_file_error("https://plugins.dprint.dev/info.json", "Some Error");
    environment.clone().run_in_runtime(async move {
      let message = read_info_file(&environment).await.err().unwrap();
      assert_eq!(message.to_string(), "Some Error");
    });
  }
}