Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add toml plugin, use taplo inside instead #314

Merged
merged 22 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This repository is a monorepo managed by [changesets][] what means we actually p
| [`prettier-plugin-pkg`](/packages/pkg) | An opinionated package.json formatter plugin for Prettier | [![npm](https://img.shields.io/npm/v/prettier-plugin-pkg.svg)](https://www.npmjs.com/package/prettier-plugin-pkg) |
| [`prettier-plugin-sh`](/packages/sh) | An opinionated `shellscript` formatter plugin for Prettier, also support simple format of `Dockerfile`, `properties`, `gitignore`, `dotenv`, `hosts`, `jvmoptions`... | [![npm](https://img.shields.io/npm/v/prettier-plugin-sh.svg)](https://www.npmjs.com/package/prettier-plugin-sh) |
| [`prettier-plugin-sql`](/packages/sql) | An opinionated sql formatter plugin for Prettier | [![npm](https://img.shields.io/npm/v/prettier-plugin-sql.svg)](https://www.npmjs.com/package/prettier-plugin-sql) |
| [`prettier-plugin-toml`](/packages/toml) | An opinionated `toml` formatter plugin for Prettier | [![npm](https://img.shields.io/npm/v/prettier-plugin-toml.svg)](https://www.npmjs.com/package/prettier-plugin-toml) |

## Sponsors

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"prettier-plugin-pkg": "workspace:*",
"prettier-plugin-sh": "workspace:*",
"prettier-plugin-sql": "workspace:*",
"prettier-plugin-toml": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.18.0",
Expand All @@ -75,7 +76,8 @@
"prettier-plugin-autocorrect": "workspace:*",
"prettier-plugin-pkg": "workspace:*",
"prettier-plugin-sh": "workspace:*",
"prettier-plugin-sql": "workspace:*"
"prettier-plugin-sql": "workspace:*",
"prettier-plugin-toml": "workspace:*"
},
"browserslist": [
"extends @1stg/browserslist-config/modern"
Expand Down
1 change: 1 addition & 0 deletions packages/toml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Change Log
21 changes: 21 additions & 0 deletions packages/toml/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 UnTS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions packages/toml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# prettier-plugin-toml ![npm bundle size](https://img.shields.io/bundlephobia/min/prettier-plugin-toml) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/prettier-plugin-toml)

> An opinionated `toml` formatter plugin for [Prettier][]

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing, taking various rules into account.

This plugin adds support for `toml` through [taplo][].

## Notice

This plugin is still under development, its printer just wraps [taplo][]'s default printer.
Of course it should just work, but may not match [prettier][]'s format sometimes.

## Requirements

`prettier-plugin-toml` is an evergreen module. 🌲 This module requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+).
so1ve marked this conversation as resolved.
Show resolved Hide resolved

## Install

Using npm:

```sh
# npm
npm i -D prettier prettier-plugin-toml

# yarn
yarn add -D prettier prettier-plugin-toml
```

## Usage

Once installed, [Prettier plugins](https://prettier.io/docs/en/plugins.html) must be added to `.prettierrc`:

```json
{
"plugins": ["prettier-plugin-toml"]
}
```

Then:

```sh
# npx
npx prettier --write foo.toml

# yarn
yarn prettier --write foo.toml
```

## Sponsors

| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| [![1stG Open Collective backers and sponsors](https://opencollective.com/1stG/organizations.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers and sponsors](https://opencollective.com/rxts/organizations.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers and sponsors](https://opencollective.com/unts/organizations.svg)](https://opencollective.com/unts) |

## Backers

[![Backers](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)

| 1stG | RxTS | UnTS |
| -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| [![1stG Open Collective backers and sponsors](https://opencollective.com/1stG/individuals.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers and sponsors](https://opencollective.com/rxts/individuals.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers and sponsors](https://opencollective.com/unts/individuals.svg)](https://opencollective.com/unts) |

## Changelog

Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.md).

## License

[MIT][] © [JounQin][]@[1stG.me][]

[1stg.me]: https://www.1stg.me
[jounqin]: https://GitHub.com/JounQin
[mit]: http://opensource.org/licenses/MIT
[prettier]: https://prettier.io
[taplo]: https://github.com/tamasfe/taplo
37 changes: 37 additions & 0 deletions packages/toml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "prettier-plugin-toml",
"version": "0.0.0",
so1ve marked this conversation as resolved.
Show resolved Hide resolved
"type": "module",
"description": "An opinionated `toml` formatter plugin for Prettier",
"repository": "git@github.com/un-ts/prettier.git",
"homepage": "https://github.com/un-ts/prettier/tree/master/packages/prettier",
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
so1ve marked this conversation as resolved.
Show resolved Hide resolved
"funding": "https://opencollective.com/unts",
"license": "MIT",
"engines": {
"node": ">=16.0.0"
},
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"types": "./lib/index.d.ts",
"files": [
"lib"
],
"keywords": [
"toml",
"plugin",
"prettier",
"prettier-plugin"
],
"peerDependencies": {
"prettier": "^3.0.3"
},
"dependencies": {
"@taplo/lib": "0.4.0-alpha.2"
so1ve marked this conversation as resolved.
Show resolved Hide resolved
}
}
82 changes: 82 additions & 0 deletions packages/toml/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Taplo } from '@taplo/lib'
import type { Plugin } from 'prettier'

import { defaultOptions, options } from './options'
so1ve marked this conversation as resolved.
Show resolved Hide resolved
import type { AST, PrettierOptions, TaploOptions } from './types'

const LANGUAGE = 'toml'
const PARSER = 'toml'
const AST_NAME = 'toml-ast'
JounQin marked this conversation as resolved.
Show resolved Hide resolved

let taplo: Taplo | null = null
so1ve marked this conversation as resolved.
Show resolved Hide resolved

async function format(code: string, options: TaploOptions) {
if (!taplo) {
taplo = await Taplo.initialize()
}

return taplo.format(code, { options })
}

function removeBeginningTrailingNewline(code: string) {
JounQin marked this conversation as resolved.
Show resolved Hide resolved
code = code.replace(/^(?:\r?\n)+/, '')
code = code.replace(/(?:\r?\n)+$/, '')

return code
}

const TomlPlugin: Plugin<AST> = {
languages: [
so1ve marked this conversation as resolved.
Show resolved Hide resolved
{
name: LANGUAGE,
parsers: [PARSER],
},
],
parsers: {
[PARSER]: {
async parse(code: string, options: PrettierOptions) {
const indentString = options.useTabs
? '\t'
: ' '.repeat(options.tabWidth)

const taploOptions: TaploOptions = {
so1ve marked this conversation as resolved.
Show resolved Hide resolved
alignEntries: options.taploAlignEntries,
alignComments: options.taploAlignComments,
arrayTrailingComma: options.trailingComma !== 'none',
arrayAutoExpand: options.taploArrayAutoExpand,
arrayAutoCollapse: options.taploArrayAutoCollapse,
compactArrays: options.taploCompactArrays,
compactInlineTables: options.taploCompactInlineTables,
compactEntries: options.taploCompactEntries,
columnWidth: options.printWidth,
indentTables: options.taploIndentTables,
indentEntries: options.taploIndentEntries,
indentString,
trailingNewline: true,
reorderKeys: options.taploReorderKeys,
allowedBlankLines: 1,
JounQin marked this conversation as resolved.
Show resolved Hide resolved
crlf: options.endOfLine === 'crlf',
}

return {
so1ve marked this conversation as resolved.
Show resolved Hide resolved
formatted: await format(
removeBeginningTrailingNewline(code),
taploOptions,
),
}
},
astFormat: AST_NAME,
locStart: () => -1,
locEnd: () => -1,
},
},
printers: {
[AST_NAME]: {
print: ({ node: { formatted } }) => formatted,
},
},
defaultOptions,
so1ve marked this conversation as resolved.
Show resolved Hide resolved
options,
}

export default TomlPlugin
99 changes: 99 additions & 0 deletions packages/toml/src/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import type { RequiredOptions, SupportOption } from 'prettier'

import type { PrettierTaploOptions } from './types'

/**
* @see https://github.com/tamasfe/taplo/blob/848722f2c604de68535e5a3e0bb2a2c1d3c7dc74/crates/taplo/src/formatter/mod.rs#L150-L168
*/
export const options = {
taploAlignEntries: {
name: 'taploAlignEntries',
type: 'boolean',
category: 'taplo',
default: false,
description: 'Align consecutive entries vertically.',
},
taploAlignComments: {
name: 'taploAlignComments',
type: 'boolean',
category: 'taplo',
default: true,
description:
'Align consecutive comments after entries and items vertically. This applies to comments that are after entries or array items.',
},
taploArrayAutoExpand: {
name: 'taploArrayAutoExpand',
type: 'boolean',
category: 'taplo',
default: true,
description:
'Expand arrays to multiple lines that exceed the maximum column width.',
},
taploArrayAutoCollapse: {
name: 'taploArrayAutoCollapse',
type: 'boolean',
category: 'taplo',
default: true,
description:
"Collapse arrays that don't exceed the maximum column width and don't contain comments.",
},
taploCompactArrays: {
name: 'taploCompactArrays',
type: 'boolean',
category: 'taplo',
default: true,
description: 'Omit white space padding from single-line arrays.',
},
taploCompactInlineTables: {
name: 'taploCompactInlineTables',
type: 'boolean',
category: 'taplo',
default: false,
description:
'Omit white space padding from the start and end of inline tables.',
},
taploCompactEntries: {
name: 'taploCompactEntries',
type: 'boolean',
category: 'taplo',
default: false,
description: 'Omit white space around `=`.',
},
taploIndentTables: {
name: 'taploIndentTables',
type: 'boolean',
category: 'taplo',
default: false,
description:
'Indent based on tables and arrays of tables and their subtables, subtables out of order are not indented.',
},
taploIndentEntries: {
name: 'taploIndentEntries',
type: 'boolean',
category: 'taplo',
default: false,
description: 'Indent entries under tables.',
},
taploReorderKeys: {
name: 'taploReorderKeys',
type: 'boolean',
category: 'taplo',
default: false,
description:
'Alphabetically reorder keys that are not separated by empty lines.',
},
} satisfies Record<keyof PrettierTaploOptions, SupportOption>

export const defaultOptions: Partial<RequiredOptions> &
Required<PrettierTaploOptions> = {
taploAlignEntries: options.taploAlignComments.default,
taploAlignComments: options.taploAlignComments.default,
taploArrayAutoExpand: options.taploArrayAutoExpand.default,
taploArrayAutoCollapse: options.taploArrayAutoCollapse.default,
taploCompactArrays: options.taploCompactArrays.default,
taploCompactInlineTables: options.taploCompactInlineTables.default,
taploCompactEntries: options.taploCompactEntries.default,
taploIndentTables: options.taploIndentTables.default,
taploIndentEntries: options.taploIndentEntries.default,
taploReorderKeys: options.taploReorderKeys.default,
}
24 changes: 24 additions & 0 deletions packages/toml/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { FormatOptions } from '@taplo/lib'
import type { ParserOptions } from 'prettier'

export interface AST {
formatted: string
}

export type TaploOptions = NonNullable<FormatOptions['options']>
export type PrettierTaploOptions = Omit<
{
[K in keyof TaploOptions as `taplo${Capitalize<
K & string
>}`]?: TaploOptions[K]
},
`taplo${Capitalize<
| 'allowedBlankLines'
| 'arrayTrailingComma'
| 'columnWidth'
| 'crlf'
| 'indentString'
| 'trailingNewline'
>}`
>
export type PrettierOptions = ParserOptions<AST> & PrettierTaploOptions
12 changes: 12 additions & 0 deletions packages/toml/test/__snapshots__/fixtures.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`parser and printer > should format all fixtures > fixture1.toml 1`] = `
"[foo]
asfd = 1
bar = [
'averyloooooooooooooooooooooooooooooooooooooooooooooooooooong',
\\"arrrrrrrrrrrrrrrrrrrrrrrray\\",
'thatexceedsthemaximumcolumnwidth',
]
"
`;