Skip to content

Commit

Permalink
Implement Vue Beautifier
Browse files Browse the repository at this point in the history
  • Loading branch information
aidistan committed Aug 16, 2016
1 parent 06773e3 commit 7ebc41b
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
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>
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

This comment has been minimized.

Copy link
@kaven276

kaven276 Feb 16, 2017

Contributor

only <template></template><script></script><style></style> that start at begining of line should be considered markers for section.
change
regexp = /(<(template|script|style)[^>]*>)((\s|\S)*?)<\/\2>/gi
to
regexp = /(^<(template|script|style)[^>]*>)((\s|\S)*?)^<\/\2>/gim


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")

This comment has been minimized.

Copy link
@kaven276

kaven276 Feb 16, 2017

Contributor

match.replace(text, "\n" + require("pug-beautify")(text, options) + "\n")
should remove + "\n",
match.replace(text, "\n" + require("pug-beautify")(text, options) )

the reason is

  • inner template,script,style tags should not be processed
  • template, script, style open/close tags should start at new line
  • </template>, </script>, </style> have linefeed before them already, do not append a "\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"

}

1 comment on commit 7ebc41b

@kaven276
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a vue file like this will format bad

<template>
   <div>xxx</div>
</template>

<script>
export default {
}
</script>

<style>
body { margin: 0}
</style>

formated to that add additional one blank line before </template></script></style>

<template>
   <div>xxx</div>

</template>

<script>
export default {
}

</script>

<style>
body { margin: 0}

</style>

Please sign in to comment.