forked from antfu/eslint-plugin-antfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-dedupe.ts
57 lines (53 loc) · 1.34 KB
/
import-dedupe.ts
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
import { createEslintRule } from "../utils";
export const RULE_NAME = "import-dedupe";
export type MessageIds = "importDedupe";
export type Options = [];
export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: "problem",
docs: {
description: "Fix duplication in imports",
recommended: "strict"
},
fixable: "code",
schema: [],
messages: {
importDedupe: "Expect no duplication in imports"
}
},
defaultOptions: [],
create: (context) => {
return {
ImportDeclaration(node) {
if (node.specifiers.length <= 1) {
return;
}
const names = new Set<string>();
node.specifiers.forEach((n) => {
const id = n.local.name;
if (names.has(id)) {
context.report({
node,
loc: {
start: n.loc.end,
end: n.loc.start
},
messageId: "importDedupe",
fix(fixer) {
const s = n.range[0];
let e = n.range[1];
if (context.getSourceCode().text[e] === ",") {
e += 1;
}
return fixer.removeRange([s, e]);
}
});
}
names.add(id);
});
// console.log(node)
}
};
}
});