Blame view

node_modules/eslint/lib/rules/object-property-newline.js 3.2 KB
f7563de62   Palak Handa   first commit
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
  /**
   * @fileoverview Rule to enforce placing object properties on separate lines.
   * @author Vitor Balocco
   */
  
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = {
      meta: {
          docs: {
              description: "enforce placing object properties on separate lines",
              category: "Stylistic Issues",
              recommended: false
          },
  
          schema: [
              {
                  type: "object",
                  properties: {
                      allowMultiplePropertiesPerLine: {
                          type: "boolean"
                      }
                  },
                  additionalProperties: false
              }
          ],
  
          fixable: "whitespace"
      },
  
      create(context) {
          const allowSameLine = context.options[0] && Boolean(context.options[0].allowMultiplePropertiesPerLine);
          const errorMessage = allowSameLine
              ? "Object properties must go on a new line if they aren't all on the same line."
              : "Object properties must go on a new line.";
  
          const sourceCode = context.getSourceCode();
  
          return {
              ObjectExpression(node) {
                  if (allowSameLine) {
                      if (node.properties.length > 1) {
                          const firstTokenOfFirstProperty = sourceCode.getFirstToken(node.properties[0]);
                          const lastTokenOfLastProperty = sourceCode.getLastToken(node.properties[node.properties.length - 1]);
  
                          if (firstTokenOfFirstProperty.loc.end.line === lastTokenOfLastProperty.loc.start.line) {
  
                              // All keys and values are on the same line
                              return;
                          }
                      }
                  }
  
                  for (let i = 1; i < node.properties.length; i++) {
                      const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.properties[i - 1]);
                      const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.properties[i]);
  
                      if (lastTokenOfPreviousProperty.loc.end.line === firstTokenOfCurrentProperty.loc.start.line) {
                          context.report({
                              node,
                              loc: firstTokenOfCurrentProperty.loc.start,
                              message: errorMessage,
                              fix(fixer) {
                                  const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty);
                                  const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]];
  
                                  // Don't perform a fix if there are any comments between the comma and the next property.
                                  if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) {
                                      return null;
                                  }
  
                                  return fixer.replaceTextRange(rangeAfterComma, "
  ");
                              }
                          });
                      }
                  }
              }
          };
      }
  };