eslint-community / eslint-plugin-eslint-plugin

An ESLint plugin for linting ESLint plugins
MIT License
198 stars 29 forks source link

Rule Proposal: `prefer-global-parser-options` #85

Open bmish opened 5 years ago

bmish commented 5 years ago

If all test cases specify the same parserOptions, then parserOptions should be set globally in the test file when creating the RuleTester to simplify the individual test cases.

Before:

const rule = require('../../../lib/rules/my-rule');
const RuleTester = require('eslint').RuleTester;

const eslintTester = new RuleTester();

eslintTester.run('my-rule', rule, {
  valid: [
    {
      code: 'someFunction()',
      parserOptions: { ecmaVersion: 6, sourceType: 'module' },
    },
    {
      code: 'otherFunction()',
      parserOptions: { ecmaVersion: 6, sourceType: 'module' },
    },
  ],
  invalid: [
    {
      code: 'finalFunction()',
      parserOptions: { ecmaVersion: 6, sourceType: 'module' },
      output: null,
      errors: [
        {
          message: 'Some error message',
          type: 'CallExpression',
        },
      ],
    },
  ],
});

After:

const rule = require('../../../lib/rules/my-rule');
const RuleTester = require('eslint').RuleTester;

const eslintTester = new RuleTester({
  parserOptions: { ecmaVersion: 6, sourceType: 'module' },
});

eslintTester.run('my-rule', rule, {
  valid: ['someFunction()', 'otherFunction()'],
  invalid: [
    {
      code: 'finalFunction()',
      output: null,
      errors: [
        {
          message: 'Some error message',
          type: 'CallExpression',
        },
      ],
    },
  ],
});
aladdin-add commented 10 months ago

related: https://github.com/eslint-community/eslint-plugin-eslint-plugin/issues/4

It's fine to use the global option when it's the same in all tests. The question is what to do if some options are different.