eslint 5.2.0 → 5.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/conf/eslint-recommended.js +4 -0
  3. package/lib/cli-engine.js +13 -14
  4. package/lib/code-path-analysis/code-path-analyzer.js +1 -1
  5. package/lib/config/config-initializer.js +7 -7
  6. package/lib/config.js +1 -1
  7. package/lib/formatters/codeframe.js +2 -2
  8. package/lib/ignored-paths.js +125 -37
  9. package/lib/linter.js +1 -1
  10. package/lib/rules/array-bracket-newline.js +1 -1
  11. package/lib/rules/array-bracket-spacing.js +1 -1
  12. package/lib/rules/array-callback-return.js +1 -1
  13. package/lib/rules/array-element-newline.js +1 -1
  14. package/lib/rules/arrow-body-style.js +1 -1
  15. package/lib/rules/arrow-parens.js +1 -1
  16. package/lib/rules/arrow-spacing.js +1 -1
  17. package/lib/rules/block-spacing.js +1 -1
  18. package/lib/rules/brace-style.js +1 -1
  19. package/lib/rules/camelcase.js +1 -1
  20. package/lib/rules/capitalized-comments.js +1 -1
  21. package/lib/rules/comma-dangle.js +1 -1
  22. package/lib/rules/comma-spacing.js +1 -1
  23. package/lib/rules/comma-style.js +2 -2
  24. package/lib/rules/complexity.js +3 -3
  25. package/lib/rules/computed-property-spacing.js +1 -1
  26. package/lib/rules/consistent-return.js +1 -1
  27. package/lib/rules/curly.js +1 -1
  28. package/lib/rules/dot-location.js +1 -1
  29. package/lib/rules/dot-notation.js +1 -1
  30. package/lib/rules/eqeqeq.js +1 -1
  31. package/lib/rules/for-direction.js +5 -2
  32. package/lib/rules/func-call-spacing.js +8 -4
  33. package/lib/rules/func-name-matching.js +13 -7
  34. package/lib/rules/func-names.js +82 -21
  35. package/lib/rules/func-style.js +8 -4
  36. package/lib/rules/function-paren-newline.js +12 -6
  37. package/lib/rules/generator-star-spacing.js +18 -9
  38. package/lib/rules/getter-return.js +8 -6
  39. package/lib/rules/indent-legacy.js +1 -1
  40. package/lib/rules/indent.js +43 -7
  41. package/lib/rules/jsx-quotes.js +1 -1
  42. package/lib/rules/key-spacing.js +2 -2
  43. package/lib/rules/keyword-spacing.js +1 -1
  44. package/lib/rules/line-comment-position.js +3 -3
  45. package/lib/rules/linebreak-style.js +1 -1
  46. package/lib/rules/lines-around-comment.js +1 -1
  47. package/lib/rules/lines-around-directive.js +1 -1
  48. package/lib/rules/lines-between-class-members.js +1 -1
  49. package/lib/rules/max-depth.js +2 -2
  50. package/lib/rules/max-len.js +1 -1
  51. package/lib/rules/max-lines-per-function.js +1 -1
  52. package/lib/rules/max-lines.js +2 -2
  53. package/lib/rules/max-nested-callbacks.js +2 -2
  54. package/lib/rules/max-params.js +5 -4
  55. package/lib/rules/max-statements-per-line.js +1 -1
  56. package/lib/rules/max-statements.js +3 -3
  57. package/lib/rules/multiline-comment-style.js +1 -1
  58. package/lib/rules/multiline-ternary.js +1 -1
  59. package/lib/rules/new-parens.js +1 -1
  60. package/lib/rules/newline-after-var.js +1 -1
  61. package/lib/rules/newline-per-chained-call.js +1 -1
  62. package/lib/rules/no-alert.js +1 -1
  63. package/lib/rules/no-async-promise-executor.js +33 -0
  64. package/lib/rules/no-catch-shadow.js +1 -1
  65. package/lib/rules/no-class-assign.js +1 -1
  66. package/lib/rules/no-cond-assign.js +1 -1
  67. package/lib/rules/no-confusing-arrow.js +1 -1
  68. package/lib/rules/no-console.js +1 -1
  69. package/lib/rules/no-const-assign.js +1 -1
  70. package/lib/rules/no-constant-condition.js +2 -0
  71. package/lib/rules/no-dupe-keys.js +1 -1
  72. package/lib/rules/no-else-return.js +1 -1
  73. package/lib/rules/no-empty-function.js +1 -1
  74. package/lib/rules/no-empty.js +1 -1
  75. package/lib/rules/no-eval.js +1 -1
  76. package/lib/rules/no-ex-assign.js +1 -1
  77. package/lib/rules/no-extend-native.js +1 -1
  78. package/lib/rules/no-extra-bind.js +1 -1
  79. package/lib/rules/no-extra-boolean-cast.js +1 -1
  80. package/lib/rules/no-extra-label.js +1 -1
  81. package/lib/rules/no-extra-parens.js +2 -2
  82. package/lib/rules/no-extra-semi.js +1 -1
  83. package/lib/rules/no-floating-decimal.js +1 -1
  84. package/lib/rules/no-func-assign.js +1 -1
  85. package/lib/rules/no-implicit-coercion.js +1 -1
  86. package/lib/rules/no-inline-comments.js +1 -1
  87. package/lib/rules/no-invalid-this.js +1 -1
  88. package/lib/rules/no-irregular-whitespace.js +1 -1
  89. package/lib/rules/no-label-var.js +1 -1
  90. package/lib/rules/no-labels.js +1 -1
  91. package/lib/rules/no-magic-numbers.js +7 -3
  92. package/lib/rules/no-misleading-character-class.js +189 -0
  93. package/lib/rules/no-mixed-operators.js +1 -1
  94. package/lib/rules/no-multi-spaces.js +1 -1
  95. package/lib/rules/no-multi-str.js +1 -1
  96. package/lib/rules/no-regex-spaces.js +1 -1
  97. package/lib/rules/no-restricted-globals.js +1 -1
  98. package/lib/rules/no-restricted-imports.js +1 -1
  99. package/lib/rules/no-restricted-modules.js +1 -1
  100. package/lib/rules/no-restricted-properties.js +1 -1
  101. package/lib/rules/no-return-assign.js +1 -1
  102. package/lib/rules/no-return-await.js +1 -1
  103. package/lib/rules/no-self-assign.js +1 -1
  104. package/lib/rules/no-sequences.js +1 -1
  105. package/lib/rules/no-shadow.js +1 -1
  106. package/lib/rules/no-this-before-super.js +1 -1
  107. package/lib/rules/no-throw-literal.js +1 -1
  108. package/lib/rules/no-trailing-spaces.js +1 -1
  109. package/lib/rules/no-undef-init.js +1 -1
  110. package/lib/rules/no-unexpected-multiline.js +1 -1
  111. package/lib/rules/no-unmodified-loop-condition.js +2 -3
  112. package/lib/rules/no-unneeded-ternary.js +1 -1
  113. package/lib/rules/no-unsafe-negation.js +1 -1
  114. package/lib/rules/no-unused-vars.js +1 -1
  115. package/lib/rules/no-useless-call.js +1 -1
  116. package/lib/rules/no-useless-computed-key.js +1 -1
  117. package/lib/rules/no-useless-concat.js +1 -1
  118. package/lib/rules/no-useless-escape.js +1 -1
  119. package/lib/rules/no-useless-return.js +2 -11
  120. package/lib/rules/no-var.js +1 -1
  121. package/lib/rules/no-warning-comments.js +1 -1
  122. package/lib/rules/no-whitespace-before-property.js +1 -1
  123. package/lib/rules/object-curly-newline.js +1 -1
  124. package/lib/rules/object-curly-spacing.js +1 -1
  125. package/lib/rules/object-shorthand.js +1 -1
  126. package/lib/rules/one-var.js +6 -6
  127. package/lib/rules/operator-assignment.js +1 -1
  128. package/lib/rules/operator-linebreak.js +1 -1
  129. package/lib/rules/padded-blocks.js +6 -6
  130. package/lib/rules/padding-line-between-statements.js +2 -2
  131. package/lib/rules/prefer-const.js +2 -11
  132. package/lib/rules/prefer-object-spread.js +9 -3
  133. package/lib/rules/prefer-promise-reject-errors.js +1 -1
  134. package/lib/rules/prefer-reflect.js +1 -1
  135. package/lib/rules/prefer-spread.js +1 -1
  136. package/lib/rules/prefer-template.js +1 -1
  137. package/lib/rules/quotes.js +1 -1
  138. package/lib/rules/radix.js +1 -1
  139. package/lib/rules/require-atomic-updates.js +239 -0
  140. package/lib/rules/require-await.js +1 -1
  141. package/lib/rules/require-unicode-regexp.js +65 -0
  142. package/lib/rules/semi-spacing.js +3 -3
  143. package/lib/rules/semi-style.js +1 -1
  144. package/lib/rules/semi.js +1 -1
  145. package/lib/rules/sort-keys.js +1 -1
  146. package/lib/rules/space-before-blocks.js +1 -1
  147. package/lib/rules/space-before-function-paren.js +1 -1
  148. package/lib/rules/space-in-parens.js +1 -1
  149. package/lib/rules/space-unary-ops.js +2 -2
  150. package/lib/rules/spaced-comment.js +1 -1
  151. package/lib/rules/strict.js +1 -1
  152. package/lib/rules/switch-colon-spacing.js +1 -1
  153. package/lib/rules/symbol-description.js +1 -1
  154. package/lib/rules/template-curly-spacing.js +1 -1
  155. package/lib/rules/valid-jsdoc.js +1 -1
  156. package/lib/rules/wrap-iife.js +1 -1
  157. package/lib/rules/yoda.js +1 -1
  158. package/lib/testers/rule-tester.js +7 -7
  159. package/lib/token-store/index.js +1 -1
  160. package/lib/{ast-utils.js → util/ast-utils.js} +0 -0
  161. package/lib/{file-finder.js → util/file-finder.js} +2 -2
  162. package/lib/util/fix-tracker.js +1 -1
  163. package/lib/util/{glob-util.js → glob-utils.js} +4 -4
  164. package/lib/util/lint-result-cache.js +2 -2
  165. package/lib/util/naming.js +2 -2
  166. package/lib/util/node-event-generator.js +3 -3
  167. package/lib/util/{npm-util.js → npm-utils.js} +0 -0
  168. package/lib/util/{path-util.js → path-utils.js} +1 -1
  169. package/lib/util/source-code-fixer.js +1 -1
  170. package/lib/util/{source-code-util.js → source-code-utils.js} +3 -3
  171. package/lib/util/source-code.js +1 -1
  172. package/lib/util/unicode/index.js +11 -0
  173. package/lib/util/unicode/is-combining-character.js +13 -0
  174. package/lib/util/unicode/is-emoji-modifier.js +13 -0
  175. package/lib/util/unicode/is-regional-indicator-symbol.js +13 -0
  176. package/lib/util/unicode/is-surrogate-pair.js +14 -0
  177. package/package.json +13 -14
  178. package/lib/rules/.eslintrc.yml +0 -4
@@ -10,7 +10,7 @@
10
10
  //------------------------------------------------------------------------------
11
11
 
12
12
  const lodash = require("lodash");
13
- const astUtils = require("../ast-utils");
13
+ const astUtils = require("../util/ast-utils");
14
14
 
15
15
  //------------------------------------------------------------------------------
16
16
  // Helpers
@@ -4,7 +4,7 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
- const astUtils = require("../ast-utils");
7
+ const astUtils = require("../util/ast-utils");
8
8
 
9
9
  //------------------------------------------------------------------------------
10
10
  // Rule Definition
@@ -5,7 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
- const astUtils = require("../ast-utils");
8
+ const astUtils = require("../util/ast-utils");
9
9
 
10
10
  //------------------------------------------------------------------------------
11
11
  // Rule Definition
@@ -58,7 +58,7 @@ module.exports = {
58
58
  NewExpression: true
59
59
  };
60
60
 
61
- if (context.options.length === 2 && context.options[1].hasOwnProperty("exceptions")) {
61
+ if (context.options.length === 2 && Object.prototype.hasOwnProperty.call(context.options[1], "exceptions")) {
62
62
  const keys = Object.keys(context.options[1].exceptions);
63
63
 
64
64
  for (let i = 0; i < keys.length; i++) {
@@ -12,7 +12,7 @@
12
12
 
13
13
  const lodash = require("lodash");
14
14
 
15
- const astUtils = require("../ast-utils");
15
+ const astUtils = require("../util/ast-utils");
16
16
 
17
17
  //------------------------------------------------------------------------------
18
18
  // Rule Definition
@@ -61,10 +61,10 @@ module.exports = {
61
61
  const option = context.options[0];
62
62
  let THRESHOLD = 20;
63
63
 
64
- if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
64
+ if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
65
65
  THRESHOLD = option.maximum;
66
66
  }
67
- if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
67
+ if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
68
68
  THRESHOLD = option.max;
69
69
  }
70
70
  if (typeof option === "number") {
@@ -4,7 +4,7 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
- const astUtils = require("../ast-utils");
7
+ const astUtils = require("../util/ast-utils");
8
8
 
9
9
  //------------------------------------------------------------------------------
10
10
  // Rule Definition
@@ -10,7 +10,7 @@
10
10
 
11
11
  const lodash = require("lodash");
12
12
 
13
- const astUtils = require("../ast-utils");
13
+ const astUtils = require("../util/ast-utils");
14
14
 
15
15
  //------------------------------------------------------------------------------
16
16
  // Helpers
@@ -8,7 +8,7 @@
8
8
  // Requirements
9
9
  //------------------------------------------------------------------------------
10
10
 
11
- const astUtils = require("../ast-utils");
11
+ const astUtils = require("../util/ast-utils");
12
12
 
13
13
  //------------------------------------------------------------------------------
14
14
  // Rule Definition
@@ -5,7 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
- const astUtils = require("../ast-utils");
8
+ const astUtils = require("../util/ast-utils");
9
9
 
10
10
  //------------------------------------------------------------------------------
11
11
  // Rule Definition
@@ -8,7 +8,7 @@
8
8
  // Requirements
9
9
  //------------------------------------------------------------------------------
10
10
 
11
- const astUtils = require("../ast-utils");
11
+ const astUtils = require("../util/ast-utils");
12
12
 
13
13
  //------------------------------------------------------------------------------
14
14
  // Rule Definition
@@ -9,7 +9,7 @@
9
9
  // Requirements
10
10
  //------------------------------------------------------------------------------
11
11
 
12
- const astUtils = require("../ast-utils");
12
+ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  //------------------------------------------------------------------------------
15
15
  // Rule Definition
@@ -18,7 +18,10 @@ module.exports = {
18
18
  url: "https://eslint.org/docs/rules/for-direction"
19
19
  },
20
20
  fixable: null,
21
- schema: []
21
+ schema: [],
22
+ messages: {
23
+ incorrectDirection: "The update clause in this loop moves the variable in the wrong direction."
24
+ }
22
25
  },
23
26
 
24
27
  create(context) {
@@ -31,7 +34,7 @@ module.exports = {
31
34
  function report(node) {
32
35
  context.report({
33
36
  node,
34
- message: "The update clause in this loop moves the variable in the wrong direction."
37
+ messageId: "incorrectDirection"
35
38
  });
36
39
  }
37
40
 
@@ -9,7 +9,7 @@
9
9
  // Requirements
10
10
  //------------------------------------------------------------------------------
11
11
 
12
- const astUtils = require("../ast-utils");
12
+ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  //------------------------------------------------------------------------------
15
15
  // Rule Definition
@@ -57,6 +57,10 @@ module.exports = {
57
57
  maxItems: 2
58
58
  }
59
59
  ]
60
+ },
61
+ messages: {
62
+ unexpected: "Unexpected newline between function name and paren.",
63
+ missing: "Missing space between function name and paren."
60
64
  }
61
65
  },
62
66
 
@@ -116,7 +120,7 @@ module.exports = {
116
120
  context.report({
117
121
  node,
118
122
  loc: lastCalleeToken.loc.start,
119
- message: "Unexpected space between function name and paren.",
123
+ messageId: "unexpected",
120
124
  fix(fixer) {
121
125
 
122
126
  /*
@@ -134,7 +138,7 @@ module.exports = {
134
138
  context.report({
135
139
  node,
136
140
  loc: lastCalleeToken.loc.start,
137
- message: "Missing space between function name and paren.",
141
+ messageId: "missing",
138
142
  fix(fixer) {
139
143
  return fixer.insertTextBefore(parenToken, " ");
140
144
  }
@@ -143,7 +147,7 @@ module.exports = {
143
147
  context.report({
144
148
  node,
145
149
  loc: lastCalleeToken.loc.start,
146
- message: "Unexpected newline between function name and paren.",
150
+ messageId: "unexpected",
147
151
  fix(fixer) {
148
152
  return fixer.replaceTextRange([prevToken.range[1], parenToken.range[0]], " ");
149
153
  }
@@ -9,7 +9,7 @@
9
9
  // Requirements
10
10
  //--------------------------------------------------------------------------
11
11
 
12
- const astUtils = require("../ast-utils");
12
+ const astUtils = require("../util/ast-utils");
13
13
  const esutils = require("esutils");
14
14
 
15
15
  //--------------------------------------------------------------------------
@@ -87,6 +87,12 @@ module.exports = {
87
87
  additionalItems: false,
88
88
  items: [optionsObject]
89
89
  }]
90
+ },
91
+ messages: {
92
+ matchProperty: "Function name `{{funcName}}` should match property name `{{name}}`",
93
+ matchVariable: "Function name `{{funcName}}` should match variable name `{{name}}`",
94
+ notMatchProperty: "Function name `{{funcName}}` should not match property name `{{name}}`",
95
+ notMatchVariable: "Function name `{{funcName}}` should not match variable name `{{name}}`"
90
96
  }
91
97
  },
92
98
 
@@ -132,20 +138,20 @@ module.exports = {
132
138
  * @returns {void}
133
139
  */
134
140
  function report(node, name, funcName, isProp) {
135
- let message;
141
+ let messageId;
136
142
 
137
143
  if (nameMatches === "always" && isProp) {
138
- message = "Function name `{{funcName}}` should match property name `{{name}}`";
144
+ messageId = "matchProperty";
139
145
  } else if (nameMatches === "always") {
140
- message = "Function name `{{funcName}}` should match variable name `{{name}}`";
146
+ messageId = "matchVariable";
141
147
  } else if (isProp) {
142
- message = "Function name `{{funcName}}` should not match property name `{{name}}`";
148
+ messageId = "notMatchProperty";
143
149
  } else {
144
- message = "Function name `{{funcName}}` should not match variable name `{{name}}`";
150
+ messageId = "notMatchVariable";
145
151
  }
146
152
  context.report({
147
153
  node,
148
- message,
154
+ messageId,
149
155
  data: {
150
156
  name,
151
157
  funcName
@@ -9,7 +9,7 @@
9
9
  // Requirements
10
10
  //------------------------------------------------------------------------------
11
11
 
12
- const astUtils = require("../ast-utils");
12
+ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  /**
15
15
  * Checks whether or not a given variable is a function name.
@@ -33,16 +33,55 @@ module.exports = {
33
33
  url: "https://eslint.org/docs/rules/func-names"
34
34
  },
35
35
 
36
- schema: [
37
- {
38
- enum: ["always", "as-needed", "never"]
39
- }
40
- ]
36
+ schema: {
37
+ definitions: {
38
+ value: {
39
+ enum: [
40
+ "always",
41
+ "as-needed",
42
+ "never"
43
+ ]
44
+ }
45
+ },
46
+ items: [
47
+ {
48
+ $ref: "#/definitions/value"
49
+ },
50
+ {
51
+ type: "object",
52
+ properties: {
53
+ generators: {
54
+ $ref: "#/definitions/value"
55
+ }
56
+ },
57
+ additionalProperties: false
58
+ }
59
+ ]
60
+ },
61
+ messages: {
62
+ unnamed: "Unexpected unnamed {{name}}.",
63
+ named: "Unexpected named {{name}}."
64
+ }
41
65
  },
42
66
 
43
67
  create(context) {
44
- const never = context.options[0] === "never";
45
- const asNeeded = context.options[0] === "as-needed";
68
+
69
+ /**
70
+ * Returns the config option for the given node.
71
+ * @param {ASTNode} node - A node to get the config for.
72
+ * @returns {string} The config option.
73
+ */
74
+ function getConfigForNode(node) {
75
+ if (
76
+ node.generator &&
77
+ context.options.length > 1 &&
78
+ context.options[1].generators
79
+ ) {
80
+ return context.options[1].generators;
81
+ }
82
+
83
+ return context.options[0] || "always";
84
+ }
46
85
 
47
86
  /**
48
87
  * Determines whether the current FunctionExpression node is a get, set, or
@@ -79,6 +118,32 @@ module.exports = {
79
118
  (parent.type === "AssignmentPattern" && parent.right === node);
80
119
  }
81
120
 
121
+ /**
122
+ * Reports that an unnamed function should be named
123
+ * @param {ASTNode} node - The node to report in the event of an error.
124
+ * @returns {void}
125
+ */
126
+ function reportUnexpectedUnnamedFunction(node) {
127
+ context.report({
128
+ node,
129
+ messageId: "unnamed",
130
+ data: { name: astUtils.getFunctionNameWithKind(node) }
131
+ });
132
+ }
133
+
134
+ /**
135
+ * Reports that a named function should be unnamed
136
+ * @param {ASTNode} node - The node to report in the event of an error.
137
+ * @returns {void}
138
+ */
139
+ function reportUnexpectedNamedFunction(node) {
140
+ context.report({
141
+ node,
142
+ messageId: "named",
143
+ data: { name: astUtils.getFunctionNameWithKind(node) }
144
+ });
145
+ }
146
+
82
147
  return {
83
148
  "FunctionExpression:exit"(node) {
84
149
 
@@ -90,23 +155,19 @@ module.exports = {
90
155
  }
91
156
 
92
157
  const hasName = Boolean(node.id && node.id.name);
93
- const name = astUtils.getFunctionNameWithKind(node);
158
+ const config = getConfigForNode(node);
94
159
 
95
- if (never) {
160
+ if (config === "never") {
96
161
  if (hasName) {
97
- context.report({
98
- node,
99
- message: "Unexpected named {{name}}.",
100
- data: { name }
101
- });
162
+ reportUnexpectedNamedFunction(node);
163
+ }
164
+ } else if (config === "as-needed") {
165
+ if (!hasName && !hasInferredName(node)) {
166
+ reportUnexpectedUnnamedFunction(node);
102
167
  }
103
168
  } else {
104
- if (!hasName && (asNeeded ? !hasInferredName(node) : !isObjectOrClassMethod(node))) {
105
- context.report({
106
- node,
107
- message: "Unexpected unnamed {{name}}.",
108
- data: { name }
109
- });
169
+ if (!hasName && !isObjectOrClassMethod(node)) {
170
+ reportUnexpectedUnnamedFunction(node);
110
171
  }
111
172
  }
112
173
  }
@@ -30,7 +30,11 @@ module.exports = {
30
30
  },
31
31
  additionalProperties: false
32
32
  }
33
- ]
33
+ ],
34
+ messages: {
35
+ expression: "Expected a function expression.",
36
+ declaration: "Expected a function declaration."
37
+ }
34
38
  },
35
39
 
36
40
  create(context) {
@@ -45,7 +49,7 @@ module.exports = {
45
49
  stack.push(false);
46
50
 
47
51
  if (!enforceDeclarations && node.parent.type !== "ExportDefaultDeclaration") {
48
- context.report({ node, message: "Expected a function expression." });
52
+ context.report({ node, messageId: "expression" });
49
53
  }
50
54
  },
51
55
  "FunctionDeclaration:exit"() {
@@ -56,7 +60,7 @@ module.exports = {
56
60
  stack.push(false);
57
61
 
58
62
  if (enforceDeclarations && node.parent.type === "VariableDeclarator") {
59
- context.report({ node: node.parent, message: "Expected a function declaration." });
63
+ context.report({ node: node.parent, messageId: "declaration" });
60
64
  }
61
65
  },
62
66
  "FunctionExpression:exit"() {
@@ -79,7 +83,7 @@ module.exports = {
79
83
  const hasThisExpr = stack.pop();
80
84
 
81
85
  if (enforceDeclarations && !hasThisExpr && node.parent.type === "VariableDeclarator") {
82
- context.report({ node: node.parent, message: "Expected a function declaration." });
86
+ context.report({ node: node.parent, messageId: "declaration" });
83
87
  }
84
88
  };
85
89
  }
@@ -8,7 +8,7 @@
8
8
  // Requirements
9
9
  //------------------------------------------------------------------------------
10
10
 
11
- const astUtils = require("../ast-utils");
11
+ const astUtils = require("../util/ast-utils");
12
12
 
13
13
  //------------------------------------------------------------------------------
14
14
  // Rule Definition
@@ -41,7 +41,13 @@ module.exports = {
41
41
  }
42
42
  ]
43
43
  }
44
- ]
44
+ ],
45
+ messages: {
46
+ expectedBefore: "Expected newline before ')'.",
47
+ expectedAfter: "Expected newline after '('.",
48
+ unexpectedBefore: "Unexpected newline before '('.",
49
+ unexpectedAfter: "Unexpected newline after ')'."
50
+ }
45
51
  },
46
52
 
47
53
  create(context) {
@@ -99,7 +105,7 @@ module.exports = {
99
105
  if (hasLeftNewline && !needsNewlines) {
100
106
  context.report({
101
107
  node: leftParen,
102
- message: "Unexpected newline after '('.",
108
+ messageId: "unexpectedAfter",
103
109
  fix(fixer) {
104
110
  return sourceCode.getText().slice(leftParen.range[1], tokenAfterLeftParen.range[0]).trim()
105
111
 
@@ -111,7 +117,7 @@ module.exports = {
111
117
  } else if (!hasLeftNewline && needsNewlines) {
112
118
  context.report({
113
119
  node: leftParen,
114
- message: "Expected a newline after '('.",
120
+ messageId: "expectedAfter",
115
121
  fix: fixer => fixer.insertTextAfter(leftParen, "\n")
116
122
  });
117
123
  }
@@ -119,7 +125,7 @@ module.exports = {
119
125
  if (hasRightNewline && !needsNewlines) {
120
126
  context.report({
121
127
  node: rightParen,
122
- message: "Unexpected newline before ')'.",
128
+ messageId: "unexpectedBefore",
123
129
  fix(fixer) {
124
130
  return sourceCode.getText().slice(tokenBeforeRightParen.range[1], rightParen.range[0]).trim()
125
131
 
@@ -131,7 +137,7 @@ module.exports = {
131
137
  } else if (!hasRightNewline && needsNewlines) {
132
138
  context.report({
133
139
  node: rightParen,
134
- message: "Expected a newline before ')'.",
140
+ messageId: "expectedBefore",
135
141
  fix: fixer => fixer.insertTextBefore(rightParen, "\n")
136
142
  });
137
143
  }
@@ -55,7 +55,13 @@ module.exports = {
55
55
  }
56
56
  ]
57
57
  }
58
- ]
58
+ ],
59
+ messages: {
60
+ missingBefore: "Missing space before *.",
61
+ missingAfter: "Missing space after *.",
62
+ unexpectedBefore: "Unexpected space before *.",
63
+ unexpectedAfter: "Unexpected space after *."
64
+ }
59
65
  },
60
66
 
61
67
  create(context) {
@@ -119,6 +125,15 @@ module.exports = {
119
125
  );
120
126
  }
121
127
 
128
+ /**
129
+ * capitalize a given string.
130
+ * @param {string} str the given string.
131
+ * @returns {string} the capitalized string.
132
+ */
133
+ function capitalize(str) {
134
+ return str[0].toUpperCase() + str.slice(1);
135
+ }
136
+
122
137
  /**
123
138
  * Checks the spacing between two tokens before or after the star token.
124
139
  *
@@ -135,17 +150,11 @@ module.exports = {
135
150
  const after = leftToken.value === "*";
136
151
  const spaceRequired = modes[kind][side];
137
152
  const node = after ? leftToken : rightToken;
138
- const type = spaceRequired ? "Missing" : "Unexpected";
139
- const message = "{{type}} space {{side}} *.";
140
- const data = {
141
- type,
142
- side
143
- };
153
+ const messageId = `${spaceRequired ? "missing" : "unexpected"}${capitalize(side)}`;
144
154
 
145
155
  context.report({
146
156
  node,
147
- message,
148
- data,
157
+ messageId,
149
158
  fix(fixer) {
150
159
  if (spaceRequired) {
151
160
  if (after) {
@@ -9,7 +9,7 @@
9
9
  // Requirements
10
10
  //------------------------------------------------------------------------------
11
11
 
12
- const astUtils = require("../ast-utils");
12
+ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  //------------------------------------------------------------------------------
15
15
  // Helpers
@@ -61,7 +61,11 @@ module.exports = {
61
61
  },
62
62
  additionalProperties: false
63
63
  }
64
- ]
64
+ ],
65
+ messages: {
66
+ expected: "Expected to return a value in {{name}}.",
67
+ expectedAlways: "Expected {{name}} to always return a value."
68
+ }
65
69
  },
66
70
 
67
71
  create(context) {
@@ -93,9 +97,7 @@ module.exports = {
93
97
  context.report({
94
98
  node,
95
99
  loc: getId(node).loc.start,
96
- message: funcInfo.hasReturn
97
- ? "Expected {{name}} to always return a value."
98
- : "Expected to return a value in {{name}}.",
100
+ messageId: funcInfo.hasReturn ? "expectedAlways" : "expected",
99
101
  data: {
100
102
  name: astUtils.getFunctionNameWithKind(funcInfo.node)
101
103
  }
@@ -161,7 +163,7 @@ module.exports = {
161
163
  if (!options.allowImplicit && !node.argument) {
162
164
  context.report({
163
165
  node,
164
- message: "Expected to return a value in {{name}}.",
166
+ messageId: "expected",
165
167
  data: {
166
168
  name: astUtils.getFunctionNameWithKind(funcInfo.node)
167
169
  }
@@ -12,7 +12,7 @@
12
12
  // Requirements
13
13
  //------------------------------------------------------------------------------
14
14
 
15
- const astUtils = require("../ast-utils");
15
+ const astUtils = require("../util/ast-utils");
16
16
 
17
17
  //------------------------------------------------------------------------------
18
18
  // Rule Definition