0.1.1 • Published 5 years ago

@humanwhocodes/eslint-simple-rule v0.1.1

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
5 years ago

ESLint Simple Rule

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation.

Description

ESLint rules require some boilerplate that ranges from trivial to complicated depending on what type of rule you're trying to create. This utility helps create ESLint rules with as little boilerplate as possible and is designed for creating the simplest of rules: those that simply flag some syntax as a problem and doesn't try to fix it. The goal is to allow anyone to create custom rules faster and with less code than with using the built-in ESLint rule syntax.

Usage

Node.js

Install using npm or yarn:

npm install @humanwhocodes/eslint-simple-rule --save

# or

yarn add @humanwhocodes/eslint-simple-rule

Import into your Node.js project:

// CommonJS
const { rule } = require("@humanwhocodes/eslint-simple-rule");

// ESM
import { rule } from "@humanwhocodes/eslint-simple-rule";

Deno

Import into your Deno project:

import { rule } from "https://unpkg.com/@humanwhocodes/eslint-simple-rule/dist/rule.js";

Browser

It's recommended to import the minified version to save bandwidth:

import { rule } from "https://unpkg.com/@humanwhocodes/eslint-simple-rule/dist/rule.min.js";

However, you can also import the unminified version for debugging purposes:

import { rule } from "https://unpkg.com/@humanwhocodes/eslint-simple-rule/dist/rule.js";

API

After importing, you can create a new rule by using the rule() function and passing in an object literal where the keys are esquery selectors indicating the node to warn on and the values are the messages to display in ESLint when a node matches the selector:

export default rule({

    // warn whenever "null" is used
    "Literal[raw=\"null\"]": "Do not use null.",

    // warn whenever a "var" declaration is used
    "VariableDeclaration[kind=var]": "Use either 'let' or 'const' instead of 'var'."
});

Or if you are using CommonJS:

module.exports = rule({

    // warn whenever "null" is used
    "Literal[raw=\"null\"]": "Do not use null.",

    // warn whenever a "var" declaration is used
    "VariableDeclaration[kind=var]": "Use either 'let' or 'const' instead of 'var'."
});