Skip to content

Commit 3eaeba4

Browse files
authored
feat: add support for multiple dprint plugins (#13)
1 parent b4f7a21 commit 3eaeba4

File tree

6 files changed

+87
-24
lines changed

6 files changed

+87
-24
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ export default [
5454
'format/dprint': ['error', { language: 'toml', languageOptions: { indentWidth: 2 } }],
5555
},
5656
},
57+
58+
// use dprint to format HTML and CSS in style tags
59+
{
60+
files: ['**/*.html'],
61+
languageOptions: {
62+
parser: format.parserPlain,
63+
},
64+
plugins: {
65+
format,
66+
},
67+
rules: {
68+
'format/dprint': ['error', {
69+
plugins: [
70+
{
71+
plugin: 'node_modules/dprint-plugin-malva/plugin.wasm',
72+
options: {},
73+
},
74+
{
75+
plugin: 'node_modules/dprint-plugin-markup/plugin.wasm',
76+
options: {
77+
scriptIndent: true,
78+
styleIndent: true,
79+
},
80+
},
81+
],
82+
useTabs: false,
83+
indentWidth: 2,
84+
}],
85+
},
86+
},
5787
]
5888
```
5989

@@ -74,10 +104,15 @@ Use dprint to format files.
74104

75105
#### Options
76106

107+
Either:
77108
- `language` (required) - the language to format, or can be a filepath or URL to the WASM binary. [Supported languages](https://dprint.dev/plugins/)
78109
- `languageOptions` - the options for the language
79110
- The rest options are passed as dprint's general options
80111

112+
Or:
113+
- `plugins` (required) - Array of plugins, defined as an object containing `plugin` (same as `language` above) and `options`, which is the same as `languageOptions` above.
114+
- The rest of the options are passed as dprint's general options
115+
81116
## Sponsors
82117

83118
<p align="center">

dts/rule-options.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export interface RuleOptions {
77

88
export { PrettierOptions }
99

10-
export interface DprintOptions {
11-
language: string
12-
languageOptions?: Record<string, unknown>
13-
[x: string]: unknown
14-
}
10+
export type DprintOptions
11+
= | { language: string, languageOptions?: Record<string, unknown>, [x: string]: unknown }
12+
| { plugins: Array<{ plugin: string, options?: Record<string, unknown> }>, [x: string]: unknown }

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"eslint": "^8.40.0 || ^9.0.0"
6666
},
6767
"dependencies": {
68-
"@dprint/formatter": "^0.4.1",
68+
"@dprint/formatter": "^0.5.1",
6969
"@dprint/markdown": "^0.20.0",
7070
"@dprint/toml": "^0.7.0",
7171
"eslint-formatting-reporter": "^0.0.0",

pnpm-lock.yaml

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rules/dprint.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export default {
2626
languageOptions: {
2727
type: 'object',
2828
},
29+
plugins: {
30+
type: 'array',
31+
},
2932
},
3033
additionalProperties: true,
3134
},

workers/dprint.cjs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// @ts-check
21
const { Buffer } = require('node:buffer')
32
const fs = require('node:fs/promises')
3+
// @ts-check
44
const { runAsWorker } = require('synckit')
55

66
let dprint
@@ -38,16 +38,32 @@ runAsWorker(async (code, filename, options) => {
3838
dockerfile: '@dprint/dockerfile',
3939
}
4040

41-
const lang = builtInLangs[options.language] || options.language
42-
const promise = cache.has(lang)
43-
? cache.get(lang)
44-
: cache.set(lang, loadBuffer(lang).then(r => dprint.createFromBuffer(r))).get(lang)
45-
46-
const formatter = await promise
47-
const { lang: _, languageOptions = {}, ...rest } = options
48-
formatter.setConfig(rest, languageOptions)
49-
return formatter.formatText({
50-
filePath: filename,
51-
fileText: code,
52-
})
41+
if (options.language) {
42+
const lang = builtInLangs[options.language] || options.language
43+
const promise = cache.has(lang)
44+
? cache.get(lang)
45+
: cache.set(lang, loadBuffer(lang).then(r => dprint.createFromBuffer(r))).get(lang)
46+
47+
const formatter = await promise
48+
const { lang: _, languageOptions = {}, ...rest } = options
49+
formatter.setConfig(rest, languageOptions)
50+
return formatter.formatText({
51+
filePath: filename,
52+
fileText: code,
53+
})
54+
}
55+
else {
56+
// TODO: context caching??
57+
const { plugins, ...rest } = options
58+
const context = dprint.createContext(rest)
59+
60+
for (const plugin of plugins) {
61+
context.addPlugin(await loadBuffer(plugin.plugin), plugin.options || {})
62+
}
63+
64+
return context.formatText({
65+
filePath: filename,
66+
fileText: code,
67+
})
68+
}
5369
})

0 commit comments

Comments
 (0)