unicorn/prefer-array-flat Pedantic
它的作用
优先使用 Array#flat(),而不是使用旧式技术来展平数组。
为什么这不好?
ES2019 引入了一个新方法 Array#flat(),用于展平数组。
此规则旨在将展平数组的方式统一为使用 Array#flat(),而不是旧式技术。
示例
以下是此规则的错误代码示例:
javascript
const foo = array.flatMap((x) => x);
const foo = array.reduce((a, b) => a.concat(b), []);
const foo = array.reduce((a, b) => [...a, ...b], []);
const foo = [].concat(maybeArray);
const foo = [].concat(...array);
const foo = [].concat.apply([], array);
const foo = Array.prototype.concat.apply([], array);
const foo = Array.prototype.concat.call([], maybeArray);
const foo = Array.prototype.concat.call([], ...array);以下是此规则的正确代码示例:
javascript
const foo = array.flat();
const foo = [maybeArray].flat();如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"unicorn/prefer-array-flat": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/prefer-array-flat": "error",
},
});bash
oxlint --deny unicorn/prefer-array-flat版本
此规则于 v0.0.20 中添加。
