Skip to content

Commit 2f82747

Browse files
committed
chore: update builder and format code
1 parent 1e4e565 commit 2f82747

12 files changed

+63
-40
lines changed

.rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
max_width = 160
2+
tab_spaces = 2

dprint.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"incremental": true,
33
"indentWidth": 2,
44
"rustfmt": {
5-
"lineWidth": 160
5+
"lineWidth": 160,
6+
"imports_granularity": "Item"
67
},
78
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md,toml,rs}"],
89
"excludes": [
@@ -12,10 +13,10 @@
1213
"**/target"
1314
],
1415
"plugins": [
15-
"https://plugins.dprint.dev/typescript-0.62.1.wasm",
16-
"https://plugins.dprint.dev/json-0.14.0.wasm",
17-
"https://plugins.dprint.dev/markdown-0.12.1.wasm",
18-
"https://plugins.dprint.dev/toml-0.5.3.wasm",
16+
"https://plugins.dprint.dev/typescript-0.62.2.wasm",
17+
"https://plugins.dprint.dev/json-0.14.1.wasm",
18+
"https://plugins.dprint.dev/markdown-0.12.2.wasm",
19+
"https://plugins.dprint.dev/toml-0.5.4.wasm",
1920
"https://plugins.dprint.dev/rustfmt-0.4.0.exe-plugin@c6bb223ef6e5e87580177f6461a0ab0554ac9ea6b54f78ea7ae8bf63b14f5bc2"
2021
]
2122
}

src/configuration/builder.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ impl ConfigurationBuilder {
136136
self.insert("preferHanging", value.into())
137137
}
138138

139+
/// Behaviour to use for quotes on property names.
140+
///
141+
/// Default: `preserve`
142+
pub fn quote_props(&mut self, value: QuoteProps) -> &mut Self {
143+
self.insert("quoteProps", value.to_string().into())
144+
}
145+
139146
/// Where to place the opening brace.
140147
///
141148
/// Default: `BracePosition::SameLineUnlessHanging`
@@ -947,6 +954,7 @@ mod tests {
947954
.single_body_position(SingleBodyPosition::SameLine)
948955
.trailing_commas(TrailingCommas::Never)
949956
.use_braces(UseBraces::WhenNotSingleLine)
957+
.quote_props(QuoteProps::AsNeeded)
950958
.prefer_hanging(false)
951959
/* situational */
952960
.arrow_function_use_parentheses(UseParentheses::Maintain)
@@ -1101,7 +1109,7 @@ mod tests {
11011109
.while_statement_space_after_while_keyword(true);
11021110

11031111
let inner_config = config.get_inner_config();
1104-
assert_eq!(inner_config.len(), 153);
1112+
assert_eq!(inner_config.len(), 154);
11051113
let diagnostics = resolve_config(inner_config, &resolve_global_config(HashMap::new(), &Default::default()).config).diagnostics;
11061114
assert_eq!(diagnostics.len(), 0);
11071115
}

src/configuration/resolve_config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
292292

293293
#[cfg(test)]
294294
mod tests {
295-
use dprint_core::configuration::{resolve_global_config, NewLineKind};
295+
use dprint_core::configuration::resolve_global_config;
296+
use dprint_core::configuration::NewLineKind;
296297
use std::collections::HashMap;
297298

298299
use super::super::builder::ConfigurationBuilder;

src/configuration/types.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use dprint_core::configuration::*;
22
use dprint_core::generate_str_to_from;
3-
use serde::{Deserialize, Serialize};
3+
use serde::Deserialize;
4+
use serde::Serialize;
45

56
/// Semi colon possibilities.
67
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
@@ -206,7 +207,7 @@ pub enum JsxQuoteStyle {
206207

207208
generate_str_to_from![JsxQuoteStyle, [PreferDouble, "preferDouble"], [PreferSingle, "preferSingle"]];
208209

209-
/// How to decide to use single or double quotes.
210+
/// Behaviour to use for quotes on property names.
210211
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
211212
#[serde(rename_all = "camelCase")]
212213
pub enum QuoteProps {
@@ -216,12 +217,7 @@ pub enum QuoteProps {
216217
AsNeeded,
217218
}
218219

219-
generate_str_to_from![
220-
QuoteProps,
221-
[Preserve, "preserve"],
222-
[AsNeeded, "asNeeded"]
223-
];
224-
220+
generate_str_to_from![QuoteProps, [Preserve, "preserve"], [AsNeeded, "asNeeded"]];
225221

226222
/// Whether to surround a JSX element or fragment with parentheses
227223
/// when it's the top JSX node and it spans multiple lines.

src/generation/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use deno_ast::swc::common::Span;
44
use deno_ast::swc::common::Spanned;
55
use deno_ast::swc::parser::token::TokenAndSpan;
66
use deno_ast::view::*;
7-
use dprint_core::formatting::{ConditionReference, Info};
7+
use dprint_core::formatting::ConditionReference;
8+
use dprint_core::formatting::Info;
89
use rustc_hash::FxHashMap;
910
use rustc_hash::FxHashSet;
1011

src/generation/generate.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
use deno_ast::swc::common::comments::{Comment, CommentKind};
2-
use deno_ast::swc::common::{BytePos, Span, Spanned};
1+
use deno_ast::swc::common::comments::Comment;
2+
use deno_ast::swc::common::comments::CommentKind;
3+
use deno_ast::swc::common::BytePos;
4+
use deno_ast::swc::common::Span;
5+
use deno_ast::swc::common::Spanned;
36
use deno_ast::swc::parser::lexer::util::CharExt;
4-
use deno_ast::swc::parser::token::{Token, TokenAndSpan};
7+
use deno_ast::swc::parser::token::Token;
8+
use deno_ast::swc::parser::token::TokenAndSpan;
59
use deno_ast::view::*;
610
use deno_ast::MediaType;
711
use deno_ast::ParsedSource;
12+
use dprint_core::formatting::condition_resolvers;
13+
use dprint_core::formatting::conditions::*;
14+
use dprint_core::formatting::ir_helpers::*;
815
use dprint_core::formatting::*;
9-
use dprint_core::formatting::{condition_resolvers, conditions::*, ir_helpers::*};
1016
use std::rc::Rc;
1117

1218
use super::sorting::*;
@@ -3506,28 +3512,27 @@ fn gen_string_literal<'a>(node: &'a Str, context: &mut Context<'a>) -> PrintItem
35063512
Node::TsGetterSignature(parent) => match_key_expr(parent.key),
35073513
Node::TsSetterSignature(parent) => match_key_expr(parent.key),
35083514
Node::TsMethodSignature(parent) => match_key_expr(parent.key),
3509-
_ => false
3515+
_ => false,
35103516
}
35113517
}
35123518

35133519
fn match_key_expr(key: Expr) -> bool {
35143520
match key {
3515-
Expr::Lit(Lit::Str(_str)) => true,
3516-
Expr::Tpl(_tpl) => true,
3517-
_ => false
3521+
Expr::Lit(Lit::Str(_str)) => true,
3522+
Expr::Tpl(_tpl) => true,
3523+
_ => false,
35183524
}
35193525
}
35203526
fn match_key_prop_name(key: PropName) -> bool {
35213527
match key {
35223528
PropName::Str(_str) => true,
3523-
_ => false
3529+
_ => false,
35243530
}
35253531
}
3526-
35273532

35283533
fn get_string_literal_text(string_value: String, is_jsx_attribute: bool, should_remove_quotes_if_identifier: bool, context: &mut Context) -> String {
35293534
if should_remove_quotes_if_identifier && is_valid_identifier(&string_value) {
3530-
return string_value
3535+
return string_value;
35313536
}
35323537
return if is_jsx_attribute {
35333538
// JSX attributes cannot contain escaped quotes so regardless of
@@ -3547,12 +3552,14 @@ fn gen_string_literal<'a>(node: &'a Str, context: &mut Context<'a>) -> PrintItem
35473552
};
35483553

35493554
fn is_valid_identifier(string_value: &str) -> bool {
3550-
if string_value.len() == 0 { return false }
3555+
if string_value.len() == 0 {
3556+
return false;
3557+
}
35513558
for (i, c) in string_value.chars().enumerate() {
35523559
if (i == 0 && !c.is_ident_start()) || !c.is_ident_part() {
3553-
return false
3560+
return false;
35543561
}
3555-
};
3562+
}
35563563
true
35573564
}
35583565

src/generation/sorting/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
mod module_specifiers;
22
use module_specifiers::*;
33

4-
use deno_ast::swc::common::{Span, Spanned};
4+
use deno_ast::swc::common::Span;
5+
use deno_ast::swc::common::Spanned;
56
use deno_ast::view::*;
67
use std::cmp::Ordering;
78

src/swc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use anyhow::anyhow;
22
use anyhow::bail;
33
use anyhow::Result;
44
use deno_ast::swc::parser::error::SyntaxError;
5-
use deno_ast::{Diagnostic, ParsedSource, SourceTextInfo};
5+
use deno_ast::Diagnostic;
6+
use deno_ast::ParsedSource;
7+
use deno_ast::SourceTextInfo;
68
use std::path::Path;
79

810
pub fn parse_swc_ast(file_path: &Path, file_text: &str) -> Result<ParsedSource> {

src/utils/char_iterator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::{iter::Peekable, str::Chars};
1+
use std::iter::Peekable;
2+
use std::str::Chars;
23

34
pub trait IteratorCharExt: Iterator<Item = char> {
45
fn peek(&mut self) -> Option<&char>;

src/utils/file_text_has_ignore_comment.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::utils::char_iterator::IteratorCharExt;
2-
use std::{
3-
iter::{Iterator, Peekable},
4-
str::Chars,
5-
};
2+
use std::iter::Iterator;
3+
use std::iter::Peekable;
4+
use std::str::Chars;
65

76
pub fn file_text_has_ignore_comment(file_text: &str, ignore_text: &str) -> bool {
87
let mut chars = file_text.chars().peekable();

src/wasm_plugin.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use anyhow::Result;
2-
use dprint_core::configuration::{ConfigKeyMap, GlobalConfiguration, ResolveConfigurationResult};
2+
use dprint_core::configuration::ConfigKeyMap;
3+
use dprint_core::configuration::GlobalConfiguration;
4+
use dprint_core::configuration::ResolveConfigurationResult;
35
use dprint_core::generate_plugin_code;
4-
use dprint_core::plugins::{PluginHandler, PluginInfo};
6+
use dprint_core::plugins::PluginHandler;
7+
use dprint_core::plugins::PluginInfo;
58
use std::path::Path;
69

7-
use super::configuration::{resolve_config, Configuration};
10+
use super::configuration::resolve_config;
11+
use super::configuration::Configuration;
812

913
struct TypeScriptPluginHandler {}
1014

0 commit comments

Comments
 (0)