Skip to content

Commit

Permalink
fix(linter): eslint-plugin-import no-named-as-default-member rule (ox…
Browse files Browse the repository at this point in the history
  • Loading branch information
XantreDev authored and IWANABETHATGUY committed May 29, 2024
1 parent 464cc38 commit 48ff90e
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 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 @@ -3,14 +3,15 @@ use std::collections::HashMap;

use dashmap::mapref::one::Ref;
use oxc_ast::{
ast::{BindingPatternKind, Expression, MemberExpression},
ast::{BindingPatternKind, Expression, IdentifierReference, MemberExpression},
AstKind,
};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_semantic::SymbolId;
use oxc_span::{Atom, Span};
use oxc_syntax::module_record::ImportImportName;

Expand Down Expand Up @@ -44,12 +45,21 @@ declare_oxc_lint!(
NoNamedAsDefaultMember,
nursery
);
fn get_symbol_id_from_ident(
ctx: &LintContext<'_>,
ident: &IdentifierReference,
) -> Option<SymbolId> {
let reference_id = ident.reference_id.get().unwrap();
let reference = &ctx.symbols().references[reference_id];
reference.symbol_id()
}

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, _, _>, Atom)> = HashMap::default();
let mut has_members_map: HashMap<SymbolId, (Ref<'_, Atom, _, _>, Atom)> =
HashMap::default();
for import_entry in &module_record.import_entries {
let ImportImportName::Default(_) = import_entry.import_name else {
continue;
Expand All @@ -62,7 +72,7 @@ impl Rule for NoNamedAsDefaultMember {

if !remote_module_record_ref.exported_bindings.is_empty() {
has_members_map.insert(
import_entry.local_name.name(),
ctx.symbols().get_symbol_id_from_span(&import_entry.local_name.span()).unwrap(),
(remote_module_record_ref, import_entry.module_request.name().to_owned()),
);
}
Expand All @@ -71,15 +81,18 @@ impl Rule for NoNamedAsDefaultMember {
if has_members_map.is_empty() {
return;
};
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.to_string())
} else {
None
}
})
};
let get_external_module_name_if_has_entry =
|ident: &IdentifierReference, entry_name: &Atom| {
get_symbol_id_from_ident(ctx, ident)
.and_then(|symbol_id| has_members_map.get(&symbol_id))
.and_then(|it| {
if it.0.exported_bindings.contains_key(entry_name) {
Some(it.1.to_string())
} else {
None
}
})
};

let process_member_expr = |member_expr: &MemberExpression| {
let Expression::Identifier(ident) = member_expr.object() else {
Expand All @@ -89,7 +102,7 @@ impl Rule for NoNamedAsDefaultMember {
return;
};
if let Some(module_name) =
get_external_module_name_if_has_entry(&ident.name, &Atom::new_inline(prop_str))
get_external_module_name_if_has_entry(ident, &Atom::new_inline(prop_str))
{
ctx.diagnostic(NoNamedAsDefaultMemberDignostic(
match member_expr {
Expand All @@ -108,21 +121,19 @@ impl Rule for NoNamedAsDefaultMember {
match item.kind() {
AstKind::MemberExpression(member_expr) => process_member_expr(member_expr),
AstKind::VariableDeclarator(decl) => {
if let Some(Expression::MemberExpression(member_expr)) = &decl.init {
process_member_expr(member_expr);
return;
}
let Some(Expression::Identifier(ident)) = &decl.init else {
return;
continue;
};
let BindingPatternKind::ObjectPattern(object_pattern) = &decl.id.kind else {
return;
continue;
};

for prop in &*object_pattern.properties {
let Some(name) = prop.key.static_name() else { return };
let Some(name) = prop.key.static_name() else {
continue;
};
if let Some(module_name) =
get_external_module_name_if_has_entry(&ident.name, &name)
get_external_module_name_if_has_entry(ident, &name)
{
ctx.diagnostic(NoNamedAsDefaultMemberDignostic(
decl.span,
Expand Down

0 comments on commit 48ff90e

Please sign in to comment.