File: no-deprecated-context-methods.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 (84 lines) | stat: -rw-r--r-- 2,819 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 * @fileoverview Disallows usage of deprecated methods on rule context objects
 * @author Teddy Katz
 */

'use strict';

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

const DEPRECATED_PASSTHROUGHS = {
  getSource: 'getText',
  getSourceLines: 'getLines',
  getAllComments: 'getAllComments',
  getNodeByRangeIndex: 'getNodeByRangeIndex',
  getComments: 'getComments',
  getCommentsBefore: 'getCommentsBefore',
  getCommentsAfter: 'getCommentsAfter',
  getCommentsInside: 'getCommentsInside',
  getJSDocComment: 'getJSDocComment',
  getFirstToken: 'getFirstToken',
  getFirstTokens: 'getFirstTokens',
  getLastToken: 'getLastToken',
  getLastTokens: 'getLastTokens',
  getTokenAfter: 'getTokenAfter',
  getTokenBefore: 'getTokenBefore',
  getTokenByRangeStart: 'getTokenByRangeStart',
  getTokens: 'getTokens',
  getTokensAfter: 'getTokensAfter',
  getTokensBefore: 'getTokensBefore',
  getTokensBetween: 'getTokensBetween',
};

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

module.exports = {
  meta: {
    docs: {
      description: 'disallow usage of deprecated methods on rule context objects',
      category: 'Rules',
      recommended: false,
    },
    type: 'suggestion',
    fixable: 'code',
    schema: [],
  },

  create (context) {
    const sourceCode = context.getSourceCode();

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

    return {
      'Program:exit' () {
        Array.from(utils.getContextIdentifiers(context, sourceCode.ast))
          .filter(
            contextId =>
              contextId.parent.type === 'MemberExpression' &&
              contextId === contextId.parent.object &&
              contextId.parent.property.type === 'Identifier' &&
              Object.prototype.hasOwnProperty.call(DEPRECATED_PASSTHROUGHS, contextId.parent.property.name)
          ).forEach(
            contextId =>
              context.report({
                node: contextId.parent,
                message: 'Use `{{contextName}}.getSourceCode().{{replacement}}` instead of `{{contextName}}.{{original}}`.',
                data: {
                  contextName: contextId.name,
                  original: contextId.parent.property.name,
                  replacement: DEPRECATED_PASSTHROUGHS[contextId.parent.property.name],
                },
                fix: fixer => [
                  fixer.insertTextAfter(contextId, '.getSourceCode()'),
                  fixer.replaceText(contextId.parent.property, DEPRECATED_PASSTHROUGHS[contextId.parent.property.name]),
                ],
              })
          );
      },
    };
  },
};