File: consistent-output.js

package info (click to toggle)
node-eslint-plugin-eslint-plugin 2.3.0%2B~0.3.0-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 652 kB
  • sloc: javascript: 5,372; makefile: 34; sh: 1
file content (59 lines) | stat: -rw-r--r-- 1,744 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * @fileoverview Enforce consistent use of output assertions in rule tests
 * @author Teddy Katz
 */

'use strict';

const utils = require('../utils');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
  meta: {
    docs: {
      description: 'enforce consistent use of output assertions in rule tests',
      category: 'Tests',
      recommended: false,
    },
    type: 'suggestion',
    fixable: null, // or "code" or "whitespace"
    schema: [
      {
        type: 'string',
        enum: ['always', 'consistent'],
      },
    ],
  },

  create (context) {
    // ----------------------------------------------------------------------
    // Public
    // ----------------------------------------------------------------------
    const always = context.options[0] && context.options[0] === 'always';

    return {
      Program (ast) {
        utils.getTestInfo(context, ast).forEach(testRun => {
          const readableCases = testRun.invalid.filter(testCase => testCase.type === 'ObjectExpression');
          const casesWithoutOutput = readableCases
            .filter(testCase => testCase.properties.map(utils.getKeyName).indexOf('output') === -1);

          if (
            (casesWithoutOutput.length < readableCases.length) ||
            (always && casesWithoutOutput.length > 0)
          ) {
            casesWithoutOutput.forEach(testCase => {
              context.report({
                node: testCase,
                message: 'This test case should have an output assertion.',
              });
            });
          }
        });
      },
    };
  },
};