Skip to content

observation/eslint-rules

Repository files navigation

no-function-without-logging

This ESLint plugin enforces the no-function-without-logging rule: Every function code block should contain a logging statement ( call to Log.debug), with the first argument having at least its filename/classname and function name, seperated by a colon: Log.debug('foo:bar').

In addition to debug, the following options are also possible: trace, warning, info, error.

Examples

❌ Incorrect ✅ Correct
function functionName(){}
function functionName(){
    Log.trace('file:functionName');
}
const functionName = () => {}
const functionName = () => {
    Log.debug('file:functionName');
}
static function functionName(){}
static function functionName(){
    Log.trace('file:functionName');
}
class ClassName { 
    functionName(){}
}
class ClassName { 
    functionName(){
        Log.trace('ClassName:functionName');
    }
}
class ClassName { 
    functionName = () => {}
}
class ClassName { 
    functionName = () => {
        Log.debug('ClassName:functionName');
    }
}
function functionName(){ 
    Log.debug('file')
}
function functionName(){ 
    Log.debug('file:functionName')
}
function functionName(){ 
    Log.debug('functionName')
}
function functionName(){ 
    Log.debug('file:functionName')
}
const functionName = () => { 
    Log.debug()
}
const functionName = () => { 
    Log.debug('file:functionName')
}

Exceptions

Logging statement can include multiple arguments:

function functionName(){ 
    Log.debug('file:functionName', 1)
}

Logging statement can have extended text:

function functionName(){ 
    Log.debug('file:functionName with extra text')
}

Lambda function with body does not need logging statement:

const functionName = () => otherFunction()

Component declaration does not need logging statement:

const Component = () => {}

Component level logging only includes component name:

const Component = () => { 
    Log.debug('Component') 
}

Constructors do not need logging:

class ClassName {
    constructor(){}
}

Getter and setter functions do not need logging:

class ClassName {
    _value: number

    get value(){
        return this.value
    }

    set value(value: nuber){
        this._value = value
    }
}

Setter like functions (class method definition starting with set[A-Z] returning void) do not need logging:

class ClassName {
    _value: number

    setValue(value: nuber){
        this._value = value
    }
}

Build & publish

  1. run tsc in the working folder, this creates the javascript files that will be run by ESLint
  2. Get your changes to the develop branch

Install

Run the command:

yarn install -D observation/eslint-rules

You need to have read access to the observation/eslint-rules repo to do this. If an automated process needs this rule set, you can set up github deploy keys.

About

A plugin for ESLint to enforce coding rules

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published