import nodeTest from 'node:test'; import {runInNewContext} from 'node:vm'; import {Linter} from 'eslint'; import outdent from 'outdent'; import {getTester, parsers} from './utils/test.js'; const {test, rule} = getTester(import.meta); const linter = new Linter(); const config = { plugins: {unicorn: {rules: {'prefer-object-iterable-methods': rule}}}, rules: {'unicorn/prefer-object-iterable-methods': 'error'}, }; // `Object.values()` reads every value before iteration, so a loop or callback that changes the object can see different values after the fix. That is a documented limitation, so the common forms with calls are still autofixed. nodeTest('converting `Object.keys()` is autofixed when the value is passed to a call', t => { for (const [code, expectedOutput] of [ [ 'const object = {a: 1, b: 2}; const foo = (value, key) => key + value; JSON.stringify(Object.keys(object).map(key => foo(object[key], key)));', 'const object = {a: 1, b: 2}; const foo = (value, key) => key + value; JSON.stringify(Object.entries(object).map(([key, value]) => foo(value, key)));', ], [ 'const object = {a: 1, b: 2}; const result = []; for (const key of Object.keys(object)) { result.push(object[key]); } JSON.stringify(result);', 'const object = {a: 1, b: 2}; const result = []; for (const value of Object.values(object)) { result.push(value); } JSON.stringify(result);', ], ]) { const {output, fixed, messages} = linter.verifyAndFix(code, config); t.assert.strictEqual(fixed, true, code); t.assert.deepStrictEqual(messages, []); t.assert.strictEqual(output, expectedOutput); t.assert.strictEqual(runInNewContext(output), runInNewContext(code)); } }); nodeTest('converting `Object.keys()` is autofixed when the callback has no side effects', t => { const code = 'const object = {a: 1, b: 2}; JSON.stringify(Object.keys(object).map(key => [key, object[key] * 2]));'; const {output, fixed, messages} = linter.verifyAndFix(code, config); t.assert.strictEqual(fixed, true); t.assert.deepStrictEqual(messages, []); t.assert.strictEqual(output, 'const object = {a: 1, b: 2}; JSON.stringify(Object.entries(object).map(([key, value]) => [key, value * 2]));'); t.assert.strictEqual(runInNewContext(output), runInNewContext(code)); }); const typescript = code => ({ code, languageOptions: { parser: parsers.typescript, }, }); test.snapshot({ valid: [ 'for (const key of Object.keys(object)) {\n\tfoo(key);\n}', 'for (const value of Object.values(object)) {\n\tfoo(value);\n}', 'for (const [key, value] of Object.entries(object)) {\n\tfoo(key, value);\n}', 'Object.keys(object).map(key => key);', 'Object.values(object).map(value => foo(value));', 'Object.entries(object).map(([key, value]) => foo(key, value));', 'for (const key of Object.keys(object)) {\n\tobject[key] = value;\n}', 'for (const key of Object.keys(object)) {\n\tdelete object[key];\n}', 'for (const key of Object.keys(object)) {\n\tobject[key]++;\n}', 'Object.keys(object).map(key => {\n\tobject[key] = value;\n});', 'for (const key of Object.keys(object)) {\n\tobject[key]();\n}', 'for (const key of Object.keys(object)) {\n\t[object[key]] = values;\n}', 'for (const key of Object.keys(object)) {\n\t({foo: object[key]} = source);\n}', 'for (const key of Object.keys(object)) {\n\tobject.other = value;\n\tfoo(object[key]);\n}', 'for (const key of Object.keys(object)) {\n\t{\n\t\tconst object = otherObject;\n\t\tfoo(object[key]);\n\t}\n}', 'for (const key of Object.keys(object)) {\n\tfoo(otherObject[key]);\n}', 'Object.keys(object).map(key => foo(otherObject[key]));', 'for (const key of Object.keys(...objects)) {\n\tfoo(object[key]);\n}', 'for (const key of Object?.keys(object)) {\n\tfoo(object[key]);\n}', 'for (const key of Object.keys?.(object)) {\n\tfoo(object[key]);\n}', 'for (const key of Object["keys"](object)) {\n\tfoo(object[key]);\n}', 'for (key of Object.keys(object)) {\n\tfoo(object[key]);\n}', 'for (const {key} of Object.keys(object)) {\n\tfoo(object[key]);\n}', 'Object.keys(object).map((key, index) => foo(object[key], index));', 'Object.keys(object).map((key = "default") => foo(object[key]));', 'Object.keys(object).map((...keys) => foo(object[keys[0]]));', 'const readers = Object.keys(object).map(key => () => object[key]);', 'Object.keys(collection[name]).map(key => {\n\tconst name = "other";\n\treturn collection[name][key];\n});', 'Object.keys(object).forEach(key => foo(object[key]));', 'Object.keys(object).reduce((result, key) => ({...result, [key]: object[key]}), {});', // `Object.keys().flatMap` is out of scope (only `Object.keys().map` is handled) 'Object.keys(object).flatMap(key => [object[key]]);', // Async callback is not converted 'Object.keys(object).map(async key => foo(object[key]));', // `for await` is skipped 'async function f() { for await (const key of Object.keys(object)) { foo(object[key]); } }', 'for (const key in object) {\n\tfoo(object[key]);\n}', 'Object.entries(object).map(([key, value]) => {\n\tvalue = key;\n\treturn key;\n});', 'Object.entries(object).map(([key, value, extra]) => key);', 'Object.entries(object).filter(([key, value]) => key);', 'Object.entries(object).find(([key, value]) => key);', 'Object.entries(object).findLast(([key, value]) => value);', 'Object.entries(object).reduce((result, [key, value]) => result + key);', 'Object.entries(object).reduceRight((result, [key, value]) => result + value);', 'Object.entries(object).reduce((result, [key, value]) => result + key, ...initialValue);', 'Object.entries(object).map(([key, value], index) => [key, value, index]);', 'Object.entries(object).reduce((result, [key, value], index) => result + key + value + index, "");', 'Object.entries(object).every(([key, value], index, entries) => entries[index][0] === key);', 'Object.entries(object).reduce((result, [key, value], index, entries) => entries[index][1] === value, true);', 'Object.entries(object).some(async ([key, value]) => key);', 'Object.entries(object).flatMap(function * ([key, value]) {\n\tyield key;\n});', 'Object.keys(object).map(function (key) {\n\treturn [object[key], arguments[0]];\n});', 'Object.keys(object).map(function (key) {\n\treturn () => [object[key], arguments[0]];\n});', 'Object.entries(object).map(function ([key, value]) {\n\treturn [key, arguments[0]];\n});', 'Object.entries(object).some(function ([key, value]) {\n\treturn [key, arguments[0]];\n});', 'Object.entries(object).reduce(function (result, [key, value]) {\n\treturn [result, value, arguments[1]];\n}, []);', 'Object.entries(object).reduce(function (result = arguments[1], [key]) {\n\treturn result ?? key;\n}, undefined);', typescript('Object.keys(object as Record).map(key => otherObject[key]);'), // Inconsistent casts across accesses can't be unified into a single argument typescript('Object.keys(object).map(key => foo((object as A)[key], object[key]));'), typescript('Object.keys(object).map(key => foo((object as A)[key], (object as B)[key]));'), 'for (var key of Object.keys(object)) {\n\tfoo(object[key]);\n}', 'let object = {};\nfor (const key of Object.keys(object)) {\n\tfoo(object[key]);\n\tobject = {};\n}', 'for (let key of Object.keys(object)) {\n\tfoo(object[key]);\n\tkey = "other";\n}', 'for (const key of Object.keys(object.data)) {\n\tfoo(object.other[key]);\n}', 'for (const [key, value] of Object.entries(object)) {\n\tfoo(key, key, value);\n}', ], invalid: [ 'for (const key of Object.keys(object)) {\n\tfoo(object[key]);\n}', 'for (const key of Object.keys(object)) {\n\tfoo(object[key], key);\n}', 'const result = Object.keys(object).map(key => foo(object[key]));', 'const result = Object.keys(object).map(key => foo(object[key], key));', 'const result = Object.keys(object).map((key) => foo(object[key], key));', 'const result = Object.keys(object).map(function (key) {\n\treturn foo(object[key], key);\n});', 'const result = Object.keys(object).map(function (key) {\n\treturn this.format(object[key]);\n}, formatter);', 'for (const key of Object.keys(object)) {\n\tupdate(object);\n\tfoo(object[key]);\n}', 'for (const key of Object.keys(object)) {\n\tfoo(object[key]);\n\tupdate(object);\n}', 'for (const key of Object.keys(object)) {\n\tfoo(object[key]);\n\tupdate();\n}', 'Object.keys(object).map(key => {\n\tconst result = foo(object[key]);\n\tupdate(object);\n\treturn result;\n});', 'Object.keys(object).map(key => {\n\tconst result = foo(object[key]);\n\tupdate();\n\treturn result;\n});', 'Object.keys(object).map(key => {\n\tthrow object[key];\n});', 'for (const key of Object.keys(object)) {\n\tconst value = getValue;\n\tconst item = object[key];\n}', 'for (const key of Object.keys(object)) {\n\tconst value = getValue;\n\tconst item = object[key] + key.length;\n}', 'for (const key of Object.keys(object)) {\n\tconst item = object[key];\n\tbreak;\n}', 'for (const key of Object.keys(object)) {\n\tif (skip(key)) {\n\t\tcontinue;\n\t}\n\n\tfoo(object[key]);\n}', 'function getFirstValue() {\n\tfor (const key of Object.keys(object)) {\n\t\treturn object[key];\n\t}\n}', 'for (const key of Object.keys(object)) {\n\tthrow object[key];\n}', 'for (const [key] of Object.entries(object)) {\n\tfoo(key);\n}', 'for (const [key, value] of Object.entries(object)) {\n\tfoo(key);\n}', 'for (const [key, value] of Object.entries(object)) {\n\tfoo(value);\n}', 'for (const [, value] of Object.entries(object)) {\n\tfoo(value);\n}', 'const result = Object.entries(object).map(([key]) => foo(key));', 'const result = Object.entries(object).map(([key, value]) => foo(key));', 'const result = Object.entries(object).map(([key, value]) => foo(value));', 'const result = Object.entries(object).map(([, value]) => foo(value));', 'const result = Object.entries(object).map(([key], index) => foo(key, index));', 'const result = Object.entries(object).map(function ([key]) {\n\treturn this.has(key);\n}, names);', 'Object.entries(object).map(([key, value]) => {\n\tthrow key;\n});', 'const result = Object.entries(object).every(([key, value]) => foo(key));', 'const result = Object.entries(object).every(([key, value]) => foo(value));', 'const result = Object.entries(object).every(([key], index) => foo(key, index));', 'const result = Object.entries(object).every(function ([key]) {\n\treturn this.has(key);\n}, names);', 'const result = Object.entries(object).some(([key, value]) => foo(key));', 'const result = Object.entries(object).some(([key, value]) => foo(value));', 'Object.entries(object).some(([key]) => {\n\tthrow key;\n});', 'const result = Object.entries(object).findIndex(([key, value]) => foo(key));', 'const result = Object.entries(object).findIndex(([key, value]) => foo(value));', 'const result = Object.entries(object).findLastIndex(([key, value]) => foo(key));', 'const result = Object.entries(object).findLastIndex(([key, value]) => foo(value));', 'const result = Object.entries(object).flatMap(([key, value]) => foo(key));', 'const result = Object.entries(object).flatMap(([key, value]) => foo(value));', 'Object.entries(object).forEach(([key, value]) => foo(key));', 'Object.entries(object).forEach(([key, value]) => foo(value));', 'const result = Object.entries(object).reduce((result, [key, value]) => result + key, "");', 'const result = Object.entries(object).reduce((result, [key, value]) => result + value, 0);', 'const result = Object.entries(object).reduce((result, [, value], index) => result + value + index, 0);', 'const result = Object.entries(object).reduceRight((result, [key, value]) => result + key, "");', 'const result = Object.entries(object).reduceRight((result, [key, value]) => result + value, 0);', 'const result = Object.entries(object).reduceRight((result, [, value], index) => result + value + index, 0);', 'for (const key of Object.keys((object))) {\n\tfoo((object)[key]);\n}', typescript('Object.keys(object as Record).map(key => (object as Record)[key]);'), typescript('Object.keys(object!).map(key => object![key]);'), typescript('Object.keys(object satisfies Record).map(key => (object satisfies Record)[key]);'), // Cast only on the value access: it's moved onto the iterable-method argument typescript('for (const key of Object.keys(object)) {\n\tfoo((object as Record)[key]);\n}'), typescript('Object.keys(object).map(key => foo((object as Record)[key]));'), // `entries` branch (key also used), cast moved onto the argument typescript('for (const key of Object.keys(object)) {\n\tfoo(key, (object as Record)[key]);\n}'), // Angle-bracket assertion form typescript('for (const key of Object.keys(object)) {\n\tfoo((>object)[key]);\n}'), // Cast wrapping the whole access is preserved naturally (object has no cast) typescript('for (const key of Object.keys(object)) {\n\tfoo(object[key] as Foo);\n}'), // A comment inside the argument cast would be dropped when moving the value cast, so it is reported without a fix typescript('Object.keys(object /* keep this */ as A).map(key => (object as A)[key]);'), outdent` for (const key of Object.keys(object)) { foo(object[ // Keep this. key ]); } `, outdent` for (const [key, /* Keep this. */ value] of Object.entries(object)) { foo(key); } `, 'Object.entries(object).every(([key /* Keep this. */]) => key);', // Nested functions and classes are not searched for value reads 'for (const key of Object.keys(object)) {\n\tfoo(object[key]);\n\tconst read = () => object[key];\n\tconst Reader = class {\n\t\tread() {\n\t\t\treturn object[key];\n\t\t}\n\t};\n}', // `arguments` in a nested function does not refer to the callback arguments 'Object.entries(object).map(function ([key, value]) {\n\tconst read = function () {\n\t\treturn arguments;\n\t};\n\treturn read(key);\n});', 'const result = Object.keys(object).map(key => [object[key], object[key]]);', 'const {result = Object.keys(object).map(key => object[key])} = options;', 'Object.keys(object /* keep argument */).map(key => /* keep body */ object[key]);', { code: '
{Object.keys(object).map(key => object[key])}
;', languageOptions: {parserOptions: {ecmaFeatures: {jsx: true}}}, }, typescript('for (const key of Object.keys(object!)) { consume(key, object![key]); }'), // Keyword spacing 'Object.keys(object).map(key => typeof(object)[key]);', 'for (const key of Object.keys(object)) {\n\tfoo((object)[key]in bar);\n}', 'for (const[key, value]of Object.entries(object)) {\n\tfoo(key);\n}', ], }); // `Object.entries()` reads every value and `Object.keys()` does not, so a getter is no longer invoked. Accessor side effects are unsupported, so it still autofixes. test({ valid: [], invalid: [ { code: 'Object.entries(object).map(([key, value]) => foo(key));', output: 'Object.keys(object).map((key) => foo(key));', errors: 1, }, { code: 'for (const [key, value] of Object.entries(object)) { foo(key); }', output: 'for (const key of Object.keys(object)) { foo(key); }', errors: 1, }, { code: 'Object.entries(object).map(([, value]) => foo(value));', output: 'Object.values(object).map((value) => foo(value));', errors: 1, }, ], }); // Calling or tagging an indexed method must retain its receiver. test({ valid: [ 'Object.keys(object).map(key => object[key]`template`);', 'for (const key of Object.keys(object)) { object[key]`template`; }', 'Object.keys(object).map(key => object[key]?.());', ...[ 'Object.keys(object).map(key => object[key]!());', 'Object.keys(object).map(key => (object[key] as Function)());', 'Object.keys(object).map(key => (object[key] as Function)?.());', 'Object.keys(object).map(key => (object[key] satisfies Function)`template`);', ].map(code => ({code, languageOptions: {parser: parsers.typescript}})), ], invalid: [], }); // A chain inherited from the receiver still preserves the method reference. nodeTest('indexed calls and tags through optional receivers keep their receiver', t => { for (const suffix of ['()', '`template`']) { const code = `const object = {foo: {method() { return this === object.foo; }}}; JSON.stringify(Object.keys(object?.foo).map(key => (object?.foo[key])${suffix}));`; const {output, fixed, messages} = linter.verifyAndFix(code, config); t.assert.strictEqual(fixed, false); t.assert.deepStrictEqual(messages, []); t.assert.strictEqual(runInNewContext(output), '[true]'); } }); test({ valid: [ 'Object.keys(object?.foo).map(key => (object?.foo[key])());', 'Object.keys(object?.foo).map(key => (object?.foo[key])`template`);', ], invalid: [], });