ESLint custom rules are useful

Here’s an example of a custom eslint rule that detects usage of the knex library in a unit test file and reports it as a linter error.

module.exports = {
  rules: {
    'no-db-unit-tests': {
      create: function (context) {
        return {
          ImportDeclaration(node) {
            const fileName = context.getFilename();
            if (node.specifiers) {
              node.specifiers.map((item) => {
                if (
                  item.local.name === 'knex' &&
                  fileName.includes('unit.spec')
                ) {
                  return context.report(
                    node,
                    item.loc,
                    `Do not use ${item.local.name} in unit test file ${fileName}`,
                  );
                }

                return null;
              });
            }
          },
        };
      },
    },
  },
};

View post: ESLint custom rules are useful