1.0.0 • Published 9 months ago

eslint-plugin-forbid-log-strings v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

eslint-plugin-forbid-log-strings šŸ–„ļø

Version

Forbid certain strings in logger statements.

Can be used with the built-in console - or with any custom logger.

Examples

Example #1 - simple case

You might configure the plugin to show an error if a console statement includes a hardcoded 'debug' string:

Plugin Configuration:

// .eslintrc.js

module.exports = {
  // ...
  plugins: [
    "forbid-log-strings",
    // ... maybe other plugins here ...
  ],
  rules: {
    "forbid-log-strings/check": [
      "error",
      [
        {
          forbiddenPatterns: ["/debug/i"], // <--- can be a string or a RegExp, both have to be defined as strings though (with quotes)
        },
      ],
    ],
    // ... maybe other rules here ...
  },
};

→ Result:

// some javascript or typescript file

console.log("some text..."); // <--- āœ… ok
console.log("debug", "some text..."); // <--- āŒ throws error
console.log("DEBUG", "some text..."); // <--- āŒ throws error
console.log("debug some text..."); // <--- āŒ throws error
console.log("DEBUG some text..."); // <--- āŒ throws error

Example #2 - use a custom logger + exclude log level 'error'

The following example uses cheese-log as a custom logger.

Plugin Configuration:

// .eslintrc.js

module.exports = {
  // ...
  plugins: [
    "forbid-log-strings",
    // ... maybe other plugins here ...
  ],
  rules: {
    "forbid-log-strings/check": [
      "error",
      [
        {
          forbiddenPatterns: ["/debug/i"],
          customLoggers: ["cheese"], // <--- a cheese-log method call would look like cheese.log("hello world!"), therefore we set "cheese" as the object name
          excludeLevels: ["error"],
        },
      ],
    ],
    // ... maybe other rules here ...
  },
};

→ Result:

// some javascript or typescript file

cheese.log("some text..."); // <--- āœ… ok
cheese.error("debug", "some text..."); // <--- āœ… also ok, because we exluded level 'error'
cheese.log("debug", "some text..."); // <--- āŒ throws error
console.log("debug", "some text..."); // <--- āœ… throws no error, since we are looking at the cheese-log logger, not console!

Installation

Via npm:

npm i eslint-plugin-forbid-log-strings --save-dev

Via yarn:

yarn add -D eslint-plugin-forbid-log-strings

Configuration Details

The plugin can be configured with 1 or more configurations.

Every configuration knows the following properties:

property namerequiredallowed values
forbiddenPatternsyesArray consisting of patterns in the shape of strings and/or Regular Expressions. If you want to provide a RegExp also provide it as a string (see examples above) - the plugin will automatically detect whether it is a RegExp or a plain string value.
logicYes, if the length of forbiddenPatterns is >= 2. (Make sure to not provide this property if the length === 1, otherwise the plugin will throw a general error)Must be one of "AND", "OR" and "XOR". It defines which logic is being used when multiple forbiddenPatterns are provided.
customLoggersnoArray of custom loggers. Make sure to provide the object name on which the logger methods are called. For example, for the cheese-log logger the statements would look like cheese.debug("hello world!"), etc. - therefore the provided value in the plugin configuration must be ["cheese"]. You can provide 1 or more custom loggers.
excludedLevelsnoArray of log levels that should be ignored. For instance ["info", "warn", "debug"].

If you enjoy using this...