Skip to content

Commit

Permalink
Merge pull request #197 from Automattic/add/explicit-member-visibility
Browse files Browse the repository at this point in the history
Add @typescript-eslint/explicit-member-accessibility
  • Loading branch information
chriszarate authored Jun 21, 2024
2 parents 0b6f0ff + 1b5f048 commit 656c6b0
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
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

0 comments on commit 656c6b0

Please sign in to comment.