Fixes #1557. Fix Vue beautifier adding extra newlines

This commit is contained in:
Glavin Wiechert 2017-04-15 16:03:56 -03:00
parent cd89f6f723
commit 3435e16eb4
5 changed files with 95 additions and 21 deletions

View File

@ -0,0 +1,29 @@
<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>

View File

@ -0,0 +1,4 @@
<template lang="jade">
.test text...
.test2 text2...
</template>

View File

@ -0,0 +1,28 @@
<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>

View File

@ -0,0 +1,4 @@
<template lang="jade">
.test text...
.test2 text2...
</template>

View File

@ -1,7 +1,5 @@
"use strict"
Beautifier = require('./beautifier')
prettydiff = require("prettydiff")
_ = require('lodash')
module.exports = class VueBeautifier extends Beautifier
name: "Vue Beautifier"
@ -11,40 +9,51 @@ module.exports = class VueBeautifier extends Beautifier
Vue: true
beautify: (text, language, options) ->
return new @Promise((resolve, reject) ->
regexp = /(^<(template|script|style)[^>]*>)((\s|\S)*)^<\/\2>/gim
return new @Promise((resolve, reject) =>
prettydiff = require("prettydiff")
_ = require('lodash')
regexp = /(^<(template|script|style)[^>]*>)((\s|\S)*?)^<\/\2>/gim
resolve(text.replace(regexp, (match, begin, type, text) ->
results = text.replace(regexp, (match, begin, type, text) =>
lang = /lang\s*=\s*['"](\w+)["']/.exec(begin)?[1]
switch type
replaceText = text
text = text.trim()
beautifiedText = (switch type
when "template"
switch lang
when "pug", "jade"
match.replace(text, "\n" + require("pug-beautify")(text, options) + "\n")
require("pug-beautify")(text, options)
when undefined
match.replace(text, "\n" + require("js-beautify").html(text, options) + "\n")
require("js-beautify").html(text, options)
else
match
undefined
when "script"
match.replace(text, "\n" + require("js-beautify")(text, options) + "\n")
require("js-beautify")(text, options)
when "style"
switch lang
when "sass", "scss"
options = _.merge options,
options = _.merge(options,
source: text
lang: "scss"
mode: "beautify"
match.replace(text, prettydiff.api(options)[0])
)
prettydiff.api(options)[0]
when "less"
options = _.merge options,
source: text
lang: "less"
mode: "beautify"
match.replace(text, prettydiff.api(options)[0])
options = _.merge(options,
source: text
lang: "less"
mode: "beautify"
)
prettydiff.api(options)[0]
when undefined
match.replace(text, "\n" + require("js-beautify").css(text, options) + "\n")
require("js-beautify").css(text, options)
else
match
))
undefined
)
result = if beautifiedText then match.replace(replaceText, "\n#{beautifiedText.trim()}\n") else match
@verbose("Vue part", match, begin, type, text, lang, result)
return result
)
@verbose("Vue final results", results)
resolve(results)
)