Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
22 contributors

Users who have contributed to this file

@feross @yoga1234 @wayou @LinusU @HCLonely @Flet @Divlo @yyjazsf @tumobi @LitoMore @LionNatsu @domdomegg
661 lines (464 sloc) 30.3 KB

Standard - JavaScript 浠g爜瑙勮寖
JavaScript Standard Style

travis npm version npm downloads Standard - JavaScript Style Guide

Sponsored by聽聽聽聽Socket 鈥 Supply Chain Dependency Security for JavaScript and npm聽聽聽聽Wormhole

EnglishEspa帽ol (Latinoam茅rica)Fran莽aisBahasa IndonesiaItaliano (Italian)鏃ユ湰瑾 (Japanese)頃滉淡鞏 (Korean)Portugu锚s (Brasil)绠浣撲腑鏂 (Simplified Chinese)绻侀珨涓枃 (Taiwanese Mandarin)

JavaScript 浠g爜瑙勮寖锛岃嚜甯 linter & 浠g爜鑷姩淇

鏈伐鍏烽氳繃浠ヤ笅涓夌鏂瑰紡涓轰綘锛堝強浣犵殑鍥㈤槦锛夎妭鐪佸ぇ閲忔椂闂达細

  • 鏃犻』閰嶇疆銆 鍙蹭笂鏈渚挎嵎鐨勭粺涓浠g爜椋庢牸鐨勬柟寮忥紝杞绘澗鎷ユ湁銆
  • 鑷姩浠g爜鏍煎紡鍖栥 鍙渶杩愯 standard --fix 浠庢鍜岃剰涔卞樊鐨勪唬鐮佽鍐嶈銆
  • 鎻愬墠鍙戠幇椋庢牸鍙婄▼搴忛棶棰樸 鍑忓皯浠g爜瀹℃煡杩囩▼涓弽鍙嶅澶嶇殑淇敼杩囩▼锛岃妭绾︽椂闂淬

鏃犻』鐘硅鲍銆傚啀涔熶笉鐢ㄧ淮鎶 .eslintrc, .jshintrc, or .jscsrc 銆傚紑绠卞嵆鐢ㄣ

瀹夎锛

npm install standard --save-dev

缁嗗垯

  • 浣跨敤涓や釜绌烘牸 鈥 杩涜缂╄繘
  • 瀛楃涓蹭娇鐢ㄥ崟寮曞彿 鈥 闇瑕佽浆涔夌殑鍦版柟闄ゅ
  • 涓嶅啀鏈夊啑浣欑殑鍙橀噺 鈥 杩欐槸瀵艰嚧 澶ч噺 bug 鐨勬簮澶!
  • 鏃犲垎鍙娌′粈涔堜笉濂姐涓嶉獥浣狅紒
  • 琛岄涓嶈浠 (, [, or ` 寮澶
    • 杩欐槸鐪佺暐鍒嗗彿鏃鍞竴浼氶犳垚闂鐨勫湴鏂 鈥 宸ュ叿閲屽凡鍔犱簡鑷姩妫娴嬶紒
    • 璇︽儏
  • 鍏抽敭瀛楀悗鍔犵┖鏍 if (condition) { ... }
  • 鍑芥暟鍚嶅悗鍔犵┖鏍 function name (arg) { ... }
  • 鍧氭寔浣跨敤鍏ㄧ瓑 === 鎽掑純 == 涓浣嗗湪闇瑕佹鏌 null || undefined 鏃跺彲浠ヤ娇鐢 obj == null
  • 涓瀹氳澶勭悊 Node.js 涓敊璇洖璋冧紶閫掕繘鏉ョ殑 err 鍙傛暟銆
  • 浣跨敤娴忚鍣ㄥ叏灞鍙橀噺鏃跺姞涓 window 鍓嶇紑 鈥 documentnavigator 闄ゅ
    • 閬垮厤鏃犳剰涓娇鐢ㄥ埌浜嗚繖浜涘懡鍚嶇湅涓婂幓寰堟櫘閫氱殑鍏ㄥ眬鍙橀噺锛 open, length, event 杩樻湁 name
  • 鏌ョ湅鏇村涓轰綍涓嶈瘯璇 standard 瑙勮寖鍛紒

璇翠簡閭d箞澶氾紝鐪嬬湅杩欎釜閬靛惊浜 Standard 瑙勮寖鐨勭ず渚嬫枃浠 涓殑浠g爜鍚с傛垨鑰咃紝杩欓噷杩樻湁涓澶ф尝浣跨敤浜嗘瑙勮寖鐨勯」鐩 浠g爜鍙緵鍙傝冦

鐩綍

瀹夎

浣跨敤鏈鑼冩渶渚挎嵎鐨勬柟寮忔槸鍏ㄥ眬瀹夎锛岃繍琛岋細

$ npm install standard --global

鎴栬呴潪鍏ㄥ眬鐨勬柟寮忥紝閽堝鏌愪釜椤圭洰杩涜瀹夎锛

$ npm install standard --save-dev

娉ㄦ剰锛氳繍琛屼互涓婂懡浠ょ殑鍓嶆彁鏄凡缁忓畨瑁呬簡 Node.jsnpm

浣跨敤

瀹夎瀹屽氨鍙互寮蹇冧娇鐢ㄤ簡銆傛渶绠鍗曠殑浣跨敤鍦烘櫙鏄鏌ラ」鐩唴鎵鏈夌殑 JavaScript 鏂囦欢锛

$ standard
Error: Use JavaScript Standard Style
  lib/torrent.js:950:11: Expected '===' and instead saw '=='.

鍙互璺熶笂 glob 褰㈠紡鐨勮矾寰勫弬鏁帮紝浣嗚寰楀甫寮曞彿浠ョ‘淇 standard 宸ュ叿姝g‘瑙f瀽锛屽惁鍒欎細琚懡浠よ瑙f瀽銆

$ standard "src/util/**/*.js" "test/**/*.js"

娉ㄦ剰锛 standard 榛樿鏌ユ壘 **/*.js, **/*.jsx 鎵鍖归厤鍒扮殑鏂囦欢銆

濡傛灉浣犺仾鏄庣殑璇濅細杩欐牱鍋

  1. 娣诲姞閰嶇疆鍒 package.json
{
  "name": "my-cool-package",
  "devDependencies": {
    "standard": "*"
  },
  "scripts": {
    "test": "standard && node my-tests.js"
  }
}
  1. 杩欐牱妫鏌ュ伐浣滃氨浼氬湪杩愯 npm test 鑷姩杩涜
$ npm test
Error: Use JavaScript Standard Style
  lib/torrent.js:950:11: Expected '===' and instead saw '=='.
  1. 浠庢鍛婂埆鍦ㄦ彁 PR 鏃剁殑浠g爜椋庢牸鐨勯棶棰橈紒

涓轰綍瑕佷娇鐢 JavaScript Standard 瑙勮寖锛

鏈鑼冪壒鐐逛箣涓鏄畝娲併傝皝涔熶笉鎯充负姣忎釜椤圭洰缁存姢涓浠芥湁鎴愮櫨涓婂崈琛岃鍙ョ殑浠g爜椋庢牸閰嶇疆鏂囦欢銆傛湁姝よ鑼冨氨澶熶簡銆

鏈伐鍏烽氳繃浠ヤ笅涓夌鏂瑰紡涓轰綘锛堝強浣犵殑鍥㈤槦锛夎妭鐪佸ぇ閲忔椂闂达細

  • 鏃犻』閰嶇疆銆 鍙蹭笂鏈渚挎嵎鐨勭粺涓浠g爜椋庢牸鐨勬柟寮忥紝杞绘澗鎷ユ湁銆
  • 鑷姩鐨勪唬鐮佹牸寮忓寲銆 鍙渶杩愯 standard --fix 浠庢鍜岃剰涔卞樊鐨勪唬鐮佽鍐嶈銆
  • 鎻愬墠鍙戠幇椋庢牸鍙婄▼搴忛棶棰樸 鍑忓皯浠g爜瀹℃煡鏃剁殑鍙嶅弽澶嶅淇敼杩囩▼锛岃妭绾︽椂闂淬

涓鏃︿娇鐢 standard 瑙勮寖琛ㄦ槑浠g爜鐨勭畝鏄庢у強绀惧尯鐨勭害瀹氳楂樹簬涓汉鐨勭紪鐮侀鏍笺傝繖涓嶄竴瀹100%閫傜敤浜庢墍鏈夐」鐩拰澶氬厓鐨勭紪绋嬫枃鍖栵紝浣嗗紑婧愰」鐩唬鐮佸鏄撳彈鍒版柊鎵嬬殑褰卞搷銆傛妸瑙勮寖璁叉槑锛屼弗鏍兼墽琛屽浜庨」鐩殑闀胯繙缁存姢涓嶆棤瑁ㄧ泭銆

璋佸湪鐢 JavaScript Standard 瑙勮寖锛

鎴戜滑鏄湁缇や紬鍩虹鐨勶紒

Free MIDIs, MIDI file downloads College essays, AP notes
Your logo here Your logo here Your logo here

闄ゅ叕鍙哥粍缁囧锛寰堝涓汉涔熷湪椤圭洰涓娇鐢紝杩欓噷灏变笉涓涓缃楀垪浜嗐

骞朵笖 standard 鍦 GitHub 鐨浠g爜妫鏌ョ被宸ュ叿 灞曠ず鍒楄〃涓篃鎺掑悕绗竴銆

鏈夌幇鎴愮殑缂栬緫鍣ㄦ彃浠跺悧锛

棣栧厛瀹夎 standard銆傚墿涓嬬殑灏辨槸涓嶅悓缂栬瘧鍣ㄥ畨瑁呭搴旂殑鎻掍欢锛

Sublime Text

閫氳繃 Package Control锛屽畨瑁 SublimeLinterSublimeLinter-contrib-standard

濡傛灉鎯宠淇濆瓨鏃惰嚜鍔ㄦ牸寮忓寲锛岃繕闇瀹夎 StandardFormat

Atom

瀹夎 linter-js-standard

濡傛灉鎯宠淇濆瓨鏃惰嚜鍔ㄦ牸寮忓寲锛岃繕闇瀹夎 standard-formatter銆傚畨瑁 standardjs-snippets 鍙互鑾峰緱 snippets 鐗规с

Visual Studio Code

瀹夎 vscode-standard锛堝凡缁忓寘鍚簡鑷姩鏍煎紡鍖栵級銆

瀹夎 vscode-standardjs-snippets 浠ヨ幏寰 JS snippets銆傚畨瑁 vscode-react-standard 浠ヨ幏寰 React snippets銆

Vim

瀹夎 Syntastic 骞舵坊鍔犲涓嬮厤缃埌 .vimrc

let g:syntastic_javascript_checkers = ['standard']

濡傛灉鎯宠淇濆瓨鏃惰嚜鍔ㄦ牸寮忓寲锛屾坊鍔犱互涓嬮厤缃埌 .vimrc

autocmd bufwritepost *.js silent !standard --fix %
set autoread

Emacs

瀹夎 Flycheck 鍚庢煡鐪 manual 浠ヤ簡瑙e浣曞湪椤圭洰鍦ㄥ惎鐢ㄣ

Brackets

鎻掍欢涓悳绱 "Standard Code Style" 鐒跺悗鐐瑰嚮 "Install"銆

WebStorm (PhpStorm, IntelliJ, RubyMine, JetBrains 绛 jetbrains 鍏ㄥ妗剁郴鍒)

WebStorm 鏈杩戝甯鍦ㄥ叾 IDE 涓 鑷甫 standard 瑙勮寖銆

浣嗘槸濡傛灉浣犱粛鐒舵兂鑷繁鍔ㄦ墜閰嶇疆锛閭d箞璇风湅姝ゆ暀绋銆傛鏁欑▼閫傜敤浜 JetBrains 鍏ㄥ妗讹紝鍖呮嫭 PhpStorm銆両ntelliJ銆丷ubyMine 绛夈

鏈変笓灞炲窘绔犲彲浠ョ敤鏉ユ斁鍒伴」鐩殑 README 鏂囦欢涓悧锛

蹇呴渶鐨勶紒濡傛灉浣犵殑椤圭洰浣跨敤浜 standard 瑙勮寖锛屽彲浠ヤ换閫変竴涓笅闈㈢殑寰界珷鏀惧叆椤圭洰涓潵杩涜灞曠ず銆

JavaScript Style Guide

[![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard)

JavaScript Style Guide

[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

濡傛灉鎴戜笉鍚屾剰鏌愭潯瑙勫垯锛屽彲浠ユ敼鍚楋紵

涓嶈銆傚埗瀹氳繖濂 standard 瑙勮寖鐨勭洰鐨勫氨鏄澶у閮戒笉蹇呭啀鑺辨椂闂存氮璐瑰湪鏃犺皳鐨浠g爜椋庢牸涔嬩簤涓婇潰浜嗐傚叧浜庣缉杩涜鐢ㄥ埗琛ㄧ杩樻槸绌烘牸杩欎釜闂宸茬粡浜夎浜嗗緢涔呬簡锛屾案杩滀篃娌℃湁绛旀銆備簤璁鸿繖涓兘鍙互鎶婇渶姹傛彁鍓嶅啓瀹屼簡銆傞伒寰 standard 瑙勮寖锛屼綘灏变笉鐢ㄥ啀鐘硅鲍浜嗭紝姣曠珶涓嶇鎬庢牱浜夎鎬诲綊浼氶夋嫨涓绉嶉鏍肩殑銆傚笇鏈涘ぇ瀹朵篃鑳藉湪涓汉璇箟鍜屾櫘閫備环鍊间笂鍋氫竴涓潈琛°

濡傛灉浣犻潪瑕佽嚜宸卞幓閰嶇疆鎴愮櫨涓婂崈椤圭殑 ESLint 瑙勫垯锛岄偅浣犲彲浠ョ洿鎺ヤ娇鐢 eslint-config-standard 鏉ュ皢涓汉閰嶇疆鍖呰鍦ㄤ笂灞傘

灏忚创澹細閫夋嫨 standard 鐒跺悗淇濇寔鍚с傛妸鏃堕棿鐣欎笅鏉ヨВ鍐冲叾浠栨湁鎰忎箟鐨勯棶棰橈紒(^____^)/

姣曠珶杩欎笉鏄竴浠芥寮忕殑 Web 瑙勮寖鍟婏紒

纭疄锛佽繖浠借鑼冧笉闅跺睘浜庝换浣曞畼鏂圭粍缁囷紝鎵浠ユ墠鍙 standard/standard 鑰屼笉鏄 ECMA/standard 鍢涖

standard (鏍囧噯) 涓璇嶅湪杩欓噷涓嶅眬闄愪簬 鈥渨eb 鏍囧噯鈥 :-) 銆 涓句釜渚嬪瓙锛

  • 杩欎釜妯″潡甯姪鎴戜滑灏嗕唬鐮佺淮鎸佸湪涓涓楂樼殑姘村噯锛坰tandard of quality锛
  • 杩欎釜妯″潡纭畾椤圭洰涓殑鏂版墜閬靛惊涓浜涘熀鏈殑鏍峰紡瑙勮寖锛坰tyle standards锛

鏈夎嚜鍔ㄦ牸寮忓寲宸ュ叿涔堬紵

褰撶劧锛佷綘鍙互浣跨敤 standard --fix 鏉ョ籂姝eぇ閮ㄥ垎鐨勪唬鐮侀棶棰樸

standard --fix 鍙互淇澶ч儴鍒嗙害瀹氫織鎴愮殑闂锛屼絾鏈変簺閿欒锛堣濡傚繕璁颁簡閿欒澶勭悊锛夊彧鑳芥墜鍔ㄥ幓淇浜嗐

涓轰簡浣跨敤鏂逛究锛standard 浼氬湪妫娴嬪埌鏈夎兘澶熻嚜鍔ㄨ淇鐨勯棶棰樼殑鏃跺欙紝缁欏嚭鐩稿簲鐨勬彁绀 "杩愯 standard --fix 鏉ヨ嚜鍔ㄤ慨姝d竴浜涢棶棰"銆

濡備綍鎺掗櫎鏌愪簺鏂囦欢锛

node_modules/coverage/vendor/*.min.jsbundle.js 杩欎簺鐩綍锛岃繕鏈変互 . 寮澶寸殑鏂囦欢锛堣濡 .git/锛夋垨鑰呮枃浠跺す鑷姩琚帓闄ゅ湪澶栥

.gitignore 閲岄厤缃殑鏂囦欢涔熶細鑷姩鎺掗櫎鎺夈

鏈夋椂浣犺繕鏄渶瑕佹坊鍔犱竴浜涜嚜瀹氫箟鐨勬帓闄ゆ枃浠讹紝鍙互鍦 package.json 閲屾坊鍔 standard.ignore 灞炴ф潵閰嶇疆锛

"standard": {
  "ignore": [
    "**/out/",
    "/lib/select2/",
    "/lib/ckeditor/",
    "tmp.js"
  ]
}

濡備綍闅愯棌鏌愮被璀﹀憡锛

寰堝皯鐨勬儏鍐典笅浣犻渶瑕佺粫寮 standard 浠ラ殣钘忔煇浜涜鍛婁俊鎭

JavaScript Standard 浠g爜瑙勮寖搴曞眰浣跨敤鐨勬槸 ESLint銆傛墍浠ュ鏋滀綘鎯抽殣钘忔煇浜涜鍛婏紝鏂规硶鍜屼娇鐢 ESLint 鏃朵竴鏍枫

瀵规煇涓琛岀鐢鎵鏈夎鍒

file = 'I know what I am doing' // eslint-disable-line

鎴栬咃紝鍙"no-use-before-define" 杩欐潯瑙勫垯锛

file = 'I know what I am doing' // eslint-disable-line no-use-before-define

鎴栬咃紝瀵澶氳绂佺敤 "no-use-before-define" 杩欎竴瑙勫垯锛

/* eslint-disable no-use-before-define */
console.log('offending code goes here...')
console.log('offending code goes here...')
console.log('offending code goes here...')
/* eslint-enable no-use-before-define */

浣跨敤鐨勪笁鏂规彃浠跺悜鍏ㄥ眬鏆撮湶浜嗗彉閲忥紝濡備綍閬垮厤 "variable is not defined" 鐨勯敊璇彁绀猴紵

涓浜涗笁鏂瑰簱锛堟瘮濡 mocha锛変細鍚戝叏灞鏆撮湶鍙橀噺锛describeit锛夈傝繖浜涘彉閲忔垨鏂规硶鍗虫病鏈夊畾涔夛紝涔熸病鏈夎 require 杩涙潵锛屾墍浠 standard 浼氭姤鍑哄彉閲忔湭瀹氫箟鐨勮鍛婏紙杩欑璀﹀憡閫氬父鎯呭喌涓嬫槸寰堟湁鐢ㄧ殑锛夈傝繖绉嶆儏鍐典笅鎴戜滑鎯冲杩欎簺鍏ㄥ眬鍙橀噺绂佺敤妫鏌ャ

涓轰簡璁 standard 妫娴嬮氳繃锛堝悓鏃朵篃浣夸唬鐮佹洿鍔犳槗鎳傦級锛屽湪鏂囦欢椤堕儴娣诲姞濡備笅閰嶇疆锛

/* global myVar1, myVar2 */

浣嗗鏋滀綘闇瑕佹坊鍔犵殑鏂囦欢澶锛岃繖绉嶆柟寮忓氨鏄惧緱绻佺悙浜嗐傝繖绉嶆儏鍐典笅锛岃繍琛岋細

$ standard --global myVar1 --global myVar2

鎴栬呭湪 package.json 閲岄厤缃細

{
  "standard": {
    "globals": [ "myVar1", "myVar2" ]
  }
}

娉ㄦ剰锛globalglobals 鏁堟灉涓鏍

濡備綍鎵嶈兘浣跨敤澶勪簬瀹為獙闃舵鐨 JavaScript 鐗规э紙璀 ES Next锛夛紵

standard 鏀寔鏈鏂扮殑 ECMAScript 鐗规э紝ES8锛圗S2017锛夛紝鍖呮嫭澶勪簬 鈥淪tage 4鈥 浠嶅湪鎻愭闃舵鐨勭壒鎬с

涓轰簡鏀寔瀹為獙鎬х殑鐗规э紝standard 鏀寔鑷畾涔 JavaScript 瑙f瀽鍣ㄣ傛坊鍔犺嚜瀹氫箟瑙f瀽鍣ㄥ墠璇锋濊冧竴涓嬪繀瑕佹с

浠 npm 瀹夎骞朵娇鐢ㄨ嚜瀹氫箟鐨勮В鏋愬櫒锛堢ず渚嬶細npm install @babel/eslint-parser锛夛細

$ standard --parser @babel/eslint-parser

鎴栬呭皢鍏舵坊鍔犲埌 package.json 閰嶇疆涓細

{
  "standard": {
    "parser": "@babel/eslint-parser"
  }
}

濡傛灉鍏ㄥ眬瀹夎锛npm install standard --global锛変簡 standard 鐨勮瘽锛岄偅涔堣纭繚 @babel/eslint-parser 涔熺敤 npm install @babel/eslint-parser --global 鍏ㄥ眬瀹夎銆

鎴戣兘浣跨敤鍏朵粬 JavaScript 鍙樼鍚楋紝渚嬪 Flow锛

鍚屾牱鍦帮紝鎯宠浣跨敤涓涓 JS 鍙樼涔嬪墠锛屽厛鑰冭檻娣诲姞鍜屼娇鐢ㄥ畠鎵甯︽潵鐨勫鏉傚害鐪嬫槸鍚﹀煎緱杩欎箞鍘诲仛銆

standard 鏀寔 ESLint 鎻掍欢銆傚湪 standard 澶勭悊浠g爜鍓嶏紝浣跨敤浠讳綍涓涓彃浠舵潵灏嗕唬鐮佺紪璇戞垚鍚堟硶鐨 JS 鍗冲彲銆 浠 npm 瀹夎涓涓嚜瀹氫箟鐨勮В鏋愬櫒 (绀轰緥锛npm install eslint-plugin-flowtype) 鐒跺悗杩愯锛

$ standard --plugin flowtype

鎴栬呮坊鍔犲埌package.json:

{
  "standard": {
    "plugins": [ "flowtype" ]
  }
}

濡傛灉鍏ㄥ眬瀹夎锛npm install standard --global锛変簡 standard 鐨勮瘽锛岄偅涔堣纭繚 eslint-plugin-flowtype 涔熺敤 npm install eslint-plugin-flowtype --global 鍏ㄥ眬瀹夎銆

娉ㄦ剰锛pluginplugins 绛変环

濡備綍涓 Mocha锛孞asmine 鍜 QUnit 杩欎簺娴嬭瘯宸ュ叿鎼厤宸ヤ綔?

涓轰簡鑳借 mocha 鍦ㄤ綘鐨勬祴璇曟枃浠朵腑宸ヤ綔锛屽皢浠ヤ笅閰嶇疆娣诲姞鍒版祴璇曟枃浠跺ご閮細

/* eslint-env mocha */

鎴栬呰繍琛岋細

$ standard --env mocha

涓婇潰 mocha 涔熷彲浠ユ槸 jasmine, qunit, phantomjs 绛夊悓绫诲伐鍏枫傝繖閲屾湁涓潵鑷 ESLint 鐨勫畬鏁村垪琛 鐜閰嶇疆 鏂囨。銆傜幆澧冧腑鎵鏈夊彲鐢ㄧ殑鍏ㄥ眬鍙橀噺鍙互杩欎釜 globals npm 鍖呬腑鏌ュ埌銆

娉ㄦ剰: envenvs 鐢ㄥ摢涓兘涓鏍枫

Web Workers 鏈夎冭檻杩囦箞锛

娣诲姞浠ヤ笅娉ㄩ噴鍒版枃浠跺ご閮細

/* eslint-env serviceworker */

杩欐牱鍙互璁 standard 鐭ラ亾 self 鏄 web worker 涓殑鍏ㄥ眬鍙橀噺锛堝悓鏃朵篃璁╀汉鏇村鏄撶湅鎳傦級銆

Markdown 鎴栬 HTML 鏂囦欢涓殑浠g爜鑳芥鏌ュ埌鍚楋紵

鍙互浣跨敤 standard-markdown 鏉ユ鏌 Markdown 閲岀殑鍖轰綅鐮併

姝ゅ锛屼篃鏈 ESLint 鎻掍欢鍙互妫鏌 Markdown銆丠TML 绛夊叾浠栫被鍨嬫枃浠朵腑鐨勪唬鐮侊細

妫鏌 Markdown 鏂囦欢涓殑浠g爜锛屽彲浠ョ敤杩欎釜 ESLint 鎻掍欢锛

$ npm install eslint-plugin-markdown

鐒跺悗杩愯浠ヤ笅鍛戒护鏉ユ鏌ユ枃浠朵唬鐮佸潡涓殑浠g爜锛

$ standard --plugin markdown '**/*.md'

HTML 鏂囦欢鍙互涓嬮潰杩欎釜鎻掍欢锛

$ npm install eslint-plugin-html

鐒跺悗杩愯浠ヤ笅鍛戒护鏉ユ鏌ュ寘鍚湪 <script> 鏍囩涓殑浠g爜锛

$ standard --plugin html '**/*.html'

鏈変负 git 娣诲姞 pre-commit 閽╁瓙涔堬紵

杩欎釜闂闂緱濂斤紒

#!/bin/bash

# 纭繚灏嗚鎻愪氦鐨勬墍鏈 JavaScript 浠g爜閫氳繃 standard 瑙勮寖鐨勬鏌
function xargs-r() {
  # Portable version of "xargs -r". The -r flag is a GNU extension that
  # prevents xargs from running if there are no input files.
  if IFS= read -r -d $'\n' path; then
    echo "$path" | cat - | xargs "$@"
  fi
}
git diff --name-only --cached --relative | grep '\.jsx\?$' | sed 's/[^[:alnum:]]/\\&/g' | xargs-r -E '' -t standard
if [[ $? -ne 0 ]]; then
  echo 'JavaScript Standard Style errors were detected. Aborting commit.'
  exit 1
fi

鎬庢牱浣胯緭鍑哄ソ鐪嬩簺锛屽甫棰滆壊锛

鑷甫鐨勮緭鍑轰俊鎭畝娲佸師濮嬶紝濡傛灉鎯宠鐐叿濂界湅锛屽畨瑁 snazzy

$ npm install snazzy

鐒跺悗杩愯锛

$ standard | snazzy

杩樻湁 standard-tapstandard-jsonstandard-reporterstandard-summary 杩欎簺绫讳技鐨勫伐鍏枫

鏈夌浉鍏崇殑 Node.js API 娌★紵

鏈夛紒

async standard.lintText(text, [opts])

妫鏌ヤ紶鍏ョ殑 text銆傞渶瑕佹彁渚涗竴涓 opts 閰嶇疆鍙傛暟锛

{
  // unique to lintText
  filename: '',         // path of file containing the text being linted

  // common to lintText and lintFiles
  cwd: '',              // current working directory (default: process.cwd())
  fix: false,           // automatically fix problems
  extensions: [],       // file extensions to lint (has sane defaults)
  globals: [],          // custom global variables to declare
  plugins: [],          // custom eslint plugins
  envs: [],             // custom eslint environment
  parser: '',           // custom js parser (e.g. babel-eslint)
  usePackageJson: true, // use options from nearest package.json?
  useGitIgnore: true    // use file ignore patterns from .gitignore?
}

濡傛灉 package.json 鏈夌浉搴旈厤缃篃浼氳嚜鍔ㄨ璇诲彇鍒般

鍖呭惈缁撴灉鐨 results 鍖呭惈濡備笅灞炴э細

const results = {
  results: [
    {
      filePath: '',
      messages: [
        { ruleId: '', message: '', line: 0, column: 0 }
      ],
      errorCount: 0,
      warningCount: 0,
      output: '' // fixed source code (only present with {fix: true} option)
    }
  ],
  errorCount: 0,
  warningCount: 0
}

async standard.lintFiles(files, [opts])

妫鏌ヤ互 glob 褰㈠紡鎸囧畾鐨 files 鍙傛暟鎵鍖归厤鍒扮殑鎵鏈夋枃浠躲傚彲浠ヤ紶鍏ヤ竴涓 opts 閰嶇疆鍙傛暟锛

{
  // unique to lintFiles
  ignore: [],           // file globs to ignore (has sane defaults)

  // common to lintText and lintFiles
  cwd: '',              // current working directory (default: process.cwd())
  fix: false,           // automatically fix problems
  extensions: [],       // file extensions to lint (has sane defaults)
  globals: [],          // custom global variables to declare
  plugins: [],          // custom eslint plugins
  envs: [],             // custom eslint environment
  parser: '',           // custom js parser (e.g. babel-eslint)
  usePackageJson: true, // use options from nearest package.json?
  useGitIgnore: true    // use file ignore patterns from .gitignore?
}

鍥炶皟鍚屼笂銆

濡備綍鍙備笌鍒 standard 瑙勮寖涓潵锛

娆㈣繋鍙備笌杩涙潵锛侀涢 issues 鎴栬 PRs 椤甸潰锛屽鏋滃彂鐜版病鏈夋壘鍒版兂瑕佺殑闂鍙互鑷繁鎻愪竴涓

鏉 freenode 鐨 #standard 棰戦亾涓庡叾浠栦汉涓璧疯亰澶╄璁猴紒

浠ヤ笅鏄竴浜 standard 鐢熸侀噷姣旇緝閲嶈鐨勫寘锛

杩樻湁寰堝 缂栬緫鍣ㄦ彃浠銆佷竴涓 **浣跨敤 standard 瑙勮寖寮鍙戠殑 npm 鍖鐨勫垪琛ㄣ佽繕鏈変竴鍒嗛潪甯告鐨 standard 鐢熸佷腑鎵浣跨敤鍒扮殑鍖**鐨勫垪琛ㄣ

鍗忚

MIT. Copyright (c) Feross Aboukhadijeh.