Skip to content

Latest commit

 

History

History
67 lines (45 loc) · 1.76 KB

no-keyword-prefix.md

File metadata and controls

67 lines (45 loc) · 1.76 KB

Disallow identifiers starting with new or class

🚫 This rule is disabled in the ✅ recommended config.

new Foo and newFoo look very similar. Use alternatives that do not look like keyword usage.

Fail

const newFoo = 'foo';
const classFoo = 'foo';

Pass

const foo = 'foo';
const _newFoo = 'foo';
const new_foo = 'foo';
const fooNew = 'foo';

Options

disallowedPrefixes

If you want a custom list of disallowed prefixes you can set them with disallowedPrefixes:

// eslint unicorn/no-keyword-prefix: ["error", {"disallowedPrefixes": ["new", "for"]}]
const classFoo = "a"; // pass

// eslint unicorn/no-keyword-prefix: ["error", {"disallowedPrefixes": ["new", "for"]}]
const forFoo = "a"; // fail

The default is ["new", "class"].

checkProperties

If you want to disable this rule for properties, set checkProperties to false:

// eslint unicorn/no-keyword-prefix: ["error", {"checkProperties": true}]
foo.newFoo = 2; // fail

// eslint unicorn/no-keyword-prefix: ["error", {"checkProperties": false}]
var foo = {newFoo: 1}; // pass

// eslint unicorn/no-keyword-prefix: ["error", {"checkProperties": false}]
foo.newFoo = 2; // pass

onlyCamelCase

The default behavior is to check for camel case usage. If you want to disallow the prefix entirely, set onlyCamelCase to false:

// eslint unicorn/no-keyword-prefix: ["error", {"onlyCamelCase": true}]
const new_foo = "foo"; // pass

// eslint unicorn/no-keyword-prefix: ["error", {"onlyCamelCase": false}]
const new_foo = "foo"; // fail