huridocs/uwazi

View on GitHub
app/react/Entities/components/V2NewRelationshipsBoard.js

Summary

Maintainability
A
0 mins
Test Coverage
F
7%

Expected to return a value at the end of method 'showReferenceText'.
Open

  showReferenceText(relationship, i) {

title: consistent-return

rule_type: suggestion

Unlike statically-typed languages which enforce that a function returns a specified type of value, JavaScript allows different code paths in a function to return different types of values.

A confusing aspect of JavaScript is that a function returns undefined if any of the following are true:

  • it does not execute a return statement before it exits
  • it executes return which does not specify a value explicitly
  • it executes return undefined
  • it executes return void followed by an expression (for example, a function call)
  • it executes return followed by any other expression which evaluates to undefined

If any code paths in a function return a value explicitly but some code path do not return a value explicitly, it might be a typing mistake, especially in a large function. In the following example:

  • a code path through the function returns a Boolean value true
  • another code path does not return a value explicitly, therefore returns undefined implicitly
function doSomething(condition) {
    if (condition) {
        return true;
    } else {
        return;
    }
}

Rule Details

This rule requires return statements to either always or never specify values. This rule ignores function definitions where the name begins with an uppercase letter, because constructors (when invoked with the new operator) return the instantiated object implicitly if they do not return another object explicitly.

Examples of incorrect code for this rule:

::: incorrect

/*eslint consistent-return: "error"*/

function doSomething(condition) {
    if (condition) {
        return true;
    } else {
        return;
    }
}

function doSomething(condition) {
    if (condition) {
        return true;
    }
}

:::

Examples of correct code for this rule:

::: correct

/*eslint consistent-return: "error"*/

function doSomething(condition) {
    if (condition) {
        return true;
    } else {
        return false;
    }
}

function Foo() {
    if (!(this instanceof Foo)) {
        return new Foo();
    }

    this.a = 0;
}

:::

Options

This rule has an object option:

  • "treatUndefinedAsUnspecified": false (default) always either specify values or return undefined implicitly only.
  • "treatUndefinedAsUnspecified": true always either specify values or return undefined explicitly or implicitly.

treatUndefinedAsUnspecified

Examples of incorrect code for this rule with the default { "treatUndefinedAsUnspecified": false } option:

::: incorrect

/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": false }]*/

function foo(callback) {
    if (callback) {
        return void callback();
    }
    // no return statement
}

function bar(condition) {
    if (condition) {
        return undefined;
    }
    // no return statement
}

:::

Examples of incorrect code for this rule with the { "treatUndefinedAsUnspecified": true } option:

::: incorrect

/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/

function foo(callback) {
    if (callback) {
        return void callback();
    }
    return true;
}

function bar(condition) {
    if (condition) {
        return undefined;
    }
    return true;
}

:::

Examples of correct code for this rule with the { "treatUndefinedAsUnspecified": true } option:

::: correct

/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/

function foo(callback) {
    if (callback) {
        return void callback();
    }
    // no return statement
}

function bar(condition) {
    if (condition) {
        return undefined;
    }
    // no return statement
}

:::

When Not To Use It

If you want to allow functions to have different return behavior depending on code branching, then it is safe to disable this rule. Source: http://eslint.org/docs/rules/

Expected 'this' to be used by class method 'showReferenceText'.
Open

  showReferenceText(relationship, i) {

title: class-methods-use-this ruletype: suggestion furtherreading: - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

If a class method does not use this, it can sometimes be made into a static function. If you do convert the method into a static function, instances of the class that call that particular method have to be converted to a static call as well (MyClass.callStaticMethod())

It's possible to have a class method which doesn't use this, such as:

class A {
    constructor() {
        this.a = "hi";
    }

    print() {
        console.log(this.a);
    }

    sayHi() {
        console.log("hi");
    }
}

let a = new A();
a.sayHi(); // => "hi"

In the example above, the sayHi method doesn't use this, so we can make it a static method:

class A {
    constructor() {
        this.a = "hi";
    }

    print() {
        console.log(this.a);
    }

    static sayHi() {
        console.log("hi");
    }
}

A.sayHi(); // => "hi"

Also note in the above examples that if you switch a method to a static method, instances of the class that call the static method (let a = new A(); a.sayHi();) have to be updated to being a static call (A.sayHi();) instead of having the instance of the class call the method

Rule Details

This rule is aimed to flag class methods that do not use this.

Examples of incorrect code for this rule:

::: incorrect

/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/

class A {
    foo() {
        console.log("Hello World");     /*error Expected 'this' to be used by class method 'foo'.*/
    }
}

:::

Examples of correct code for this rule:

::: correct

/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/
class A {
    foo() {
        this.bar = "Hello World"; // OK, this is used
    }
}

class A {
    constructor() {
        // OK. constructor is exempt
    }
}

class A {
    static foo() {
        // OK. static methods aren't expected to use this.
    }

    static {
        // OK. static blocks are exempt.
    }
}

:::

Options

This rule has two options:

  • "exceptMethods" allows specified method names to be ignored with this rule.
  • "enforceForClassFields" enforces that functions used as instance field initializers utilize this. (default: true)

exceptMethods

"class-methods-use-this": [<enabled>, { "exceptMethods": [&lt;...exceptions&gt;] }]</enabled>

The "exceptMethods" option allows you to pass an array of method names for which you would like to ignore warnings. For example, you might have a spec from an external library that requires you to overwrite a method as a regular function (and not as a static method) and does not use this inside the function body. In this case, you can add that method to ignore in the warnings.

Examples of incorrect code for this rule when used without "exceptMethods":

::: incorrect

/*eslint class-methods-use-this: "error"*/

class A {
    foo() {
    }
}

:::

Examples of correct code for this rule when used with exceptMethods:

::: correct

/*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */

class A {
    foo() {
    }
    #bar() {
    }
}

:::

enforceForClassFields

"class-methods-use-this": [<enabled>, { "enforceForClassFields": true | false }]</enabled>

The enforceForClassFields option enforces that arrow functions and function expressions used as instance field initializers utilize this. (default: true)

Examples of incorrect code for this rule with the { "enforceForClassFields": true } option (default):

::: incorrect

/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */

class A {
    foo = () => {}
}

:::

Examples of correct code for this rule with the { "enforceForClassFields": true } option (default):

::: correct

/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */

class A {
    foo = () => {this;}
}

:::

Examples of correct code for this rule with the { "enforceForClassFields": false } option:

::: correct

/*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */

class A {
    foo = () => {}
}

::: Source: http://eslint.org/docs/rules/

Definition for rule 'node/no-restricted-import' was not found.
Open

import _ from 'lodash';

For more information visit Source: http://eslint.org/docs/rules/

A control must be associated with a text label.
Open

              <th />

For more information visit Source: http://eslint.org/docs/rules/

A control must be associated with a text label.
Open

          <input

For more information visit Source: http://eslint.org/docs/rules/

Prop type "object" is forbidden
Open

  searchResults: PropTypes.object.isRequired,

For more information visit Source: http://eslint.org/docs/rules/

A control must be associated with a text label.
Open

              <th />

For more information visit Source: http://eslint.org/docs/rules/

Prop type "array" is forbidden
Open

  relationTypes: PropTypes.array.isRequired,

For more information visit Source: http://eslint.org/docs/rules/

Prefer named exports.
Open

export default connect(mapStateToProps, mapDispatchToProps)(V2NewRelationshipsBoard);

For more information visit Source: http://eslint.org/docs/rules/

Prop type "object" is forbidden
Open

  uiState: PropTypes.object.isRequired,

For more information visit Source: http://eslint.org/docs/rules/

There are no issues that match your filters.

Category
Status