清心荷 發表於 2019-7-17 09:03:00

vsCode格式化插件

<p><strong>ESlint</strong>:是用来统一<strong>JavaScript</strong>代码风格的工具,<strong>不包含css、html等</strong>。</p>
<h2>背景</h2>
<p>近来研究前端,然后一直在百度上找VScode格式化(ESlint)的插件,结果找了半天都不靠谱。目前没有一个可以格式化html、css、符合ESlint的js、vue的插件,所以自己东拼西凑加实践找到解决方法。</p>
<h2>一、安装插件</h2>
<h2><img src="https://img2018.cnblogs.com/blog/1173315/201907/1173315-20190717102442392-815606889.png"></h2>
<p>1)<strong>ESlint:</strong>javascript代码检测工具,可以配置每次保存时格式化js,但每次保存只格式化一点点,你得连续按住Ctrl+S好几次,才格式化好,自行体会~~<br>
2)<strong>vetur:</strong>可以格式化html、标准css(有分号 、大括号的那种)、标准js(有分号 、双引号的那种)、vue文件,<br>
<strong>但是!</strong>格式化的标准js文件不符合ESlint规范,会给你加上双引号、分号等,像这样</p>
<p><img src="//upload-images.jianshu.io/upload_images/6879756-a7f531f9a2bdbb99.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/366/format/webp"></p>
<p>
3)<strong>Prettier - Code formatter:</strong>只关注格式化,并不具有eslint检查语法等能力,只关心格式化文件(最大长度、混合标签和空格、引用样式等),包括JavaScript · Flow · TypeScript · CSS · SCSS · Less · JSX · Vue · GraphQL · JSON · Markdown<br>
4)<strong>Manta's Stylus Supremacy:</strong> 格式化stylus的插件(不用就不装),因为vetur会把css格式化有分号 、大括号的那种,此插件会把css格式化成stylus风格,像这样</p>
<div class="image-package">
<div class="image-container">
<div class="image-view" data-width="382" data-height="199"><img src="//upload-images.jianshu.io/upload_images/6879756-ffaf25cc495575ea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/382/format/webp"></div>












</div>













</div>
<h2>二、配置settings.json信息</h2>
<p>File-&gt;Preference-&gt;Settings【也可以快捷键 ctr + ,(window系统) 直接打开】</p>
<p><img src="https://img2018.cnblogs.com/blog/1173315/201907/1173315-20190717104256901-4276878.png"></p>
<p>现在看到的是界面配置模式,点击右上角的大括号(如下图),可以打开 settings.json 文件。</p>
<p><img src="https://img2018.cnblogs.com/blog/1173315/201907/1173315-20190717103934915-111308914.png"></p>
<p>粘贴以下代码,保存即可</p>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1173315/201907/1173315-20190717103424526-1185614657.png"></p>
<p>把代码贡献一下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> vscode默认启用了根据文件类型自动设置tabsize的选项</span>
    "editor.detectIndentation": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 重新设定tabsize</span>
    "editor.tabSize": 4<span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #值设置为true时,每次保存的时候自动格式化;值设置为false时,代码格式化请按shift+alt+F</span>
    "editor.formatOnSave": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #每次保存的时候将代码按eslint格式进行修复</span>
    "eslint.autoFixOnSave": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加 vue 支持</span>
    "eslint.validate"<span style="color: rgba(0, 0, 0, 1)">: [
      </span>"javascript"<span style="color: rgba(0, 0, 0, 1)">,
      </span>"javascriptreact"<span style="color: rgba(0, 0, 0, 1)">,
      {
            </span>"language": "vue"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"autoFix": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      }
    ],
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">#让prettier使用eslint的代码格式进行校验</span>
    "prettier.eslintIntegration": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">#去掉代码结尾的分号</span>
    "prettier.semi": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">#使用带引号替代双引号</span>
    "prettier.singleQuote": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    </span>"prettier.tabWidth": 4<span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">#让函数(名)和后面的括号之间加个空格</span>
    "javascript.format.insertSpaceBeforeFunctionParenthesis": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #这个按用户自身习惯选择</span>
    "vetur.format.defaultFormatter.html": "js-beautify-html"<span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #让vue中的js按"prettier"格式进行格式化</span>
    "vetur.format.defaultFormatter.js": "prettier"<span style="color: rgba(0, 0, 0, 1)">,
    </span>"vetur.format.defaultFormatterOptions"<span style="color: rgba(0, 0, 0, 1)">: {
      </span>"js-beautify-html"<span style="color: rgba(0, 0, 0, 1)">: {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #vue组件中html代码格式化样式</span>
            "wrap_attributes": "force-aligned", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">也可以设置为“auto”,效果会不一样</span>
            "wrap_line_length": 200<span style="color: rgba(0, 0, 0, 1)">,
            </span>"end_with_newline": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
            </span>"semi": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
            </span>"singleQuote": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      },
      </span>"prettier"<span style="color: rgba(0, 0, 0, 1)">: {
            </span>"semi": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
            </span>"singleQuote": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      }
    },
    </span>""<span style="color: rgba(0, 0, 0, 1)">: {
      </span>"editor.defaultFormatter": "esbenp.prettier-vscode"<span style="color: rgba(0, 0, 0, 1)">
    },
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 格式化stylus, 需安装Manta's Stylus Supremacy插件</span>
    "stylusSupremacy.insertColons": <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否插入冒号</span>
    "stylusSupremacy.insertSemicolons": <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否插入分号</span>
    "stylusSupremacy.insertBraces": <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否插入大括号</span>
    "stylusSupremacy.insertNewLineAroundImports": <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> import之后是否换行</span>
    "stylusSupremacy.insertNewLineAroundBlocks": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
    </span>"prettier.useTabs": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    </span>"files.autoSave": "off"<span style="color: rgba(0, 0, 0, 1)">,
    </span>"explorer.confirmDelete": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
    </span>""<span style="color: rgba(0, 0, 0, 1)">: {
      </span>"editor.defaultFormatter": "esbenp.prettier-vscode"<span style="color: rgba(0, 0, 0, 1)">
    },
    </span>""<span style="color: rgba(0, 0, 0, 1)">: {
      </span>"editor.defaultFormatter": "esbenp.prettier-vscode"<span style="color: rgba(0, 0, 0, 1)">
    },
    </span>"diffEditor.ignoreTrimWhitespace": <span style="color: rgba(0, 0, 255, 1)">false</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 两个选择器中是否换行</span>
}</pre>
</div>
<p>从此直接 Ctrl+S 就能一键格式化了。</p><br><br>
来源:https://www.cnblogs.com/zhoudawei/p/11198781.html
頁: [1]
查看完整版本: vsCode格式化插件