-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
no_named_as_default_member.rs
189 lines (172 loc) · 6.79 KB
/
no_named_as_default_member.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#![allow(clippy::significant_drop_tightening)]
use std::collections::HashMap;
use dashmap::mapref::one::Ref;
use oxc_ast::{
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::{CompactStr, Span};
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): {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)]
pub struct NoNamedAsDefaultMember;
declare_oxc_lint!(
/// ### What it does
///
/// Reports use of an exported name as a property on the default export.
///
/// ### Example
///
/// ```javascript
/// // ./bar.js
/// export function bar() { return null }
/// export default () => { return 1 }
///
/// // ./foo.js
/// import bar from './bar'
/// const bar = foo.bar // trying to access named export via default
/// ```
NoNamedAsDefaultMember,
suspicious
);
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<SymbolId, (Ref<'_, CompactStr, _, _>, CompactStr)> =
HashMap::default();
for import_entry in &module_record.import_entries {
let ImportImportName::Default(_) = import_entry.import_name else {
continue;
};
let specifier = import_entry.module_request.name();
let Some(remote_module_record_ref) = module_record.loaded_modules.get(specifier) else {
continue;
};
if !remote_module_record_ref.exported_bindings.is_empty() {
has_members_map.insert(
ctx.symbols().get_symbol_id_from_span(&import_entry.local_name.span()).unwrap(),
(remote_module_record_ref, import_entry.module_request.name().clone()),
);
}
}
if has_members_map.is_empty() {
return;
};
let get_external_module_name_if_has_entry =
|ident: &IdentifierReference, entry_name: &str| {
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 {
return;
};
let Some(prop_str) = member_expr.static_property_name() else {
return;
};
if let Some(module_name) = get_external_module_name_if_has_entry(ident, 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,
));
};
};
for item in ctx.semantic().nodes().iter() {
match item.kind() {
AstKind::MemberExpression(member_expr) => process_member_expr(member_expr),
AstKind::VariableDeclarator(decl) => {
let Some(Expression::Identifier(ident)) = &decl.init else {
continue;
};
let BindingPatternKind::ObjectPattern(object_pattern) = &decl.id.kind else {
continue;
};
for prop in &*object_pattern.properties {
let Some(name) = prop.key.static_name() else {
continue;
};
if let Some(module_name) =
get_external_module_name_if_has_entry(ident, &name)
{
ctx.diagnostic(NoNamedAsDefaultMemberDignostic(
decl.span,
ident.name.to_string(),
name.to_string(),
module_name,
));
}
}
}
_ => {}
}
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
r#"import baz, {a} from "./named-exports""#,
r#"import baz from "./named-exports"; const jjj = bar.jjj"#,
r#"import {a} from "./named-exports"; const baz = a.baz"#,
r#"import baz from "./default_export_default_property"; const d = baz.default;"#,
r#"import baz, {foo} from "./named-and-default-export"; const d = baz.default;"#,
r"import baz from './named-exports';
{
const baz = {};
const a = baz.a;
}",
r"import baz from './named-exports';
{
const baz = {};
const a = baz.looooooooooooooooooooooooong;
}",
];
let fail = vec![
r#"import baz from "./named-exports"; const a = baz.a;"#,
r#"import baz from "./named-exports"; const a = baz["a"];"#,
r#"import baz from "./named-exports"; baz.a();"#,
r"import baz from './named-exports';
{
const a = baz.a;
}",
r#"import baz, { bar } from "./named-exports"; const {a} = baz"#,
r#"import baz from "./named-and-default-export"; const {foo: _foo} = baz"#,
];
Tester::new(NoNamedAsDefaultMember::NAME, pass, fail)
.change_rule_path("index.js")
.with_import_plugin(true)
.test_and_snapshot();
}