|
1 |
| -# eslint-plugin-miniprogram |
| 1 | +# eslint-plugin-miniprogram |
| 2 | + |
| 3 | +## About |
| 4 | + |
| 5 | +### Status |
| 6 | + |
| 7 | +⚠️ work in progress |
| 8 | + |
| 9 | +This ESLint plugin exists to help you lint your WeChat Mini Program code. |
| 10 | + |
| 11 | +### `.mina` files |
| 12 | + |
| 13 | +For developing with `.mina` files, you can refer to [mina-webpack](https://github.com/tinajs/mina-webpack) repo for details. |
| 14 | + |
| 15 | +## Rules |
| 16 | + |
| 17 | +### Prefer wx `promisify` (`prefer-wx-promisfy`) |
| 18 | + |
| 19 | +WeChat Mini Program introduce a new style of callbacks whick could be a new |
| 20 | +[callback hell](http://callbackhell.com/). |
| 21 | + |
| 22 | +Please use `promisify` to enter the Promise world. |
| 23 | + |
| 24 | +#### Details |
| 25 | + |
| 26 | +Prefer `promify` over wx style callbacks including `success`, `fail` and `complete`. |
| 27 | + |
| 28 | +Examples of **incorrect** code for this rule: |
| 29 | + |
| 30 | +```ts |
| 31 | +wx.request({ |
| 32 | + url: "https://api.airbnb.cn/", |
| 33 | + success(res) { |
| 34 | + console.log(res); |
| 35 | + }, |
| 36 | + fail(error) { |
| 37 | + console.error(error); |
| 38 | + }, |
| 39 | + complete() { |
| 40 | + console.log("complete"); |
| 41 | + } |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +Examples of **correct** code for this rule: |
| 46 | + |
| 47 | +```ts |
| 48 | +try { |
| 49 | + const res = await promisify(wx.request)({ |
| 50 | + url: "https://api.airbnb.cn/" |
| 51 | + }); |
| 52 | + console.log(res); |
| 53 | +} catch (error) { |
| 54 | + console.error(error); |
| 55 | +} finally { |
| 56 | + console.log("complete"); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +#### Related Rules |
| 61 | + |
| 62 | +- no-wx-sync-api |
| 63 | + |
| 64 | +### Disallow the use of wx.xxSync API (`no-wx-sync-api`) |
| 65 | + |
| 66 | +Sync API will block JavaScript running and cause bad performance. |
| 67 | + |
| 68 | +For example `wx.getStorageSync` costs 30~100ms CPU time: |
| 69 | + |
| 70 | +```ts |
| 71 | +console.time("sync"); |
| 72 | +wx.setStorageSync("key", "value"); |
| 73 | +console.timeEnd("sync"); |
| 74 | +``` |
| 75 | + |
| 76 | +#### Rule Details |
| 77 | + |
| 78 | +Disallow any `wx.xxxSync` API call. |
| 79 | + |
| 80 | +Examples of **incorrect** code for this rule: |
| 81 | + |
| 82 | +```ts |
| 83 | +wx.setStorageSync("key", "value"); |
| 84 | +``` |
| 85 | + |
| 86 | +Examples of **correct** code for this rule: |
| 87 | + |
| 88 | +```ts |
| 89 | +await promisify(wx.setStorage)({ |
| 90 | + key: "key", |
| 91 | + data: "value" |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +#### Related Rules |
| 96 | + |
| 97 | +- prefer-wx-promisfy |
| 98 | + |
| 99 | +### No unused component (`no-unused-components`) |
| 100 | + |
| 101 | +#### Rule Details |
| 102 | + |
| 103 | +Bad case: |
| 104 | + |
| 105 | +```vue |
| 106 | +<config> |
| 107 | +{ |
| 108 | + "component": ture, |
| 109 | + "usingComponent": { |
| 110 | + // unused `my-comp` |
| 111 | + "my-comp": "/path/to/myComp.mina" |
| 112 | + } |
| 113 | +} |
| 114 | +</config> |
| 115 | +
|
| 116 | +<template> |
| 117 | + <view>hi</view> |
| 118 | +</template> |
| 119 | +``` |
| 120 | + |
| 121 | +### No unregistered component (`no-unregistered-components`) |
| 122 | + |
| 123 | +Bad case: |
| 124 | + |
| 125 | +```vue |
| 126 | +<config> |
| 127 | +{ |
| 128 | + "component": ture, |
| 129 | + "usingComponent": { |
| 130 | + "my-comp": "/path/to/myComp.mina" |
| 131 | + } |
| 132 | +} |
| 133 | +</config> |
| 134 | +
|
| 135 | +<template> |
| 136 | + <!-- typo here --> |
| 137 | + <my-compp /> |
| 138 | +</template> |
| 139 | +``` |
| 140 | + |
| 141 | +### Validate fields in component / page config file (config-json-validate-fields) |
| 142 | + |
| 143 | +| | [WeChat](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html#%E4%BD%BF%E7%94%A8-component-%E6%9E%84%E9%80%A0%E5%99%A8%E6%9E%84%E9%80%A0%E9%A1%B5%E9%9D%A2) | [Baidu](https://smartprogram.baidu.com/docs/develop/framework/custom-component_comp/#%E4%BD%BF%E7%94%A8-Component-%E6%9E%84%E9%80%A0%E5%99%A8%E6%9E%84%E9%80%A0%E9%A1%B5%E9%9D%A2) | |
| 144 | +| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 145 | +| Use `Page` for page | no `components` | no `components` | |
| 146 | +| Use `Component` for page | `usingComponents` | `component` | |
| 147 | +| Use `Component` for component | `usingComponents` | `component` | |
| 148 | +| `navigationBarTextStyle` values | can only be `black`/`white` | |
| 149 | +| `backgroundTextStyle` values | can only be `dark`/`light` | |
| 150 | + |
| 151 | +Different Mini Program runtime has different required fields in config file ( `.json` file ). |
| 152 | + |
| 153 | +Should add `"conponent": true` if using `Component` function. |
| 154 | + |
| 155 | +```ts |
| 156 | +// comp.js |
| 157 | +Component({}); |
| 158 | +``` |
| 159 | + |
| 160 | +```html |
| 161 | +<!-- comp.mina --> |
| 162 | +<config> |
| 163 | + { "component": true, "usingComponents": {} } |
| 164 | +</config> |
| 165 | +``` |
| 166 | + |
| 167 | +Should not use `"conponent": true` if using `Page` function. |
| 168 | + |
| 169 | +```ts |
| 170 | +// page.js |
| 171 | +Page({}); |
| 172 | +``` |
| 173 | + |
| 174 | +```html |
| 175 | +<!-- page.mina --> |
| 176 | +<config> |
| 177 | + { "usingComponents": {} } |
| 178 | +</config> |
| 179 | +``` |
| 180 | + |
| 181 | +Should always add `"usingComponents": {}`. |
| 182 | + |
| 183 | +```html |
| 184 | +<!-- comp.mina --> |
| 185 | +<config> |
| 186 | + { "component": true, "usingComponents": {} } |
| 187 | +</config> |
| 188 | +``` |
| 189 | + |
| 190 | +You should only use `black` or `white` for `navigationBarTextStyle` values. |
| 191 | + |
| 192 | +You should only use `dark` or `light` for `backgroundTextStyle` values. |
| 193 | + |
| 194 | +### Lint `usingComponents` name (`component-name`) |
| 195 | + |
| 196 | +Some use cases: |
| 197 | + |
| 198 | +```json5 |
| 199 | +{ |
| 200 | + comp: "/path/to/myComp.mina", // should be `my-comp |
| 201 | + comp: "/path/to/anotherComp/index.mina" // should be `another-comp` |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +### Check event name case (`attribute-event-name-case`) |
| 206 | + |
| 207 | +| | (Demo) | WeChat | Baidu | |
| 208 | +| ---------- | ------------- | ------ | ----- | |
| 209 | +| Camel Case | bind:myEvent | √ | √ | |
| 210 | +| Kebab Case | bind:my-event | √ | × | |
| 211 | +| Snake Case | bind:my_event | √ | √ | |
| 212 | + |
| 213 | +- `'camel'` |
| 214 | + |
| 215 | +```html |
| 216 | +<comp bind:myEvent="onClick" /> |
| 217 | +``` |
| 218 | + |
| 219 | +- `'kebab'` |
| 220 | + |
| 221 | +```html |
| 222 | +<comp bind:my-event="onClick" /> |
| 223 | +``` |
| 224 | + |
| 225 | +- `'snake'` |
| 226 | + |
| 227 | +```html |
| 228 | +<comp bind:my_event="onClick" /> |
| 229 | +``` |
0 commit comments