File: amd-function-arity.js

package info (click to toggle)
node-eslint-plugin-requirejs 4.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 916 kB
  • sloc: javascript: 4,677; perl: 48; makefile: 31; sh: 6
file content (111 lines) | stat: -rw-r--r-- 3,384 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * @file    Rule to ensure AMD-style callbacks contain correct number of
 *          arguments
 * @author  Kevin Partington
 */

"use strict";

const rjs = require("../utils/rjs");
const ast = require("../utils/ast");

const hasCallback = ast.hasCallback;
const getDependencyNodes = rjs.getDependencyNodes;
const getModuleFunction = rjs.getModuleFunction;
const isAmdDefine = rjs.isAmdDefine;
const isRequireCall = rjs.isRequireCall;

// -----------------------------------------------------------------------------
// Configuration
// -----------------------------------------------------------------------------

const docs = {
    description: "Ensure AMD-style callbacks contain correct number of arguments",
    category: "Stylistic Choices",
    recommended: false,
    url: "https://github.com/cvisco/eslint-plugin-requirejs/blob/master/docs/rules/amd-function-arity.md"
};

const schema = [
    {
        type: "object",
        properties: {
            allowExtraDependencies: {
                anyOf: [
                    {
                        type: "boolean",
                        default: false
                    },
                    {
                        type: "array",
                        uniqueItems: true,
                        items: {
                            type: "string"
                        }
                    }
                ]
            }
        },
        additionalProperties: false
    }
];

const defaults = {
    allowExtraDependencies: false
};

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

const isBoolean  = value => typeof value === "boolean";
const unassigned = (paths, params) => paths.slice(params.length);
const includes   = list => path => list.indexOf(path.value) !== -1;

const isAmdWithCallback = node =>
    isAmdDefine(node) || isRequireCall(node) && hasCallback(node);

const reportTooFew = (expected, actual) =>
    `Not enough parameters in callback (expected ${expected}, found ${actual}).`;

const reportTooMany = (expected, actual) =>
    `Too many parameters in callback (expected ${expected}, found ${actual}).`;

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

function create(context) {
    const opts = Object.assign({}, defaults, context.options[0]);
    const allowed = opts.allowExtraDependencies;

    const isAllowed = paths =>
        isBoolean(allowed) ? allowed : paths.every(includes(allowed));

    return {
        CallExpression(node) {
            if (!isAmdWithCallback(node)) return;

            const paths = getDependencyNodes(node);
            const pathCount = paths.length;

            if (!pathCount) return;

            const params = getModuleFunction(node).params;
            const paramCount = params.length;

            if (pathCount < paramCount) {
                context.report(node, reportTooMany(pathCount, paramCount));
            }

            if (pathCount > paramCount && !isAllowed(unassigned(paths, params))) {
                context.report(node, reportTooFew(pathCount, paramCount));
            }
        }
    };
}

module.exports = {
    meta: { docs, schema },
    create
};