This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 664
/
language.rs
239 lines (208 loc) · 7.58 KB
/
language.rs
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
use rome_formatter::{FormatContext, FormatResult, Formatted, IndentStyle, LineWidth, Printed};
use rome_formatter_test::TestFormatLanguage;
use rome_js_formatter::context::trailing_comma::TrailingComma;
use rome_js_formatter::context::{
ArrowParentheses, JsFormatContext, JsFormatOptions, QuoteProperties, QuoteStyle, Semicolons,
};
use rome_js_formatter::{format_node, format_range, JsFormatLanguage};
use rome_js_parser::{parse, JsParserOptions};
use rome_js_syntax::{JsFileSource, JsLanguage};
use rome_parser::AnyParse;
use rome_rowan::{FileSource, SyntaxNode};
use rome_text_size::TextRange;
use serde::{Deserialize, Serialize};
pub struct JsTestFormatLanguage {
source_type: JsFileSource,
}
impl JsTestFormatLanguage {
pub fn new(source_type: JsFileSource) -> Self {
JsTestFormatLanguage { source_type }
}
}
impl TestFormatLanguage for JsTestFormatLanguage {
type SyntaxLanguage = JsLanguage;
type Options = JsFormatOptions;
type Context = JsFormatContext;
type FormatLanguage = JsFormatLanguage;
fn parse(&self, text: &str) -> AnyParse {
let parse = parse(
text,
self.source_type,
JsParserOptions::default().with_parse_class_parameter_decorators(),
);
AnyParse::new(
parse.syntax().as_send().unwrap(),
parse.into_diagnostics(),
self.source_type.as_any_file_source(),
)
}
fn deserialize_format_options(
&self,
options: &str,
) -> Vec<<Self::Context as FormatContext>::Options> {
let test_options: TestOptions = serde_json::from_str(options).unwrap();
test_options
.cases
.into_iter()
.map(|case| case.into_format_options(self.source_type))
.collect()
}
fn format_node(
&self,
options: Self::Options,
node: &SyntaxNode<Self::SyntaxLanguage>,
) -> FormatResult<Formatted<Self::Context>> {
format_node(options, node)
}
fn format_range(
&self,
options: Self::Options,
node: &SyntaxNode<Self::SyntaxLanguage>,
range: TextRange,
) -> FormatResult<Printed> {
format_range(options, node, range)
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum JsSerializableIndentStyle {
/// Tab
Tab,
/// Space, with its quantity
Space(u8),
}
impl From<JsSerializableIndentStyle> for IndentStyle {
fn from(test: JsSerializableIndentStyle) -> Self {
match test {
JsSerializableIndentStyle::Tab => IndentStyle::Tab,
JsSerializableIndentStyle::Space(spaces) => IndentStyle::Space(spaces),
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum JsSerializableQuoteStyle {
Double,
Single,
}
impl From<JsSerializableQuoteStyle> for QuoteStyle {
fn from(test: JsSerializableQuoteStyle) -> Self {
match test {
JsSerializableQuoteStyle::Double => QuoteStyle::Double,
JsSerializableQuoteStyle::Single => QuoteStyle::Single,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum JsSerializableQuoteProperties {
AsNeeded,
Preserve,
}
impl From<JsSerializableQuoteProperties> for QuoteProperties {
fn from(test: JsSerializableQuoteProperties) -> Self {
match test {
JsSerializableQuoteProperties::AsNeeded => QuoteProperties::AsNeeded,
JsSerializableQuoteProperties::Preserve => QuoteProperties::Preserve,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum JsSerializableTrailingComma {
All,
ES5,
None,
}
impl From<JsSerializableTrailingComma> for TrailingComma {
fn from(test: JsSerializableTrailingComma) -> Self {
match test {
JsSerializableTrailingComma::All => TrailingComma::All,
JsSerializableTrailingComma::ES5 => TrailingComma::Es5,
JsSerializableTrailingComma::None => TrailingComma::None,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum JsSerializableSemicolons {
Always,
AsNeeded,
}
impl From<JsSerializableSemicolons> for Semicolons {
fn from(test: JsSerializableSemicolons) -> Self {
match test {
JsSerializableSemicolons::Always => Semicolons::Always,
JsSerializableSemicolons::AsNeeded => Semicolons::AsNeeded,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum JsSerializableArrowParentheses {
Always,
AsNeeded,
}
impl From<JsSerializableArrowParentheses> for ArrowParentheses {
fn from(test: JsSerializableArrowParentheses) -> Self {
match test {
JsSerializableArrowParentheses::Always => ArrowParentheses::Always,
JsSerializableArrowParentheses::AsNeeded => ArrowParentheses::AsNeeded,
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
pub struct JsSerializableFormatOptions {
/// The indent style.
pub indent_style: Option<JsSerializableIndentStyle>,
/// What's the max width of a line. Defaults to 80.
pub line_width: Option<u16>,
/// The style for quotes. Defaults to double.
pub quote_style: Option<JsSerializableQuoteStyle>,
/// The style for JSX quotes. Defaults to double.
pub jsx_quote_style: Option<JsSerializableQuoteStyle>,
/// When properties in objects are quoted. Defaults to as-needed.
pub quote_properties: Option<JsSerializableQuoteProperties>,
/// Print trailing commas wherever possible in multi-line comma-separated syntactic structures. Defaults to "all".
pub trailing_comma: Option<JsSerializableTrailingComma>,
/// Whether the formatter prints semicolons for all statements or only in for statements where it is necessary because of ASI.
pub semicolons: Option<JsSerializableSemicolons>,
/// Whether to add non-necessary parentheses to arrow functions. Defaults to "always".
pub arrow_parentheses: Option<JsSerializableArrowParentheses>,
}
impl JsSerializableFormatOptions {
fn into_format_options(self, source_type: JsFileSource) -> JsFormatOptions {
JsFormatOptions::new(source_type)
.with_indent_style(
self.indent_style
.map_or_else(|| IndentStyle::Tab, |value| value.into()),
)
.with_line_width(
self.line_width
.and_then(|width| LineWidth::try_from(width).ok())
.unwrap_or_default(),
)
.with_jsx_quote_style(
self.jsx_quote_style
.map_or_else(|| QuoteStyle::Double, |value| value.into()),
)
.with_quote_style(
self.quote_style
.map_or_else(|| QuoteStyle::Double, |value| value.into()),
)
.with_quote_properties(
self.quote_properties
.map_or_else(|| QuoteProperties::AsNeeded, |value| value.into()),
)
.with_trailing_comma(
self.trailing_comma
.map_or_else(|| TrailingComma::All, |value| value.into()),
)
.with_semicolons(
self.semicolons
.map_or_else(|| Semicolons::Always, |value| value.into()),
)
.with_arrow_parentheses(
self.arrow_parentheses
.map_or_else(|| ArrowParentheses::Always, |value| value.into()),
)
}
}
#[derive(Debug, Deserialize, Serialize)]
struct TestOptions {
cases: Vec<JsSerializableFormatOptions>,
}