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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use serde::{Serialize, Deserialize};
use dprint_core::generate_str_to_from;
use dprint_core::configuration::*;

/// Semi colon possibilities.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SemiColons {
    /// Always uses semi-colons where applicable.
    Always,
    /// Prefers to use semi-colons, but doesn't add one in certain scenarios
    /// such as for the last member of a single-line type literal.
    Prefer,
    /// Uses automatic semi-colon insertion. Only adds a semi-colon at the start
    /// of some expression statements when necessary.
    Asi,
}

impl SemiColons {
    /// Gets if this option is "Always" or "Prefer".
    pub(crate) fn is_true(&self) -> bool {
        match self {
            SemiColons::Always | SemiColons::Prefer => true,
            SemiColons::Asi => false,
        }
    }
}

generate_str_to_from![
    SemiColons,
    [Always, "always"],
    [Prefer, "prefer"],
    [Asi, "asi"]
];

/// Trailing comma possibilities.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TrailingCommas {
    /// Trailing commas should not be used.
    Never,
    /// Trailing commas should always be used.
    Always,
    /// Trailing commas should only be used in multi-line scenarios.
    OnlyMultiLine,
}

generate_str_to_from![
    TrailingCommas,
    [Always, "always"],
    [Never, "never"],
    [OnlyMultiLine, "onlyMultiLine"]
];

/// Where to place the opening brace.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum BracePosition {
    /// Maintains the brace being on the next line or the same line.
    Maintain,
    /// Forces the brace to be on the same line.
    SameLine,
    /// Forces the brace to be on the next line.
    NextLine,
    /// Forces the brace to be on the next line if the same line is hanging, but otherwise uses the same line.
    SameLineUnlessHanging,
}

generate_str_to_from![
    BracePosition,
    [Maintain, "maintain"],
    [SameLine, "sameLine"],
    [NextLine, "nextLine"],
    [SameLineUnlessHanging, "sameLineUnlessHanging"]
];

/// How to space members.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum MemberSpacing {
    /// Maintains whether a newline or blankline is used.
    Maintain,
    /// Forces a new line between members.
    #[serde(rename = "newLine")]
    NewLine,
    /// Forces a blank line between members.
    #[serde(rename = "blankLine")]
    BlankLine,
}

generate_str_to_from![
    MemberSpacing,
    [Maintain, "maintain"],
    [BlankLine, "blankLine"],
    [NewLine, "newLine"]
];

/// Where to place the next control flow within a control flow statement.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum NextControlFlowPosition {
    /// Maintains the next control flow being on the next line or the same line.
    Maintain,
    /// Forces the next control flow to be on the same line.
    SameLine,
    /// Forces the next control flow to be on the next line.
    NextLine,
}

generate_str_to_from![
    NextControlFlowPosition,
    [Maintain, "maintain"],
    [SameLine, "sameLine"],
    [NextLine, "nextLine"]
];

/// Where to place the operator for expressions that span multiple lines.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum OperatorPosition {
    /// Maintains the operator being on the next line or the same line.
    Maintain,
    /// Forces the operator to be on the same line.
    SameLine,
    /// Forces the operator to be on the next line.
    NextLine,
}

generate_str_to_from![
    OperatorPosition,
    [Maintain, "maintain"],
    [SameLine, "sameLine"],
    [NextLine, "nextLine"]
];

/// Where to place the expression of a statement that could possibly be on one line (ex. `if (true) console.log(5);`).
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SingleBodyPosition {
    /// Maintains the position of the expression.
    Maintain,
    /// Forces the whole statement to be on one line.
    SameLine,
    /// Forces the expression to be on the next line.
    NextLine,
}

generate_str_to_from![
    SingleBodyPosition,
    [Maintain, "maintain"],
    [SameLine, "sameLine"],
    [NextLine, "nextLine"]
];

/// If braces should be used or not in certain scenarios.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum UseBraces {
    /// Uses braces if they're used. Doesn't use braces if they're not used.
    Maintain,
    /// Uses braces when the body is on a different line.
    WhenNotSingleLine,
    /// Forces the use of braces. Will add them if they aren't used.
    Always,
    /// Forces no braces when when the header is one line and body is one line. Otherwise forces braces.
    PreferNone,
}

generate_str_to_from![
    UseBraces,
    [Maintain, "maintain"],
    [WhenNotSingleLine, "whenNotSingleLine"],
    [Always, "always"],
    [PreferNone, "preferNone"]
];

/// Whether to use parentheses around a single parameter in an arrow function.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum UseParentheses {
    /// Maintains the current state of the parentheses.
    Maintain,
    /// Forces parentheses.
    Force,
    /// Prefers not using parentheses when possible.
    PreferNone,
}

generate_str_to_from![
    UseParentheses,
    [Maintain, "maintain"],
    [Force, "force"],
    [PreferNone, "preferNone"]
];

/// How to decide to use single or double quotes.
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum QuoteStyle {
    /// Always use double quotes.
    AlwaysDouble,
    /// Always use single quotes.
    AlwaysSingle,
    /// Prefer using double quotes except in scenarios where the string
    /// contains more double quotes than single quotes.
    PreferDouble,
    /// Prefer using single quotes except in scenarios where the string
    /// contains more single quotes than double quotes.
    PreferSingle,
}

generate_str_to_from![
    QuoteStyle,
    [AlwaysDouble, "alwaysDouble"],
    [AlwaysSingle, "alwaysSingle"],
    [PreferDouble, "preferDouble"],
    [PreferSingle, "preferSingle"]
];

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Configuration {
    pub indent_width: u8,
    pub line_width: u32,
    pub use_tabs: bool,
    pub new_line_kind: NewLineKind,
    pub quote_style: QuoteStyle,
    pub semi_colons: SemiColons,
    /* situational */
    #[serde(rename = "arrowFunction.useParentheses")]
    pub arrow_function_use_parentheses: UseParentheses,
    #[serde(rename = "binaryExpression.linePerExpression")]
    pub binary_expression_line_per_expression: bool,
    #[serde(rename = "memberExpression.linePerExpression")]
    pub member_expression_line_per_expression: bool,
    /* ignore comments */
    pub ignore_node_comment_text: String,
    pub ignore_file_comment_text: String,
    /* brace position */
    #[serde(rename = "arrowFunction.bracePosition")]
    pub arrow_function_brace_position: BracePosition,
    #[serde(rename = "classDeclaration.bracePosition")]
    pub class_declaration_brace_position: BracePosition,
    #[serde(rename = "classExpression.bracePosition")]
    pub class_expression_brace_position: BracePosition,
    #[serde(rename = "constructor.bracePosition")]
    pub constructor_brace_position: BracePosition,
    #[serde(rename = "doWhileStatement.bracePosition")]
    pub do_while_statement_brace_position: BracePosition,
    #[serde(rename = "enumDeclaration.bracePosition")]
    pub enum_declaration_brace_position: BracePosition,
    #[serde(rename = "getAccessor.bracePosition")]
    pub get_accessor_brace_position: BracePosition,
    #[serde(rename = "ifStatement.bracePosition")]
    pub if_statement_brace_position: BracePosition,
    #[serde(rename = "interfaceDeclaration.bracePosition")]
    pub interface_declaration_brace_position: BracePosition,
    #[serde(rename = "forStatement.bracePosition")]
    pub for_statement_brace_position: BracePosition,
    #[serde(rename = "forInStatement.bracePosition")]
    pub for_in_statement_brace_position: BracePosition,
    #[serde(rename = "forOfStatement.bracePosition")]
    pub for_of_statement_brace_position: BracePosition,
    #[serde(rename = "functionDeclaration.bracePosition")]
    pub function_declaration_brace_position: BracePosition,
    #[serde(rename = "functionExpression.bracePosition")]
    pub function_expression_brace_position: BracePosition,
    #[serde(rename = "method.bracePosition")]
    pub method_brace_position: BracePosition,
    #[serde(rename = "moduleDeclaration.bracePosition")]
    pub module_declaration_brace_position: BracePosition,
    #[serde(rename = "setAccessor.bracePosition")]
    pub set_accessor_brace_position: BracePosition,
    #[serde(rename = "switchCase.bracePosition")]
    pub switch_case_brace_position: BracePosition,
    #[serde(rename = "switchStatement.bracePosition")]
    pub switch_statement_brace_position: BracePosition,
    #[serde(rename = "tryStatement.bracePosition")]
    pub try_statement_brace_position: BracePosition,
    #[serde(rename = "whileStatement.bracePosition")]
    pub while_statement_brace_position: BracePosition,
    /* prefer hanging */
    #[serde(rename = "arguments.preferHanging")]
    pub arguments_prefer_hanging: bool,
    #[serde(rename = "arrayExpression.preferHanging")]
    pub array_expression_prefer_hanging: bool,
    #[serde(rename = "arrayPattern.preferHanging")]
    pub array_pattern_prefer_hanging: bool,
    #[serde(rename = "doWhileStatement.preferHanging")]
    pub do_while_statement_prefer_hanging: bool,
    #[serde(rename = "exportDeclaration.preferHanging")]
    pub export_declaration_prefer_hanging: bool,
    #[serde(rename = "extendsClause.preferHanging")]
    pub extends_clause_prefer_hanging: bool,
    #[serde(rename = "forStatement.preferHanging")]
    pub for_statement_prefer_hanging: bool,
    #[serde(rename = "forInStatement.preferHanging")]
    pub for_in_statement_prefer_hanging: bool,
    #[serde(rename = "forOfStatement.preferHanging")]
    pub for_of_statement_prefer_hanging: bool,
    #[serde(rename = "ifStatement.preferHanging")]
    pub if_statement_prefer_hanging: bool,
    #[serde(rename = "implementsClause.preferHanging")]
    pub implements_clause_prefer_hanging: bool,
    #[serde(rename = "importDeclaration.preferHanging")]
    pub import_declaration_prefer_hanging: bool,
    #[serde(rename = "jsxAttributes.preferHanging")]
    pub jsx_attributes_prefer_hanging: bool,
    #[serde(rename = "objectExpression.preferHanging")]
    pub object_expression_prefer_hanging: bool,
    #[serde(rename = "objectPattern.preferHanging")]
    pub object_pattern_prefer_hanging: bool,
    #[serde(rename = "parameters.preferHanging")]
    pub parameters_prefer_hanging: bool,
    #[serde(rename = "sequenceExpression.preferHanging")]
    pub sequence_expression_prefer_hanging: bool,
    #[serde(rename = "switchStatement.preferHanging")]
    pub switch_statement_prefer_hanging: bool,
    #[serde(rename = "tupleType.preferHanging")]
    pub tuple_type_prefer_hanging: bool,
    #[serde(rename = "typeLiteral.preferHanging")]
    pub type_literal_prefer_hanging: bool,
    #[serde(rename = "typeParameters.preferHanging")]
    pub type_parameters_prefer_hanging: bool,
    #[serde(rename = "unionAndIntersectionType.preferHanging")]
    pub union_and_intersection_type_prefer_hanging: bool,
    #[serde(rename = "variableStatement.preferHanging")]
    pub variable_statement_prefer_hanging: bool,
    #[serde(rename = "whileStatement.preferHanging")]
    pub while_statement_prefer_hanging: bool,
    /* member spacing */
    #[serde(rename = "enumDeclaration.memberSpacing")]
    pub enum_declaration_member_spacing: MemberSpacing,
    /* next control flow position */
    #[serde(rename = "ifStatement.nextControlFlowPosition")]
    pub if_statement_next_control_flow_position: NextControlFlowPosition,
    #[serde(rename = "tryStatement.nextControlFlowPosition")]
    pub try_statement_next_control_flow_position: NextControlFlowPosition,
    /* operator position */
    #[serde(rename = "binaryExpression.operatorPosition")]
    pub binary_expression_operator_position: OperatorPosition,
    #[serde(rename = "conditionalExpression.operatorPosition")]
    pub conditional_expression_operator_position: OperatorPosition,
    /* single body position */
    #[serde(rename = "ifStatement.singleBodyPosition")]
    pub if_statement_single_body_position: SingleBodyPosition,
    #[serde(rename = "forStatement.singleBodyPosition")]
    pub for_statement_single_body_position: SingleBodyPosition,
    #[serde(rename = "forInStatement.singleBodyPosition")]
    pub for_in_statement_single_body_position: SingleBodyPosition,
    #[serde(rename = "forOfStatement.singleBodyPosition")]
    pub for_of_statement_single_body_position: SingleBodyPosition,
    #[serde(rename = "whileStatement.singleBodyPosition")]
    pub while_statement_single_body_position: SingleBodyPosition,
    /* trailing commas */
    #[serde(rename = "arguments.trailingCommas")]
    pub arguments_trailing_commas: TrailingCommas,
    #[serde(rename = "parameters.trailingCommas")]
    pub parameters_trailing_commas: TrailingCommas,
    #[serde(rename = "arrayExpression.trailingCommas")]
    pub array_expression_trailing_commas: TrailingCommas,
    #[serde(rename = "arrayPattern.trailingCommas")]
    pub array_pattern_trailing_commas: TrailingCommas,
    #[serde(rename = "enumDeclaration.trailingCommas")]
    pub enum_declaration_trailing_commas: TrailingCommas,
    #[serde(rename = "exportDeclaration.trailingCommas")]
    pub export_declaration_trailing_commas: TrailingCommas,
    #[serde(rename = "importDeclaration.trailingCommas")]
    pub import_declaration_trailing_commas: TrailingCommas,
    #[serde(rename = "objectPattern.trailingCommas")]
    pub object_pattern_trailing_commas: TrailingCommas,
    #[serde(rename = "objectExpression.trailingCommas")]
    pub object_expression_trailing_commas: TrailingCommas,
    #[serde(rename = "tupleType.trailingCommas")]
    pub tuple_type_trailing_commas: TrailingCommas,
    #[serde(rename = "typeParameters.trailingCommas")]
    pub type_parameters_trailing_commas: TrailingCommas,
    /* use braces */
    #[serde(rename = "ifStatement.useBraces")]
    pub if_statement_use_braces: UseBraces,
    #[serde(rename = "forStatement.useBraces")]
    pub for_statement_use_braces: UseBraces,
    #[serde(rename = "forOfStatement.useBraces")]
    pub for_of_statement_use_braces: UseBraces,
    #[serde(rename = "forInStatement.useBraces")]
    pub for_in_statement_use_braces: UseBraces,
    #[serde(rename = "whileStatement.useBraces")]
    pub while_statement_use_braces: UseBraces,
    /* prefer single line */
    #[serde(rename = "arrayExpression.preferSingleLine")]
    pub array_expression_prefer_single_line: bool,
    #[serde(rename = "arrayPattern.preferSingleLine")]
    pub array_pattern_prefer_single_line: bool,
    #[serde(rename = "arguments.preferSingleLine")]
    pub arguments_prefer_single_line: bool,
    #[serde(rename = "binaryExpression.preferSingleLine")]
    pub binary_expression_prefer_single_line: bool,
    #[serde(rename = "computed.preferSingleLine")]
    pub computed_prefer_single_line: bool,
    #[serde(rename = "conditionalExpression.preferSingleLine")]
    pub conditional_expression_prefer_single_line: bool,
    #[serde(rename = "conditionalType.preferSingleLine")]
    pub conditional_type_prefer_single_line: bool,
    #[serde(rename = "decorators.preferSingleLine")]
    pub decorators_prefer_single_line: bool,
    #[serde(rename = "exportDeclaration.preferSingleLine")]
    pub export_declaration_prefer_single_line: bool,
    #[serde(rename = "forStatement.preferSingleLine")]
    pub for_statement_prefer_single_line: bool,
    #[serde(rename = "importDeclaration.preferSingleLine")]
    pub import_declaration_prefer_single_line: bool,
    #[serde(rename = "jsxAttributes.preferSingleLine")]
    pub jsx_attributes_prefer_single_line: bool,
    #[serde(rename = "jsxElement.preferSingleLine")]
    pub jsx_element_prefer_single_line: bool,
    #[serde(rename = "mappedType.preferSingleLine")]
    pub mapped_type_prefer_single_line: bool,
    #[serde(rename = "memberExpression.preferSingleLine")]
    pub member_expression_prefer_single_line: bool,
    #[serde(rename = "objectExpression.preferSingleLine")]
    pub object_expression_prefer_single_line: bool,
    #[serde(rename = "objectPattern.preferSingleLine")]
    pub object_pattern_prefer_single_line: bool,
    #[serde(rename = "parameters.preferSingleLine")]
    pub parameters_prefer_single_line: bool,
    #[serde(rename = "parentheses.preferSingleLine")]
    pub parentheses_prefer_single_line: bool,
    #[serde(rename = "tupleType.preferSingleLine")]
    pub tuple_type_prefer_single_line: bool,
    #[serde(rename = "typeLiteral.preferSingleLine")]
    pub type_literal_prefer_single_line: bool,
    #[serde(rename = "typeParameters.preferSingleLine")]
    pub type_parameters_prefer_single_line: bool,
    #[serde(rename = "unionAndIntersectionType.preferSingleLine")]
    pub union_and_intersection_type_prefer_single_line: bool,
    #[serde(rename = "variableStatement.preferSingleLine")]
    pub variable_statement_prefer_single_line: bool,

    /* use space separator */

    #[serde(rename = "binaryExpression.spaceSurroundingBitwiseAndArithmeticOperator")]
    pub binary_expression_space_surrounding_bitwise_and_arithmetic_operator: bool,
    #[serde(rename = "commentLine.forceSpaceAfterSlashes")]
    pub comment_line_force_space_after_slashes: bool,
    #[serde(rename = "constructSignature.spaceAfterNewKeyword")]
    pub construct_signature_space_after_new_keyword: bool,
    #[serde(rename = "constructor.spaceBeforeParentheses")]
    pub constructor_space_before_parentheses: bool,
    #[serde(rename = "constructorType.spaceAfterNewKeyword")]
    pub constructor_type_space_after_new_keyword: bool,
    #[serde(rename = "doWhileStatement.spaceAfterWhileKeyword")]
    pub do_while_statement_space_after_while_keyword: bool,
    #[serde(rename = "exportDeclaration.spaceSurroundingNamedExports")]
    pub export_declaration_space_surrounding_named_exports: bool,
    #[serde(rename = "forStatement.spaceAfterForKeyword")]
    pub for_statement_space_after_for_keyword: bool,
    #[serde(rename = "forStatement.spaceAfterSemiColons")]
    pub for_statement_space_after_semi_colons: bool,
    #[serde(rename = "forInStatement.spaceAfterForKeyword")]
    pub for_in_statement_space_after_for_keyword: bool,
    #[serde(rename = "forOfStatement.spaceAfterForKeyword")]
    pub for_of_statement_space_after_for_keyword: bool,
    #[serde(rename = "functionDeclaration.spaceBeforeParentheses")]
    pub function_declaration_space_before_parentheses: bool,
    #[serde(rename = "functionExpression.spaceBeforeParentheses")]
    pub function_expression_space_before_parentheses: bool,
    #[serde(rename = "functionExpression.spaceAfterFunctionKeyword")]
    pub function_expression_space_after_function_keyword: bool,
    #[serde(rename = "getAccessor.spaceBeforeParentheses")]
    pub get_accessor_space_before_parentheses: bool,
    #[serde(rename = "ifStatement.spaceAfterIfKeyword")]
    pub if_statement_space_after_if_keyword: bool,
    #[serde(rename = "importDeclaration.spaceSurroundingNamedImports")]
    pub import_declaration_space_surrounding_named_imports: bool,
    #[serde(rename = "jsxExpressionContainer.spaceSurroundingExpression")]
    pub jsx_expression_container_space_surrounding_expression: bool,
    #[serde(rename = "method.spaceBeforeParentheses")]
    pub method_space_before_parentheses: bool,
    #[serde(rename = "setAccessor.spaceBeforeParentheses")]
    pub set_accessor_space_before_parentheses: bool,
    #[serde(rename = "taggedTemplate.spaceBeforeLiteral")]
    pub tagged_template_space_before_literal: bool,
    #[serde(rename = "typeAnnotation.spaceBeforeColon")]
    pub type_annotation_space_before_colon: bool,
    #[serde(rename = "typeAssertion.spaceBeforeExpression")]
    pub type_assertion_space_before_expression: bool,
    #[serde(rename = "whileStatement.spaceAfterWhileKeyword")]
    pub while_statement_space_after_while_keyword: bool,
}