/
no_duplicates.rs
215 lines (198 loc) · 8.54 KB
/
no_duplicates.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
use itertools::Itertools;
use oxc_diagnostics::miette::{miette, LabeledSpan, Severity};
use oxc_macros::declare_oxc_lint;
use crate::{context::LintContext, rule::Rule};
/// <https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-duplicates.md>
#[derive(Debug, Default, Clone)]
pub struct NoDuplicates;
declare_oxc_lint!(
/// ### What it does
///
/// Reports if a resolved path is imported more than once.
NoDuplicates,
nursery
);
impl Rule for NoDuplicates {
fn run_once(&self, ctx: &LintContext<'_>) {
let module_record = ctx.semantic().module_record();
let groups = module_record
.requested_modules
.iter()
.map(|(source, spans)| {
let resolved_absolute_path = module_record.loaded_modules.get(source).map_or_else(
|| source.to_string(),
|module| module.resolved_absolute_path.to_string_lossy().to_string(),
);
(resolved_absolute_path, spans)
})
.group_by(|r| r.0.clone());
for (_path, group) in &groups {
let labels = group
.into_iter()
.flat_map(|(_path, spans)| spans)
.map(|span| LabeledSpan::underline(*span))
.collect::<Vec<_>>();
if labels.len() > 1 {
ctx.diagnostic(miette!(
severity = Severity::Warning,
labels = labels,
"eslint-plugin-import(no-duplicates): Forbid repeated import of the same module in multiple places"
));
}
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
r#"import "./malformed.js""#,
r"import { x } from './foo'; import { y } from './bar'",
r#"import foo from "234artaf"; import { shoop } from "234q25ad""#,
// r#"import { x } from './foo'; import type { y } from './foo'"#,
// TODO: considerQueryString
// r#"import x from './bar?optionX'; import y from './bar?optionY';"#,
r"import x from './foo'; import y from './bar';",
// TODO: separate namespace
// r#"import * as ns from './foo'; import {y} from './foo'"#,
// r#"import {y} from './foo'; import * as ns from './foo'"#,
// TypeScript
// TODO: distinguish type imports in module record
// r#"import type { x } from './foo'; import y from './foo'"#,
// r#"import type x from './foo'; import type y from './bar'"#,
// r#"import type {x} from './foo'; import type {y} from './bar'"#,
// r#"import type x from './foo'; import type {y} from './foo'"#,
// r#"import type {} from './module';
// import {} from './module2';"#,
// r#"import type { Identifier } from 'module';
// declare module 'module2' {
// import type { Identifier } from 'module';
// }
// declare module 'module3' {
// import type { Identifier } from 'module';
// }"#,
// r#"import { type x } from './foo'; import y from './foo'"#,
// r#"import { type x } from './foo'; import { y } from './foo'"#,
// r#"import { type x } from './foo'; import type y from 'foo'"#,
];
let fail = vec![
r"import { x } from './foo'; import { y } from './foo'",
r"import {x} from './foo'; import {y} from './foo'; import { z } from './foo'",
// TODO: settings: { 'import/resolve': { paths: [path.join(process.cwd(), 'tests', 'files')], }, },
// r#"import { x } from './bar'; import { y } from 'bar';"#,
r"import x from './bar.js?optionX'; import y from './bar?optionX';",
r"import x from './bar?optionX'; import y from './bar?optionY';",
r"import x from './bar?optionX'; import y from './bar.js?optionX';",
// we can't figure out non-existent files
// r#"import foo from 'non-existent'; import bar from 'non-existent';"#,
// r#"import type { x } from './foo'; import type { y } from './foo'"#,
r"import './foo'; import './foo'",
r"import { x, /* x */ } from './foo'; import {//y
y//y2
} from './foo'",
r"import {x} from './foo'; import {} from './foo'",
r"import {a} from './foo'; import { a } from './foo'",
r"import {a,b} from './foo'; import { b, c } from './foo'; import {b,c,d} from './foo'",
r"import {a} from './foo'; import { a/*,b*/ } from './foo'",
r"import {a} from './foo'; import { a } from './foo'",
r"import {a,b} from './foo'; import { b, c } from './foo'; import {b,c,d} from './foo'",
r"import {a} from './foo'; import { a/*,b*/ } from './foo'",
r"import {x} from './foo'; import {} from './foo'; import {/*c*/} from './foo'; import {y} from './foo'",
r"import { } from './foo'; import {x} from './foo'",
r"import './foo'; import {x} from './foo'",
r"import'./foo'; import {x} from './foo'",
r"import './foo'; import { /*x*/} from './foo'; import {//y
} from './foo'; import {z} from './foo'",
r"import './foo'; import def, {x} from './foo'",
r"import './foo'; import def from './foo'",
r"import def from './foo'; import {x} from './foo'",
r"import {x} from './foo'; import def from './foo'",
r"import{x} from './foo'; import def from './foo'",
r"import {x} from './foo'; import def, {y} from './foo'",
r"import * as ns1 from './foo'; import * as ns2 from './foo'",
r"import * as ns from './foo'; import {x} from './foo'; import {y} from './foo'",
r"import {x} from './foo'; import * as ns from './foo'; import {y} from './foo'; import './foo'",
r"// some-tool-disable-next-line
import {x} from './foo'
import {//y
y} from './foo'",
r"import {x} from './foo'
// some-tool-disable-next-line
import {y} from './foo'",
r"import {x} from './foo' // some-tool-disable-line
import {y} from './foo'",
r"import {x} from './foo'
import {y} from './foo' // some-tool-disable-line",
r"import {x} from './foo'
/* comment */ import {y} from './foo'",
r"import {x} from './foo'
import {y} from './foo' /* comment
multiline */",
r"import {x} from './foo'
import {y} from './foo'
// some-tool-disable-next-line",
r"import {x} from './foo'
// comment
import {y} from './foo'",
r"import {x} from './foo'
import/* comment */{y} from './foo'",
r"import {x} from './foo'
import/* comment */'./foo'",
r"import {x} from './foo'
import{y}/* comment */from './foo'",
r"import {x} from './foo'
import{y}from/* comment */'./foo'",
r"import {x} from
// some-tool-disable-next-line
'./foo'
import {y} from './foo'",
r"import { Foo } from './foo';
import { Bar } from './foo';
export const value = {}",
r"import { Foo } from './foo';
import Bar from './foo';
export const value = {}",
r"import {
DEFAULT_FILTER_KEYS,
BULK_DISABLED,
} from '../constants';
import React from 'react';
import {
BULK_ACTIONS_ENABLED
} from '../constants';
const TestComponent = () => {
return <div>
</div>;
}
export default TestComponent;",
// TODO: figure out module imports
r"import {A1,} from 'foo';
import {B1,} from 'foo';
import {C1,} from 'foo';
import {
A2,
} from 'bar';
import {
B2,
} from 'bar';
import {
C2,
} from 'bar';",
// TypeScript
// TODO: distinguish type imports in module record
// r#"import type x from './foo'; import type y from './foo'"#,
// r#"import type x from './foo'; import type x from './foo'"#,
// r#"import type {x} from './foo'; import type {y} from './foo'"#,
// r#"import {type x} from './foo'; import type {y} from './foo'"#,
// r#"import {type x} from 'foo'; import type {y} from 'foo'"#,
// r#"import {type x} from 'foo'; import type {y} from 'foo'"#,
// r#"import {type x} from './foo'; import {type y} from './foo'"#,
// r#"import {type x} from './foo'; import {type y} from './foo'"#,
// r#"import {AValue, type x, BValue} from './foo'; import {type y} from './foo'"#,
// r#"import {AValue} from './foo'; import type {AType} from './foo'"#,
];
Tester::new(NoDuplicates::NAME, pass, fail)
.change_rule_path("index.ts")
.with_import_plugin(true)
.test_and_snapshot();
}