summaryrefslogtreecommitdiff
path: root/deps/node/deps/node-inspect/tools/eslint-rules/align-function-arguments.js
blob: 015552489a9d4457f53ed0a2f0ccf072eaaed9bf (plain)
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
/**
 * @fileoverview Align arguments in multiline function calls
 * @author Rich Trott
 */
'use strict';

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

function checkArgumentAlignment(context, node) {

  function isNodeFirstInLine(node, byEndLocation) {
    const firstToken = byEndLocation === true ? context.getLastToken(node, 1) :
      context.getTokenBefore(node);
    const startLine = byEndLocation === true ? node.loc.end.line :
      node.loc.start.line;
    const endLine = firstToken ? firstToken.loc.end.line : -1;

    return startLine !== endLine;
  }

  if (node.arguments.length === 0)
    return;

  var msg = '';
  const first = node.arguments[0];
  var currentLine = first.loc.start.line;
  const firstColumn = first.loc.start.column;

  const ignoreTypes = [
    'ArrowFunctionExpression',
    'FunctionExpression',
    'ObjectExpression',
  ];

  const args = node.arguments;

  // For now, don't bother trying to validate potentially complicating things
  // like closures. Different people will have very different ideas and it's
  // probably best to implement configuration options.
  if (args.some((node) => { return ignoreTypes.indexOf(node.type) !== -1; })) {
    return;
  }

  if (!isNodeFirstInLine(node)) {
    return;
  }

  var misaligned;

  args.slice(1).forEach((argument) => {
    if (!misaligned) {
      if (argument.loc.start.line === currentLine + 1) {
        if (argument.loc.start.column !== firstColumn) {
          if (isNodeFirstInLine(argument)) {
            msg = 'Function argument in column ' +
                  `${argument.loc.start.column + 1}, ` +
                  `expected in ${firstColumn + 1}`;
            misaligned = argument;
          }
        }
      }
    }
    currentLine = argument.loc.start.line;
  });

  if (msg)
    context.report(misaligned, msg);
}

module.exports = function(context) {
  return {
    'CallExpression': (node) => checkArgumentAlignment(context, node)
  };
};