Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @typescript-eslint/explicit-member-accessibility #197

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __fixtures__/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ export function base64encode( str ) {
}

export const constantBinary = x() || x() !== {};

export class CoolClass {
// JavaScript doesn't have accessibility modifiers, so we expect this to be allowed.
secret = 'shhh';

implicitlyPublicMethod() {
return 'hi';
}
}
14 changes: 13 additions & 1 deletion __fixtures__/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ function someCoolDecorator( totallyRadArgument, _value ): typeof totallyRadArgum
}

@someCoolDecorator
export class EmptyClass {}
export class CoolClass {
secret: string;

constructor() {
// Constructors are exempt from explicit-member-accessibility rules, but
// properties are not.
this.secret = 'shhh';
}

implicitlyPublicMethod() {
return 'hi';
}
}

export function base64encode( str: string ): string {
return Buffer.from( str ).toString( 'base64' );
Expand Down
110 changes: 110 additions & 0 deletions __tests__/__snapshots__/check-fixtures.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -454,5 +454,115 @@ import { unusedEs6Import } from './stub/unusedEs6Import';
"ruleId": "@typescript-eslint/no-shadow",
"severity": 2,
},
{
"column": 2,
"endColumn": 17,
"endLine": 38,
"line": 38,
"message": "Missing accessibility modifier on class property secret.",
"messageId": "missingAccessibility",
"nodeType": "PropertyDefinition",
"ruleId": "@typescript-eslint/explicit-member-accessibility",
"severity": 2,
"suggestions": [
{
"data": {
"type": "public",
},
"desc": "Add 'public' accessibility modifier",
"fix": {
"range": [
675,
675,
],
"text": "public ",
},
"messageId": "addExplicitAccessibility",
},
{
"data": {
"type": "private",
},
"desc": "Add 'private' accessibility modifier",
"fix": {
"range": [
675,
675,
],
"text": "private ",
},
"messageId": "addExplicitAccessibility",
},
{
"data": {
"type": "protected",
},
"desc": "Add 'protected' accessibility modifier",
"fix": {
"range": [
675,
675,
],
"text": "protected ",
},
"messageId": "addExplicitAccessibility",
},
],
},
{
"column": 2,
"endColumn": 3,
"endLine": 48,
"line": 46,
"message": "Missing accessibility modifier on method definition implicitlyPublicMethod.",
"messageId": "missingAccessibility",
"nodeType": "MethodDefinition",
"ruleId": "@typescript-eslint/explicit-member-accessibility",
"severity": 2,
"suggestions": [
{
"data": {
"type": "public",
},
"desc": "Add 'public' accessibility modifier",
"fix": {
"range": [
838,
838,
],
"text": "public ",
},
"messageId": "addExplicitAccessibility",
},
{
"data": {
"type": "private",
},
"desc": "Add 'private' accessibility modifier",
"fix": {
"range": [
838,
838,
],
"text": "private ",
},
"messageId": "addExplicitAccessibility",
},
{
"data": {
"type": "protected",
},
"desc": "Add 'protected' accessibility modifier",
"fix": {
"range": [
838,
838,
],
"text": "protected ",
},
"messageId": "addExplicitAccessibility",
},
],
},
]
`;
11 changes: 11 additions & 0 deletions configs/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ module.exports = {
},

rules: {
// Require explicity visibility for class methods and properties to avoid
// implicit public access. Allow constructors to be implicitly public.
'@typescript-eslint/explicit-member-accessibility': [
'error',
{
overrides: {
constructors: 'off',
},
},
],

// TypeScript `any` type must not be used. This is a warning in the base
// config, and is elevated to an error here.
'@typescript-eslint/no-explicit-any': 'error',
Expand Down