Skip to content

Commit

Permalink
feat: init prettier shellscript plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Lihua Tang authored and JounQin committed Oct 2, 2019
1 parent 5925fc5 commit 14a7329
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 3 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"build": "run-p build:*",
"build:bundle": "r -g '{\"prettier/parser-babylon\":\"prettierPlugins.babylon\",\"prettier-plugin-pkg\":\"prettierPlugins.pkg\"}' -p",
"build:bundle": "r -g '{\"prettier/parser-babylon\":\"prettierPlugins.babylon\",\"prettier-plugin-pkg\":\"prettierPlugins.pkg\",\"prettier-plugin-sh\":\"prettierPlugins.sh\"}' -p",
"build:ts": "tsc -b",
"lint": "run-p lint:*",
"lint:es": "eslint . --ext js,md,ts -f friendly",
Expand Down Expand Up @@ -73,7 +73,8 @@
"<rootDir>/packages/*/src/**"
],
"moduleNameMapper": {
"^prettier-plugin-pkg$": "<rootDir>/packages/pkg/src"
"^prettier-plugin-pkg$": "<rootDir>/packages/pkg/src",
"^prettier-plugin-sh$": "<rootDir>/packages/sh/src"
}
},
"prettier": "@1stg/prettier-config",
Expand Down
Empty file removed packages/sh/.gitkeep
Empty file.
32 changes: 32 additions & 0 deletions packages/sh/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "prettier-plugin-sh",
"version": "0.0.1",
"description": "An opinionated shellscript formatter plugin for Prettier",
"repository": "git@github.com/rx-ts/prettier.git",
"homepage": "https://github.com/rx-ts/prettier/blob/master/packages/sh",
"author": "JounQin <admin@1stg.me>",
"license": "MPL-2.0",
"main": "lib/cjs",
"module": "lib",
"es2015": "lib/es2015",
"fesm5": "lib/esm",
"jsdelivr": "lib/umd",
"unpkg": "lib/umd",
"types": "lib",
"files": [
"lib",
"typings.d.ts"
],
"keywords": [
"shellscript",
"plugin",
"prettier",
"prettier-plugin"
],
"peerDependencies": {
"prettier": "^1.18.2"
},
"dependencies": {
"mvdan-sh": "^0.4.0"
}
}
31 changes: 31 additions & 0 deletions packages/sh/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Doc, FastPath, Plugin } from 'prettier'
// @ts-ignore
const sh = require('mvdan-sh')

const syntax = sh.syntax
const parser = syntax.NewParser()
const printer = syntax.NewPrinter()

export default {
name: 'prettier-plugin-sh',
languages: [
{
name: 'ShellScript',
parsers: ['sh-parse'],
},
],
parsers: {
'sh-parse': {
parse: (text: string) => parser.Parse(text, 'src'),
astFormat: 'sh-parse',
locStart: () => -1,
locEnd: () => -1,
hasPragma: () => false,
},
},
printers: {
'sh-parse': {
print: (path: FastPath): Doc => printer.Print(path.getValue()),
},
},
} as Plugin
6 changes: 6 additions & 0 deletions packages/sh/test/__snapshots__/shellscript.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parsing a simple program 1`] = `
"echo 'foo'
"
`;
23 changes: 23 additions & 0 deletions packages/sh/test/shellscript.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import prettier from 'prettier'
import ShPlugin from 'prettier-plugin-sh'

test('parsing a simple program', () => {
const output = prettier.format(`echo 'foo'`, {
// @ts-ignore
parser: 'sh-parse',
plugins: [ShPlugin],
})

expect(output).toMatchSnapshot()
})

test('fatal parse error', () => {
expect(() =>
prettier.format(`echo )`, {
filepath: 'broken.sh',
// @ts-ignore
parser: 'sh-parse',
plugins: [ShPlugin],
}),
).toThrow()
})
9 changes: 9 additions & 0 deletions packages/sh/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "lib"
},
"include": ["src"]
}
1 change: 1 addition & 0 deletions packages/sh/typing.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'mvdan-sh'
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
"baseUrl": ".",
"incremental": false,
"paths": {
"prettier-plugin-pkg": ["packages/pkg/src"]
"prettier-plugin-pkg": ["packages/pkg/src"],
"prettier-plugin-sh": ["packages/sh/src"]
},
"noEmit": true
},
"references": [
{
"path": "packages/pkg"
},
{
"path": "packages/sh"
}
]
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7349,6 +7349,11 @@ mute-stream@~0.0.4:
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==

mvdan-sh@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/mvdan-sh/-/mvdan-sh-0.4.0.tgz#e1e5ed870bc866ce0180ee79abab55c3bf1c2293"
integrity sha512-pJ8f58eHR5LgJ5VY3yUpIAkvnd6rpLQj9WV4zwxQPInVyRRmE+dzBSEL3gcShLTodeORtcA8UG+HNMOhdMthrA==

mz@^2.4.0, mz@^2.5.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32"
Expand Down

0 comments on commit 14a7329

Please sign in to comment.