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

feat(linter): Implement plugin-jsdoc/check-property-names #2989

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 19 additions & 14 deletions crates/oxc_linter/src/rules/jsdoc/check_property_names.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use miette::{miette, LabeledSpan};
use oxc_diagnostics::{
miette::{self, Diagnostic},
miette::{self, Diagnostic, Severity},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use rustc_hash::FxHashSet;
use rustc_hash::FxHashMap;

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

#[derive(Debug, Error, Diagnostic)]
enum CheckPropertyNamesDiagnostic {
#[error("eslint-plugin-jsdoc(check-property-names): Duplicate @property found.")]
#[diagnostic(severity(warning), help("@property `{1}` is duplicated on the same block."))]
Duplicated(#[label] Span, String),

#[error("eslint-plugin-jsdoc(check-property-names): No root defined for @property path.")]
#[diagnostic(
severity(warning),
Expand Down Expand Up @@ -67,7 +64,7 @@ impl Rule for CheckPropertyNames {
let resolved_property_tag_name = settings.resolve_tag_name("property");

for jsdoc in ctx.semantic().jsdoc().iter_all() {
let mut seen = FxHashSet::default();
let mut seen = FxHashMap::default();
for tag in jsdoc.tags() {
if tag.kind.parsed() != resolved_property_tag_name {
continue;
Expand All @@ -80,13 +77,22 @@ impl Rule for CheckPropertyNames {
let type_name = name_part.parsed();

// Check duplicated
if seen.contains(&type_name) {
ctx.diagnostic(CheckPropertyNamesDiagnostic::Duplicated(
name_part.span,
type_name.to_string(),
if let Some(duplicated_span) = seen.get(&type_name) {
ctx.diagnostic(miette!(
severity = Severity::Warning,
labels = [duplicated_span, &name_part.span]
.iter()
.map(|span| LabeledSpan::at(
(span.start as usize)..(span.end as usize),
"Duplicated property".to_string(),
))
.collect::<Vec<_>>(),
help = format!("@property `{type_name}` is duplicated on the same block."),
"eslint-plugin-jsdoc(check-property-names): Duplicate @property found."
Boshen marked this conversation as resolved.
Show resolved Hide resolved
));
}

// Check property path has a root
if type_name.contains('.') {
let mut parts = type_name.split('.').collect::<Vec<_>>();
// `foo[].bar` -> `foo[]`
Expand All @@ -95,16 +101,15 @@ impl Rule for CheckPropertyNames {
// `foo[]` -> `foo`
let parent_name = parent_name.trim_end_matches("[]");

// Check property has a root
if !seen.contains(&parent_name) {
if !seen.contains_key(&parent_name) {
ctx.diagnostic(CheckPropertyNamesDiagnostic::NoRoot(
name_part.span,
type_name.to_string(),
));
}
}

seen.insert(type_name);
seen.insert(type_name, name_part.span);
}
}
}
Expand Down
48 changes: 36 additions & 12 deletions crates/oxc_linter/src/snapshots/check_property_names.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,66 @@ expression: check_property_names
help: @property path declaration `employees[].department` appears before any real property.

⚠ eslint-plugin-jsdoc(check-property-names): Duplicate @property found.
╭─[check_property_names.tsx:5:27]
╭─[check_property_names.tsx:4:27]
3 │ * @typedef {SomeType} SomeTypedef
4 │ * @property foo
· ─┬─
· ╰── Duplicated property
5 │ * @property foo
Boshen marked this conversation as resolved.
Show resolved Hide resolved
· ───
· ─┬─
· ╰── Duplicated property
6 │ */
╰────
help: @property `foo` is duplicated on the same block.

⚠ eslint-plugin-jsdoc(check-property-names): Duplicate @property found.
╭─[check_property_names.tsx:6:27]
╭─[check_property_names.tsx:5:27]
4 │ * @property cfg
5 │ * @property cfg.foo
· ───┬───
· ╰── Duplicated property
6 │ * @property cfg.foo
· ───────
· ───┬───
· ╰── Duplicated property
7 │ */
╰────
help: @property `cfg.foo` is duplicated on the same block.

⚠ eslint-plugin-jsdoc(check-property-names): Duplicate @property found.
╭─[check_property_names.tsx:7:27]
╭─[check_property_names.tsx:6:27]
5 │ * @property cfg
6 │ * @property cfg.foo
· ───┬───
· ╰── Duplicated property
7 │ * @property cfg.foo
· ───────
· ───┬───
· ╰── Duplicated property
8 │ */
╰────
help: @property `cfg.foo` is duplicated on the same block.

⚠ eslint-plugin-jsdoc(check-property-names): Duplicate @property found.
╭─[check_property_names.tsx:6:27]
╭─[check_property_names.tsx:5:27]
4 │ * @property cfg
5 │ * @property cfg.foo
· ───┬───
· ╰── Duplicated property
6 │ * @property [cfg.foo]
· ─────────
· ────┬────
· ╰── Duplicated property
7 │ * @property baz
╰────
help: @property `cfg.foo` is duplicated on the same block.

⚠ eslint-plugin-jsdoc(check-property-names): Duplicate @property found.
╭─[check_property_names.tsx:6:27]
╭─[check_property_names.tsx:5:27]
4 │ * @property cfg
5 │ * @property cfg.foo
· ───┬───
· ╰── Duplicated property
6 │ * @property [cfg.foo="with a default"]
· ──────────────────────────
· ─────────────┬────────────
· ╰── Duplicated property
7 │ * @property baz
╰────
help: @property `cfg.foo` is duplicated on the same block.
Expand All @@ -102,10 +122,14 @@ expression: check_property_names
help: @property path declaration `foo[].bar[].baz` appears before any real property.

⚠ eslint-plugin-jsdoc(check-property-names): Duplicate @property found.
╭─[check_property_names.tsx:5:23]
╭─[check_property_names.tsx:4:23]
3 │ * @typedef {SomeType} SomeTypedef
4 │ * @prop foo
· ─┬─
· ╰── Duplicated property
5 │ * @prop foo
· ───
· ─┬─
· ╰── Duplicated property
6 │ */
╰────
help: @property `foo` is duplicated on the same block.
Loading