useCapitalizedConstructors (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/useCapitalizedConstructors - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
new-cap
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useCapitalizedConstructors": "error" } } }}Description
Section titled “Description”Require constructor names to begin with a capital letter.
A constructor is a function or class that creates an object when it’s called with new,
such as new Date().
By convention, constructor names start with an uppercase letter,
and the names of other functions start with a lowercase letter.
When code follows this convention, a function’s name shows whether it needs new.
This makes two mistakes easier to spot: calling a constructor without new,
and calling a regular function with new.
This rule reports:
newfollowed by a name that starts with a lowercase letter, such asnew person();- a call without
newto a name that starts with an uppercase letter, such asPerson().
For a property access, such as people.Person() or people["Person"](),
the rule checks the last name, Person.
The rule ignores names that don’t start with a letter, such as $() or _().
It also ignores calls whose name is only known when the code runs,
such as factories[index]().
The following built-in functions can be called without new:
Array, BigInt, Boolean, Date, Error, Function, Number, Object,
RegExp, String, and Symbol.
The built-in Date.UTC() method is allowed too.
These exceptions also apply when the built-ins are accessed through the global object
as globalThis, window, self, or global, such as globalThis.String().
They only apply to the built-ins: if your code declares its own variable, function,
parameter, or import with one of these names, calls to it are reported.
The rule ignores calls used as decorators, such as @Component() in TypeScript,
because frameworks such as Angular and NestJS give these functions names
that start with an uppercase letter.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const friend = new person();code-block.js:1:20 lint/nursery/useCapitalizedConstructors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This constructor name starts with a lowercase letter.
> 1 │ const friend = new person();
│ ^^^^^^
2 │
ℹ By convention, only constructors start with an uppercase letter, so a lowercase name suggests a regular function that isn't meant to be called with new.
ℹ Rename the constructor so that it starts with an uppercase letter, or remove new if this is a regular function.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const friend = new people.person();code-block.js:1:27 lint/nursery/useCapitalizedConstructors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This constructor name starts with a lowercase letter.
> 1 │ const friend = new people.person();
│ ^^^^^^
2 │
ℹ By convention, only constructors start with an uppercase letter, so a lowercase name suggests a regular function that isn't meant to be called with new.
ℹ Rename the constructor so that it starts with an uppercase letter, or remove new if this is a regular function.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const friend = Person();code-block.js:1:16 lint/nursery/useCapitalizedConstructors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function name starts with an uppercase letter, but it's called without new.
> 1 │ const friend = Person();
│ ^^^^^^
2 │
ℹ By convention, names starting with an uppercase letter are reserved for constructors, which usually must be called with new.
ℹ Add new if this is a constructor, or rename the function so that it starts with a lowercase letter.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const friend = people.Person();code-block.js:1:23 lint/nursery/useCapitalizedConstructors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function name starts with an uppercase letter, but it's called without new.
> 1 │ const friend = people.Person();
│ ^^^^^^
2 │
ℹ By convention, names starting with an uppercase letter are reserved for constructors, which usually must be called with new.
ℹ Add new if this is a constructor, or rename the function so that it starts with a lowercase letter.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
// This `String` is a local function, not the built-in `String`.function String(value) { return value;}String(42);code-block.js:5:1 lint/nursery/useCapitalizedConstructors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function name starts with an uppercase letter, but it's called without new.
3 │ return value;
4 │ }
> 5 │ String(42);
│ ^^^^^^
6 │
ℹ By convention, names starting with an uppercase letter are reserved for constructors, which usually must be called with new.
ℹ Add new if this is a constructor, or rename the function so that it starts with a lowercase letter.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const friend = new Person();const neighbor = new people.Person();const name = person();const id = $("#id");const label = String(42);const date = globalThis.Date.UTC(2000, 0);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.