/
named.rs
218 lines (204 loc) · 8.85 KB
/
named.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::module_record::{ExportImportName, ImportImportName};
use crate::{context::LintContext, rule::Rule};
#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-import(named): named import {0:?} not found")]
#[diagnostic(severity(warning), help("does {1:?} have the export {0:?}?"))]
struct NamedDiagnostic(String, String, #[label] pub Span);
/// <https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/named.md>
#[derive(Debug, Default, Clone)]
pub struct Named;
declare_oxc_lint!(
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```javascript
/// ```
Named,
nursery
);
impl Rule for Named {
fn run_once(&self, ctx: &LintContext<'_>) {
let semantic = ctx.semantic();
// This rule is disabled in the typescript config.
if semantic.source_type().is_typescript() {
return;
}
let module_record = semantic.module_record();
for import_entry in &module_record.import_entries {
// Get named import
let ImportImportName::Name(import_name) = &import_entry.import_name else {
continue;
};
let specifier = import_entry.module_request.name();
// Get remote module record
let Some(remote_module_record_ref) = module_record.loaded_modules.get(specifier) else {
continue;
};
let remote_module_record = remote_module_record_ref.value();
if remote_module_record.not_esm {
continue;
}
// Check remote bindings
if remote_module_record.exported_bindings.contains_key(import_name.name()) {
continue;
}
// check re-export
if remote_module_record
.exported_bindings_from_star_export
.iter()
.any(|entry| entry.value().contains(import_name.name()))
{
continue;
}
ctx.diagnostic(NamedDiagnostic(
import_name.name().to_string(),
specifier.to_string(),
import_name.span(),
));
}
for export_entry in &module_record.indirect_export_entries {
let Some(module_request) = &export_entry.module_request else {
continue;
};
let ExportImportName::Name(import_name) = &export_entry.import_name else {
continue;
};
let specifier = module_request.name();
// Get remote module record
let Some(remote_module_record_ref) = module_record.loaded_modules.get(specifier) else {
continue;
};
let remote_module_record = remote_module_record_ref.value();
// Check remote bindings
if remote_module_record.exported_bindings.contains_key(import_name.name()) {
continue;
}
ctx.diagnostic(NamedDiagnostic(
import_name.name().to_string(),
specifier.to_string(),
import_name.span(),
));
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
"import './malformed.js'",
"import { foo } from './bar'",
"import { foo } from './empty-module'",
"import bar from './bar.js'",
"import bar, { foo } from './bar.js'",
"import {a, b, d} from './named-exports'",
"import {ExportedClass} from './named-exports'",
"import { destructingAssign } from './named-exports'",
"import { destructingRenamedAssign } from './named-exports'",
"import { ActionTypes } from './qc'",
"import {a, b, c, d} from './re-export'",
// "import {RuleTester} from './re-export-node_modules'",
"import { jsxFoo } from './jsx/AnotherComponent'",
"import {a, b, d} from './common'; // eslint-disable-line named",
"import { foo, bar } from './re-export-names'",
// TODO: module.exports
// "import {a, b, c} from './re-export-common-star'",
// "import { foo, bar } from './common'",
// ignore core modules by default
"import { foo } from 'crypto'",
// "import { zoob } from 'a'",
"import { someThing } from './test-module'",
"export { foo } from './bar'",
"export { foo as bar } from './bar'",
"export { foo } from './does-not-exist'",
// No longer valid syntax
// "export bar, { foo } from './bar'",
// "import { foo, bar } from './named-trampoline'",
"let foo; export { foo as bar }",
"import { destructuredProp } from './named-exports'",
"import { arrayKeyProp } from './named-exports'",
"import { deepProp } from './named-exports'",
"import { deepSparseElement } from './named-exports'",
// Flow not supported
// "import type { MissingType } from './flowtypes'",
// "import typeof { MissingType } from './flowtypes'",
// "import type { MyOpaqueType } from './flowtypes'",
// "import typeof { MyOpaqueType } from './flowtypes'",
// "import { type MyOpaqueType, MyClass } from './flowtypes'",
// "import { typeof MyOpaqueType, MyClass } from './flowtypes'",
// "import typeof MissingType from './flowtypes'",
// "import typeof * as MissingType from './flowtypes'",
// "export type { MissingType } from './flowtypes'",
// "export type { MyOpaqueType } from './flowtypes'",
"/*jsnext*/ import { createStore } from 'redux'",
"/*jsnext*/ import { createStore } from 'redux'",
"import { foo } from 'es6-module'",
"import { me, soGreat } from './narcissist'",
"import { foo, bar, baz } from './re-export-default'",
"import { common } from './re-export-default'",
// "import {a, b, d} from './common'",
// settings: { 'import/ignore': ['bar'] },
// "import { baz } from './bar'",
"import { common } from './re-export-default'",
// "const { destructuredProp } = require('./named-exports')",
// "let { arrayKeyProp } = require('./named-exports')",
// "const { deepProp } = require('./named-exports')",
// "const { foo, bar } = require('./re-export-names')",
// "const { baz } = require('./bar')",
// "const { baz } = require('./bar')",
// "const { default: defExport } = require('./bar')",
// "import { ExtfieldModel, Extfield2Model } from './models';", filename: testFilePath('./export-star/downstream.js'),
// "const { something } = require('./dynamic-import-in-commonjs')",
// "import { something } from './dynamic-import-in-commonjs'",
"import { 'foo' as foo } from './bar'",
"import { 'foo' as foo } from './empty-module'",
// export all
"import { foo } from './export-all'",
// TypeScript export assignment
"import x from './typescript-export-assign-object'",
];
let fail = vec![
"import { somethingElse } from './test-module'",
"import { baz } from './bar'",
"import { baz, bop } from './bar'",
"import {a, b, c} from './named-exports'",
"import { a } from './default-export'",
"import { ActionTypes1 } from './qc'",
"import {a, b, c, d, e} from './re-export'",
"import { a } from './re-export-names'",
"export { bar } from './bar'",
"export bar2, { bar } from './bar'",
// old babel parser
// "import { foo, bar, baz } from './named-trampoline'",
// "import { baz } from './broken-trampoline'",
// cjs
// "const { baz } = require('./bar')",
// "let { baz } = require('./bar')",
// "const { baz: bar, bop } = require('./bar'), { a } = require('./re-export-names')",
// "const { default: defExport } = require('./named-exports')",
// flow
// "import { type MyOpaqueType, MyMissingClass } from './flowtypes'",
// jsnext
// "/*jsnext*/ import { createSnorlax } from 'redux'",
"import { baz } from 'es6-module'",
"import { foo, bar, bap } from './re-export-default'",
"import { default as barDefault } from './re-export'",
// export all
"import { bar } from './export-all'",
// TypeScript
// Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead.
"import { NotExported } from './typescript-export-assign-object'",
"import { FooBar } from './typescript-export-assign-object'",
];
Tester::new(Named::NAME, pass, fail)
.change_rule_path("index.js")
.with_import_plugin(true)
.test_and_snapshot();
}