Skip to content

Commit dff43af

Browse files
committed
refactor(prettier-plugin-toml): use camelCase option keys in js
Keep the internal tombi config builder using JavaScript camelCase option keys and convert them to tombi's required kebab-case only when serializing the virtual `tombi.toml`. Also document the Node.js v18 requirement and add a test covering the camelCase to kebab-case conversion.
1 parent b476469 commit dff43af

3 files changed

Lines changed: 59 additions & 23 deletions

File tree

‎packages/toml/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Of course it should just work, but may not match [prettier][]'s format sometimes
1313

1414
## Requirements
1515

16-
`prettier-plugin-toml` is an evergreen module. 🌲 This module requires an [LTS](https://github.com/nodejs/Release) Node version (v16.0.0+).
16+
`prettier-plugin-toml` is an evergreen module. 🌲 This module requires an [LTS](https://github.com/nodejs/Release) Node version (v18.0.0+).
1717

1818
## Install
1919

‎packages/toml/src/config.ts‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,43 @@ type TombiRuleValue = boolean | number | string | undefined
1111
const serializeValue = (value: Exclude<TombiRuleValue, undefined>) =>
1212
JSON.stringify(value)
1313

14+
/**
15+
* Tombi's configuration is `kebab-case`, while the options are plain
16+
* JavaScript `camelCase` identifiers.
17+
*/
18+
const toKebabCase = (value: string) =>
19+
value.replaceAll(/[A-Z]/g, char => `-${char.toLowerCase()}`)
20+
1421
/**
1522
* Build a virtual `tombi.toml` configuration from the resolved Prettier
16-
* options. Prettier's own `printWidth`, `tabWidth` and `useTabs` options are
17-
* mapped to their Tombi counterparts.
23+
* options. Prettier's own `printWidth`, `tabWidth`, `useTabs`, `singleQuote`
24+
* and `bracketSpacing` options are mapped to their Tombi counterparts.
1825
*/
1926
export function buildTombiConfig(options: PrettierOptions): string {
2027
const rules: Record<string, TombiRuleValue> = {
21-
'array-bracket-space-width': options.arrayBracketSpaceWidth,
22-
'array-comma-space-width': options.arrayCommaSpaceWidth,
23-
'comment-style': options.commentStyle,
24-
'date-time-delimiter': options.dateTimeDelimiter,
25-
'group-blank-lines-limit': options.groupBlankLinesLimit,
26-
'indent-style': options.useTabs ? 'tab' : 'space',
27-
'indent-sub-tables': options.indentSubTables,
28-
'indent-table-key-value-pairs': options.indentTableKeyValuePairs,
29-
'indent-width': options.tabWidth,
30-
'inline-table-brace-space-width':
28+
arrayBracketSpaceWidth: options.arrayBracketSpaceWidth,
29+
arrayCommaSpaceWidth: options.arrayCommaSpaceWidth,
30+
commentStyle: options.commentStyle,
31+
dateTimeDelimiter: options.dateTimeDelimiter,
32+
groupBlankLinesLimit: options.groupBlankLinesLimit,
33+
indentStyle: options.useTabs ? 'tab' : 'space',
34+
indentSubTables: options.indentSubTables,
35+
indentTableKeyValuePairs: options.indentTableKeyValuePairs,
36+
indentWidth: options.tabWidth,
37+
inlineTableBraceSpaceWidth:
3138
options.inlineTableBraceSpaceWidth ?? (options.bracketSpacing ? 1 : 0),
32-
'inline-table-comma-space-width': options.inlineTableCommaSpaceWidth,
33-
'key-value-equals-sign-alignment': options.keyValueEqualsSignAlignment,
34-
'key-quote-style': options.keyQuoteStyle,
35-
'key-value-equals-sign-space-width': options.keyValueEqualsSignSpaceWidth,
36-
'line-width': Number.isFinite(options.printWidth)
39+
inlineTableCommaSpaceWidth: options.inlineTableCommaSpaceWidth,
40+
keyValueEqualsSignAlignment: options.keyValueEqualsSignAlignment,
41+
keyQuoteStyle: options.keyQuoteStyle,
42+
keyValueEqualsSignSpaceWidth: options.keyValueEqualsSignSpaceWidth,
43+
lineWidth: Number.isFinite(options.printWidth)
3744
? options.printWidth
3845
: undefined,
39-
'string-quote-style':
46+
stringQuoteStyle:
4047
options.stringQuoteStyle ?? (options.singleQuote ? 'single' : 'double'),
41-
'table-blank-lines': options.tableBlankLines,
42-
'trailing-comment-alignment': options.trailingCommentAlignment,
43-
'trailing-comment-space-width': options.trailingCommentSpaceWidth,
48+
tableBlankLines: options.tableBlankLines,
49+
trailingCommentAlignment: options.trailingCommentAlignment,
50+
trailingCommentSpaceWidth: options.trailingCommentSpaceWidth,
4451
}
4552

4653
return [
@@ -60,7 +67,7 @@ export function buildTombiConfig(options: PrettierOptions): string {
6067
(entry): entry is [string, Exclude<TombiRuleValue, undefined>] =>
6168
entry[1] != null,
6269
)
63-
.map(([key, value]) => `${key} = ${serializeValue(value)}`),
70+
.map(([key, value]) => `${toKebabCase(key)} = ${serializeValue(value)}`),
6471
'',
6572
].join('\n')
6673
}

‎packages/toml/test/config.spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { buildTombiConfig } from '../src/config.js'
2+
import type { PrettierOptions } from '../src/types.js'
3+
4+
describe('buildTombiConfig', () => {
5+
it('should serialize camelCase options to kebab-case tombi rules', () => {
6+
const config = buildTombiConfig({
7+
bracketSpacing: false,
8+
printWidth: 100,
9+
singleQuote: true,
10+
tabWidth: 4,
11+
useTabs: false,
12+
} as PrettierOptions)
13+
14+
expect(config).toContain('line-width = 100')
15+
expect(config).toContain('indent-width = 4')
16+
expect(config).toContain('indent-style = "space"')
17+
expect(config).toContain('string-quote-style = "single"')
18+
expect(config).toContain('inline-table-brace-space-width = 0')
19+
20+
const ruleKeys = config
21+
.split('[format.rules]\n')[1]
22+
.trim()
23+
.split('\n')
24+
.map(line => line.split(' = ')[0])
25+
26+
expect(ruleKeys).not.toHaveLength(0)
27+
expect(ruleKeys.every(key => key === key.toLowerCase())).toBe(true)
28+
})
29+
})

0 commit comments

Comments
 (0)