File: require-meta-type.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 (63 lines) | stat: -rw-r--r-- 2,026 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
60
61
62
63
/**
 * @fileoverview require rules to implement a meta.type property
 * @author 薛定谔的猫<weiran.zsd@outlook.com>
 */

'use strict';

const utils = require('../utils');
const VALID_TYPES = new Set(['problem', 'suggestion', 'layout']);

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

module.exports = {
  meta: {
    docs: {
      description: 'require rules to implement a meta.type property',
      category: 'Rules',
      recommended: false, // TODO: enable it in a major release.
    },
    type: 'problem',
    fixable: null,
    schema: [],
    messages: {
      missing: '`meta.type` is required (must be either `problem`, `suggestion`, or `layout`).',
      unexpected: '`meta.type` must be either `problem`, `suggestion` or `layout`.',
    },
  },

  create (context) {
    const sourceCode = context.getSourceCode();
    const info = utils.getRuleInfo(sourceCode.ast, sourceCode.scopeManager);

    // ----------------------------------------------------------------------
    // Helpers
    // ----------------------------------------------------------------------

    // ----------------------------------------------------------------------
    // Public
    // ----------------------------------------------------------------------

    return {
      Program () {
        if (info === null || info.meta === null) {
          return;
        }

        const metaNode = info.meta;
        const typeNode =
          metaNode &&
          metaNode.properties &&
          metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'type');

        if (typeNode && typeNode.value.type === 'Literal' && !VALID_TYPES.has(typeNode.value.value)) {
          context.report({ node: metaNode, messageId: 'unexpected' });
        } else if (!typeNode) {
          context.report({ node: metaNode, messageId: 'missing' });
        }
      },
    };
  },
};