1.0.1 • Published 4 years ago

eslint-plugin-frichti-customs v1.0.1

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
4 years ago

eslint-plugin-import

NPM JavaScript Style Guide

This plugin contains some rules that we defined in order to respect the Frichti's front-end coding guidelines.

Installation

npm install --save-dev eslint-plugin-frichti-customs

All rules are off by default. However, you may configure them manually in your .eslintrc, or extend one of the canned configs:

{
  // ...
  "rules": {
    //...
    "frichti-customs/no-call-outside-method": [
      2,
      {
        "forbiddenMethod": "shouldNotBeCalledOutsideDoSomething",
        "exception": "^doSomething$"
      }
    ],
    "frichti-customs/no-more-than-one-call": [
      2,
      {
        "methodToCheck": "shouldBeCalledOnceInDoSomething",
        "forbiddenToCallFrom": "^doSomething$"
      }
    ]
  }
  "plugins": ["frichti-customs"],
  // ...
}

Rules

no-call-outside-method

Ensures that a method is called only inside authorized functions.

For example, we want that only "tracking" methods could call the segment helper. So we can use the rule as follows:

    "frichti-customs/no-call-outside-method": [
      2,
      {
        "forbiddenMethod": "segment",
        "exception": "^track.*(View|Click|Identify|Opening|Sent|Initialization|Selection|Submit|Closing|Change)$"
      }
    ],

šŸ‘Ž Examples of incorrect code for this rule (defined as above):

  /**
   * BAD
  */

  // The method doSomething doesn't match with the exception regex defined in the rule options
  function doSomething() {
    segment()
  }

  // The method doSomeTracking doesn't match with the exception regex defined in the rule options
  function doSomeTracking() {
    segment()
  }

šŸ‘ Examples of correct code for this rule (defined as above):

  /**
   * GOOD
  */
  // The method trackSomethigView do match with the exception regex defined in the rule options
  function trackSomethigView() {
    segment()
  }

  // The method trackSomethigClick do match with the exception regex defined in the rule options
  function trackSomethigClick() {
    segment()
  }

no-more-than-one-call

Ensures that a method is called only once inside a function.

For example, we want the segment helper to be called only once in tracking method. So we can use the rule as follows:

    "frichti-customs/no-more-than-one-call": [
      2,
      {
        "methodToCheck": "segment",
        "forbiddenToCallFrom": "^track.*(View|Click|Identify|Sent|Initialization|Selection|Submit|Closing|Change)$"
      }
    ],

šŸ‘Ž Examples of incorrect code for this rule (defined as above):

  /**
   * BAD
  */

  // segment is called twice in a method that matches with the forbiddenToCallFrom regex
  function trackSomethigView() {
    segment()
    segment()
  }

  // segment is called twice in a method that matches with the forbiddenToCallFrom regex
  function trackSomethigClick() {
    segment()
    doAnotherThingHere()
    segment()
  }

šŸ‘ Examples of correct code for this rule (defined as above):

  /**
   * GOOD
  */
  // The segment helper is used only once in a method that matches with the forbiddenToCallFrom regex
  function trackSomethigView() {
    segment()
  }

  // segment is called twice in a method that doesn't matches with the forbiddenToCallFrom regex
  function DoSomeThings() {
    segment()
  }
1.0.1

4 years ago