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

docs(linter): improve docs for eslint/getter-return #6229

Merged
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
58 changes: 50 additions & 8 deletions crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, AstNode};
use crate::{
context::{ContextHost, LintContext},
rule::Rule,
AstNode,
};

fn getter_return_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Expected to always return a value in getter.")
Expand All @@ -38,29 +42,46 @@ const METHODS_TO_WATCH_FOR: [(&str, &str); 4] = [

declare_oxc_lint!(
/// ### What it does
/// Requires all getters to have a return statement
///
/// Requires all getters to have a `return` statement.
///
/// ### Why is this bad?
/// Getters should always return a value. If they don't, it's probably a mistake.
///
/// This rule does not run on TypeScript files, since type checking will
/// catch getters that do not return a value.
///
/// ### Example
///
/// Examples of **incorrect** code for this rule:
/// ```javascript
/// class Person{
/// get name(){
/// class Person {
/// get name() {
/// // no return
/// }
/// }
///
/// const obj = {
/// get foo() {
/// // object getter are also checked
/// }
/// }
/// ```
///
/// Examples of **correct** code for this rule:
/// ```javascript
/// class Person {
/// get name() {
/// return this._name;
/// }
/// }
/// ```
GetterReturn,
nursery
);

impl Rule for GetterReturn {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
// https://eslint.org/docs/latest/rules/getter-return#handled_by_typescript
if ctx.source_type().is_typescript() {
return;
}
match node.kind() {
AstKind::Function(func) if !func.is_typescript_syntax() => {
self.run_diagnostic(node, ctx, func.span);
Expand All @@ -81,6 +102,11 @@ impl Rule for GetterReturn {

Self { allow_implicit }
}

fn should_run(&self, ctx: &ContextHost) -> bool {
// https://eslint.org/docs/latest/rules/getter-return#handled_by_typescript
!ctx.source_type().is_typescript()
}
}

impl GetterReturn {
Expand Down Expand Up @@ -494,4 +520,20 @@ fn test() {
Tester::new(GetterReturn::NAME, pass, fail)
.change_rule_path_extension("js")
.test_and_snapshot();

// TypeScript tests
let pass = vec![(
"var foo = {
get bar(): boolean | undefined {
if (Math.random() > 0.5) {
return true;
}
}
};",
None,
)];

let fail = vec![];

Tester::new(GetterReturn::NAME, pass, fail).test();
}