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

@spinnaker/eslint-plugin

v3.0.2

Published

This package is an ESLint plugin containing:

Downloads

1,956

Readme

@spinnaker/eslint-plugin

This package is an ESLint plugin containing:

  • A base ESLint config
    • Parser configured for typescript
    • A set of default plugins, e.g. react-hooks plugin
    • Recommended rule sets, e.g. prettier/@typescript-eslint
      • Specific from the recommended rule sets are disabled
  • Custom ESLint rules specific to Spinnaker

Use

To use the rules, create a .eslintrc.js containing:

module.exports = {
  plugins: ['@spinnaker/eslint-plugin'],
  extends: ['plugin:@spinnaker/base'],
};

Creating a custom lint rule

This yarn create-rule command will:

  • Scaffolds a sample rule
  • Scaffolds a test for the sample rule
  • Adds the rule to the plugin (eslint-plugin.ts)
  • Adds the rule as an "error" to the plugin's base config base.config.js)

The rule should examine AST nodes to detect a lint violation. Optionally, it can provide an automatic code fixer.

Write a rule

A rule file exports a Rule.RuleContext object.

import { Rule } from 'eslint';
const rule: Rule.RuleModule = {
  meta: {
    type: 'problem',
    docs: { description: `Rule Description` },
    fixable: 'code',
  },
  create: function myRuleFunction(context: Rule.RuleContext) {
    return {
      // rule contents here
    };
  },
};
export default rule;

See: the official docs in a couple ways.

Spinnaker rules can be written in Typescript instead of CommonJS

myRuleFunction is a callback that receives an eslint context and returns an object containing callbacks for AST node types.

Each callback will be called when the parser encounters a node of that type. When a lint violation is detected, the callback should report it to the context object.

import { Rule } from 'eslint';
import { SimpleLiteral } from 'estree';
//  ...
function myRuleFunction(context: Rule.RuleContext) {
  return {
    // This callback is called whenever a 'Literal' node is encountered
    Literal: function (literalNode: SimpleLiteral & Rule.NodeParentExtension) {
      if (literalNode.raw.includes('JenkinsX')) {
        // lint violation encountered; report it
        const message = 'String literals may not include JenkinsX';
        context.report({ node, message });
      }
    },
  };
}

This example explicitly types the context and literalNode parameters, but these can be automatically inferred by Typescript

In addition to callbacks that trigger on a simple node type (Literal in the example above), you can also trigger a callback using an eslint selector.

Think of an eslint selector as a CSS selector, but for an AST. Selectors can reduce boilerplate while writing a rule, but more importantly they can potentially improve readability.

// Using a selector
function myRuleFunction(context: Rule.RuleContext) {
  return {
    // Find an ExpressionStatement
    // - that is a CallExpression
    //   - that has a callee object named 'React'
    //   - and has a callee property named 'useEffect'
    "ExpressionStatement > CallExpression[callee.object.name = 'React'][callee.property.name = 'useEffect']"(
      node: ExpressionStatement,
    ) {
      const message = 'Prefer bare useEffect() over React.useEffect()';
      context.report({ node, message });
    },
  };
}

// Not using a selector
function myRuleFunction(context: Rule.RuleContext) {
  return {
    ExpressionStatement(node) {
      const expression = node.expression;
      if (
        expression?.type === 'CallExpression' &&
        expression.callee.type === 'MemberExpression' &&
        expression.callee.object.name === 'React' &&
        expression.callee.property.name === 'useEffect'
      ) {
        const message = 'Prefer bare useEffect() over React.useEffect()';
        context.report({ node, message });
      }
    },
  };
}

One downside of using eslint selectors is the node type is not automatically inferred in the callback. When using selectors, you should explicitly type the node parameter.

Test a rule

We run the tests using Jest, but we do not use jest assertions. Instead, we use the RuleTester API from eslint to define our assertions.

import { ruleTester } from '../utils/ruleTester';
import { rule } from './my-cool-rule';

ruleTester.run('my-cool-rule', rule, {
  valid: [
    /** code that doesn't trigger the rule */
  ],
  invalid: [
    /** code that triggers the rule */
  ],
});

Make sure to add at least one valid and one invalid test cases:

ruleTester.run('my-cool-rule', rule, {
  valid: [
    {
      code: 'var foo = "bar";',
    },
  ],
  invalid: [
    {
      code: 'var foo = "JenkinsX";',
      error: 'String literals may not include JenkinsX',
    },
    {
      code: 'createTodo("learn more about JenkinsX foundations");',
      error: 'String literals may not include JenkinsX',
    },
  ],
});

Run the tests from /packages/eslint-plugin:

❯ yarn test
yarn run v1.22.4
$ jest
 PASS  test/my-cool-rule.spec.js

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.095s
Ran all test suites.
✨  Done in 1.69s.

While writing tests, it's useful to run Jest in watch mode: yarn test --watch

If you need to debug your tests, run yarn test:debug and launch the Chrome Debugger (enter chrome://inspect into the Chrome URL bar).

You can (and should) run your work-in-progress rule against the spinnaker OSS codebase:

./test_rule_against_deck_source.sh my-rule

Write a fixer

Once your tests are passing, consider writing an auto-fixer. Auto-fixers can be applied in downstream projects using eslint --fix. An auto-fixer replaces AST nodes which violate the rule with non-violating code.

When reporting a lint violation for your rule, return a fix function.

Literal(literalNode) {
  if (literalNode.raw.includes('JenkinsX')) {
    // lint violation encountered; report it
    const message = 'String literals may not include JenkinsX';
    const fix = (fixer) => {
      const fixedValue = literalNode.value.replaceAll('JenkinsX', 'JengaX');
      return fixer.replaceText(literalNode, '"' + fixedValue + '"');
    }
    context.report({ fix, node, message });
  }
}

Review the fixer api docs for more details.

If you need to fix more than one thing for a given rule, you may return an array of fixes.

const fix = (fixer) => {
  const fixedValue = literalNode.value.replaceAll('JenkinsX', 'JengaX');
  return [
    fixer.replaceText(literalNode, '"' + fixedValue '"'),
    fixer.insertTextBefore(literalNode, `/* Jengafied */ `),
  ]
}

Test a fixer

The result of a fixer should be added to the tests. Add an output key to all invalid test cases that can be auto-fixed.

invalid: [
  {
    code: 'var foo = "JenkinsX";',
    error: 'String literals may not include JenkinsX',
    output: 'var foo = /* Jengafied */ "JengaX";',
  },
];

Publishing

After committing and pushing your new rule, bump the version in package.json (commit and push) and then run npm publish manually.