Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-ts-ban-snippets

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-ts-ban-snippets

Ban snippets of TypeScript from your project, using a custom eslint rule 'ts-ban-snippets'.

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

:no_entry_sign: eslint-plugin-ts-ban-snippets readme

Ban snippets of TypeScript from your project, using a custom eslint rule 'ts-ban-snippets'.

examples: "return void reject", "it.only", "debugger".

This is an eslint port of tslint-ban-snippets

CI CD Build

Size

Dependencies

npm Package NPM Downloads

styled with prettier semantic-release

License: MIT

ko-fi

features

  • a custom eslint rule that can detect code snippets that are not desired
  • configurable for multiple code snippets
  • can include/exclude file paths
  • the error message can also be configured

The rule is quite flexible and could potentially avoid having to create multiple custom eslint rules.

Installation

This plugin requires your project to use TypeScript (>=4.1.3).

yarn add eslint-plugin-ts-ban-snippets --dev

Example Configurations

The plugin relies on TypeScript compiler services to resolve types.

The following examples show how to configure banned snippets in the .eslintrc file(s) of your project.

note: in the .eslintrc file, you need to set your tsconfig.json file in your eslint configuration via parserOptions:

{
  "plugins": ["ts-ban-snippets"],
  "parserOptions": {
    "project": "./tsconfig.json"
  }
}

Simple example .eslintrc

This example detects use of return void reject or return void resolve and raises an error.

{
  "plugins": ["ts-ban-snippets"],
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "rules": {
    "ts-ban-snippets/ts-ban-snippets": [
      "error",
      {
        "banned": [
          {
            "snippets": ["return void reject", "return void resolve"],
            "message": "Please do not return void - instead place the return statement on the following line."
          }
        ]
      }
    ]
  }
}

Regex example .eslintrc

This example also detects use of return void reject or return void resolve and raises an error - but using a regular expression.

{
  "plugins": ["ts-ban-snippets"],
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "rules": {
    "ts-ban-snippets/ts-ban-snippets": [
      "error",
      {
        "banned": [
          {
            "regexSnippets": ["return void [reject|resolve]"],
            "message": "Please do not return void - instead place the return statement on the following line."
          }
        ]
      }
    ]
  }
}

Example .eslintrc with multiple banned snippets

This example has a list of banned snippets:

  • ban return void reject or return void resolve
  • ban only enabling some tests via it.only or describe.only
  • ban disabling tests via it.skip or test suites via describe.skip
{
  "plugins": ["ts-ban-snippets"],
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "rules": {
    "ts-ban-snippets/ts-ban-snippets": [
      "error",
      {
        "banned": [
          {
            "snippets": ["return void reject", "return void resolve"],
            "message": "Please do not return void - instead place the return statement on the following line."
          },
          {
            "snippets": ["it.only", "describe.only"],
            "message": "Do not enable only some tests."
          },
          {
            "snippets": ["it.skip", "describe.skip"],
            "message": "Do not skip tests."
          }
        ]
      }
    ]
  }
}

Example .eslintrc with included and excluded paths

This example has one banned snippet (return void reject or return void resolve).

Only files with a path that contains my-component and does not contain excluded are scanned.

{
  "plugins": ["ts-ban-snippets"],
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "rules": {
    "ts-ban-snippets/ts-ban-snippets": [
      "error",
      {
        "banned": [
          {
            "snippets": ["return void reject", "return void resolve"],
            "message": "Please do not return void - instead place the return statement on the following line.",
            "includePaths": ["my-component"],
            "excludePaths": ["excluded"]
          }
        ]
      }
    ]
  }
}

For working tests, please see the unit tests.

For a real code example, see the test harness. In particular, the .eslintrc file.

authors

Original work by Sean Ryan - mr.sean.ryan(at gmail.com)

licence = MIT

This project is licensed under the MIT License - see the LICENSE file for details

Rules

For available options and for examples, see the Rule doc:

Keywords

FAQs

Package last updated on 16 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc