2.1.0 • Published 5 months ago

@rocketmakers/eslint v2.1.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
5 months ago

Rocketmakers eslint

Package to return eslint config based on a set of internally defined standards.

Installation

Run the following to install the package as a dev dependency:

npm i --save-dev @rocketmakers/eslint

Once you have installed @rocketmakers/eslint you do not need to install any other linting package such as eslint, eslint-config-* or eslint-plugin-* as these will be installed as transitive dependencies and you will still be able to run npx eslint ... successfully.

Config setup

Create a .eslintrc.js file in the directory you intend to run eslint from. Import createEslintConfig in order to generate your base project config. In depth descriptions of this function and its mandatory/optional params can be found in our full docs.

An example .eslintrc.js can be found below - the second block should only be used if you need specific extra files covered:

// .eslintrc.js
const { createEslintConfig } = require('@rocketmakers/eslint');

const config = createEslintConfig(
  {
    project: ['tsconfig.json'],
    ignorePatterns: ['**/node_modules/**/*.*', 'lib/*', '*.js'],
  },
  {
    'import/no-extraneous-dependencies': [
      'error',
      {
        devDependencies: ['.eslintrc.js', 'prettier.config.js'],
      },
    ],
  }
);

module.exports = config;

Lerna monorepo setup

When working inside a monorepo many package imports can be linted as extraneous dependencies, when they are actually installed at the project root. In order to resolve these linting errors you must provide a list of core modules to specify which dependencies you know are accessible from the root repository node_modules. An example is given below:

// .eslintrc.js

const config = createEslintConfig(
  {
    ...,
    // When installed at the root, this would be classed as an extraneous dev dep due to lerna monorepo setup
    coreModules: ['@rocketmakers/log'],
  }
);

JSX linting

To properly lint JSX components, the package needs to underrstand which of your custom components link back to 'standard' ones and what type of component they are.

// .eslintrc.js

const config = createEslintConfig(
  {
    ...,
    // Accessibility - maps custom component directly to html component
    jsxAllyComponents: {
      MyButton: 'button'
    },
    jsxFormComponents: [
      // Simple form wrapper that uses 'action' to hold the target url
      'MyForm',
      // More complicated form wrapper with own target attribute
      { name: 'MyComplexForm', formAttribute: 'endpoint' },
    ],
    jsxLinkComponent: [
      // Simple link wrapper that uses 'href' to hold the target url
      'MyLink',
      // More complicated link wrapper with own target attribute
      { name: 'Link', linkAttribute: 'to' }
    ]
  }
);

Linting

Run the following scripts to lint your code:

# return errors
npx eslint .

# fix auto-fixable linting errors
npx eslint . --fix

Ignore files

Using the above setup, .eslintignore files are respected when linting. You do not need to include an .eslintignore file as you can pass an ignorePatterns property as part of the createEslintConfig() params.

Config checking

If you want to check your ESLint config in its complete output state to see what plugins, rules etc are enabled, run:

npx eslint --print-config file.js > eslintconfig.json

This will produce a complete json object for the configuration post-compilation. Be aware that this is a very large file.