动态

详情 返回 返回

VSCode 樣式格式化 - 动态 详情

在使用VSCode的插件進行less文件格式化的時候,發現會存在問題。

index.less

@prefix: test;

@{prefix}-input{
    color :red;
    width :     @base-with;
    height :         @base-height;
}

@base-size : 10px;
@base-with : @base-size + 2px;
@base-height : @base-with + 3px;

存在的問題

  1. 合併+號和數組

格式化後為:

@prefix: test;

@{prefix}-input {
    color: red;
    width: @base-with;
    height: @base-height;
}

@base-size : 10px;
@base-with : @base-size +2px;
@base-height : @base-with +3px;

編譯後為

image.png

雖然沒有報錯,但是不是你想要的結果,如果要對變量做運算的話,則會報錯

@base-height : @base-with * 2;

image.png

涉及的插件:

image.png

image.png

image.png

image.png

image.png

  1. @{prefix}和-中間會被插入空格

格式化後為:

@prefix: test;

@{prefix} -input {
  color: red;
  width: @base-with;
  height: @base-height;
}
@base-size: 10px;
@base-with: @base-size + 2px;
@base-height: @base-with + 3px;

編譯後為:

image.png

涉及的插件:

image.png

  1. 會拆分${prefix}

格式化後為:

@prefix: test;
@ {
    prefix
}

-input {
    color: red;
    width: @base-with;
    height: @base-height;
}

@base-size : 10px;
@base-with : @base-size + 2px;
@base-height : @base-with * 2;

編譯失敗:

image.png

涉及的插件:

image.png

  1. 格式化報錯,CssSyntaxError: Unknown word

image.png

涉及的插件:

image.png

image.png

  1. 格式化報錯,Now Validation Error

image.png

涉及的插件:

image.png

解決方案

基於js-beautify開發了一套VSCode插件,插件商店裏搜索formats

image.png

格式化後為:

@prefix: test;

@{prefix}-input {
    color: red;
    width: @base-with;
    height: @base-height;
}

@base-size : 10px;
@base-with : @base-size + 2px;
@base-height : @base-with + 3px;

編譯為:
image.png

user avatar Leesz 头像 aqiongbei 头像 razyliang 头像 ZhongQianwen 头像 huajianketang 头像 solvep 头像 bugDiDiDi 头像 weidewei 头像 DingyLand 头像 youyoufei 头像 ldh-blog 头像 hea1066 头像
点赞 31 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.