Skip to content

Commit

Permalink
feat(prettier-plugin-sh): improve type definitions, add more details …
Browse files Browse the repository at this point in the history
…and test cases
  • Loading branch information
JounQin committed Oct 2, 2019
1 parent 14a7329 commit e99227f
Show file tree
Hide file tree
Showing 24 changed files with 2,646 additions and 1,212 deletions.
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage
fixtures
lib
node_modules
661 changes: 0 additions & 661 deletions assets/banner.ai

This file was deleted.

253 changes: 1 addition & 252 deletions assets/banner.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 14 additions & 16 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\",\"prettier-plugin-sh\":\"prettierPlugins.sh\"}' -p",
"build:r": "r -g \"{'prettier/parser-babylon':'prettierPlugins.babylon','prettier-plugin-pkg':'prettierPlugins.pkg','prettier-plugin-sh':'prettierPlugins.sh'}\" -x mvdan-sh -p",
"build:ts": "tsc -b",
"lint": "run-p lint:*",
"lint:es": "eslint . --ext js,md,ts -f friendly",
Expand All @@ -21,33 +21,34 @@
"type-coverage": "type-coverage --cache --detail --ignore-files *.d.ts --strict"
},
"devDependencies": {
"@1stg/babel-preset": "^0.7.0",
"@1stg/babel-preset": "^0.7.2",
"@1stg/commitlint-config": "^0.1.0",
"@1stg/eslint-config": "^0.12.13",
"@1stg/eslint-config": "^0.12.18",
"@1stg/husky-config": "^0.3.0",
"@1stg/lint-staged": "^0.8.0",
"@1stg/prettier-config": "^0.3.2",
"@1stg/lint-staged": "^0.8.3",
"@1stg/prettier-config": "^0.3.4",
"@1stg/remark-config": "^0.2.2",
"@1stg/rollup-config": "^0.10.5",
"@1stg/rollup-config": "^0.12.0",
"@1stg/tsconfig": "^0.6.0",
"@1stg/tslint-config": "^0.4.1",
"@1stg/tslint-config": "^0.4.2",
"@babel/core": "^7.6.2",
"@babel/types": "^7.6.1",
"@commitlint/cli": "^8.2.0",
"@pkgr/imagemin": "^0.1.0",
"@types/jest": "^24.0.18",
"@types/lodash": "^4.14.139",
"@types/lodash": "^4.14.141",
"@types/prettier": "^1.18.2",
"eslint": "^6.4.0",
"husky": "^3.0.5",
"eslint": "^6.5.1",
"husky": "^3.0.7",
"jest": "^24.9.0",
"lerna": "^3.16.4",
"lerna-changelog": "^0.8.2",
"lint-staged": "^9.3.0",
"lint-staged": "^9.4.1",
"lodash": "^4.17.15",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"react": "^16.9.0",
"rollup": "^1.21.4",
"react": "^16.10.1",
"rollup": "^1.22.0",
"ts-jest": "^24.1.0",
"tslint": "^5.20.0",
"type-coverage": "^2.3.0",
Expand All @@ -69,9 +70,6 @@
"jest": {
"preset": "ts-jest",
"collectCoverage": true,
"collectCoverageFrom": [
"<rootDir>/packages/*/src/**"
],
"moduleNameMapper": {
"^prettier-plugin-pkg$": "<rootDir>/packages/pkg/src",
"^prettier-plugin-sh$": "<rootDir>/packages/sh/src"
Expand Down
21 changes: 21 additions & 0 deletions packages/sh/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 RxTS

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.
4 changes: 2 additions & 2 deletions packages/sh/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "prettier-plugin-sh",
"version": "0.0.1",
"version": "0.0.0",
"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",
"license": "MIT",
"main": "lib/cjs",
"module": "lib",
"es2015": "lib/es2015",
Expand Down
70 changes: 70 additions & 0 deletions packages/sh/shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
declare module 'mvdan-sh' {
namespace sh {
interface Pos {
After(p: Pos): boolean
Col(): number
IsValid(): boolean
Line(): number
Offset(): number
String(): string
}

interface Node {
Pos(): Pos
End(): Pos
}

// eslint-disable-next-line @typescript-eslint/no-type-alias
type Command = Node

interface Stmt {
Comments: Comment[]
Cmd: Command
Position: Pos
Semicolon: Pos
Negated: boolean
Background: boolean
Coprocess: boolean
}

interface Comment extends Node {
Hash: Pos
Text: string
}

interface File extends Node {
Name: string
Stmts?: Stmt[]
Last: Stmt[]
}

type ParserOption = (parser: Parser) => void

interface Parser {
keepComments: boolean
(...options: ParserOption[]): Parser
Parse(text: string, path?: string): File
}

interface Printer {
(): Printer
Print(node: Node): string
}

interface ShellScript {
syntax: {
NewParser: Parser
NewPrinter: Printer
KeepComments(parser: sh.Parser, keep: boolean): ParserOption
DebugPrint(node: Node): void
NodeType(node: Node): string
ValidName(value: string): boolean
Walk(node: Node, walker: (node: Node) => boolean): void
}
}
}

const sh: sh.ShellScript

export = sh
}
33 changes: 15 additions & 18 deletions packages/sh/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import { Doc, FastPath, Plugin } from 'prettier'
// @ts-ignore
const sh = require('mvdan-sh')
import sh from 'mvdan-sh'
import { FastPath, Plugin } from 'prettier'

const syntax = sh.syntax
import { languages } from './languages'

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

syntax.KeepComments(parser, true)

export default {
name: 'prettier-plugin-sh',
languages: [
{
name: 'ShellScript',
parsers: ['sh-parse'],
},
],
languages,
parsers: {
'sh-parse': {
parse: (text: string) => parser.Parse(text, 'src'),
astFormat: 'sh-parse',
locStart: () => -1,
locEnd: () => -1,
hasPragma: () => false,
sh: {
parse: (text, _parsers, { filepath }) => parser.Parse(text, filepath),
astFormat: 'sh',
locStart: (node: sh.Node) => node.Pos().Offset(),
locEnd: (node: sh.Node) => node.End().Offset(),
},
},
printers: {
'sh-parse': {
print: (path: FastPath): Doc => printer.Print(path.getValue()),
sh: {
print: (path: FastPath<sh.Node>) => printer.Print(path.getValue()),
},
},
} as Plugin

0 comments on commit e99227f

Please sign in to comment.