Skip to content

Commit

Permalink
Merge pull request #2 from ben12/feature/malva-support
Browse files Browse the repository at this point in the history
Add Malva plugin support
  • Loading branch information
ben12 authored Dec 21, 2024
2 parents 777090b + 49b48e3 commit fa8c730
Show file tree
Hide file tree
Showing 13 changed files with 658 additions and 51 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ $ npm install -D @dprint/json
$ npm install -D @dprint/markdown
$ npm install -D @dprint/toml
$ npm install -D @dprint/typescript
$ npm install -D dprint-plugin-malva
```

## 📖 Usage
Expand Down Expand Up @@ -113,6 +114,7 @@ Then run ESLint with `--fix`!
| [@ben_12/dprint/markdown] | Format markdown code with [@dprint/markdown]. |
| [@ben_12/dprint/toml] | Format toml code with [@dprint/toml]. |
| [@ben_12/dprint/typescript] | Format typescript code with [@dprint/typescript]. |
| [@ben_12/dprint/malva] | Format css/scss/less/sass code with [malva]. |

### Available Configs

Expand All @@ -124,6 +126,7 @@ Then run ESLint with `--fix`!
| [plugin:@ben_12/dprint/markdown-recommended] | Enable the [@ben_12/dprint/markdown] rule. |
| [plugin:@ben_12/dprint/toml-recommended] | Enable the [@ben_12/dprint/toml] rule. |
| [plugin:@ben_12/dprint/typescript-recommended] | Enable the [@ben_12/dprint/typescript] rule along with the [plugin:@ben_12/dprint/disable-typescript-conflict-rules] preset. |
| [plugin:@ben_12/dprint/malva-recommended] | Enable the [@ben_12/dprint/malva] rule. |

- Put the [plugin:@ben_12/dprint/recommended] or [plugin:@ben_12/dprint/disable-conflict-rules] config into the last of your `extends` list in order to ensure disabling conflict rules where came from other base configurations.

Expand All @@ -149,15 +152,18 @@ Please use GitHub's Issues/PRs.
[@dprint/markdown]: https://github.com/dprint/dprint-plugin-markdown
[@dprint/toml]: https://github.com/dprint/dprint-plugin-toml
[@dprint/typescript]: https://github.com/dprint/dprint-plugin-typescript
[malva]: https://github.com/g-plane/malva
[npm]: https://www.npmjs.com/
[@ben_12/dprint/dockerfile]: docs/rules/dprint-dockerfile.md
[@ben_12/dprint/json]: docs/rules/dprint-json.md
[@ben_12/dprint/markdown]: docs/rules/dprint-markdown.md
[@ben_12/dprint/toml]: docs/rules/dprint-toml.md
[@ben_12/dprint/typescript]: docs/rules/dprint-typescript.md
[@ben_12/dprint/malva]: docs/rules/dprint-malva.md
[plugin:@ben_12/dprint/disable-typescript-conflict-rules]: https://github.com/ben12/eslint-plugin-dprint/blob/master/lib/configs/disable-typescript-conflict-rules.ts
[plugin:@ben_12/dprint/dockerfile-recommended]: https://github.com/ben12/eslint-plugin-dprint/blob/master/lib/configs/recommended.ts#L3
[plugin:@ben_12/dprint/json-recommended]: https://github.com/ben12/eslint-plugin-dprint/blob/master/lib/configs/recommended.ts#L10
[plugin:@ben_12/dprint/markdown-recommended]: https://github.com/ben12/eslint-plugin-dprint/blob/master/lib/configs/recommended.ts#L17
[plugin:@ben_12/dprint/toml-recommended]: https://github.com/ben12/eslint-plugin-dprint/blob/master/lib/configs/recommended.ts#L24
[plugin:@ben_12/dprint/typescript-recommended]: https://github.com/ben12/eslint-plugin-dprint/blob/master/lib/configs/recommended.ts#L31
[plugin:@ben_12/dprint/malva-recommended]: https://github.com/ben12/eslint-plugin-dprint/blob/master/lib/configs/recommended.ts#L39
29 changes: 29 additions & 0 deletions docs/rules/dprint-malva.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# @ben_12/dprint/malva

> Format code with [dprint].
## Rule Details

Run [dprint] to format code.

## Options

```jsonc
{
"@ben_12/dprint/malva": [
"error",
{
// Use dprint JSON configuration file (default: "dprint.json")
// It may be created using `dprint init` command
// See also https://dprint.dev/config/
"configFile": "dprint.json",
"config": {
// The TypeScript configuration of dprint
// See also https://dprint.dev/plugins/malva/config/
}
}
]
}
```

[dprint]: https://github.com/dprint/dprint
7 changes: 7 additions & 0 deletions lib/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ export const typescriptRecommended = {
"@ben_12/dprint/typescript": "warn",
},
}

export const malvaRecommended = {
plugins: ["@ben_12/dprint"],
rules: {
"@ben_12/dprint/malva": "warn",
},
}
83 changes: 53 additions & 30 deletions lib/dprint/dprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,66 @@ interface Plugin {
getBuffer?(): Buffer
}

const plugins = [
"@dprint/typescript",
"@dprint/json",
"@dprint/markdown",
"@dprint/toml",
"@dprint/dockerfile",
] as const

const formatters: Formatter[] = []

for (const module of plugins) {
try {
const plugin = require(module) as Plugin
let buffer: Buffer | undefined = undefined
if (plugin.getPath) {
buffer = fs.readFileSync(plugin.getPath())
} else if (plugin.getBuffer) {
buffer = plugin.getBuffer()
}
if (buffer) {
const formatter = createFromBuffer(buffer)
formatters.push(formatter)
}
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
catch (e) {
// plugin unavailable
}
const plugins: Readonly<Record<string, string>> = {
"typescript": "@dprint/typescript",
"json": "@dprint/json",
"markdown": "@dprint/markdown",
"toml": "@dprint/toml",
"dockerfile": "@dprint/dockerfile",
"malva": "dprint-plugin-malva",
}

function getFormatter(filePath: string): Formatter | undefined {
for (const formatter of formatters) {
const formatters: Readonly<Record<string, Formatter>> = Object.entries(plugins).reduce(
(formatters, [name, module]) => {
try {
let packageJson
try {
packageJson = require(module + "/package.json")
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
catch (e) {
// plugin unavailable
return formatters
}
let buffer: Buffer | undefined = undefined
if (packageJson.main?.endsWith(".js")) {
const plugin = require(module) as Plugin
if (plugin.getPath) {
buffer = fs.readFileSync(plugin.getPath())
} else if (plugin.getBuffer) {
buffer = plugin.getBuffer()
}
} else if (packageJson.exports?.["."]?.endsWith(".wasm")) {
buffer = fs.readFileSync(require.resolve(module))
}
if (buffer) {
const formatter = createFromBuffer(buffer)
formatters[name] = formatter
}
} catch (e) {
console.error("Fail to load plugin", module, ":", e)
// plugin unavailable
}
return formatters
},
{} as Record<string, Formatter>,
)

function getFormatter(filePath: string, configName: string): Formatter | undefined {
const formatter = formatters[configName]
if (formatter) {
const pluginInfo = formatter.getPluginInfo()
const fileExtensions = pluginInfo.fileExtensions || []
const fileNames = pluginInfo.fileNames || []
const basename = path.basename(filePath)
if (fileExtensions.some(ext => basename.endsWith("." + ext)) || fileNames.some(file => file === basename)) {
return formatter
} else {
console.warn("File %s not supported by %s", filePath, plugins[configName])
}
} else if (plugins[configName]) {
console.error("Plugin not found: %s", plugins[configName])
} else {
console.error("Unknown plugin for %s", configName)
}
return undefined
}
Expand All @@ -70,8 +92,9 @@ export function format(
config: Record<string, unknown>,
filePath: string,
fileText: string,
configName: string,
): string | undefined {
const formatter = getFormatter(filePath)
const formatter = getFormatter(filePath, configName)
if (formatter) {
const configKey = formatter.getPluginInfo().configKey
const newConfig = JSON.stringify(config)
Expand Down
Loading

0 comments on commit fa8c730

Please sign in to comment.