Skip to content

Commit 968a02a

Browse files
authored
feat: Support arbitrary module namespace names in no-useless-rename (#15493)
* feat: Support arbitrary module namespace names in no-useless-rename Refs #15465 * remove extra blank line
1 parent ba6317b commit 968a02a

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

docs/rules/no-useless-rename.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ Examples of **incorrect** code for this rule by default:
5252
/*eslint no-useless-rename: "error"*/
5353

5454
import { foo as foo } from "bar";
55+
import { "foo" as foo } from "bar";
5556
export { foo as foo };
57+
export { foo as "foo" };
5658
export { foo as foo } from "bar";
59+
export { "foo" as "foo" } from "bar";
5760
let { foo: foo } = bar;
5861
let { 'foo': foo } = bar;
5962
function foo({ bar: bar }) {}
@@ -68,10 +71,13 @@ Examples of **correct** code for this rule by default:
6871
import * as foo from "foo";
6972
import { foo } from "bar";
7073
import { foo as bar } from "baz";
74+
import { "foo" as bar } from "baz";
7175

7276
export { foo };
7377
export { foo as bar };
78+
export { foo as "bar" };
7479
export { foo as bar } from "foo";
80+
export { "foo" as "bar" } from "foo";
7581

7682
let { foo } = bar;
7783
let { foo: bar } = baz;

lib/rules/no-useless-rename.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ module.exports = {
132132
return;
133133
}
134134

135-
if (node.imported.name === node.local.name &&
136-
node.imported.range[0] !== node.local.range[0]) {
135+
if (
136+
node.imported.range[0] !== node.local.range[0] &&
137+
astUtils.getModuleExportName(node.imported) === node.local.name
138+
) {
137139
reportError(node, node.imported, "Import");
138140
}
139141
}
@@ -148,8 +150,10 @@ module.exports = {
148150
return;
149151
}
150152

151-
if (node.local.name === node.exported.name &&
152-
node.local.range[0] !== node.exported.range[0]) {
153+
if (
154+
node.local.range[0] !== node.exported.range[0] &&
155+
astUtils.getModuleExportName(node.local) === astUtils.getModuleExportName(node.exported)
156+
) {
153157
reportError(node, node.local, "Export");
154158
}
155159

tests/lib/rules/no-useless-rename.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,43 @@ ruleTester.run("no-useless-rename", rule, {
4747
"import {foo} from 'foo';",
4848
"import {foo as bar} from 'foo';",
4949
"import {foo as bar, baz as qux} from 'foo';",
50+
{
51+
code: "import {'foo' as bar} from 'baz';",
52+
parserOptions: { ecmaVersion: 2022 }
53+
},
5054
"export {foo} from 'foo';",
5155
"var foo = 0;export {foo as bar};",
5256
"var foo = 0; var baz = 0; export {foo as bar, baz as qux};",
5357
"export {foo as bar} from 'foo';",
5458
"export {foo as bar, baz as qux} from 'foo';",
59+
{
60+
code: "var foo = 0; export {foo as 'bar'};",
61+
parserOptions: { ecmaVersion: 2022 }
62+
},
63+
{
64+
code: "export {foo as 'bar'} from 'baz';",
65+
parserOptions: { ecmaVersion: 2022 }
66+
},
67+
{
68+
code: "export {'foo' as bar} from 'baz';",
69+
parserOptions: { ecmaVersion: 2022 }
70+
},
71+
{
72+
code: "export {'foo' as 'bar'} from 'baz';",
73+
parserOptions: { ecmaVersion: 2022 }
74+
},
75+
{
76+
code: "export {'' as ' '} from 'baz';",
77+
parserOptions: { ecmaVersion: 2022 }
78+
},
79+
{
80+
code: "export {' ' as ''} from 'baz';",
81+
parserOptions: { ecmaVersion: 2022 }
82+
},
83+
{
84+
code: "export {'foo'} from 'bar';",
85+
parserOptions: { ecmaVersion: 2022 }
86+
},
5587
{
5688
code: "const {...stuff} = myObject;",
5789
parserOptions: { ecmaVersion: 2018 }
@@ -380,6 +412,12 @@ ruleTester.run("no-useless-rename", rule, {
380412
output: "import {foo} from 'foo';",
381413
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Import", name: "foo" } }]
382414
},
415+
{
416+
code: "import {'foo' as foo} from 'foo';",
417+
output: "import {foo} from 'foo';",
418+
parserOptions: { ecmaVersion: 2022 },
419+
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Import", name: "foo" } }]
420+
},
383421
{
384422
code: "import {\\u0061 as a} from 'foo';",
385423
output: "import {a} from 'foo';",
@@ -418,6 +456,42 @@ ruleTester.run("no-useless-rename", rule, {
418456
output: "var foo = 0; export {foo};",
419457
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Export", name: "foo" } }]
420458
},
459+
{
460+
code: "var foo = 0; export {foo as 'foo'};",
461+
output: "var foo = 0; export {foo};",
462+
parserOptions: { ecmaVersion: 2022 },
463+
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Export", name: "foo" } }]
464+
},
465+
{
466+
code: "export {foo as 'foo'} from 'bar';",
467+
output: "export {foo} from 'bar';",
468+
parserOptions: { ecmaVersion: 2022 },
469+
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Export", name: "foo" } }]
470+
},
471+
{
472+
code: "export {'foo' as foo} from 'bar';",
473+
output: "export {'foo'} from 'bar';",
474+
parserOptions: { ecmaVersion: 2022 },
475+
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Export", name: "foo" } }]
476+
},
477+
{
478+
code: "export {'foo' as 'foo'} from 'bar';",
479+
output: "export {'foo'} from 'bar';",
480+
parserOptions: { ecmaVersion: 2022 },
481+
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Export", name: "foo" } }]
482+
},
483+
{
484+
code: "export {' 👍 ' as ' 👍 '} from 'bar';",
485+
output: "export {' 👍 '} from 'bar';",
486+
parserOptions: { ecmaVersion: 2022 },
487+
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Export", name: " 👍 " } }]
488+
},
489+
{
490+
code: "export {'' as ''} from 'bar';",
491+
output: "export {''} from 'bar';",
492+
parserOptions: { ecmaVersion: 2022 },
493+
errors: [{ messageId: "unnecessarilyRenamed", data: { type: "Export", name: "" } }]
494+
},
421495
{
422496
code: "var a = 0; export {a as \\u0061};",
423497
output: "var a = 0; export {a};",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy