From 90f043fe669c955447e149c6b3f176cc3f984d35 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 26 Mar 2023 19:55:01 +0800 Subject: [PATCH] feat(linter): improve target span and message on isolated_declaration --- .../rules/typescript/isolated_declaration.rs | 16 ++++++---- .../src/snapshots/isolated_declaration.snap | 32 +++++++++---------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/crates/oxc_linter/src/rules/typescript/isolated_declaration.rs b/crates/oxc_linter/src/rules/typescript/isolated_declaration.rs index b4d595f30..fedd934f4 100644 --- a/crates/oxc_linter/src/rules/typescript/isolated_declaration.rs +++ b/crates/oxc_linter/src/rules/typescript/isolated_declaration.rs @@ -1,6 +1,6 @@ use oxc_ast::{ ast::{Class, ClassElement, Function, PropertyDefinition}, - AstKind, Span, + AstKind, GetSpan, Span, }; use oxc_diagnostics::{ miette::{self, Diagnostic}, @@ -13,13 +13,15 @@ use crate::{ast_util::IsPrivate, context::LintContext, rule::Rule}; #[derive(Debug, Error, Diagnostic)] enum IsolatedDeclarationDiagnostic { - #[error("isolated-declaration: Requires type annotation on non-private property definition")] + #[error("isolated-declaration: Requires type annotation on exported properties")] #[diagnostic(severity(warning))] Property(#[label] Span), - #[error("isolated-declaration: Requires type annotation on parameters of non-private method")] + + #[error("isolated-declaration: Requires type annotation on export parameters")] #[diagnostic(severity(warning))] FunctionParam(#[label] Span), - #[error("isolated-declaration: Requires return type annotation of non-private method")] + + #[error("isolated-declaration: Requires return type annotation on exported functions")] #[diagnostic(severity(warning))] FunctionReturnType(#[label] Span), } @@ -85,14 +87,16 @@ impl IsolatedDeclaration { } } if function.return_type.is_none() { - ctx.diagnostic(IsolatedDeclarationDiagnostic::FunctionReturnType(function.span)); + let start = function.params.span.end; + let span = Span::new(start, start + 1); + ctx.diagnostic(IsolatedDeclarationDiagnostic::FunctionReturnType(span)); } } /// Checks that the property as a type annotation pub fn check_property_definition(property: &PropertyDefinition, ctx: &LintContext<'_>) { if property.type_annotation.is_none() { - ctx.diagnostic(IsolatedDeclarationDiagnostic::Property(property.span)); + ctx.diagnostic(IsolatedDeclarationDiagnostic::Property(property.key.span())); } } diff --git a/crates/oxc_linter/src/snapshots/isolated_declaration.snap b/crates/oxc_linter/src/snapshots/isolated_declaration.snap index b8546f26e..45f03665f 100644 --- a/crates/oxc_linter/src/snapshots/isolated_declaration.snap +++ b/crates/oxc_linter/src/snapshots/isolated_declaration.snap @@ -3,57 +3,57 @@ source: crates/oxc_linter/src/tester.rs expression: isolated_declaration --- - ⚠ isolated-declaration: Requires type annotation on parameters of non-private method + ⚠ isolated-declaration: Requires type annotation on export parameters ╭─[isolated_declaration.tsx:1:1] 1 │ export function foo(a) { return a; } · ─ ╰──── - ⚠ isolated-declaration: Requires return type annotation of non-private method + ⚠ isolated-declaration: Requires return type annotation on exported functions ╭─[isolated_declaration.tsx:1:1] 1 │ export function foo(a) { return a; } - · ───────────────────────────── + · ─ ╰──── - ⚠ isolated-declaration: Requires type annotation on non-private property definition + ⚠ isolated-declaration: Requires type annotation on exported properties ╭─[isolated_declaration.tsx:1:1] 1 │ export class A { public a; } - · ───────── + · ─ ╰──── - ⚠ isolated-declaration: Requires return type annotation of non-private method + ⚠ isolated-declaration: Requires return type annotation on exported functions ╭─[isolated_declaration.tsx:1:1] 1 │ export class A { foo() { return 0; } } - · ──────────────── + · ─ ╰──── - ⚠ isolated-declaration: Requires return type annotation of non-private method + ⚠ isolated-declaration: Requires return type annotation on exported functions ╭─[isolated_declaration.tsx:1:1] 1 │ export abstract class A { abstract foo() { return 0; } } - · ──────────────── + · ─ ╰──── - ⚠ isolated-declaration: Requires type annotation on non-private property definition + ⚠ isolated-declaration: Requires type annotation on exported properties ╭─[isolated_declaration.tsx:1:1] 1 │ export abstract class A { abstract a; } - · ─────────── + · ─ ╰──── - ⚠ isolated-declaration: Requires return type annotation of non-private method + ⚠ isolated-declaration: Requires return type annotation on exported functions ╭─[isolated_declaration.tsx:1:1] 1 │ export class A { get foo() { return 0; } } - · ──────────────── + · ─ ╰──── - ⚠ isolated-declaration: Requires type annotation on parameters of non-private method + ⚠ isolated-declaration: Requires type annotation on export parameters ╭─[isolated_declaration.tsx:1:1] 1 │ export class A { set foo(val) { } } · ─── ╰──── - ⚠ isolated-declaration: Requires return type annotation of non-private method + ⚠ isolated-declaration: Requires return type annotation on exported functions ╭─[isolated_declaration.tsx:1:1] 1 │ export class A { set foo(val) { } } - · ───────── + · ─ ╰────