Skip to content

Commit fc6dbe2

Browse files
author
Jiayu Liu
committed
initial commit
1 parent e0fe52e commit fc6dbe2

21 files changed

+6110
-1
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/

.eslintrc.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
parser: "@typescript-eslint/parser",
3+
extends: ["airbnb-base", "plugin:prettier/recommended"],
4+
plugins: ["@typescript-eslint"],
5+
settings: {
6+
"import/parsers": {
7+
"@typescript-eslint/parser": [".ts"]
8+
},
9+
"import/resolver": {
10+
node: {
11+
extensions: [".js", ".jsx", ".ts", ".tsx"]
12+
}
13+
}
14+
},
15+
env: {
16+
node: true,
17+
browser: false,
18+
jest: true
19+
},
20+
rules: {
21+
"no-unused-vars": 0,
22+
"no-restricted-syntax": 0,
23+
"import/prefer-default-export": 0
24+
}
25+
};

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
lib/
64+

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
3+
node_js:
4+
- "8"
5+
- "10"
6+
- "12"
7+
8+
cache:
9+
yarn: true

README.md

+229-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,229 @@
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+
```

jest.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const path = require("path");
2+
3+
module.exports = {
4+
verbose: true,
5+
collectCoverage: true,
6+
testEnvironment: "node",
7+
transform: {
8+
"\\.ts$": "ts-jest"
9+
},
10+
testMatch: ["**/src/**/__tests__/**/*.spec.[jt]s"],
11+
moduleFileExtensions: ["ts", "js", "json", "node"]
12+
};

package.json

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"name": "eslint-plugin-miniprogram",
3+
"keywords": [
4+
"eslint",
5+
"plugin",
6+
"mini-program",
7+
"mini-app"
8+
],
9+
"engines": {
10+
"node": ">= 8.0"
11+
},
12+
"repository": "[email protected]:airbnb/eslint-plugin-miniprogram.git",
13+
"author": "Chong Ma <[email protected]>",
14+
"contributors": [
15+
"Jiayu Liu <[email protected]>"
16+
],
17+
"license": "MIT",
18+
"private": false,
19+
"version": "0.0.1",
20+
"description": "eslint plugin for mini programs",
21+
"main": "lib/index.js",
22+
"files": [
23+
"lib/"
24+
],
25+
"scripts": {
26+
"prebuild": "npm run clean",
27+
"start": "npm run build -- --watch",
28+
"lint": "eslint .",
29+
"build": "tsc -p .",
30+
"clean": "rimraf lib",
31+
"test": "jest"
32+
},
33+
"devDependencies": {
34+
"@types/eslint": "^4.16.6",
35+
"@types/eslint-scope": "^3.7.0",
36+
"@types/eslint-visitor-keys": "^1.0.0",
37+
"@types/jest": "^24.0.13",
38+
"@types/json5": "^0.0.30",
39+
"@types/lodash": "4.14.123",
40+
"@types/node": "^12.0.4",
41+
"@typescript-eslint/eslint-plugin": "^1.9.0",
42+
"@typescript-eslint/parser": "^1.9.0",
43+
"cross-env": "^5.2.0",
44+
"eslint": "^5.16.0",
45+
"eslint-config-airbnb-base": "^13.1.0",
46+
"eslint-config-prettier": "^4.3.0",
47+
"eslint-plugin-import": "^2.17.3",
48+
"eslint-plugin-prettier": "^3.1.0",
49+
"husky": "^2.4.0",
50+
"jest": "^24.8.0",
51+
"lint-staged": "^8.1.7",
52+
"prettier": "^1.17.1",
53+
"rimraf": "^2.6.3",
54+
"ts-jest": "^24.0.2",
55+
"typescript": "~3.4.0"
56+
},
57+
"dependencies": {
58+
"@tinajs/mina-sfc": "^0.2.2",
59+
"eslint-plugin-vue": "^5.2.2",
60+
"json5": "^2.1.0",
61+
"lodash": "^4.17.11",
62+
"vue-eslint-parser": "^6.0.4"
63+
},
64+
"husky": {
65+
"hooks": {
66+
"pre-commit": "lint-staged"
67+
}
68+
},
69+
"lint-staged": {
70+
"*.{ts,js,md,json}": [
71+
"prettier --write",
72+
"git add"
73+
],
74+
"*.{ts,js}": [
75+
"eslint"
76+
]
77+
}
78+
}

0 commit comments

Comments
 (0)