unicorn/prefer-class-fields
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-class-fields": "error",
},
},
];Prefer class field declarations over this assignments in constructors.
class Model {
constructor() {
this.items = [];
this.options = {enabled: true};
}
}class Foo {
foo = 'foo';
constructor() {
this.foo = 'bar';
}
}class Foo {
#foo = 'foo';
constructor() {
this.#foo = 'bar';
}
}class Model {
items = [];
options = {enabled: true};
}class Foo {
foo = 'bar';
}class Foo {
#foo = 'bar';
}Examples from the ESLint documentation.
unicorn/prefer-class-fields
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-class-fields": "error",
},
},
];unicorn/prefer-class-fields
unicorn plugin · style
{
"rules": {
"unicorn/prefer-class-fields": "error"
}
}Biome does not have this rule.
Prefer Blob#arrayBuffer() over FileReader#readAsArrayBuffer(…) and Blob#text() over FileReader#readAsText(…).
Prefer using Element#classList.toggle() to toggle class names.
Prefer Array#flat() over legacy techniques to flatten arrays.
Prefer String#codePointAt(…) over String#charCodeAt(…) and String.fromCodePoint(…) over String.fromCharCode(…).
Prefer .find(…) and .findLast(…) over the first or last element from .filter(…).
Prefer default parameters and destructuring defaults over reassignment and fallback expressions.