# no-useless-rename

不允许将导入、导出和解构的分配重命名为相同的名称

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

ES2015 允许重命名 import 和 export 语句中的引用以及解构赋值。这为程序员在重命名这些引用时执行这些操作提供了简洁的语法:

import { foo as bar } from "baz";
export { foo as bar };
let { foo: bar } = baz;

使用此语法,可以将引用重命名为相同名称。这是一个完全多余的操作,因为这与根本不重命名是一样的。例如,这个:

import { foo as foo } from "bar";
export { foo as foo };
let { foo: foo } = bar;

是相同的:

import { foo } from "bar";
export { foo };
let { foo } = bar;

# 规则详情

此规则不允许将导入、导出和解构分配重命名为相同名称。

# 选项

此规则允许使用以下选项进行更细粒度的控制:

  • ignoreImport:设置为 true 时,此规则不检查导入
  • ignoreExport:设置为 true 时,此规则不检查导出
  • ignoreDestructuring:设置为 true 时,此规则不检查解构分配

默认情况下,所有选项都设置为 false

"no-useless-rename": ["error", {
    "ignoreDestructuring": false,
    "ignoreImport": false,
    "ignoreExport": false
}]

默认情况下,此规则的错误代码示例:

/*eslint no-useless-rename: "error"*/

import { foo as foo } from "bar";
import { "foo" as foo } from "bar";
export { foo as foo };
export { foo as "foo" };
export { foo as foo } from "bar";
export { "foo" as "foo" } from "bar";
let { foo: foo } = bar;
let { 'foo': foo } = bar;
function foo({ bar: bar }) {}
({ foo: foo }) => {}

默认情况下此规则的正确代码示例:

/*eslint no-useless-rename: "error"*/

import * as foo from "foo";
import { foo } from "bar";
import { foo as bar } from "baz";
import { "foo" as bar } from "baz";

export { foo };
export { foo as bar };
export { foo as "bar" };
export { foo as bar } from "foo";
export { "foo" as "bar" } from "foo";

let { foo } = bar;
let { foo: bar } = baz;
let { [foo]: foo } = bar;

function foo({ bar }) {}
function foo({ bar: baz }) {}

({ foo }) => {}
({ foo: bar }) => {}

此规则的正确代码示例(带有 { ignoreImport: true }):

/*eslint no-useless-rename: ["error", { ignoreImport: true }]*/

import { foo as foo } from "bar";

此规则的正确代码示例(带有 { ignoreExport: true }):

/*eslint no-useless-rename: ["error", { ignoreExport: true }]*/

export { foo as foo };
export { foo as foo } from "bar";

此规则的正确代码示例(带有 { ignoreDestructuring: true }):

/*eslint no-useless-rename: ["error", { ignoreDestructuring: true }]*/

let { foo: foo } = bar;
function foo({ bar: bar }) {}
({ foo: foo }) => {}

# 何时不使用

如果您不关心重复地重命名导入、导出和解构分配,则可以安全地禁用此规则。

# 兼容性

  • JSCSdisallowIdenticalDestructuringNames
Last Updated: 5/13/2023, 8:55:38 PM