Skip to content

Commit df1efbc

Browse files
committed
Format shell scripts
1 parent 1c9d6f2 commit df1efbc

File tree

7 files changed

+63
-24
lines changed

7 files changed

+63
-24
lines changed

.oxlintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"plugins": [
33
"eslint",
44
"import",
5+
"jsdoc",
56
"node",
67
"oxc",
78
"promise",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Curated Prettier config for modern TypeScript projects.
88

99
- Enables the [`oxc` parser](https://oxc.rs/docs/guide/usage/parser.html) for lightning-fast TypeScript formatting.
10+
- Formats shell scripts out of the box, including `git` hooks.
1011
- Sorts all JSON files, with curated order patterns for common config files: `package.json`, `tsconfig.json`, `.oxlintrc.json`, and more.
1112

1213
## Install

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@prettier/plugin-oxc": "^0.1.3",
3636
"klona": "^2.0.6",
3737
"prettier-plugin-packagejson": "2.5.20",
38+
"prettier-plugin-sh": "^0.18.0",
3839
"prettier-plugin-sort-json": "4.1.1"
3940
},
4041
"peerDependencies": {

src/config.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import type { Config } from 'prettier';
1+
import type { Config, Options } from 'prettier';
22
import { expectTypeOf, test } from 'vitest';
3-
import BASE_CONFIG from './config.ts';
3+
import { DEFAULT_CONFIG, DEFAULT_OPTIONS } from './config.ts';
44

55
test('is a valid Prettier config', () => {
6-
expectTypeOf(BASE_CONFIG).toExtend<Config>();
6+
expectTypeOf(DEFAULT_CONFIG).toExtend<Config>();
7+
expectTypeOf(DEFAULT_OPTIONS).toExtend<Options>();
78

89
/* @ts-expect-error */
9-
BASE_CONFIG.quoteProps = 'all';
10+
DEFAULT_CONFIG.quoteProps = 'all';
11+
12+
/* @ts-expect-error */
13+
DEFAULT_OPTIONS.quoteProps = 'all';
1014
});

src/config.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,90 @@
1-
import type { Config } from 'prettier';
1+
import type { Config, Options } from 'prettier';
22
import * as pluginOxidation from '@prettier/plugin-oxc';
33
import * as pluginPackageJSON from 'prettier-plugin-packagejson';
44
import * as pluginSortJSON from 'prettier-plugin-sort-json';
5+
import * as pluginShell from 'prettier-plugin-sh';
56
import prioritizeKeys from './prioritize-keys/index.ts';
67

7-
const config = {
8+
/**
9+
* Shell files can’t be reliably identified by name alone—they’re recognized by
10+
* the shebang, not the extension. As a result, shell formatting options
11+
* (two-space indentation) must be defined as the global defaults.
12+
*
13+
* This requires overriding those options for other file types individually
14+
* with what we consider the actual defaults. This object defines them.
15+
*/
16+
export const DEFAULT_OPTIONS = {
17+
tabWidth: 4,
18+
useTabs: true,
19+
} as const satisfies Options;
20+
21+
export const DEFAULT_CONFIG = {
22+
plugins: [pluginOxidation, pluginPackageJSON, pluginShell, pluginSortJSON],
823
bracketSpacing: true,
9-
plugins: [pluginOxidation, pluginPackageJSON, pluginSortJSON],
1024
printWidth: 80,
1125
quoteProps: 'consistent',
1226
singleQuote: true,
13-
tabWidth: 4,
27+
tabWidth: 2,
1428
trailingComma: 'es5',
15-
useTabs: true,
29+
useTabs: false,
1630
overrides: [
1731
{
1832
files: ['*.css', '*.scss'],
1933
options: {
34+
...DEFAULT_OPTIONS,
2035
printWidth: 100,
2136
singleQuote: false,
2237
},
2338
},
39+
/**
40+
* Fish file formatting follows the output of `fish_indent`, which
41+
* defaults to four-space indentation.
42+
*/
43+
{
44+
files: ['*.fish'],
45+
options: {
46+
tabWidth: 4,
47+
},
48+
},
2449
{
25-
files: ['*.html'],
50+
files: ['*.graphql', '*.graphqls', '*.gql'],
2651
options: {
52+
...DEFAULT_OPTIONS,
53+
},
54+
},
55+
{
56+
files: ['*.html', '*.htm'],
57+
options: {
58+
...DEFAULT_OPTIONS,
2759
printWidth: 100,
2860
},
2961
},
3062
{
3163
files: ['*.js', '*.jsx', '*.cjs', '*.mjs'],
3264
options: {
65+
...DEFAULT_OPTIONS,
3366
parser: 'oxc',
3467
},
3568
},
3669
{
37-
files: ['*.json', '*.jsonc'],
70+
files: ['*.json', '*.jsonc', '*.json5'],
3871
options: {
72+
...DEFAULT_OPTIONS,
3973
jsonRecursiveSort: true,
4074
jsonSortOrder: prioritizeKeys('$schema'),
4175
},
4276
},
4377
{
44-
files: ['*.ts', '*.tsx', '*.cts', '*.mts'],
78+
files: ['*.md', '*.mdx'],
4579
options: {
46-
parser: 'oxc-ts',
80+
...DEFAULT_OPTIONS,
4781
},
4882
},
4983
{
50-
files: ['*.yaml', '*.yml'],
84+
files: ['*.ts', '*.tsx', '*.cts', '*.mts'],
5185
options: {
52-
tabWidth: 2,
53-
useTabs: false,
86+
...DEFAULT_OPTIONS,
87+
parser: 'oxc-ts',
5488
},
5589
},
5690
{
@@ -128,5 +162,3 @@ const config = {
128162
},
129163
],
130164
} as const satisfies Config;
131-
132-
export default config;

src/define-config/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Config } from 'prettier';
22
import { expect, expectTypeOf, test } from 'vitest';
3-
import BASE_CONFIG from '../config.ts';
3+
import { DEFAULT_CONFIG } from '../config.ts';
44
import defineConfig from './index.ts';
55

66
test('returns the base config by default', () => {
77
const result = defineConfig();
88

99
expectTypeOf(result).toEqualTypeOf<Config>();
10-
expect(result).toStrictEqual(BASE_CONFIG);
10+
expect(result).toStrictEqual(DEFAULT_CONFIG);
1111
});
1212

1313
test('merges custom config with the base config', () => {
@@ -31,8 +31,8 @@ test('merges custom config with the base config', () => {
3131
expect(result).toHaveProperty('useTabs', false);
3232
expect(result).toHaveProperty('overrides', expect.any(Array));
3333
expect(result).toStrictEqual({
34-
...BASE_CONFIG,
34+
...DEFAULT_CONFIG,
3535
...config,
36-
overrides: [...BASE_CONFIG.overrides, ...config.overrides],
36+
overrides: [...DEFAULT_CONFIG.overrides, ...config.overrides],
3737
});
3838
});

src/define-config/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Config } from 'prettier';
2-
import BASE_CONFIG from '../config.ts';
2+
import { DEFAULT_CONFIG } from '../config.ts';
33
import mergeConfig from '../merge-config/index.ts';
44

55
export default function defineConfig(config: Config = {}): Config {
6-
return mergeConfig(BASE_CONFIG, config);
6+
return mergeConfig(DEFAULT_CONFIG, config);
77
}

0 commit comments

Comments
 (0)