Skip to content

Commit

Permalink
Merge pull request #1136 from aidistan/master
Browse files Browse the repository at this point in the history
Implement a basic beautifer for Vue Components
  • Loading branch information
Glavin001 authored Aug 17, 2016
2 parents 06773e3 + 86f83c8 commit 6a99b0a
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ See [all supported options in the documentation at `docs/options.md`](https://g
| TypeScript | `TypeScript` |`.ts` | [`TypeScript Formatter`](https://github.com/vvakame/typescript-formatter) (Default) |
| Vala | `Vala` |`.vala`, `.vapi` | [`Uncrustify`](https://github.com/uncrustify/uncrustify) (Default) |
| Visualforce | `Visualforce` |`.page` | [`Pretty Diff`](https://github.com/prettydiff/prettydiff) (Default) |
| Vue | `Vue Component` |`.vue` | `Vue Beautifier` (Default) |
| XML | `SLD`, `XML`, `XHTML`, `XSD`, `XSL`, `JSP` |`.sld`, `.xml`, `.xhtml`, `.xsd`, `.xsl`, `.jsp` | [`JS Beautify`](https://github.com/beautify-web/js-beautify), [`Pretty Diff`](https://github.com/prettydiff/prettydiff) (Default) |
| XTemplate | `XTemplate` |`.xtemplate` | [`Pretty Diff`](https://github.com/prettydiff/prettydiff) (Default) |

Expand Down
4 changes: 3 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ install:

# Ruby & Gem
- cinst ruby -y
- "SET PATH=C:\\Ruby193\\bin;%PATH%"
- "SET PATH=C:\\tools\\ruby23\\bin;%PATH%"
# Rubocop
- gem install rubocop
- where rubocop
Expand All @@ -98,6 +98,8 @@ install:
- cinst emacs -y
- where emacs

# FIXME: Enable allowEmptyChecksums, until someone fixes the checksum issue of php
- choco feature enable -n allowEmptyChecksums
# PHP
- cinst php -y
- ps: "ls \"C:\\tools\\php\""
Expand Down
46 changes: 46 additions & 0 deletions examples/nested-jsbeautifyrc/vue/expected/test.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<template>
<base-view>
<div slot="page-body" class="row">
Your content here!
</div>
</base-view>
</template>

<script>
export default {
data: function() {
return {
text: 'Hello, world!'
}
}
}
</script>

<style lang="sass">
nav {
ul {
margin:0 padding: 0;
}
li {
display: inline-block;
}
a {
display: block;
}
}
</style>

<style lang="scss">
nav {
ul {
margin: 0;
padding: 0;
}
li {
display: inline-block;
}
a {
display: block;
}
}
</style>
45 changes: 45 additions & 0 deletions examples/nested-jsbeautifyrc/vue/original/test.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<template>
<base-view>
<div slot="page-body" class="row">
Your content here!
</div>
</base-view>
</template>

<script>
export default {
data: function() {
return {
text: 'Hello, world!'
}
}
}
</script>

<style lang="sass">
nav {
ul {
margin: 0
padding: 0
}
li {
display: inline-block
}
a {
display: block
}
}
</style>

<style lang="scss">
nav {
ul {
margin: 0;
padding: 0;
}
li { display: inline-block; }
a {
display: block;
}
}
</style>
5 changes: 5 additions & 0 deletions spec/beautify-languages-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ describe "BeautifyLanguages", ->
# All Languages for configuration
langNames = fs.readdirSync(langsDir)
for lang in langNames

# FIXME: Skip testing ocaml in Windows
if isWindows && lang == 'ocaml'
continue

do (lang) ->
# Generate the path to where al of the tests are
testsDir = path.resolve(langsDir, lang)
Expand Down
1 change: 1 addition & 0 deletions src/beautifiers/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = class Beautifiers extends EventEmitter
'stylish-haskell'
'tidy-markdown'
'typescript-formatter'
'vue-beautifier'
'yapf'
'erl_tidy'
'marko-beautifier'
Expand Down
49 changes: 49 additions & 0 deletions src/beautifiers/vue-beautifier.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict"
Beautifier = require('./beautifier')
prettydiff = require("prettydiff")
_ = require('lodash')

module.exports = class VueBeautifier extends Beautifier
name: "Vue Beautifier"

options:
Vue: true

beautify: (text, language, options) ->
return new @Promise((resolve, reject) ->
regexp = /(<(template|script|style)[^>]*>)((\s|\S)*?)<\/\2>/gi

resolve(text.replace(regexp, (match, begin, type, text) ->
lang = /lang\s*=\s*['"](\w+)["']/.exec(begin)?[1]

switch type
when "template"
switch lang
when "pug", "jade"
match.replace(text, "\n" + require("pug-beautify")(text, options) + "\n")
when undefined
match.replace(text, "\n" + require("js-beautify").html(text, options) + "\n")
else
match
when "script"
match.replace(text, "\n" + require("js-beautify")(text, options) + "\n")
when "style"
switch lang
when "sass", "scss"
options = _.merge options,
source: text
lang: "scss"
mode: "beautify"
match.replace(text, prettydiff.api(options)[0])
when "less"
options = _.merge options,
source: text
lang: "less"
mode: "beautify"
match.replace(text, prettydiff.api(options)[0])
when undefined
match.replace(text, "\n" + require("js-beautify").css(text, options) + "\n")
else
match
))
)
1 change: 1 addition & 0 deletions src/languages/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module.exports = class Languages
"twig"
"typescript"
"vala"
"vue"
"visualforce"
"xml"
"xtemplate"
Expand Down
23 changes: 23 additions & 0 deletions src/languages/vue.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {

name: "Vue"
namespace: "vue"
fallback: ['html']

###
Supported Grammars
###
grammars: [
"Vue Component"
]

###
Supported extensions
###
extensions: [
"vue"
]

defaultBeautifier: "Vue Beautifier"

}

0 comments on commit 6a99b0a

Please sign in to comment.