Skip to content

consistent-boolean-name: fine-grained control over checked types #3534

Description

@MCStreetguy

Description

Currently, we can only control whether this rule checks object, class, and TypeScript property and method names or not, while boolean variables, parameters, and functions are always enabled. It would be great to have more granular control over what this rule applies to and what is ignored. Separate toggles for each possible occurrence that the rule can check would make it suitable for any personal preference.

For example, I would like to enforce that class methods acting as boolean getters follow the naming conventions, but I don't mind if a variable or property is named completed instead of isCompleted. Similarly, I would be happy with an argument named showProgress instead of shouldShowProgress, but a method named hasTitle returning a string would not be acceptable.

If possible, it would also be a nice touch to be able to enforce the naming convention only in one direction. For example if we could set a hypothetical checkVariables to e.g. "prohibit" this would allow the usage of completed for boolean variables but still prohibit hasName = 'Sindre'. Personally, I can't think of a use case for the opposite, i.e. enforcing the convention for boolean types but allowing non-boolean types to be mislabelled. So, I would stick with the 'prohibit' option only, but please correct me if you disagree.

Examples

/* eslint unicorn/consistent-boolean-name: ["error", { checkVariables: false }] */

// ✅
const completed = true;
const isCompleted = true;
/* eslint unicorn/consistent-boolean-name: ["error", { checkArguments: false }] */

// ✅
function download(showProgress = false) {}
function download(shouldShowProgress = false) {}
/* eslint unicorn/consistent-boolean-name: ["error", { checkMethods: true, checkFields: false }] */

class Foo {

    // ✅
    completed = true;

    // ❌
    hasTitle() {
        return 'Unicorn';
    }

    // ✅
    isCompleted() {
        return true;
    }

}
/* eslint unicorn/consistent-boolean-name: ["error", { checkVariables: "prohibit" }] */

// ❌
const hasName = 'Sindre';

// ✅
const name = 'Sindre';
const completed = true;
const isCompleted = true;

Additional Info

Setting sensible defaults for these new options would preserve the current default behaviour of the rule. However, as checkProperties would then inherently cover fewer types than before, this would still constitute a breaking change for all users who previously enabled this option.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions