Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "quoteProps": "consistent" #544

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@
"type": "string",
"default": "preserve",
"oneOf": [{
"const": "preserve",
"description": "Preserve quotes around property names."
}, {
"const": "asNeeded",
"description": "Remove unnecessary quotes around property names."
}, {
"const": "consistent",
"description": "Same as 'asNeeded', but if one property requires quotes then quote them all."
}, {
"const": "preserve",
"description": "Preserve quotes around property names."
}]
},
"jsx.multiLineParens": {
Expand Down
9 changes: 5 additions & 4 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"exec": {
"associations": "**/*.rs",
"rustfmt": "rustfmt"
"commands": [{
"command": "rustfmt",
"exts": ["rs"]
}]
},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md,toml,rs}"],
"excludes": [
"**/node_modules",
"**/*-lock.json",
Expand All @@ -15,6 +16,6 @@
"https://plugins.dprint.dev/json-0.16.0.wasm",
"https://plugins.dprint.dev/markdown-0.14.3.wasm",
"https://plugins.dprint.dev/toml-0.5.4.wasm",
"https://plugins.dprint.dev/exec-0.3.2.json@8efbbb3fcfbdf84142c3c438fbdeaf1637152a020032127c837b2b14e23261c3"
"https://plugins.dprint.dev/exec-0.4.3.json@42343548b8022c99b1d750be6b894fe6b6c7ee25f72ae9f9082226dd2e515072"
]
}
8 changes: 5 additions & 3 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,15 @@ generate_str_to_from![JsxQuoteStyle, [PreferDouble, "preferDouble"], [PreferSing
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum QuoteProps {
/// Preserve quotes around property names.
Preserve,
/// Remove unnecessary quotes around property names.
AsNeeded,
/// Same as `AsNeeded`, but if one property requires quotes then quote them all.
Consistent,
/// Preserve quotes around property names.
Preserve,
}

generate_str_to_from![QuoteProps, [Preserve, "preserve"], [AsNeeded, "asNeeded"]];
generate_str_to_from![QuoteProps, [AsNeeded, "asNeeded"], [Consistent, "consistent"], [Preserve, "preserve"]];

/// Whether to surround a JSX element or fragment with parentheses
/// when it's the top JSX node and it spans multiple lines.
Expand Down
29 changes: 26 additions & 3 deletions src/generation/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Context<'a> {
pub token_finder: TokenFinder<'a>,
pub current_node: Node<'a>,
pub parent_stack: Stack<Node<'a>>,
/// Stores whether the parent requires all properties have consistent quoting.
consistent_quote_props_stack: Stack<bool>,
handled_comments: FxHashSet<SourcePos>,
stored_ln_ranges: FxHashMap<(SourcePos, SourcePos), (LineNumber, LineNumber)>,
stored_lsil: FxHashMap<(SourcePos, SourcePos), LineStartIndentLevel>,
Expand All @@ -49,14 +51,15 @@ impl<'a> Context<'a> {
comments: CommentTracker::new(program, tokens),
token_finder: TokenFinder::new(program),
current_node,
parent_stack: Stack::new(),
parent_stack: Default::default(),
consistent_quote_props_stack: Default::default(),
handled_comments: FxHashSet::default(),
stored_ln_ranges: FxHashMap::default(),
stored_lsil: FxHashMap::default(),
stored_ln: FxHashMap::default(),
stored_il: FxHashMap::default(),
end_statement_or_member_lns: Stack::new(),
before_comments_start_info_stack: Stack::new(),
end_statement_or_member_lns: Default::default(),
before_comments_start_info_stack: Default::default(),
if_stmt_last_brace_condition_ref: None,
expr_stmt_single_line_parent_brace_ref: None,
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -158,6 +161,26 @@ impl<'a> Context<'a> {
}
}

pub fn use_consistent_quote_props(&self) -> Option<bool> {
self.consistent_quote_props_stack.peek().copied()
}

pub fn with_maybe_consistent_props<TState, TReturn>(
&mut self,
state: TState,
use_consistent_quotes: impl FnOnce(&TState) -> bool,
action: impl FnOnce(&mut Self, TState) -> TReturn,
) -> TReturn {
if self.config.quote_props == QuoteProps::Consistent {
self.consistent_quote_props_stack.push((use_consistent_quotes)(&state));
}
let is_consistent = self.consistent_quote_props_stack.peek().copied().unwrap_or(true);
self.consistent_quote_props_stack.push(is_consistent);
let result = action(self, state);
self.consistent_quote_props_stack.pop();
result
}

// do any assertions for how the state of this context should be at the end of the file
#[cfg(debug_assertions)]
pub fn assert_end_of_file_state(&self) {
Expand Down
Loading
Loading