<commit_before>/**
 * @fileoverview Tests for no-array-index-key
 * @author <NAME>
 */

'use strict';

// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-array-index-key');
const RuleTester = require('eslint').RuleTester;

const parserOptions = {
  ecmaVersion: 8,
  sourceType: 'module',
  ecmaFeatures: {
    experimentalObjectRestSpread: true,
    jsx: true
  }
};

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const ruleTester = new RuleTester({parserOptions});
ruleTester.run('no-array-index-key', rule, {
  valid: [
    {code: '<Foo key="foo" />;'},
    {code: '<Foo key={i} />;'},
    {code: '<Foo key />;'},
    {code: '<Foo key={`foo-${i}`} />;'},
    {code: '<Foo key={\'foo-\' + i} />;'},

    {
      code: 'foo.bar((baz, i) => <Foo key={i} />)'
    },

    {
      code: 'foo.bar((bar, i) => <Foo key={`foo-${i}`} />)'
    },

    {
      code: 'foo.bar((bar, i) => <Foo key={\'foo-\' + i} />)'
    },

    {
      code: 'foo.map((baz) => <Foo key={baz.id} />)'
    },

    {
      code: 'foo.map((baz, i) => <Foo key={baz.id} />)'
    },

    {
      code: 'foo.map((baz, i) => <Foo key={\'foo\' + baz.id} />)'
    },

    {
      code: 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props }))'
    },

    {
      code: [
        'foo.map((item, i) => {',
        '  return React.cloneElement(someChild, {',
        '    key: item.id',
        '  })',
        '})'
      ].join('\n')
    },

    {
      code: 'foo.map((baz, i) => <Foo key />)'
    },

    {
      code: 'foo.reduce((a, b) => a.concat(<Foo key={b.id} />), [])'
    },

    {
      code: 'foo.reduce((a, b, i) => a.concat(<Foo key={b.id} />), [])'
    },

    {
      code: 'foo.reduceRight((a, b) => a.concat(<Foo key={b.id} />), [])'
    },

    {
      code: 'foo.reduceRight((a, b, i) => a.concat(<Foo key={b.id} />), [])'
    }
  ],

  invalid: [
    {
      code: 'foo.map((bar, i) => <Foo key={i} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: '[{}, {}].map((bar, i) => <Foo key={i} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, anything) => <Foo key={anything} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, i) => <Foo key={`foo-${i}`} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, i) => <Foo key={\'foo-\' + i} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, i) => <Foo key={\'foo-\' + i + \'-bar\'} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props, key: i }))',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: [
        'foo.map((item, i) => {',
        '  return React.cloneElement(someChild, {',
        '    key: i',
        '  })',
        '})'
      ].join('\n'),
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.forEach((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.filter((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.some((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.every((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.find((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.findIndex((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]<commit_msg>Rewrite no-array-index-key tests with template literals<commit_after>/**
 * @fileoverview Tests for no-array-index-key
 * @author <NAME>
 */

'use strict';

// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-array-index-key');
const RuleTester = require('eslint').RuleTester;

const parserOptions = {
  ecmaVersion: 8,
  sourceType: 'module',
  ecmaFeatures: {
    experimentalObjectRestSpread: true,
    jsx: true
  }
};

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const ruleTester = new RuleTester({parserOptions});
ruleTester.run('no-array-index-key', rule, {
  valid: [
    {code: '<Foo key="foo" />;'},
    {code: '<Foo key={i} />;'},
    {code: '<Foo key />;'},
    {code: '<Foo key={`foo-${i}`} />;'},
    {code: '<Foo key={\'foo-\' + i} />;'},

    {
      code: 'foo.bar((baz, i) => <Foo key={i} />)'
    },

    {
      code: 'foo.bar((bar, i) => <Foo key={`foo-${i}`} />)'
    },

    {
      code: 'foo.bar((bar, i) => <Foo key={\'foo-\' + i} />)'
    },

    {
      code: 'foo.map((baz) => <Foo key={baz.id} />)'
    },

    {
      code: 'foo.map((baz, i) => <Foo key={baz.id} />)'
    },

    {
      code: 'foo.map((baz, i) => <Foo key={\'foo\' + baz.id} />)'
    },

    {
      code: 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props }))'
    },

    {
      code: `
        foo.map((item, i) => {
          return React.cloneElement(someChild, {
            key: item.id
          })
        })
      `
    },

    {
      code: 'foo.map((baz, i) => <Foo key />)'
    },

    {
      code: 'foo.reduce((a, b) => a.concat(<Foo key={b.id} />), [])'
    },

    {
      code: 'foo.reduce((a, b, i) => a.concat(<Foo key={b.id} />), [])'
    },

    {
      code: 'foo.reduceRight((a, b) => a.concat(<Foo key={b.id} />), [])'
    },

    {
      code: 'foo.reduceRight((a, b, i) => a.concat(<Foo key={b.id} />), [])'
    }
  ],

  invalid: [
    {
      code: 'foo.map((bar, i) => <Foo key={i} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: '[{}, {}].map((bar, i) => <Foo key={i} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, anything) => <Foo key={anything} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, i) => <Foo key={`foo-${i}`} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, i) => <Foo key={\'foo-\' + i} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((bar, i) => <Foo key={\'foo-\' + i + \'-bar\'} />)',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props, key: i }))',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: `
        foo.map((item, i) => {
          return React.cloneElement(someChild, {
            key: i
          })
        })
      `,
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.forEach((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.filter((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.some((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.every((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.find((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
    },

    {
      code: 'foo.findIndex((bar, i) => { baz.push(<Foo key={i} />); })',
      errors: [{message: 'Do not use Array index in keys'}]
{'fraction_non_alphanumeric': 0.3045679303037438, 'fraction_numerical': 0.0002354603249352484, 'mean_word_length': 1.6621748668129113, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2541253', 'n_tokens_mistral': 3106, 'n_tokens_neox': 2942, 'n_words': 656}