Skip to content

feat(linter/typescript): implement explicit-member-accessibility#21447

Merged
camc314 merged 7 commits intooxc-project:mainfrom
htunnicliff:typescript/explicit-member-accessibility
Apr 15, 2026
Merged

feat(linter/typescript): implement explicit-member-accessibility#21447
camc314 merged 7 commits intooxc-project:mainfrom
htunnicliff:typescript/explicit-member-accessibility

Conversation

@htunnicliff
Copy link
Copy Markdown
Contributor

This PR adds the typescript/explicit-member-accessibility rule for #2180 and was generated using Cursor and Opus 4.6 Extra High.

I don't have much Rust experience, but I have worked with linters for many years and use oxlint at my company on a large monorepo.

Here's the plan I refined with Cursor to faciliate this PR, if this is of use:


name: explicit-member-accessibility rule
overview: Implement the typescript/explicit-member-accessibility rule that requires (or forbids) explicit public/private/protected modifiers on class members, with per-member-kind overrides and autofix support.
todos:

  • id: scaffold
    content: Run just new-typescript-rule explicit-member-accessibility to generate boilerplate and register the rule
    status: completed
  • id: config
    content: Define AccessibilityLevel enum, config struct with accessibility, ignoredMethodNames, overrides, and implement from_configuration
    status: completed
  • id: diagnostics
    content: Define missing_accessibility and unwanted_public_accessibility diagnostic helper functions
    status: completed
  • id: run-methods
    content: Implement Rule::run for MethodDefinition -- check constructors, methods, getters/setters with correct override resolution
    status: completed
  • id: run-properties
    content: Implement Rule::run for PropertyDefinition and AccessorProperty
    status: completed
  • id: run-params
    content: Implement Rule::run for FormalParameter (parameter properties)
    status: completed
  • id: fixes
    content: Implement fix (remove public) and suggestions (add public/private/protected)
    status: completed
  • id: tests
    content: Add comprehensive test cases covering all config permutations, member types, private identifiers, and fixes
    status: completed
  • id: finalize
    content: Run just fmt, cargo test -p oxc_linter, and just ready
    status: completed
    isProject: false

Implement typescript/explicit-member-accessibility

Reference

Rule behavior

The rule has three accessibility-level modes:

  • "explicit" (default) -- every class member must have an explicit public, private, or protected modifier
  • "no-public" -- the public keyword is forbidden (the other two are fine)
  • "off" -- no checking

A top-level accessibility option sets the default. Per-member-kind overrides can specialize behavior for: constructors, methods, accessors, properties, parameterProperties.

An ignoredMethodNames option (string array) skips named methods.

Fixes:

  • "no-public" violations get a fix that removes the public keyword
  • "explicit" violations get suggestions (not auto-fix) offering to add public, private, or protected

Members with #private identifier keys are always skipped (JS private fields already have inherent privacy).

Key AST types

All live in crates/oxc_ast/src/ast/js.rs and crates/oxc_ast/src/ast/ts.rs:

  • MethodDefinition -- has accessibility: Option<TSAccessibility>, kind: MethodDefinitionKind, key: PropertyKey
  • PropertyDefinition -- has accessibility: Option<TSAccessibility>, key: PropertyKey
  • AccessorProperty -- has accessibility: Option<TSAccessibility>, key: PropertyKey
  • FormalParameter -- has accessibility: Option<TSAccessibility>, readonly: bool, has_modifier() (parameter properties)
  • TSAccessibility -- enum: Public, Protected, Private
  • PropertyKey::is_private_identifier() -- detect #foo private fields

Implementation plan

1. Scaffold with just new-typescript-rule

just new-typescript-rule explicit-member-accessibility

This generates crates/oxc_linter/src/rules/typescript/explicit_member_accessibility.rs, registers the module in crates/oxc_linter/src/rules.rs, and runs codegen.

2. Define config struct

In the new rule file, define the configuration following the pattern in no_extraneous_class.rs:

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AccessibilityLevel { Explicit, NoPublic, Off }

#[derive(Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
pub struct ExplicitMemberAccessibility {
    accessibility: AccessibilityLevel, // default: Explicit
    ignored_method_names: Vec<String>,
    overrides: AccessibilityOverrides,
}

#[derive(Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
struct AccessibilityOverrides {
    accessors: Option<AccessibilityLevel>,
    constructors: Option<AccessibilityLevel>,
    methods: Option<AccessibilityLevel>,
    parameter_properties: Option<AccessibilityLevel>,
    properties: Option<AccessibilityLevel>,
}

Implement Rule::from_configuration to parse the JSON options (array with one object element), resolving each override against the base accessibility.

3. Implement Rule::run

Match on three AstKind variants:

  • AstKind::MethodDefinition(method): Skip if key.is_private_identifier(). Determine the effective check level based on method.kind (Constructor -> ctorCheck, Get/Set -> accessorCheck, Method -> methodCheck). Skip if the method name is in ignoredMethodNames. Then:

    • "no-public" + accessibility == Some(Public) -> report unwantedPublicAccessibility with a fix removing public
    • "explicit" + accessibility.is_none() -> report missingAccessibility with suggestions to add each modifier
  • AstKind::PropertyDefinition(prop): Skip if key.is_private_identifier(). Use propCheck. Same check/report pattern.

  • AstKind::AccessorProperty(prop): Skip if key.is_private_identifier(). Use propCheck (following the upstream which uses propCheck for all property-like members). Same check/report pattern.

For parameter properties, match on AstKind::FormalParameter(param) when param.has_modifier(). Use paramPropCheck. The "no-public" case only applies when accessibility == Some(Public) AND readonly == true (matches upstream behavior). The "explicit" case reports when accessibility.is_none() (the param has readonly/override but no explicit visibility keyword).

4. Diagnostics

Define helper functions:

fn missing_accessibility(span, member_type, name) -> OxcDiagnostic
fn unwanted_public_accessibility(span, member_type, name) -> OxcDiagnostic

5. Fixes and suggestions

  • Removing public (no-public violations): Use fixer.delete_range(...) targeting the public keyword span. The span can be computed as Span::new(node.span.start, <first-non-public-token-start>) -- but we need to be careful about decorators. A simpler approach: search for "public " in the source text within the member's span and remove it.
  • Adding modifiers (explicit violations): Use ctx.diagnostic_with_suggestion(...) with suggestions for public , private , and protected . Insert text before the member's key (or before the first token after decorators).

6. Tests

Add comprehensive test cases following the upstream test file patterns, using Tester:

  • Default { accessibility: "explicit" } -- missing modifiers on methods, properties, accessors, constructors, parameter properties
  • { accessibility: "no-public" } -- unwanted public on each member type
  • { accessibility: "off" } -- no reports
  • Override combinations (e.g. { accessibility: "no-public", overrides: { properties: "explicit" } })
  • ignoredMethodNames option
  • Private identifier (#foo) members are always skipped
  • Fix tests for public removal, suggestion tests for adding modifiers

7. Finalize

  • Run just fmt
  • Run cargo test -p oxc_linter to verify tests pass
  • Run cargo insta review if needed to accept snapshots
  • Run just ready for final checks

@github-actions github-actions Bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Apr 14, 2026
@htunnicliff htunnicliff force-pushed the typescript/explicit-member-accessibility branch 4 times, most recently from 7519972 to 21d2136 Compare April 14, 2026 23:06
@htunnicliff htunnicliff marked this pull request as ready for review April 14, 2026 23:06
@htunnicliff htunnicliff requested a review from camc314 as a code owner April 14, 2026 23:06
@htunnicliff htunnicliff force-pushed the typescript/explicit-member-accessibility branch 2 times, most recently from a0859c7 to b5aab3c Compare April 14, 2026 23:21
Adds the `explicit-member-accessibility` rule from `@typescript-eslint`,
which requires or disallows explicit accessibility modifiers on class
properties, methods, accessors, and parameter properties.

Made-with: Cursor
@htunnicliff htunnicliff force-pushed the typescript/explicit-member-accessibility branch from b5aab3c to d255319 Compare April 14, 2026 23:25
@camc314 camc314 changed the title feat(linter): add typescript/explicit-member-accessibility feat(linter/typescript): implement explicit-member-accessibility Apr 15, 2026
@camc314 camc314 self-assigned this Apr 15, 2026
Copy link
Copy Markdown
Contributor

@camc314 camc314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@camc314 camc314 merged commit cf2d281 into oxc-project:main Apr 15, 2026
25 checks passed
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 15, 2026

Merging this PR will not alter performance

✅ 4 untouched benchmarks
⏩ 47 skipped benchmarks1


Comparing htunnicliff:typescript/explicit-member-accessibility (4df5b92) with main (d48de6f)2

Open in CodSpeed

Footnotes

  1. 47 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (c9b4db9) during the generation of this report, so d48de6f was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@htunnicliff htunnicliff deleted the typescript/explicit-member-accessibility branch April 15, 2026 16:26
camc314 added a commit that referenced this pull request Apr 20, 2026
# Oxlint
### 💥 BREAKING CHANGES

- 24fb7eb allocator: [**BREAKING**] Rename `Box` and `Vec` methods
(#21395) (overlookmotel)

### 🚀 Features

- 38d8090 linter/jest: Implemented jest `version` settings in config
file. (#21522) (Said Atrahouch)
- 7dbbb99 linter/eslint: Implement suggestion for `no-case-declarations`
rule (#21508) (Mikhail Baev)
- 9b4d9f6 linter/prefer-template: Implement autofix (#21502) (François)
- daa64ed linter/no-empty-pattern: Add `allowObjectPatternsAsParameters`
option (#21474) (camc314)
- cf2d281 linter/typescript: Implement explicit-member-accessibility
(#21447) (Hunter Tunnicliff)
- d48de6f linter/unicorn: Add help messages to 3 rule diagnostics
(#21459) (Mukunda Rao Katta)
- cffdc2e linter: Backfill rule version metadata (#21391) (Old Autumn)

### 🐛 Bug Fixes

- 1e69b91 linter/no-useless-assignment: Improve diagnostic spans
(#21581) (camc314)
- f272594 linter/plugins: Align `RuleMeta.replacedBy` type with ESLint
(#21544) (bab)
- 4d57851 linter/eslint: Enhance `no-empty-function` rule to support
async and generator functions in VariableDeclarator (#21542) (Mikhail
Baev)
- 00fc136 codegen: Preserve coverage comments before object properties
(#21312) (bab)
- a56b7b9 oxlint: Dont enable gitlab formatter by default (#21501)
(camc314)
- 9c9b6a2 linter/array-callback-return: Ignore non-exit CFG dead ends
(#21497) (camc314)
- 61088e0 linter/unicorn: Handle computed property access in
`prefer-dom-node-remove` rule (#21470) (Mikhail Baev)
- eab5934 linter: Report an error on unsupported `extends` values
(#21406) (John Costa)
- 3289ba0 linter/valid-expect-in-promise: Check a jest fn to be `test`
instead of `describe` (#21422) (Said Atrahouch)
- 4417fe3 linter/prefer-ending-with-an-expect: Ignore vi.mock factory
callbacks (#21414) (Cédric Exbrayat)
- a904883 linter/consistent-type-imports: Ignore vue/svelte/astro files
(#21415) (bab)
- 2498fe6 linter/no-unused-vars: Allow segments of dotted namespace
declarations (#21416) (bab)
- 44b5b35 linter: Preserve vitest-compatible jest rules when applying
overrides (#21389) (Cameron)
- 7bd8331 linter/prefer-ending-with-an-expect: Add missing `version`
docs (#21390) (Said Atrahouch)
- 43d8f0d linter/no-useless-assignment: Ignore writes read by closures
(#21380) (camc314)

### 📚 Documentation

- c1eeae3 linter: Add version to `rule.json` (#21547) (camchenry)
- 0ec6ab2 linter: Improve the `vitest/no-importing-vitest-globals` rule
documentation. (#21557) (connorshea)
# Oxfmt
### 💥 BREAKING CHANGES

- 24fb7eb allocator: [**BREAKING**] Rename `Box` and `Vec` methods
(#21395) (overlookmotel)

### 🚀 Features

- 5aa7fe1 oxfmt: Add `--disable-nested-config` CLI flag (#21514)
(leaysgur)
- b5cb8d1 oxfmt: Update prettier to 3.8.3 (#21451) (leaysgur)
- 16713d5 oxfmt/cli: Support per-directory config (#21103) (leaysgur)
- 952de06 oxfmt/lsp: Support per-directory config (#21081) (leaysgur)

### 🐛 Bug Fixes

- a501a53 formatter: Handle comments after pipe in single-member union
types (#21487) (John Costa)
- 6f49fad oxfmt: Respect nested config.`ignorePatterns` (#21489)
(leaysgur)
- 7c98d52 oxfmt: Do not panic on finding invalid nested config (#21461)
(leaysgur)
- 41bb2d5 formatter: Preserve more `intrinsic` parens (#21449)
(leaysgur)
- f894750 formatter: Preserve parens around `intrinsic` in type alias
annotation (#21410) (Dunqing)

### ⚡ Performance

- df27b48 oxfmt: Skip ancestors check when no nested config found
(#21517) (leaysgur)
- 5e1522a oxfmt: Do not occupy the rayon thread solely for handover
(#21408) (leaysgur)

Co-authored-by: camc314 <18101008+camc314@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants