-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
437 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Change Log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+). | ||
|
||
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "prettier-plugin-toml", | ||
"version": "0.0.0", | ||
"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>", | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
import type { AST, PrettierOptions, TaploOptions } from './types' | ||
|
||
const LANGUAGE = 'toml' | ||
const PARSER = 'toml' | ||
const AST_NAME = 'toml-ast' | ||
|
||
let taplo: Taplo | null = null | ||
|
||
async function format(code: string, options: TaploOptions) { | ||
if (!taplo) { | ||
taplo = await Taplo.initialize() | ||
} | ||
|
||
return taplo.format(code, { options }) | ||
} | ||
|
||
function removeBeginningTrailingNewline(code: string) { | ||
code = code.replace(/^(?:\r?\n)+/, '') | ||
code = code.replace(/(?:\r?\n)+$/, '') | ||
|
||
return code | ||
} | ||
|
||
const TomlPlugin: Plugin<AST> = { | ||
languages: [ | ||
{ | ||
name: LANGUAGE, | ||
parsers: [PARSER], | ||
}, | ||
], | ||
parsers: { | ||
[PARSER]: { | ||
async parse(code: string, options: PrettierOptions) { | ||
const indentString = options.useTabs | ||
? '\t' | ||
: ' '.repeat(options.tabWidth) | ||
|
||
const taploOptions: TaploOptions = { | ||
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, | ||
crlf: options.endOfLine === 'crlf', | ||
} | ||
|
||
return { | ||
formatted: await format( | ||
removeBeginningTrailingNewline(code), | ||
taploOptions, | ||
), | ||
} | ||
}, | ||
astFormat: AST_NAME, | ||
locStart: () => -1, | ||
locEnd: () => -1, | ||
}, | ||
}, | ||
printers: { | ||
[AST_NAME]: { | ||
print: ({ node: { formatted } }) => formatted, | ||
}, | ||
}, | ||
defaultOptions, | ||
options, | ||
} | ||
|
||
export default TomlPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
" | ||
`; |
Oops, something went wrong.