Skip to content

Commit

Permalink
added meaningful error description
Browse files Browse the repository at this point in the history
  • Loading branch information
XantreDev committed Jan 12, 2024
1 parent 8a7cd1b commit 05a7704
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
55 changes: 38 additions & 17 deletions crates/oxc_linter/src/rules/import/no_named_as_default_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use oxc_syntax::module_record::ImportImportName;
use crate::{context::LintContext, rule::Rule};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-import(no-named-as-default-member): ")]
#[diagnostic(severity(warning), help(""))]
struct NoNamedAsDefaultMemberDignostic(#[label] pub Span);
#[error("eslint-plugin-import(no-named-as-default-member): {1:?} also has a named export {2:?}")]
#[diagnostic(severity(warning), help("Check if you meant to write `import {{{2:}}} from {3:?}`"))]
struct NoNamedAsDefaultMemberDignostic(#[label] pub Span, String, String, String);

/// <https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-named-as-default-member.md>
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Rule for NoNamedAsDefaultMember {
fn run_once(&self, ctx: &LintContext<'_>) {
let module_record = ctx.semantic().module_record();

let mut has_members_map: HashMap<&Atom, Ref<'_, Atom, _, _>> = HashMap::default();
let mut has_members_map: HashMap<&Atom, (Ref<'_, Atom, _, _>, String)> = HashMap::default();
for import_entry in &module_record.import_entries {
let ImportImportName::Default(_) = import_entry.import_name else {
continue;
Expand All @@ -61,17 +61,24 @@ impl Rule for NoNamedAsDefaultMember {
};

if !remote_module_record_ref.exported_bindings.is_empty() {
has_members_map.insert(import_entry.local_name.name(), remote_module_record_ref);
has_members_map.insert(
import_entry.local_name.name(),
(remote_module_record_ref, import_entry.module_request.name().to_string()),
);
}
}

if has_members_map.is_empty() {
return;
};
let external_module_has_entry = |module_name: &Atom, entry_name: &Atom| {
has_members_map
.get(&module_name)
.map_or(false, |it| it.exported_bindings.contains_key(entry_name))
let get_external_module_name_if_has_entry = |module_name: &Atom, entry_name: &Atom| {
has_members_map.get(&module_name).and_then(|it| {
if it.0.exported_bindings.contains_key(entry_name) {
Some(it.1.clone())
} else {
None
}
})
};

let process_member_expr = |member_expr: &MemberExpression| {
Expand All @@ -81,12 +88,19 @@ impl Rule for NoNamedAsDefaultMember {
let Some(prop_str) = member_expr.static_property_name() else {
return;
};
if external_module_has_entry(&ident.name, &Atom::new_inline(prop_str)) {
ctx.diagnostic(NoNamedAsDefaultMemberDignostic(match member_expr {
MemberExpression::ComputedMemberExpression(it) => it.span,
MemberExpression::StaticMemberExpression(it) => it.span,
MemberExpression::PrivateFieldExpression(it) => it.span,
}));
if let Some(module_name) =
get_external_module_name_if_has_entry(&ident.name, &Atom::new_inline(prop_str))
{
ctx.diagnostic(NoNamedAsDefaultMemberDignostic(
match member_expr {
MemberExpression::ComputedMemberExpression(it) => it.span,
MemberExpression::StaticMemberExpression(it) => it.span,
MemberExpression::PrivateFieldExpression(it) => it.span,
},
ident.name.to_string(),
prop_str.to_string(),
module_name,
));
};
};

Expand All @@ -107,8 +121,15 @@ impl Rule for NoNamedAsDefaultMember {

for prop in &*object_pattern.properties {
let Some(name) = prop.key.static_name() else { return };
if external_module_has_entry(&ident.name, &name) {
ctx.diagnostic(NoNamedAsDefaultMemberDignostic(decl.span));
if let Some(module_name) =
get_external_module_name_if_has_entry(&ident.name, &name)
{
ctx.diagnostic(NoNamedAsDefaultMemberDignostic(
decl.span,
ident.name.to_string(),
name.to_string(),
module_name,
));
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/oxc_linter/src/snapshots/no_named_as_default_member.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ source: crates/oxc_linter/src/tester.rs
assertion_line: 154
expression: no_named_as_default_member
---
eslint-plugin-import(no-named-as-default-member):
eslint-plugin-import(no-named-as-default-member): `"baz"` also has a named export `"a"`
╭─[index.js:1:1]
1import baz from "./named-exports"; const a = baz.a;
· ─────
╰────
help:
help: Check if you meant to write `import {a} from "./named-exports"`

eslint-plugin-import(no-named-as-default-member):
eslint-plugin-import(no-named-as-default-member): `"baz"` also has a named export `"a"`
╭─[index.js:1:1]
1import baz from "./named-exports"; const a = baz["a"];
· ────────
╰────
help:
help: Check if you meant to write `import {a} from "./named-exports"`

eslint-plugin-import(no-named-as-default-member):
eslint-plugin-import(no-named-as-default-member): `"baz"` also has a named export `"a"`
╭─[index.js:1:1]
1import baz from "./named-exports"; baz.a();
· ─────
╰────
help:
help: Check if you meant to write `import {a} from "./named-exports"`

eslint-plugin-import(no-named-as-default-member):
eslint-plugin-import(no-named-as-default-member): `"baz"` also has a named export `"a"`
╭─[index.js:1:1]
1import baz, { bar } from "./named-exports"; const {a} = baz
· ─────────
╰────
help:
help: Check if you meant to write `import {a} from "./named-exports"`


0 comments on commit 05a7704

Please sign in to comment.