Skip to content

Commit

Permalink
feat: declare all possible options for plugin-sh (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jun 26, 2021
1 parent ab986b3 commit 1ddae89
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-bags-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-sh": minor
---

feat: `switchCaseIndent` and `spaceRedirects`'s default value has been changed to be `true`
5 changes: 5 additions & 0 deletions .changeset/pretty-numbers-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-sh": minor
---

feat: declare all possible options for plugin-sh
6 changes: 6 additions & 0 deletions .changeset/sweet-students-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"prettier-plugin-pkg": minor
"prettier-plugin-sql": minor
---

chore: rebuild via esbuild, update node lts version
127 changes: 117 additions & 10 deletions packages/sh/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sh, { LangVariant, Node, Pos } from 'mvdan-sh'
import { AstPath, ParserOptions, Plugin, RequiredOptions } from 'prettier'
import {
IntSupportOption,
ParserOptions,
PathSupportOption,
Plugin,
RequiredOptions,
} from 'prettier'

import { languages } from './languages'

Expand All @@ -21,7 +27,7 @@ export interface ShOptions extends RequiredOptions {
functionNextLine: boolean
}

export type ShParserOptions = ParserOptions & ShOptions
export type ShParserOptions = ParserOptions<Node> & ShOptions

export interface ShParseError {
Filename: string
Expand All @@ -31,8 +37,7 @@ export interface ShParseError {
Error(): void
}

export default {
name: 'prettier-plugin-sh',
const ShPlugin: Plugin<Node> = {
languages,
parsers: {
sh: {
Expand Down Expand Up @@ -66,21 +71,21 @@ export default {
}
},
astFormat: 'sh',
locStart: (node: Node) => node.Pos().Offset(),
locEnd: (node: Node) => node.End().Offset(),
locStart: node => node.Pos().Offset(),
locEnd: node => node.End().Offset(),
},
},
printers: {
sh: {
print: (
path: AstPath<Node>,
path,
{
useTabs,
tabWidth,
indent = useTabs ? 0 : tabWidth,
binaryNextLine = true,
switchCaseIndent,
spaceRedirects,
switchCaseIndent = true,
spaceRedirects = true,
keepPadding,
minify,
functionNextLine,
Expand All @@ -99,4 +104,106 @@ export default {
.Print(path.getValue()),
},
},
} as Plugin
options: {
keepComments: {
since: '0.1.0',
category: 'Output',
type: 'boolean',
default: true,
description:
'KeepComments makes the parser parse comments and attach them to nodes, as opposed to discarding them.',
},
stopAt: {
since: '0.1.0',
category: 'Config',
type: 'path',
description: [
'StopAt configures the lexer to stop at an arbitrary word, treating it as if it were the end of the input. It can contain any characters except whitespace, and cannot be over four bytes in size.',
'This can be useful to embed shell code within another language, as one can use a special word to mark the delimiters between the two.',
'As a word, it will only apply when following whitespace or a separating token. For example, StopAt("$$") will act on the inputs "foo $$" and "foo;$$", but not on "foo \'$$\'".',
'The match is done by prefix, so the example above will also act on "foo $$bar".',
].join('\n'),
} as PathSupportOption,
variant: {
since: '0.1.0',
category: 'Config',
type: 'choice',
default: undefined,
choices: [
{
value: 0, // LangVariant.LangBash,
description: 'Bash',
},
{
value: 1, // LangVariant.LangPOSIX,
description: 'POSIX',
},
{
value: 2, // LangVariant.LangMirBSDKorn,
description: 'MirBSDKorn',
},
],
description:
'Variant changes the shell language variant that the parser will accept.',
},
indent: {
since: '0.1.0',
category: 'Format',
type: 'int',
description:
'Indent sets the number of spaces used for indentation. If set to 0, tabs will be used instead.',
} as IntSupportOption,
binaryNextLine: {
since: '0.1.0',
category: 'Output',
type: 'boolean',
default: true,
description:
'BinaryNextLine will make binary operators appear on the next line when a binary command, such as a pipe, spans multiple lines. A backslash will be used.',
},
switchCaseIndent: {
since: '0.1.0',
category: 'Format',
type: 'boolean',
default: true,
description:
'SwitchCaseIndent will make switch cases be indented. As such, switch case bodies will be two levels deeper than the switch itself.',
},
spaceRedirects: {
since: '0.1.0',
category: 'Format',
type: 'boolean',
default: true,
description:
"SpaceRedirects will put a space after most redirection operators. The exceptions are '>&', '<&', '>(', and '<('.",
},
keepPadding: {
since: '0.1.0',
category: 'Format',
type: 'boolean',
default: false,
description: [
'KeepPadding will keep most nodes and tokens in the same column that they were in the original source. This allows the user to decide how to align and pad their code with spaces.',
'Note that this feature is best-effort and will only keep the alignment stable, so it may need some human help the first time it is run.',
].join('\n'),
},
minify: {
since: '0.1.0',
category: 'Output',
type: 'boolean',
default: false,
description:
'Minify will print programs in a way to save the most bytes possible. For example, indentation and comments are skipped, and extra whitespace is avoided when possible.',
},
functionNextLine: {
since: '0.1.0',
category: 'Format',
type: 'boolean',
default: false,
description:
"FunctionNextLine will place a function's opening braces on the next line.",
},
},
}

export default ShPlugin
4 changes: 2 additions & 2 deletions packages/sql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const SqlPlugin: Plugin<AST | string> = {
options: {
formatter: {
since: '0.1.0',
category: 'Format',
category: 'Config',
type: 'choice',
default: SQL_FORMATTER,
description: 'Choose which formatter to be used',
Expand Down Expand Up @@ -136,7 +136,7 @@ const SqlPlugin: Plugin<AST | string> = {
},
linesBetweenQueries: {
since: '0.1.0',
category: 'Output',
category: 'Format',
type: 'int',
default: 2,
description:
Expand Down

0 comments on commit 1ddae89

Please sign in to comment.