Skip to content

Commit

Permalink
Disable explicit member accessibility for constructors (#158)
Browse files Browse the repository at this point in the history
# Rationale

Resolves #100. Constructors may now be marked `public`, `private`, or
have no accessibility modifier. Previously they had to be marked
`public` or `private`.

# Implementation

Set `overrides: { constructors: 'off' }` in the TS rule configuration.

# Testing

Added a constructor to the TypeScript test and verified the rule
previously errored if it didn't have a modifier but now allows this.
  • Loading branch information
jattasNI authored Sep 26, 2024
1 parent 5fcd774 commit bd1f97e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Disable `@typescript-eslint/explicit-member-accessibility` for constructors",
"packageName": "@ni/eslint-config-typescript",
"email": "[email protected]",
"dependentChangeType": "minor"
}
7 changes: 5 additions & 2 deletions packages/eslint-config-typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ module.exports = {
/*
Requiring an accessibility modifier helps when creating classes to ensure the
accessibility of a class member is intentionally decided and not relying on
the default of public accessibility.
the default of public accessibility. Constructors are omitted because they are
almost always public.
*/
'@typescript-eslint/explicit-member-accessibility': 'error',
'@typescript-eslint/explicit-member-accessibility': ['error',
{ accessibility: 'explicit', overrides: { constructors: 'off' } }
],

/*
All interface members should be terminated with a semicolon including single line
Expand Down
4 changes: 4 additions & 0 deletions tests/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
export class NI {
private _awesomeLevel = 1;

constructor() {
this._awesomeLevel = 11;
}

public get awesome(): boolean {
return this._awesomeLevel > 0;
}
Expand Down

0 comments on commit bd1f97e

Please sign in to comment.