Skip to content

Commit 572eb3d

Browse files
committed
refactor(prettier-plugin-toml): reduce buildTombiConfig complexity
Extract the derived option resolution and the rule collection into small helpers so the Codacy complexity and function-length checks pass.
1 parent ffb3f1f commit 572eb3d

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

‎packages/toml/src/config.ts‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ const serializeValue = (value: Exclude<TombiRuleValue, undefined>) =>
1818
const toKebabCase = (value: string) =>
1919
value.replaceAll(/[A-Z]/g, char => `-${char.toLowerCase()}`)
2020

21-
/**
22-
* Build a virtual `tombi.toml` configuration from the resolved Prettier
23-
* options. Prettier's own `printWidth`, `tabWidth`, `useTabs`, `singleQuote`
24-
* and `bracketSpacing` options are mapped to their Tombi counterparts.
25-
*/
26-
export function buildTombiConfig(options: PrettierOptions): string {
27-
const rules: Record<string, TombiRuleValue> = {
21+
/** Resolve the string quote style, inheriting Prettier's `singleQuote`. */
22+
const resolveStringQuoteStyle = (options: PrettierOptions) =>
23+
options.stringQuoteStyle ?? (options.singleQuote ? 'single' : 'double')
24+
25+
/** Resolve inline table spacing, inheriting Prettier's `bracketSpacing`. */
26+
const resolveInlineTableBraceSpaceWidth = (options: PrettierOptions) =>
27+
options.inlineTableBraceSpaceWidth ?? (options.bracketSpacing ? 1 : 0)
28+
29+
/** Resolve the line width, inheriting Prettier's `printWidth`. */
30+
const resolveLineWidth = (options: PrettierOptions) =>
31+
Number.isFinite(options.printWidth) ? options.printWidth : undefined
32+
33+
/** Collect the Tombi `[format.rules]` entries from the Prettier options. */
34+
function getRules(options: PrettierOptions): Record<string, TombiRuleValue> {
35+
return {
2836
arrayBracketSpaceWidth: options.arrayBracketSpaceWidth,
2937
arrayCommaSpaceWidth: options.arrayCommaSpaceWidth,
3038
commentStyle: options.commentStyle,
@@ -34,21 +42,26 @@ export function buildTombiConfig(options: PrettierOptions): string {
3442
indentSubTables: options.indentSubTables,
3543
indentTableKeyValuePairs: options.indentTableKeyValuePairs,
3644
indentWidth: options.tabWidth,
37-
inlineTableBraceSpaceWidth:
38-
options.inlineTableBraceSpaceWidth ?? (options.bracketSpacing ? 1 : 0),
45+
inlineTableBraceSpaceWidth: resolveInlineTableBraceSpaceWidth(options),
3946
inlineTableCommaSpaceWidth: options.inlineTableCommaSpaceWidth,
4047
keyValueEqualsSignAlignment: options.keyValueEqualsSignAlignment,
4148
keyQuoteStyle: options.keyQuoteStyle,
4249
keyValueEqualsSignSpaceWidth: options.keyValueEqualsSignSpaceWidth,
43-
lineWidth: Number.isFinite(options.printWidth)
44-
? options.printWidth
45-
: undefined,
46-
stringQuoteStyle:
47-
options.stringQuoteStyle ?? (options.singleQuote ? 'single' : 'double'),
50+
lineWidth: resolveLineWidth(options),
51+
stringQuoteStyle: resolveStringQuoteStyle(options),
4852
tableBlankLines: options.tableBlankLines,
4953
trailingCommentAlignment: options.trailingCommentAlignment,
5054
trailingCommentSpaceWidth: options.trailingCommentSpaceWidth,
5155
}
56+
}
57+
58+
/**
59+
* Build a virtual `tombi.toml` configuration from the resolved Prettier
60+
* options. Prettier's own `printWidth`, `tabWidth`, `useTabs`, `singleQuote`
61+
* and `bracketSpacing` options are mapped to their Tombi counterparts.
62+
*/
63+
export function buildTombiConfig(options: PrettierOptions): string {
64+
const rules = getRules(options)
5265

5366
return [
5467
`toml-version = ${serializeValue(options.tomlVersion ?? 'v1.0.0')}`,

0 commit comments

Comments
 (0)