我们推荐在团队中使用统一的 Lint 规范和 commit 约束项目进行规范开发, Lint 可以根据现在团队中的使用情况

推荐 airbnb javascript style guid: https://github.com/airbnb/javascript

TS 代码规范

  1. {
  2. // - Enforces function overloads to be consecutive. -
  3. adjacent-overload-signatures: true,
  4. // - 禁止逗号运算符。 -
  5. ban-comma-operator:true,
  6. // - 禁止类型 -
  7. ban-type: [true, ["object","User {} instead."],["string"]],
  8. // - 类成员必须声明 private public ...
  9. member-access: [true , "no-public"||"check-accessor"|| "check-constructor" || "check-parameter-property" ],
  10. // - 类声明排序 -
  11. member-order: [true, {order:....}],
  12. // - 不需使用any类型 -
  13. no-any: true,
  14. // - 禁止空接口 -
  15. no-empty-interface:true,
  16. // - 禁止导入带有副作用的语句
  17. no-import-side-effect: [true, {"ignore-module": "(\\.html|\\.css)$"}],
  18. // - 不允许将变量或参数初始化为数字,字符串或布尔值的显式类型声明。 -
  19. no-inferrable-types: [true, "ignore-params", "ignore-properties"],
  20. // - 不允许内部模块
  21. no-internal-module:true,
  22. // - 不允许在变量赋值之外使用常量数值。当没有指定允许值列表时,默认允许-1,0和1
  23. no-magic-numbers: [true,1,2,3],
  24. // - 不允许使用内部modules和命名空间
  25. no-namespace: [ true,"allpw-declarations"],
  26. // - 不允许使用!后缀操作符的非空断言。
  27. no-non-null-assertion: true ,
  28. // - 不允许重新分配参数
  29. no-parameter-reassignment: true,
  30. // - 禁止使用/// <reference path=> 导入 ,使用import代替
  31. no-reference: true,
  32. // - 如果类型断言没有改变表达式的类型就发出警告
  33. no-unnecessary-type-assertion: true,
  34. no-var-requires: true, //不允许使用var module = require("module"),用 import foo = require('foo')导入
  35. only-arrow-functions: [true,"allow-declarations","allow-named-functions"], //允许箭头表达式,不需要传统表达式 ; 允许独立的函数声明 ;允许表达,function foo() {}但不是function() {}
  36. prefer-for-of:true, //建议使用for(..of)
  37. promise-function-async: true, 要求异步函数返回promise
  38. typedef: [true, "call-signature", "parameter", "member-variable-declaration"], //需要定义的类型存在
  39. typedef-whitespace: true, //类型声明的冒号之前是否需要空格
  40. unified-signatures: true, //重载可以被统一联合成一个
  41. //function 专用
  42. await-promise: true, //警告不是一个promise的await
  43. ban: [
  44. true,
  45. "eval",
  46. {"name": "$", "message": "please don't"},
  47. ["describe", "only"],
  48. {"name": ["it", "only"], "message": "don't focus tests"},
  49. {
  50. "name": ["chai", "assert", "equal"],
  51. "message": "Use 'strictEqual' instead."
  52. },
  53. {"name": ["*", "forEach"], "message": "Use a regular for loop instead."}
  54. ],
  55. curly: true, //for if do while 要有括号
  56. forin:true, //用for in 必须用if进行过滤
  57. import-blacklist:true, //允许使用import require导入具体的模块
  58. label-postion: true, //允许在do/for/while/swith中使用label
  59. no-arg:true, //不允许使用 argument.callee
  60. no-bitwise:true, //不允许使用按位运算符
  61. no-conditional-assignmen: true, //不允许在do-while/for/if/while判断语句中使用赋值语句
  62. no-console: true, //不能使用console
  63. no-construct: true, //不允许使用 String/Number/Boolean的构造函数
  64. no-debugger: true, //不允许使用debugger
  65. no-duplicate-super: true, //构造函数两次用super会发出警告
  66. no-empty:true, //不允许空的块
  67. no-eval: true, //不允许使用eval
  68. no-floating-promises: true, //必须正确处理promise的返回函数
  69. no-for-in-array: true, //不允许使用for in 遍历数组
  70. no-implicit-dependencies: true, //不允许在项目的package.json中导入未列为依赖项的模块
  71. no-inferred-empty-object-type: true, //不允许在函数和构造函数中使用{}的类型推断
  72. no-invalid-template-strings: true, //警告在非模板字符中使用${
  73. no-invalid-this: true, //不允许在非class中使用 this关键字
  74. no-misused-new: true, //禁止定义构造函数或new class
  75. no-null-keyword: true, //不允许使用null关键字
  76. no-object-literal-type-assertion: true, //禁止objext出现在类型断言表达式中
  77. no-return-await: true, //不允许return await
  78. arrow-parens: true, //箭头函数定义的参数需要括号
  79. "quotemark":[true, "single","avoid-escape"], //引号的使用规则
  80. "semicolon":[true, "never", "ignore-interfaces"], //分号的使用规则
  81. "indent":[true, "tabs", 2], //使用Tab进行缩进,每次强制缩进2个字符
  82. "typedef-whitespace": false, //在类型定义的时候,是否允许使用空格, 使用false,表示不对此项进行校验,不启用此项的校验
  83. "whitespace": false, //空格的校验
  84. "member-access": false, //类成员的显示可见性声明,即显示定义一个类的成员是否可见,即对类成员定义public | static 等
  85. "one-line": false, //要求指定的标记与它们之前的表达式位于同一行
  86. "trailing-comma": [true, { //对尾随逗号的校验
  87. "multiline": {
  88. "objects": "ignore",
  89. "arrays": "never",
  90. "functions": "never",
  91. "typeLiterals": "ignore"
  92. },
  93. "esSpecCompliant": true //是否允许尾随逗号出现在剩余变量中
  94. }]
  95. }

CSS 代码规范

image.png
Those layers are:

  • Settings – used with preprocessors and contain font, colors definitions, etc.
  • Tools – globally used mixins and functions. It’s important not to output any CSS in the first 2 layers.
  • Generic – reset and/or normalize styles, box-sizing definition, etc. This is the first layer which generates actual CSS.
  • Elements – styling for bare HTML elements (like H1, A, etc.). These come with default styling from the browser so we can redefine them here.
  • Objects – class-based selectors which define undecorated design patterns, for example the media object known from OOCSS
  • Components – specific UI components. This is where most of our work takes place. We often compose UI components of Objects and Components**
  • Utilities – utilities and helper classes with ability to override anything which goes before in the triangle, e.g. hide helper class
  1. .declaration-order {
  2. display: block;
  3. float: right;
  4. position: absolute;
  5. top: 0;
  6. right: 0;
  7. bottom: 0;
  8. left: 0;
  9. z-index: 100;
  10. border: 1px solid #e5e5e5;
  11. border-radius: 3px;
  12. width: 100px;
  13. height: 100px;
  14. font: normal 13px "Helvetica Neue", sans-serif;
  15. line-height: 1.5;
  16. text-align: center;
  17. color: #333;
  18. background-color: #f5f5f5;
  19. opacity: 1;
  20. }
  21. // 下面是推荐的属性的顺序
  22. [
  23. [
  24. "display",
  25. "visibility",
  26. "float",
  27. "clear",
  28. "overflow",
  29. "overflow-x",
  30. "overflow-y",
  31. "clip",
  32. "zoom"
  33. ],
  34. [
  35. "table-layout",
  36. "empty-cells",
  37. "caption-side",
  38. "border-spacing",
  39. "border-collapse",
  40. "list-style",
  41. "list-style-position",
  42. "list-style-type",
  43. "list-style-image"
  44. ],
  45. [
  46. "-webkit-box-orient",
  47. "-webkit-box-direction",
  48. "-webkit-box-decoration-break",
  49. "-webkit-box-pack",
  50. "-webkit-box-align",
  51. "-webkit-box-flex"
  52. ],
  53. [
  54. "position",
  55. "top",
  56. "right",
  57. "bottom",
  58. "left",
  59. "z-index"
  60. ],
  61. [
  62. "margin",
  63. "margin-top",
  64. "margin-right",
  65. "margin-bottom",
  66. "margin-left",
  67. "-webkit-box-sizing",
  68. "-moz-box-sizing",
  69. "box-sizing",
  70. "border",
  71. "border-width",
  72. "border-style",
  73. "border-color",
  74. "border-top",
  75. "border-top-width",
  76. "border-top-style",
  77. "border-top-color",
  78. "border-right",
  79. "border-right-width",
  80. "border-right-style",
  81. "border-right-color",
  82. "border-bottom",
  83. "border-bottom-width",
  84. "border-bottom-style",
  85. "border-bottom-color",
  86. "border-left",
  87. "border-left-width",
  88. "border-left-style",
  89. "border-left-color",
  90. "-webkit-border-radius",
  91. "-moz-border-radius",
  92. "border-radius",
  93. "-webkit-border-top-left-radius",
  94. "-moz-border-radius-topleft",
  95. "border-top-left-radius",
  96. "-webkit-border-top-right-radius",
  97. "-moz-border-radius-topright",
  98. "border-top-right-radius",
  99. "-webkit-border-bottom-right-radius",
  100. "-moz-border-radius-bottomright",
  101. "border-bottom-right-radius",
  102. "-webkit-border-bottom-left-radius",
  103. "-moz-border-radius-bottomleft",
  104. "border-bottom-left-radius",
  105. "-webkit-border-image",
  106. "-moz-border-image",
  107. "-o-border-image",
  108. "border-image",
  109. "-webkit-border-image-source",
  110. "-moz-border-image-source",
  111. "-o-border-image-source",
  112. "border-image-source",
  113. "-webkit-border-image-slice",
  114. "-moz-border-image-slice",
  115. "-o-border-image-slice",
  116. "border-image-slice",
  117. "-webkit-border-image-width",
  118. "-moz-border-image-width",
  119. "-o-border-image-width",
  120. "border-image-width",
  121. "-webkit-border-image-outset",
  122. "-moz-border-image-outset",
  123. "-o-border-image-outset",
  124. "border-image-outset",
  125. "-webkit-border-image-repeat",
  126. "-moz-border-image-repeat",
  127. "-o-border-image-repeat",
  128. "border-image-repeat",
  129. "padding",
  130. "padding-top",
  131. "padding-right",
  132. "padding-bottom",
  133. "padding-left",
  134. "width",
  135. "min-width",
  136. "max-width",
  137. "height",
  138. "min-height",
  139. "max-height"
  140. ],
  141. [
  142. "font",
  143. "font-family",
  144. "font-size",
  145. "font-weight",
  146. "font-style",
  147. "font-variant",
  148. "font-size-adjust",
  149. "font-stretch",
  150. "font-effect",
  151. "font-emphasize",
  152. "font-emphasize-position",
  153. "font-emphasize-style",
  154. "font-smooth",
  155. "line-height",
  156. "text-align",
  157. "-webkit-text-align-last",
  158. "-moz-text-align-last",
  159. "-ms-text-align-last",
  160. "text-align-last",
  161. "vertical-align",
  162. "white-space",
  163. "text-decoration",
  164. "text-emphasis",
  165. "text-emphasis-color",
  166. "text-emphasis-style",
  167. "text-emphasis-position",
  168. "text-indent",
  169. "-ms-text-justify",
  170. "text-justify",
  171. "letter-spacing",
  172. "word-spacing",
  173. "-ms-writing-mode",
  174. "text-outline",
  175. "text-transform",
  176. "text-wrap",
  177. "-ms-text-overflow",
  178. "text-overflow",
  179. "text-overflow-ellipsis",
  180. "text-overflow-mode",
  181. "-ms-word-wrap",
  182. "word-wrap",
  183. "-ms-word-break",
  184. "word-break"
  185. ],
  186. [
  187. "color",
  188. "background",
  189. "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader",
  190. "background-color",
  191. "background-image",
  192. "background-repeat",
  193. "background-attachment",
  194. "background-position",
  195. "-ms-background-position-x",
  196. "background-position-x",
  197. "-ms-background-position-y",
  198. "background-position-y",
  199. "-webkit-background-clip",
  200. "-moz-background-clip",
  201. "background-clip",
  202. "background-origin",
  203. "-webkit-background-size",
  204. "-moz-background-size",
  205. "-o-background-size",
  206. "background-size"
  207. ],
  208. [
  209. "outline",
  210. "outline-width",
  211. "outline-style",
  212. "outline-color",
  213. "outline-offset",
  214. "opacity",
  215. "filter:progid:DXImageTransform.Microsoft.Alpha(Opacity",
  216. "-ms-filter:\\'progid:DXImageTransform.Microsoft.Alpha",
  217. "-ms-interpolation-mode",
  218. "-webkit-box-shadow",
  219. "-moz-box-shadow",
  220. "box-shadow",
  221. "filter:progid:DXImageTransform.Microsoft.gradient",
  222. "-ms-filter:\\'progid:DXImageTransform.Microsoft.gradient",
  223. "text-shadow"
  224. ],
  225. [
  226. "-webkit-transition",
  227. "-moz-transition",
  228. "-ms-transition",
  229. "-o-transition",
  230. "transition",
  231. "-webkit-transition-delay",
  232. "-moz-transition-delay",
  233. "-ms-transition-delay",
  234. "-o-transition-delay",
  235. "transition-delay",
  236. "-webkit-transition-timing-function",
  237. "-moz-transition-timing-function",
  238. "-ms-transition-timing-function",
  239. "-o-transition-timing-function",
  240. "transition-timing-function",
  241. "-webkit-transition-duration",
  242. "-moz-transition-duration",
  243. "-ms-transition-duration",
  244. "-o-transition-duration",
  245. "transition-duration",
  246. "-webkit-transition-property",
  247. "-moz-transition-property",
  248. "-ms-transition-property",
  249. "-o-transition-property",
  250. "transition-property",
  251. "-webkit-transform",
  252. "-moz-transform",
  253. "-ms-transform",
  254. "-o-transform",
  255. "transform",
  256. "-webkit-transform-origin",
  257. "-moz-transform-origin",
  258. "-ms-transform-origin",
  259. "-o-transform-origin",
  260. "transform-origin",
  261. "-webkit-animation",
  262. "-moz-animation",
  263. "-ms-animation",
  264. "-o-animation",
  265. "animation",
  266. "-webkit-animation-name",
  267. "-moz-animation-name",
  268. "-ms-animation-name",
  269. "-o-animation-name",
  270. "animation-name",
  271. "-webkit-animation-duration",
  272. "-moz-animation-duration",
  273. "-ms-animation-duration",
  274. "-o-animation-duration",
  275. "animation-duration",
  276. "-webkit-animation-play-state",
  277. "-moz-animation-play-state",
  278. "-ms-animation-play-state",
  279. "-o-animation-play-state",
  280. "animation-play-state",
  281. "-webkit-animation-timing-function",
  282. "-moz-animation-timing-function",
  283. "-ms-animation-timing-function",
  284. "-o-animation-timing-function",
  285. "animation-timing-function",
  286. "-webkit-animation-delay",
  287. "-moz-animation-delay",
  288. "-ms-animation-delay",
  289. "-o-animation-delay",
  290. "animation-delay",
  291. "-webkit-animation-iteration-count",
  292. "-moz-animation-iteration-count",
  293. "-ms-animation-iteration-count",
  294. "-o-animation-iteration-count",
  295. "animation-iteration-count",
  296. "-webkit-animation-direction",
  297. "-moz-animation-direction",
  298. "-ms-animation-direction",
  299. "-o-animation-direction",
  300. "animation-direction"
  301. ],
  302. [
  303. "content",
  304. "quotes",
  305. "counter-reset",
  306. "counter-increment",
  307. "resize",
  308. "cursor",
  309. "-webkit-user-select",
  310. "-moz-user-select",
  311. "-ms-user-select",
  312. "user-select",
  313. "nav-index",
  314. "nav-up",
  315. "nav-right",
  316. "nav-down",
  317. "nav-left",
  318. "-moz-tab-size",
  319. "-o-tab-size",
  320. "tab-size",
  321. "-webkit-hyphens",
  322. "-moz-hyphens",
  323. "hyphens",
  324. "pointer-events"
  325. ]
  326. ]