npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-miniprogram

v0.2.0

Published

eslint plugin for mini programs

Downloads

14

Readme

eslint-plugin-miniprogram

npm Build Status

中文版

About

This ESLint plugin exists to help you lint your Mini Program code.

.mina files

For developing with .mina files, you can refer to mina-webpack repo for details.

How to use

Install the plugin:

npm install --save-dev eslint eslint-plugin-miniprogram

In your .eslintrc.js file, include this plugin:

// .eslintrc.js
module.exposts = {
  // you'll need vue-eslint-parser for `.mina` files
  parser: "vue-eslint-parser",
  plugins: [
    // amongst other other plugins, e.g. prettier
    "prettier",
    // include this plugin
    "miniprogram"
  ]
};

Enable rules:

// .eslintrc.js
module.exposts = {
  rules: {
    // other rules
    "miniprogram/attribute-event-name-case": ["error", "camel"],
    "miniprogram/component-name": ["error"],
    "miniprogram/no-unused-components": ["error"],
    "miniprogram/no-unregistered-components": ["error"],
    "miniprogram/no-wx-sync-api": ["warn"],
    "miniprogram/prefer-wx-promisify": ["error"],
    "miniprogram/config-json-validate-fields": ["error"]
    // rest of the rules
  }
};

Rules

Prefer wx promisify (prefer-wx-promisify)

Mini Program API introduces a new style of callbacks whick could be a new callback hell.

You can use promisify to enter the Promise world.

Details

Prefer promisify over wx style callbacks including success, fail and complete.

Examples of incorrect code for this rule:

wx.request({
  url: "https://www.example.com",
  success(res) {
    console.log(res);
  },
  fail(error) {
    console.error(error);
  },
  complete() {
    console.log("complete");
  }
});

Examples of correct code for this rule:

try {
  const res = await promisify(wx.request)({
    url: "https://www.example.com",
  });
  console.log(res);
} catch (error) {
  console.error(error);
} finally {
  console.log("complete");
}

Related Rules

  • no-wx-sync-api

Disallow the use of wx.xxSync API (no-wx-sync-api)

Sync API will block JavaScript running and cause bad performance.

For example wx.getStorageSync costs 30~100ms CPU time:

console.time("sync");
wx.setStorageSync("key", "value");
console.timeEnd("sync");

Rule Details

Disallow any wx.xxxSync API call.

Examples of incorrect code for this rule:

wx.setStorageSync("key", "value");

Examples of correct code for this rule:

await promisify(wx.setStorage)({
  key: "key",
  data: "value"
});

Related Rules

  • prefer-wx-promisify

No unused component (no-unused-components)

Rule Details

Bad case:

<config>
{
  "component": ture,
  "usingComponent": {
    // unused `my-comp`
    "my-comp": "/path/to/myComp.mina"
  }
}
</config>

<template>
  <view>hi</view>
</template>

No unregistered component (no-unregistered-components)

Bad case:

<config>
{
  "component": ture,
  "usingComponent": {
    "my-comp": "/path/to/myComp.mina"
  }
}
</config>

<template>
  <!-- typo here -->
  <my-compp />
</template>

Validate fields in component / page config file (config-json-validate-fields)

| | WeChat | Baidu | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Use Page for page | no components | no components | | Use Component for page | usingComponents | component | | Use Component for component | usingComponents | component | | navigationBarTextStyle values | can only be black/white | | backgroundTextStyle values | can only be dark/light |

Different Mini Program runtimes have different required fields in config (.json) file.

You should add "conponent": true if you are using Component function.

// comp.js
Component({});
<!-- comp.mina -->
<config>
{ "component": true, "usingComponents": {} }
</config>

You should not use "conponent": true in Page function.

// page.js
Page({});
<!-- page.mina -->
<config>
{ "usingComponents": {} }
</config>

You should always add "usingComponents": {}, even if it's empty.

<!-- comp.mina -->
<config>
{ "component": true, "usingComponents": {} }
</config>

You should only use black or white for navigationBarTextStyle values.

You should only use dark or light for backgroundTextStyle values.

Lint usingComponents name (component-name)

Some use cases:

{
  "comp": "/path/to/myComp.mina", // should be `my-comp
  "comp": "/path/to/anotherComp/index.mina" // should be `another-comp`
}

Check event name case (attribute-event-name-case)

| | (Demo) | WeChat | Baidu | | ---------- | ------------- | ------ | ----- | | Camel Case | bind:myEvent | √ | √ | | Kebab Case | bind:my-event | √ | × | | Snake Case | bind:my_event | √ | √ |

  • 'camel'
<comp bind:myEvent="onClick" />
  • 'kebab'
<comp bind:my-event="onClick" />
  • 'snake'
<comp bind:my_event="onClick" />