Skip to content

Commit

Permalink
[#103] Specify --parser in Prettier (#107)
Browse files Browse the repository at this point in the history
Closes #103
  • Loading branch information
raxod502 committed Sep 11, 2022
1 parent 8ff4576 commit 46d373f
Show file tree
Hide file tree
Showing 52 changed files with 231 additions and 108 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ The format is based on [Keep a Changelog].
* Buffer content is now always passed to formatters using a pipe. This
fixes issues with formatters that behave differently when receiving
input on stdin versus being run on a tty ([#119]).
* Prettier now specifies `--parser` argument explicitly, so it will
work properly even when the name of the file does not match what
Prettier expects (e.g. `.yamllint` will be formatted as YAML by
Prettier as long as it is in `yaml-mode`). See [#103].

### Bugs fixed
* When a formatter has a bug and fails to return anything on stdout
Expand All @@ -29,10 +33,17 @@ The format is based on [Keep a Changelog].
* [stylua](https://github.com/JohnnyMorganz/StyLua) for Lua ([#105]).
* Native Emacs indentation of Emacs Lisp code as a formatter ([#102]).

### Bugfixes
* Prettier supports SCSS instead of SASS. The original support for
SASS in Apheleia was a bug because Prettier actually never had
support for SASS in the first place, so Apheleia would have failed
anyway on trying to format a SASS file.

[#43]: https://github.com/radian-software/apheleia/issues/43
[#100]: https://github.com/radian-software/apheleia/pull/100
[#101]: https://github.com/radian-software/apheleia/pull/101
[#102]: https://github.com/radian-software/apheleia/pull/102
[#103]: https://github.com/radian-software/apheleia/issues/103
[#105]: https://github.com/radian-software/apheleia/pull/105
[#109]: https://github.com/radian-software/apheleia/issues/109
[#110]: https://github.com/radian-software/apheleia/pull/110
Expand Down
40 changes: 31 additions & 9 deletions apheleia.el
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,26 @@ being run, for diagnostic purposes."
"--enable-outside-detected-project"))
(phpcs . ("apheleia-phpcs"))
(prettier . (npx "prettier" "--stdin-filepath" filepath))
(prettier-css
. (npx "prettier" "--stdin-filepath" filepath "--parser=css"))
(prettier-html
. (npx "prettier" "--stdin-filepath" filepath "--parser=html"))
(prettier-graphql
. (npx "prettier" "--stdin-filepath" filepath "--parser=graphql"))
(prettier-javascript
. (npx "prettier" "--stdin-filepath" filepath "--parser=babel-flow"))
(prettier-json
. (npx "prettier" "--stdin-filepath" filepath "--parser=json"))
(prettier-markdown
. (npx "prettier" "--stdin-filepath" filepath "--parser=markdown"))
(prettier-ruby
. (npx "prettier" "--stdin-filepath" filepath "--parser=ruby"))
(prettier-scss
. (npx "prettier" "--stdin-filepath" filepath "--parser=scss"))
(prettier-typescript
. (npx "prettier" "--stdin-filepath" filepath "--parser=typescript"))
(prettier-yaml
. (npx "prettier" "--stdin-filepath" filepath "--parser=yaml"))
(shfmt . ("shfmt" "-i" "4"))
(stylua . ("stylua" "-"))
(rustfmt . ("rustfmt" "--quiet" "--emit" "stdout"))
Expand Down Expand Up @@ -1073,44 +1093,46 @@ function: %s" command)))
(defcustom apheleia-mode-alist
'(;; php-mode has to come before cc-mode
(php-mode . phpcs)
;; rest are alphabetical
(beancount-mode . bean-format)
(cc-mode . clang-format)
(c-mode . clang-format)
(c++-mode . clang-format)
(caml-mode . ocamlformat)
(common-lisp-mode . lisp-indent)
(css-mode . prettier)
(css-mode . prettier-css)
(dart-mode . dart-format)
(emacs-lisp-mode . lisp-indent)
(elixir-mode . mix-format)
(elm-mode . elm-format)
(fish-mode . fish-indent)
(go-mode . gofmt)
(graphql-mode . prettier-graphql)
(haskell-mode . brittany)
(html-mode . prettier)
(html-mode . prettier-html)
(java-mode . google-java-format)
(js3-mode . prettier)
(js-mode . prettier)
(json-mode . prettier)
(js3-mode . prettier-javascript)
(js-mode . prettier-javascript)
(json-mode . prettier-json)
(kotlin-mode . ktlint)
(latex-mode . latexindent)
(LaTeX-mode . latexindent)
(lua-mode . stylua)
(lisp-mode . lisp-indent)
(nix-mode . nixfmt)
(python-mode . black)
(ruby-mode . prettier)
(ruby-mode . prettier-ruby)
(rustic-mode . rustfmt)
(rust-mode . rustfmt)
(sass-mode . prettier)
(scss-mode . prettier-scss)
(sh-mode . shfmt)
(terraform-mode . terraform)
(TeX-latex-mode . latexindent)
(TeX-mode . latexindent)
(tuareg-mode . ocamlformat)
(typescript-mode . prettier)
(typescript-mode . prettier-typescript)
(web-mode . prettier)
(yaml-mode . prettier))
(yaml-mode . prettier-yaml))
"Alist mapping major mode names to formatters to use in those modes.
This determines what formatter to use in buffers without a
setting for `apheleia-formatter'. The keys are major mode
Expand Down
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-css.bash
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-graphql.bash
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-html.bash
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-javascript.bash
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-json.bash
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-markdown.bash
15 changes: 15 additions & 0 deletions test/formatters/installers/prettier-ruby.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Need ruby for gem, need gcc and ruby headers for native gem deps
apt-get install -y ruby ruby-dev gcc

# Install the plugin
npm install -g prettier @prettier/plugin-ruby

# Have to install from source because release not tagged yet
# https://github.com/ruby-syntax-tree/syntax_tree-rbs/pull/34
# https://stackoverflow.com/a/11767563
gem install specific_install
gem specific_install -l https://github.com/ruby-syntax-tree/syntax_tree-rbs.git

# These are required dependencies documented at
# https://www.npmjs.com/package/@prettier/plugin-ruby
gem install prettier_print syntax_tree syntax_tree-haml syntax_tree-rbs
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-scss.bash
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-typescript.bash
1 change: 1 addition & 0 deletions test/formatters/installers/prettier-yaml.bash
13 changes: 13 additions & 0 deletions test/formatters/samplecode/prettier-css/in.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body

{
padding-left : 11em;
font-family
: Georgia,

"Times New Roman",
Times, serif;
color: purple;
background-color:
#d8da3d
}
6 changes: 6 additions & 0 deletions test/formatters/samplecode/prettier-css/out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
padding-left: 11em;
font-family: Georgia, "Times New Roman", Times, serif;
color: purple;
background-color: #d8da3d;
}
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier-graphql/in.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{human(id: "1000") {name height(unit: FOOT)}}
6 changes: 6 additions & 0 deletions test/formatters/samplecode/prettier-graphql/out.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
human(id: "1000") {
name
height(unit: FOOT)
}
}
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier-html/in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>Minify <abbr title="HyperText Markup Language">HTML</abbr> and any <abbr title="Cascading Style Sheets">CSS</abbr> or <abbr title="JavaScript">JS</abbr> included in your markup</h2>
5 changes: 5 additions & 0 deletions test/formatters/samplecode/prettier-html/out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h2>
Minify <abbr title="HyperText Markup Language">HTML</abbr> and any
<abbr title="Cascading Style Sheets">CSS</abbr> or
<abbr title="JavaScript">JS</abbr> included in your markup
</h2>
4 changes: 4 additions & 0 deletions test/formatters/samplecode/prettier-javascript/in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function HelloWorld({greeting = "hello", greeted = '"World"', silent = false, onMouseOver,}) {

if(!greeting){return null};
}
10 changes: 10 additions & 0 deletions test/formatters/samplecode/prettier-javascript/out.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function HelloWorld({
greeting = "hello",
greeted = '"World"',
silent = false,
onMouseOver,
}) {
if (!greeting) {
return null;
}
}
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier-json/in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"arrowParens":"always","bracketSpacing":true,"embeddedLanguageFormatting":"auto","htmlWhitespaceSensitivity":"css","insertPragma":false,"jsxBracketSameLine":false,"jsxSingleQuote":false,"printWidth":80,"proseWrap":"preserve","quoteProps":"as-needed","requirePragma":false,"semi":true,"singleQuote":false,"tabWidth":2,"trailingComma":"es5","useTabs":false,"vueIndentScriptAndStyle":false}
19 changes: 19 additions & 0 deletions test/formatters/samplecode/prettier-json/out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"embeddedLanguageFormatting": "auto",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"printWidth": 80,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
"vueIndentScriptAndStyle": false
}
4 changes: 4 additions & 0 deletions test/formatters/samplecode/prettier-markdown/in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
|col1|col 2|
|-|-|
|nice|fits|
|oh no!|it's ugly!|
4 changes: 4 additions & 0 deletions test/formatters/samplecode/prettier-markdown/out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| col1 | col 2 |
| ------ | ---------- |
| nice | fits |
| oh no! | it's ugly! |
13 changes: 13 additions & 0 deletions test/formatters/samplecode/prettier-ruby/in.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
d=[30644250780,9003106878,
30636278846,66641217692,4501790980,
671_24_603036,131_61973916,66_606629_920,
30642677916,30643069058];a,s=[],$*[0]
s.each_byte{|b|a<<("%036b"%d[b.
chr.to_i]).scan(/\d{6}/)}
a.transpose.each{ |a|
a.join.each_byte{\
|i|print i==49?\
($*[1]||"#")\
:32.chr}
puts
}
18 changes: 18 additions & 0 deletions test/formatters/samplecode/prettier-ruby/out.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
d = [
30_644_250_780,
9_003_106_878,
30_636_278_846,
66_641_217_692,
4_501_790_980,
671_24_603036,
131_61973916,
66_606629_920,
30_642_677_916,
30_643_069_058
]
a, s = [], $*[0]
s.each_byte { |b| a << ("%036b" % d[b.chr.to_i]).scan(/\d{6}/) }
a.transpose.each do |a|
a.join.each_byte { |i| print i == 49 ? ($*[1] || "#") : 32.chr }
puts
end
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier-scss/in.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Define standard variables and values for website */$bgcolor: lightblue;$textcolor: darkblue;$fontsize: 18px;/* Use the variables */body{background-color: $bgcolor; color: $textcolor; font-size: $fontsize;}
9 changes: 9 additions & 0 deletions test/formatters/samplecode/prettier-scss/out.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Define standard variables and values for website */
$bgcolor: lightblue;
$textcolor: darkblue;
$fontsize: 18px; /* Use the variables */
body {
background-color: $bgcolor;
color: $textcolor;
font-size: $fontsize;
}
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier-typescript/in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interface GreetingSettings{greeting: string; duration?: number; color?: string;}declare function greet(setting: GreetingSettings): void;
6 changes: 6 additions & 0 deletions test/formatters/samplecode/prettier-typescript/out.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
declare function greet(setting: GreetingSettings): void;
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier-yaml/in.yamllint
13 changes: 13 additions & 0 deletions test/formatters/samplecode/prettier-yaml/in.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- hosts:
all

tasks:
- name:
Get software for apt repository management.
apt:
state: present


name:
- python3-pycurl
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier-yaml/out.yamllint
10 changes: 10 additions & 0 deletions test/formatters/samplecode/prettier-yaml/out.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- hosts: all

tasks:
- name: Get software for apt repository management.
apt:
state: present

name:
- python3-pycurl
13 changes: 0 additions & 13 deletions test/formatters/samplecode/prettier/in.css

This file was deleted.

1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.css
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.graphql
1 change: 0 additions & 1 deletion test/formatters/samplecode/prettier/in.html

This file was deleted.

1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.html
4 changes: 0 additions & 4 deletions test/formatters/samplecode/prettier/in.js

This file was deleted.

1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.js
1 change: 0 additions & 1 deletion test/formatters/samplecode/prettier/in.json

This file was deleted.

1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.json
1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.md
1 change: 0 additions & 1 deletion test/formatters/samplecode/prettier/in.scss

This file was deleted.

1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.scss
1 change: 0 additions & 1 deletion test/formatters/samplecode/prettier/in.ts

This file was deleted.

1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.ts
13 changes: 0 additions & 13 deletions test/formatters/samplecode/prettier/in.yml

This file was deleted.

1 change: 1 addition & 0 deletions test/formatters/samplecode/prettier/in.yml
Loading

0 comments on commit 46d373f

Please sign in to comment.