]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-useless-rename | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | related_rules: | |
5 | - object-shorthand | |
6 | --- | |
7 | ||
8 | ||
eb39fafa DC |
9 | |
10 | ES2015 allows for the renaming of references in import and export statements as well as destructuring assignments. This gives programmers a concise syntax for performing these operations while renaming these references: | |
11 | ||
12 | ```js | |
13 | import { foo as bar } from "baz"; | |
14 | export { foo as bar }; | |
15 | let { foo: bar } = baz; | |
16 | ``` | |
17 | ||
18 | With this syntax, it is possible to rename a reference to the same name. This is a completely redundant operation, as this is the same as not renaming at all. For example, this: | |
19 | ||
20 | ```js | |
21 | import { foo as foo } from "bar"; | |
22 | export { foo as foo }; | |
23 | let { foo: foo } = bar; | |
24 | ``` | |
25 | ||
26 | is the same as: | |
27 | ||
28 | ```js | |
29 | import { foo } from "bar"; | |
30 | export { foo }; | |
31 | let { foo } = bar; | |
32 | ``` | |
33 | ||
34 | ## Rule Details | |
35 | ||
36 | This rule disallows the renaming of import, export, and destructured assignments to the same name. | |
37 | ||
eb39fafa DC |
38 | ## Options |
39 | ||
40 | This rule allows for more fine-grained control with the following options: | |
41 | ||
8f9d1d4d DC |
42 | * `ignoreImport`: When set to `true`, this rule does not check imports |
43 | * `ignoreExport`: When set to `true`, this rule does not check exports | |
44 | * `ignoreDestructuring`: When set to `true`, this rule does not check destructuring assignments | |
eb39fafa DC |
45 | |
46 | By default, all options are set to `false`: | |
47 | ||
48 | ```json | |
49 | "no-useless-rename": ["error", { | |
50 | "ignoreDestructuring": false, | |
51 | "ignoreImport": false, | |
52 | "ignoreExport": false | |
53 | }] | |
54 | ``` | |
55 | ||
56 | Examples of **incorrect** code for this rule by default: | |
57 | ||
8f9d1d4d DC |
58 | ::: incorrect |
59 | ||
eb39fafa DC |
60 | ```js |
61 | /*eslint no-useless-rename: "error"*/ | |
62 | ||
63 | import { foo as foo } from "bar"; | |
8f9d1d4d | 64 | import { "foo" as foo } from "bar"; |
eb39fafa | 65 | export { foo as foo }; |
8f9d1d4d | 66 | export { foo as "foo" }; |
eb39fafa | 67 | export { foo as foo } from "bar"; |
8f9d1d4d | 68 | export { "foo" as "foo" } from "bar"; |
eb39fafa DC |
69 | let { foo: foo } = bar; |
70 | let { 'foo': foo } = bar; | |
71 | function foo({ bar: bar }) {} | |
72 | ({ foo: foo }) => {} | |
73 | ``` | |
74 | ||
8f9d1d4d DC |
75 | ::: |
76 | ||
eb39fafa DC |
77 | Examples of **correct** code for this rule by default: |
78 | ||
8f9d1d4d DC |
79 | ::: correct |
80 | ||
eb39fafa DC |
81 | ```js |
82 | /*eslint no-useless-rename: "error"*/ | |
83 | ||
84 | import * as foo from "foo"; | |
85 | import { foo } from "bar"; | |
86 | import { foo as bar } from "baz"; | |
8f9d1d4d | 87 | import { "foo" as bar } from "baz"; |
eb39fafa DC |
88 | |
89 | export { foo }; | |
90 | export { foo as bar }; | |
8f9d1d4d | 91 | export { foo as "bar" }; |
eb39fafa | 92 | export { foo as bar } from "foo"; |
8f9d1d4d | 93 | export { "foo" as "bar" } from "foo"; |
eb39fafa DC |
94 | |
95 | let { foo } = bar; | |
96 | let { foo: bar } = baz; | |
97 | let { [foo]: foo } = bar; | |
98 | ||
99 | function foo({ bar }) {} | |
100 | function foo({ bar: baz }) {} | |
101 | ||
102 | ({ foo }) => {} | |
103 | ({ foo: bar }) => {} | |
104 | ``` | |
105 | ||
8f9d1d4d DC |
106 | ::: |
107 | ||
eb39fafa DC |
108 | Examples of **correct** code for this rule with `{ ignoreImport: true }`: |
109 | ||
8f9d1d4d DC |
110 | ::: correct |
111 | ||
eb39fafa DC |
112 | ```js |
113 | /*eslint no-useless-rename: ["error", { ignoreImport: true }]*/ | |
114 | ||
115 | import { foo as foo } from "bar"; | |
116 | ``` | |
117 | ||
8f9d1d4d DC |
118 | ::: |
119 | ||
eb39fafa DC |
120 | Examples of **correct** code for this rule with `{ ignoreExport: true }`: |
121 | ||
8f9d1d4d DC |
122 | ::: correct |
123 | ||
eb39fafa DC |
124 | ```js |
125 | /*eslint no-useless-rename: ["error", { ignoreExport: true }]*/ | |
126 | ||
127 | export { foo as foo }; | |
128 | export { foo as foo } from "bar"; | |
129 | ``` | |
130 | ||
8f9d1d4d DC |
131 | ::: |
132 | ||
eb39fafa DC |
133 | Examples of **correct** code for this rule with `{ ignoreDestructuring: true }`: |
134 | ||
8f9d1d4d DC |
135 | ::: correct |
136 | ||
eb39fafa DC |
137 | ```js |
138 | /*eslint no-useless-rename: ["error", { ignoreDestructuring: true }]*/ | |
139 | ||
140 | let { foo: foo } = bar; | |
141 | function foo({ bar: bar }) {} | |
142 | ({ foo: foo }) => {} | |
143 | ``` | |
144 | ||
8f9d1d4d DC |
145 | ::: |
146 | ||
eb39fafa DC |
147 | ## When Not To Use It |
148 | ||
149 | You can safely disable this rule if you do not care about redundantly renaming import, export, and destructuring assignments. | |
150 | ||
151 | ## Compatibility | |
152 | ||
8f9d1d4d | 153 | * **JSCS**: [disallowIdenticalDestructuringNames](https://jscs-dev.github.io/rule/disallowIdenticalDestructuringNames) |