eslint 5.7.0 → 5.11.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 (284) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/README.md +2 -2
  3. package/conf/eslint-recommended.js +1 -0
  4. package/lib/cli-engine.js +129 -32
  5. package/lib/cli.js +6 -1
  6. package/lib/config/autoconfig.js +0 -1
  7. package/lib/config/config-rule.js +4 -4
  8. package/lib/config/config-validator.js +2 -2
  9. package/lib/config.js +15 -2
  10. package/lib/linter.js +17 -120
  11. package/lib/options.js +5 -0
  12. package/lib/rules/accessor-pairs.js +4 -0
  13. package/lib/rules/array-bracket-newline.js +5 -0
  14. package/lib/rules/array-bracket-spacing.js +5 -0
  15. package/lib/rules/array-callback-return.js +2 -0
  16. package/lib/rules/array-element-newline.js +4 -1
  17. package/lib/rules/arrow-body-style.js +2 -0
  18. package/lib/rules/arrow-parens.js +2 -0
  19. package/lib/rules/arrow-spacing.js +2 -0
  20. package/lib/rules/block-scoped-var.js +2 -0
  21. package/lib/rules/block-spacing.js +2 -0
  22. package/lib/rules/brace-style.js +2 -0
  23. package/lib/rules/callback-return.js +2 -0
  24. package/lib/rules/camelcase.js +26 -14
  25. package/lib/rules/capitalized-comments.js +4 -0
  26. package/lib/rules/class-methods-use-this.js +3 -0
  27. package/lib/rules/comma-dangle.js +4 -0
  28. package/lib/rules/comma-spacing.js +2 -0
  29. package/lib/rules/comma-style.js +12 -2
  30. package/lib/rules/complexity.js +2 -0
  31. package/lib/rules/computed-property-spacing.js +2 -0
  32. package/lib/rules/consistent-return.js +2 -0
  33. package/lib/rules/consistent-this.js +2 -0
  34. package/lib/rules/constructor-super.js +2 -0
  35. package/lib/rules/curly.js +2 -0
  36. package/lib/rules/default-case.js +2 -0
  37. package/lib/rules/dot-location.js +2 -0
  38. package/lib/rules/dot-notation.js +2 -0
  39. package/lib/rules/eol-last.js +5 -0
  40. package/lib/rules/eqeqeq.js +2 -1
  41. package/lib/rules/for-direction.js +4 -0
  42. package/lib/rules/func-call-spacing.js +4 -0
  43. package/lib/rules/func-name-matching.js +3 -0
  44. package/lib/rules/func-names.js +3 -0
  45. package/lib/rules/func-style.js +3 -0
  46. package/lib/rules/function-paren-newline.js +5 -0
  47. package/lib/rules/generator-star-spacing.js +3 -0
  48. package/lib/rules/getter-return.js +5 -0
  49. package/lib/rules/global-require.js +2 -0
  50. package/lib/rules/guard-for-in.js +2 -0
  51. package/lib/rules/handle-callback-err.js +3 -1
  52. package/lib/rules/id-blacklist.js +2 -0
  53. package/lib/rules/id-length.js +2 -0
  54. package/lib/rules/id-match.js +101 -27
  55. package/lib/rules/implicit-arrow-linebreak.js +147 -2
  56. package/lib/rules/indent-legacy.js +4 -4
  57. package/lib/rules/indent.js +31 -23
  58. package/lib/rules/init-declarations.js +2 -0
  59. package/lib/rules/jsx-quotes.js +2 -0
  60. package/lib/rules/key-spacing.js +4 -3
  61. package/lib/rules/keyword-spacing.js +7 -1
  62. package/lib/rules/line-comment-position.js +2 -0
  63. package/lib/rules/linebreak-style.js +2 -0
  64. package/lib/rules/lines-around-comment.js +2 -0
  65. package/lib/rules/lines-around-directive.js +6 -2
  66. package/lib/rules/lines-between-class-members.js +2 -0
  67. package/lib/rules/max-classes-per-file.js +4 -0
  68. package/lib/rules/max-depth.js +2 -0
  69. package/lib/rules/max-len.js +2 -0
  70. package/lib/rules/max-lines-per-function.js +2 -0
  71. package/lib/rules/max-lines.js +2 -0
  72. package/lib/rules/max-nested-callbacks.js +2 -0
  73. package/lib/rules/max-params.js +2 -0
  74. package/lib/rules/max-statements-per-line.js +2 -0
  75. package/lib/rules/max-statements.js +2 -0
  76. package/lib/rules/multiline-comment-style.js +3 -0
  77. package/lib/rules/multiline-ternary.js +3 -0
  78. package/lib/rules/new-cap.js +2 -0
  79. package/lib/rules/new-parens.js +2 -1
  80. package/lib/rules/newline-after-var.js +5 -2
  81. package/lib/rules/newline-before-return.js +6 -3
  82. package/lib/rules/newline-per-chained-call.js +4 -0
  83. package/lib/rules/no-alert.js +2 -0
  84. package/lib/rules/no-array-constructor.js +2 -0
  85. package/lib/rules/no-async-promise-executor.js +3 -0
  86. package/lib/rules/no-await-in-loop.js +4 -0
  87. package/lib/rules/no-bitwise.js +2 -0
  88. package/lib/rules/no-buffer-constructor.js +4 -0
  89. package/lib/rules/no-caller.js +2 -0
  90. package/lib/rules/no-case-declarations.js +2 -0
  91. package/lib/rules/no-catch-shadow.js +6 -3
  92. package/lib/rules/no-class-assign.js +2 -0
  93. package/lib/rules/no-compare-neg-zero.js +4 -0
  94. package/lib/rules/no-cond-assign.js +2 -0
  95. package/lib/rules/no-confusing-arrow.js +2 -0
  96. package/lib/rules/no-console.js +2 -0
  97. package/lib/rules/no-const-assign.js +2 -0
  98. package/lib/rules/no-constant-condition.js +2 -1
  99. package/lib/rules/no-continue.js +2 -0
  100. package/lib/rules/no-control-regex.js +4 -2
  101. package/lib/rules/no-debugger.js +4 -0
  102. package/lib/rules/no-delete-var.js +2 -0
  103. package/lib/rules/no-div-regex.js +2 -0
  104. package/lib/rules/no-dupe-args.js +2 -0
  105. package/lib/rules/no-dupe-class-members.js +2 -0
  106. package/lib/rules/no-dupe-keys.js +2 -0
  107. package/lib/rules/no-duplicate-case.js +2 -0
  108. package/lib/rules/no-duplicate-imports.js +2 -0
  109. package/lib/rules/no-else-return.js +2 -1
  110. package/lib/rules/no-empty-character-class.js +2 -0
  111. package/lib/rules/no-empty-function.js +2 -0
  112. package/lib/rules/no-empty-pattern.js +2 -0
  113. package/lib/rules/no-empty.js +2 -0
  114. package/lib/rules/no-eq-null.js +2 -0
  115. package/lib/rules/no-eval.js +2 -0
  116. package/lib/rules/no-ex-assign.js +2 -0
  117. package/lib/rules/no-extend-native.js +2 -0
  118. package/lib/rules/no-extra-bind.js +2 -1
  119. package/lib/rules/no-extra-boolean-cast.js +2 -1
  120. package/lib/rules/no-extra-label.js +2 -1
  121. package/lib/rules/no-extra-parens.js +5 -6
  122. package/lib/rules/no-extra-semi.js +2 -0
  123. package/lib/rules/no-fallthrough.js +2 -0
  124. package/lib/rules/no-floating-decimal.js +2 -1
  125. package/lib/rules/no-func-assign.js +2 -0
  126. package/lib/rules/no-global-assign.js +2 -0
  127. package/lib/rules/no-implicit-coercion.js +3 -0
  128. package/lib/rules/no-implicit-globals.js +2 -0
  129. package/lib/rules/no-implied-eval.js +3 -1
  130. package/lib/rules/no-inline-comments.js +2 -0
  131. package/lib/rules/no-inner-declarations.js +2 -0
  132. package/lib/rules/no-invalid-regexp.js +2 -0
  133. package/lib/rules/no-invalid-this.js +2 -0
  134. package/lib/rules/no-irregular-whitespace.js +7 -9
  135. package/lib/rules/no-iterator.js +2 -0
  136. package/lib/rules/no-label-var.js +2 -0
  137. package/lib/rules/no-labels.js +2 -0
  138. package/lib/rules/no-lone-blocks.js +2 -0
  139. package/lib/rules/no-lonely-if.js +2 -1
  140. package/lib/rules/no-loop-func.js +2 -0
  141. package/lib/rules/no-magic-numbers.js +3 -0
  142. package/lib/rules/no-misleading-character-class.js +4 -0
  143. package/lib/rules/no-mixed-operators.js +3 -0
  144. package/lib/rules/no-mixed-requires.js +2 -0
  145. package/lib/rules/no-mixed-spaces-and-tabs.js +2 -0
  146. package/lib/rules/no-multi-assign.js +3 -0
  147. package/lib/rules/no-multi-spaces.js +2 -0
  148. package/lib/rules/no-multi-str.js +2 -0
  149. package/lib/rules/no-multiple-empty-lines.js +2 -0
  150. package/lib/rules/no-native-reassign.js +4 -1
  151. package/lib/rules/no-negated-condition.js +2 -0
  152. package/lib/rules/no-negated-in-lhs.js +5 -2
  153. package/lib/rules/no-nested-ternary.js +2 -0
  154. package/lib/rules/no-new-func.js +2 -0
  155. package/lib/rules/no-new-object.js +2 -0
  156. package/lib/rules/no-new-require.js +2 -0
  157. package/lib/rules/no-new-symbol.js +2 -0
  158. package/lib/rules/no-new-wrappers.js +2 -0
  159. package/lib/rules/no-new.js +2 -0
  160. package/lib/rules/no-obj-calls.js +2 -0
  161. package/lib/rules/no-octal-escape.js +2 -0
  162. package/lib/rules/no-octal.js +2 -0
  163. package/lib/rules/no-param-reassign.js +2 -0
  164. package/lib/rules/no-path-concat.js +2 -0
  165. package/lib/rules/no-plusplus.js +2 -0
  166. package/lib/rules/no-process-env.js +2 -0
  167. package/lib/rules/no-process-exit.js +2 -0
  168. package/lib/rules/no-proto.js +2 -0
  169. package/lib/rules/no-prototype-builtins.js +2 -0
  170. package/lib/rules/no-redeclare.js +2 -0
  171. package/lib/rules/no-regex-spaces.js +2 -1
  172. package/lib/rules/no-restricted-globals.js +2 -0
  173. package/lib/rules/no-restricted-imports.js +41 -23
  174. package/lib/rules/no-restricted-modules.js +2 -0
  175. package/lib/rules/no-restricted-properties.js +2 -0
  176. package/lib/rules/no-restricted-syntax.js +2 -0
  177. package/lib/rules/no-return-assign.js +2 -0
  178. package/lib/rules/no-return-await.js +4 -0
  179. package/lib/rules/no-script-url.js +2 -0
  180. package/lib/rules/no-self-assign.js +2 -0
  181. package/lib/rules/no-self-compare.js +2 -0
  182. package/lib/rules/no-sequences.js +2 -0
  183. package/lib/rules/no-shadow-restricted-names.js +2 -0
  184. package/lib/rules/no-shadow.js +2 -0
  185. package/lib/rules/no-spaced-func.js +4 -1
  186. package/lib/rules/no-sparse-arrays.js +2 -0
  187. package/lib/rules/no-sync.js +2 -0
  188. package/lib/rules/no-tabs.js +2 -0
  189. package/lib/rules/no-template-curly-in-string.js +2 -0
  190. package/lib/rules/no-ternary.js +2 -0
  191. package/lib/rules/no-this-before-super.js +2 -1
  192. package/lib/rules/no-throw-literal.js +2 -0
  193. package/lib/rules/no-trailing-spaces.js +2 -0
  194. package/lib/rules/no-undef-init.js +2 -1
  195. package/lib/rules/no-undef.js +2 -0
  196. package/lib/rules/no-undefined.js +2 -0
  197. package/lib/rules/no-underscore-dangle.js +2 -0
  198. package/lib/rules/no-unexpected-multiline.js +2 -0
  199. package/lib/rules/no-unmodified-loop-condition.js +2 -0
  200. package/lib/rules/no-unneeded-ternary.js +2 -0
  201. package/lib/rules/no-unreachable.js +2 -1
  202. package/lib/rules/no-unsafe-finally.js +2 -0
  203. package/lib/rules/no-unsafe-negation.js +3 -0
  204. package/lib/rules/no-unused-expressions.js +2 -0
  205. package/lib/rules/no-unused-labels.js +2 -1
  206. package/lib/rules/no-unused-vars.js +36 -12
  207. package/lib/rules/no-use-before-define.js +2 -0
  208. package/lib/rules/no-useless-call.js +2 -0
  209. package/lib/rules/no-useless-catch.js +51 -0
  210. package/lib/rules/no-useless-computed-key.js +2 -1
  211. package/lib/rules/no-useless-concat.js +2 -0
  212. package/lib/rules/no-useless-constructor.js +2 -0
  213. package/lib/rules/no-useless-escape.js +2 -0
  214. package/lib/rules/no-useless-rename.js +4 -0
  215. package/lib/rules/no-useless-return.js +3 -0
  216. package/lib/rules/no-var.js +2 -0
  217. package/lib/rules/no-void.js +2 -0
  218. package/lib/rules/no-warning-comments.js +2 -0
  219. package/lib/rules/no-whitespace-before-property.js +2 -0
  220. package/lib/rules/no-with.js +2 -0
  221. package/lib/rules/nonblock-statement-body-position.js +4 -0
  222. package/lib/rules/object-curly-newline.js +4 -1
  223. package/lib/rules/object-curly-spacing.js +2 -0
  224. package/lib/rules/object-property-newline.js +3 -2
  225. package/lib/rules/object-shorthand.js +2 -0
  226. package/lib/rules/one-var-declaration-per-line.js +2 -0
  227. package/lib/rules/one-var.js +2 -1
  228. package/lib/rules/operator-assignment.js +2 -0
  229. package/lib/rules/operator-linebreak.js +2 -0
  230. package/lib/rules/padded-blocks.js +2 -0
  231. package/lib/rules/padding-line-between-statements.js +41 -0
  232. package/lib/rules/prefer-arrow-callback.js +2 -0
  233. package/lib/rules/prefer-const.js +116 -11
  234. package/lib/rules/prefer-destructuring.js +3 -0
  235. package/lib/rules/prefer-numeric-literals.js +2 -1
  236. package/lib/rules/prefer-object-spread.js +4 -0
  237. package/lib/rules/prefer-promise-reject-errors.js +4 -0
  238. package/lib/rules/prefer-reflect.js +4 -1
  239. package/lib/rules/prefer-rest-params.js +2 -0
  240. package/lib/rules/prefer-spread.js +2 -1
  241. package/lib/rules/prefer-template.js +2 -1
  242. package/lib/rules/quote-props.js +2 -0
  243. package/lib/rules/quotes.js +50 -25
  244. package/lib/rules/radix.js +2 -0
  245. package/lib/rules/require-atomic-updates.js +4 -0
  246. package/lib/rules/require-await.js +3 -0
  247. package/lib/rules/require-jsdoc.js +6 -1
  248. package/lib/rules/require-unicode-regexp.js +4 -0
  249. package/lib/rules/require-yield.js +2 -0
  250. package/lib/rules/rest-spread-spacing.js +4 -0
  251. package/lib/rules/semi-spacing.js +2 -0
  252. package/lib/rules/semi-style.js +3 -0
  253. package/lib/rules/semi.js +2 -0
  254. package/lib/rules/sort-imports.js +2 -0
  255. package/lib/rules/sort-keys.js +3 -0
  256. package/lib/rules/sort-vars.js +2 -0
  257. package/lib/rules/space-before-blocks.js +46 -35
  258. package/lib/rules/space-before-function-paren.js +2 -0
  259. package/lib/rules/space-in-parens.js +2 -1
  260. package/lib/rules/space-infix-ops.js +6 -1
  261. package/lib/rules/space-unary-ops.js +2 -0
  262. package/lib/rules/spaced-comment.js +2 -0
  263. package/lib/rules/strict.js +2 -0
  264. package/lib/rules/switch-colon-spacing.js +4 -0
  265. package/lib/rules/symbol-description.js +2 -0
  266. package/lib/rules/template-curly-spacing.js +2 -0
  267. package/lib/rules/template-tag-spacing.js +2 -0
  268. package/lib/rules/unicode-bom.js +2 -0
  269. package/lib/rules/use-isnan.js +2 -0
  270. package/lib/rules/valid-jsdoc.js +6 -1
  271. package/lib/rules/valid-typeof.js +2 -0
  272. package/lib/rules/vars-on-top.js +2 -0
  273. package/lib/rules/wrap-iife.js +2 -0
  274. package/lib/rules/wrap-regex.js +3 -1
  275. package/lib/rules/yield-star-spacing.js +2 -0
  276. package/lib/rules/yoda.js +2 -0
  277. package/lib/testers/rule-tester.js +2 -2
  278. package/lib/util/config-comment-parser.js +144 -0
  279. package/lib/util/glob-utils.js +1 -1
  280. package/lib/{ignored-paths.js → util/ignored-paths.js} +4 -4
  281. package/lib/{report-translator.js → util/report-translator.js} +2 -2
  282. package/lib/util/source-code.js +2 -1
  283. package/messages/all-files-ignored.txt +1 -1
  284. package/package.json +10 -11
package/lib/linter.js CHANGED
@@ -11,7 +11,6 @@
11
11
 
12
12
  const eslintScope = require("eslint-scope"),
13
13
  evk = require("eslint-visitor-keys"),
14
- levn = require("levn"),
15
14
  lodash = require("lodash"),
16
15
  CodePathAnalyzer = require("./code-path-analysis/code-path-analyzer"),
17
16
  ConfigOps = require("./config/config-ops"),
@@ -22,9 +21,10 @@ const eslintScope = require("eslint-scope"),
22
21
  NodeEventGenerator = require("./util/node-event-generator"),
23
22
  SourceCode = require("./util/source-code"),
24
23
  Traverser = require("./util/traverser"),
25
- createReportTranslator = require("./report-translator"),
24
+ createReportTranslator = require("./util/report-translator"),
26
25
  Rules = require("./rules"),
27
26
  timing = require("./util/timing"),
27
+ ConfigCommentParser = require("./util/config-comment-parser"),
28
28
  astUtils = require("./util/ast-utils"),
29
29
  pkg = require("../package.json"),
30
30
  SourceCodeFixer = require("./util/source-code-fixer");
@@ -32,6 +32,7 @@ const eslintScope = require("eslint-scope"),
32
32
  const debug = require("debug")("eslint:linter");
33
33
  const MAX_AUTOFIX_PASSES = 10;
34
34
  const DEFAULT_PARSER_NAME = "espree";
35
+ const commentParser = new ConfigCommentParser();
35
36
 
36
37
  //------------------------------------------------------------------------------
37
38
  // Typedefs
@@ -59,117 +60,6 @@ const DEFAULT_PARSER_NAME = "espree";
59
60
  // Helpers
60
61
  //------------------------------------------------------------------------------
61
62
 
62
- /**
63
- * Parses a list of "name:boolean_value" or/and "name" options divided by comma or
64
- * whitespace.
65
- * @param {string} string The string to parse.
66
- * @param {Comment} comment The comment node which has the string.
67
- * @returns {Object} Result map object of names and boolean values
68
- */
69
- function parseBooleanConfig(string, comment) {
70
- const items = {};
71
-
72
- // Collapse whitespace around `:` and `,` to make parsing easier
73
- const trimmedString = string.replace(/\s*([:,])\s*/g, "$1");
74
-
75
- trimmedString.split(/\s|,+/).forEach(name => {
76
- if (!name) {
77
- return;
78
- }
79
- const pos = name.indexOf(":");
80
-
81
- if (pos === -1) {
82
- items[name] = {
83
- value: false,
84
- comment
85
- };
86
- } else {
87
- items[name.slice(0, pos)] = {
88
- value: name.slice(pos + 1) === "true",
89
- comment
90
- };
91
- }
92
- });
93
- return items;
94
- }
95
-
96
- /**
97
- * Parses a JSON-like config.
98
- * @param {string} string The string to parse.
99
- * @param {Object} location Start line and column of comments for potential error message.
100
- * @returns {({success: true, config: Object}|{success: false, error: Problem})} Result map object
101
- */
102
- function parseJsonConfig(string, location) {
103
- let items = {};
104
-
105
- // Parses a JSON-like comment by the same way as parsing CLI option.
106
- try {
107
- items = levn.parse("Object", string) || {};
108
-
109
- // Some tests say that it should ignore invalid comments such as `/*eslint no-alert:abc*/`.
110
- // Also, commaless notations have invalid severity:
111
- // "no-alert: 2 no-console: 2" --> {"no-alert": "2 no-console: 2"}
112
- // Should ignore that case as well.
113
- if (ConfigOps.isEverySeverityValid(items)) {
114
- return {
115
- success: true,
116
- config: items
117
- };
118
- }
119
- } catch (ex) {
120
-
121
- // ignore to parse the string by a fallback.
122
- }
123
-
124
- /*
125
- * Optionator cannot parse commaless notations.
126
- * But we are supporting that. So this is a fallback for that.
127
- */
128
- items = {};
129
- const normalizedString = string.replace(/([a-zA-Z0-9\-/]+):/g, "\"$1\":").replace(/(]|[0-9])\s+(?=")/, "$1,");
130
-
131
- try {
132
- items = JSON.parse(`{${normalizedString}}`);
133
- } catch (ex) {
134
- return {
135
- success: false,
136
- error: {
137
- ruleId: null,
138
- fatal: true,
139
- severity: 2,
140
- message: `Failed to parse JSON from '${normalizedString}': ${ex.message}`,
141
- line: location.start.line,
142
- column: location.start.column + 1
143
- }
144
- };
145
-
146
- }
147
-
148
- return {
149
- success: true,
150
- config: items
151
- };
152
- }
153
-
154
- /**
155
- * Parses a config of values separated by comma.
156
- * @param {string} string The string to parse.
157
- * @returns {Object} Result map of values and true values
158
- */
159
- function parseListConfig(string) {
160
- const items = {};
161
-
162
- // Collapse whitespace around ,
163
- string.replace(/\s*,\s*/g, ",").split(/,+/).forEach(name => {
164
- const trimmedName = name.trim();
165
-
166
- if (trimmedName) {
167
- items[trimmedName] = true;
168
- }
169
- });
170
- return items;
171
- }
172
-
173
63
  /**
174
64
  * Ensures that variables representing built-in properties of the Global Object,
175
65
  * and any globals declared by special block comments, are present in the global
@@ -248,7 +138,7 @@ function addDeclaredGlobals(globalScope, configGlobals, commentDirectives) {
248
138
  * @returns {DisableDirective[]} Directives from the comment
249
139
  */
250
140
  function createDisableDirectives(type, loc, value) {
251
- const ruleIds = Object.keys(parseListConfig(value));
141
+ const ruleIds = Object.keys(commentParser.parseListConfig(value));
252
142
  const directiveRules = ruleIds.length ? ruleIds : [null];
253
143
 
254
144
  return directiveRules.map(ruleId => ({ type, line: loc.line, column: loc.column + 1, ruleId }));
@@ -301,12 +191,12 @@ function getDirectiveComments(filename, ast, ruleMapper) {
301
191
  } else if (comment.type === "Block") {
302
192
  switch (match[1]) {
303
193
  case "exported":
304
- Object.assign(exportedVariables, parseBooleanConfig(directiveValue, comment));
194
+ Object.assign(exportedVariables, commentParser.parseBooleanConfig(directiveValue, comment));
305
195
  break;
306
196
 
307
197
  case "globals":
308
198
  case "global":
309
- Object.assign(enabledGlobals, parseBooleanConfig(directiveValue, comment));
199
+ Object.assign(enabledGlobals, commentParser.parseBooleanConfig(directiveValue, comment));
310
200
  break;
311
201
 
312
202
  case "eslint-disable":
@@ -318,7 +208,7 @@ function getDirectiveComments(filename, ast, ruleMapper) {
318
208
  break;
319
209
 
320
210
  case "eslint": {
321
- const parseResult = parseJsonConfig(directiveValue, comment.loc);
211
+ const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc);
322
212
 
323
213
  if (parseResult.success) {
324
214
  Object.keys(parseResult.config).forEach(name => {
@@ -398,7 +288,7 @@ function findEslintEnv(text) {
398
288
  eslintEnvPattern.lastIndex = 0;
399
289
 
400
290
  while ((match = eslintEnvPattern.exec(text))) {
401
- retv = Object.assign(retv || {}, parseListConfig(match[1]));
291
+ retv = Object.assign(retv || {}, commentParser.parseListConfig(match[1]));
402
292
  }
403
293
 
404
294
  return retv;
@@ -888,6 +778,15 @@ module.exports = class Linter {
888
778
  this.environments = new Environments();
889
779
  }
890
780
 
781
+ /**
782
+ * Getter for package version.
783
+ * @static
784
+ * @returns {string} The version from package.json.
785
+ */
786
+ static get version() {
787
+ return pkg.version;
788
+ }
789
+
891
790
  /**
892
791
  * Configuration object for the `verify` API. A JS representation of the eslintrc files.
893
792
  * @typedef {Object} ESLintConfig
@@ -1023,8 +922,6 @@ module.exports = class Linter {
1023
922
  * @param {(string|Object)} [filenameOrOptions] The optional filename of the file being checked.
1024
923
  * If this is not set, the filename will default to '<input>' in the rule context. If
1025
924
  * an object, then it has "filename", "saveState", and "allowInlineConfig" properties.
1026
- * @param {boolean} [saveState] Indicates if the state from the last run should be saved.
1027
- * Mostly useful for testing purposes.
1028
925
  * @param {boolean} [filenameOrOptions.allowInlineConfig] Allow/disallow inline comments' ability to change config once it is set. Defaults to true if not supplied.
1029
926
  * Useful if you want to validate JS without comments overriding rules.
1030
927
  * @param {function(string): string[]} [filenameOrOptions.preprocess] preprocessor for source text. If provided,
package/lib/options.js CHANGED
@@ -97,6 +97,11 @@ module.exports = optionator({
97
97
  default: false,
98
98
  description: "Automatically fix problems without saving the changes to the file system"
99
99
  },
100
+ {
101
+ option: "fix-type",
102
+ type: "Array",
103
+ description: "Specify the types of fixes to apply (problem, suggestion, layout)"
104
+ },
100
105
  {
101
106
  heading: "Ignoring files"
102
107
  },
@@ -72,12 +72,15 @@ function isPropertyDescriptor(node) {
72
72
 
73
73
  module.exports = {
74
74
  meta: {
75
+ type: "suggestion",
76
+
75
77
  docs: {
76
78
  description: "enforce getter and setter pairs in objects",
77
79
  category: "Best Practices",
78
80
  recommended: false,
79
81
  url: "https://eslint.org/docs/rules/accessor-pairs"
80
82
  },
83
+
81
84
  schema: [{
82
85
  type: "object",
83
86
  properties: {
@@ -90,6 +93,7 @@ module.exports = {
90
93
  },
91
94
  additionalProperties: false
92
95
  }],
96
+
93
97
  messages: {
94
98
  getter: "Getter is not present.",
95
99
  setter: "Setter is not present."
@@ -13,13 +13,17 @@ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  module.exports = {
15
15
  meta: {
16
+ type: "layout",
17
+
16
18
  docs: {
17
19
  description: "enforce linebreaks after opening and before closing array brackets",
18
20
  category: "Stylistic Issues",
19
21
  recommended: false,
20
22
  url: "https://eslint.org/docs/rules/array-bracket-newline"
21
23
  },
24
+
22
25
  fixable: "whitespace",
26
+
23
27
  schema: [
24
28
  {
25
29
  oneOf: [
@@ -42,6 +46,7 @@ module.exports = {
42
46
  ]
43
47
  }
44
48
  ],
49
+
45
50
  messages: {
46
51
  unexpectedOpeningLinebreak: "There should be no linebreak after '['.",
47
52
  unexpectedClosingLinebreak: "There should be no linebreak before ']'.",
@@ -12,13 +12,17 @@ const astUtils = require("../util/ast-utils");
12
12
 
13
13
  module.exports = {
14
14
  meta: {
15
+ type: "layout",
16
+
15
17
  docs: {
16
18
  description: "enforce consistent spacing inside array brackets",
17
19
  category: "Stylistic Issues",
18
20
  recommended: false,
19
21
  url: "https://eslint.org/docs/rules/array-bracket-spacing"
20
22
  },
23
+
21
24
  fixable: "whitespace",
25
+
22
26
  schema: [
23
27
  {
24
28
  enum: ["always", "never"]
@@ -39,6 +43,7 @@ module.exports = {
39
43
  additionalProperties: false
40
44
  }
41
45
  ],
46
+
42
47
  messages: {
43
48
  unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.",
44
49
  unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.",
@@ -141,6 +141,8 @@ function isCallbackOfArrayMethod(node) {
141
141
 
142
142
  module.exports = {
143
143
  meta: {
144
+ type: "problem",
145
+
144
146
  docs: {
145
147
  description: "enforce `return` statements in callbacks of array methods",
146
148
  category: "Best Practices",
@@ -13,13 +13,17 @@ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  module.exports = {
15
15
  meta: {
16
+ type: "layout",
17
+
16
18
  docs: {
17
19
  description: "enforce line breaks after each array element",
18
20
  category: "Stylistic Issues",
19
21
  recommended: false,
20
22
  url: "https://eslint.org/docs/rules/array-element-newline"
21
23
  },
24
+
22
25
  fixable: "whitespace",
26
+
23
27
  schema: [
24
28
  {
25
29
  oneOf: [
@@ -169,7 +173,6 @@ module.exports = {
169
173
  * Reports a given node if it violated this rule.
170
174
  *
171
175
  * @param {ASTNode} node - A node to check. This is an ObjectExpression node or an ObjectPattern node.
172
- * @param {{multiline: boolean, minItems: number}} options - An option object.
173
176
  * @returns {void}
174
177
  */
175
178
  function check(node) {
@@ -16,6 +16,8 @@ const astUtils = require("../util/ast-utils");
16
16
 
17
17
  module.exports = {
18
18
  meta: {
19
+ type: "suggestion",
20
+
19
21
  docs: {
20
22
  description: "require braces around arrow function bodies",
21
23
  category: "ECMAScript 6",
@@ -16,6 +16,8 @@ const astUtils = require("../util/ast-utils");
16
16
 
17
17
  module.exports = {
18
18
  meta: {
19
+ type: "layout",
20
+
19
21
  docs: {
20
22
  description: "require parentheses around arrow function arguments",
21
23
  category: "ECMAScript 6",
@@ -16,6 +16,8 @@ const astUtils = require("../util/ast-utils");
16
16
 
17
17
  module.exports = {
18
18
  meta: {
19
+ type: "layout",
20
+
19
21
  docs: {
20
22
  description: "enforce consistent spacing before and after the arrow in arrow functions",
21
23
  category: "ECMAScript 6",
@@ -10,6 +10,8 @@
10
10
 
11
11
  module.exports = {
12
12
  meta: {
13
+ type: "suggestion",
14
+
13
15
  docs: {
14
16
  description: "enforce the use of variables within the scope they are defined",
15
17
  category: "Best Practices",
@@ -13,6 +13,8 @@ const util = require("../util/ast-utils");
13
13
 
14
14
  module.exports = {
15
15
  meta: {
16
+ type: "layout",
17
+
16
18
  docs: {
17
19
  description: "disallow or enforce spaces inside of blocks after opening block and before closing block",
18
20
  category: "Stylistic Issues",
@@ -13,6 +13,8 @@ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  module.exports = {
15
15
  meta: {
16
+ type: "layout",
17
+
16
18
  docs: {
17
19
  description: "enforce consistent brace style for blocks",
18
20
  category: "Stylistic Issues",
@@ -10,6 +10,8 @@
10
10
 
11
11
  module.exports = {
12
12
  meta: {
13
+ type: "suggestion",
14
+
13
15
  docs: {
14
16
  description: "require `return` statements after callbacks",
15
17
  category: "Node.js and CommonJS",
@@ -11,6 +11,8 @@
11
11
 
12
12
  module.exports = {
13
13
  meta: {
14
+ type: "suggestion",
15
+
14
16
  docs: {
15
17
  description: "enforce camelcase naming convention",
16
18
  category: "Stylistic Issues",
@@ -98,14 +100,20 @@ module.exports = {
98
100
  * @private
99
101
  */
100
102
  function isInsideObjectPattern(node) {
101
- let { parent } = node;
103
+ let current = node;
104
+
105
+ while (current) {
106
+ const parent = current.parent;
107
+
108
+ if (parent && parent.type === "Property" && parent.computed && parent.key === current) {
109
+ return false;
110
+ }
102
111
 
103
- while (parent) {
104
- if (parent.type === "ObjectPattern") {
112
+ if (current.type === "ObjectPattern") {
105
113
  return true;
106
114
  }
107
115
 
108
- parent = parent.parent;
116
+ current = parent;
109
117
  }
110
118
 
111
119
  return false;
@@ -130,9 +138,10 @@ module.exports = {
130
138
 
131
139
  /*
132
140
  * Leading and trailing underscores are commonly used to flag
133
- * private/protected identifiers, strip them
141
+ * private/protected identifiers, strip them before checking if underscored
134
142
  */
135
- const name = node.name.replace(/^_+|_+$/g, ""),
143
+ const name = node.name,
144
+ nameIsUnderscored = isUnderscored(name.replace(/^_+|_+$/g, "")),
136
145
  effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
137
146
 
138
147
  // First, we ignore the node if it match the ignore list
@@ -149,11 +158,11 @@ module.exports = {
149
158
  }
150
159
 
151
160
  // Always report underscored object names
152
- if (node.parent.object.type === "Identifier" && node.parent.object.name === node.name && isUnderscored(name)) {
161
+ if (node.parent.object.type === "Identifier" && node.parent.object.name === node.name && nameIsUnderscored) {
153
162
  report(node);
154
163
 
155
164
  // Report AssignmentExpressions only if they are the left side of the assignment
156
- } else if (effectiveParent.type === "AssignmentExpression" && isUnderscored(name) && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && effectiveParent.left.property.name === node.name)) {
165
+ } else if (effectiveParent.type === "AssignmentExpression" && nameIsUnderscored && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && effectiveParent.left.property.name === node.name)) {
157
166
  report(node);
158
167
  }
159
168
 
@@ -165,19 +174,22 @@ module.exports = {
165
174
  } else if (node.parent.type === "Property" || node.parent.type === "AssignmentPattern") {
166
175
 
167
176
  if (node.parent.parent && node.parent.parent.type === "ObjectPattern") {
168
- if (node.parent.shorthand && node.parent.value.left && isUnderscored(name)) {
169
-
177
+ if (node.parent.shorthand && node.parent.value.left && nameIsUnderscored) {
170
178
  report(node);
171
179
  }
172
180
 
173
181
  const assignmentKeyEqualsValue = node.parent.key.name === node.parent.value.name;
174
182
 
183
+ if (isUnderscored(name) && node.parent.computed) {
184
+ report(node);
185
+ }
186
+
175
187
  // prevent checking righthand side of destructured object
176
188
  if (node.parent.key === node && node.parent.value !== node) {
177
189
  return;
178
190
  }
179
191
 
180
- const valueIsUnderscored = node.parent.value.name && isUnderscored(name);
192
+ const valueIsUnderscored = node.parent.value.name && nameIsUnderscored;
181
193
 
182
194
  // ignore destructuring if the option is set, unless a new identifier is created
183
195
  if (valueIsUnderscored && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
@@ -191,7 +203,7 @@ module.exports = {
191
203
  }
192
204
 
193
205
  // don't check right hand side of AssignmentExpression to prevent duplicate warnings
194
- if (isUnderscored(name) && !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && !(node.parent.right === node)) {
206
+ if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && !(node.parent.right === node)) {
195
207
  report(node);
196
208
  }
197
209
 
@@ -199,12 +211,12 @@ module.exports = {
199
211
  } else if (["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"].indexOf(node.parent.type) >= 0) {
200
212
 
201
213
  // Report only if the local imported identifier is underscored
202
- if (node.parent.local && node.parent.local.name === node.name && isUnderscored(name)) {
214
+ if (node.parent.local && node.parent.local.name === node.name && nameIsUnderscored) {
203
215
  report(node);
204
216
  }
205
217
 
206
218
  // Report anything that is underscored that isn't a CallExpression
207
- } else if (isUnderscored(name) && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) {
219
+ } else if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) {
208
220
  report(node);
209
221
  }
210
222
  }
@@ -108,13 +108,17 @@ function createRegExpForIgnorePatterns(normalizedOptions) {
108
108
 
109
109
  module.exports = {
110
110
  meta: {
111
+ type: "suggestion",
112
+
111
113
  docs: {
112
114
  description: "enforce or disallow capitalization of the first letter of a comment",
113
115
  category: "Stylistic Issues",
114
116
  recommended: false,
115
117
  url: "https://eslint.org/docs/rules/capitalized-comments"
116
118
  },
119
+
117
120
  fixable: "code",
121
+
118
122
  schema: [
119
123
  { enum: ["always", "never"] },
120
124
  {
@@ -11,12 +11,15 @@
11
11
 
12
12
  module.exports = {
13
13
  meta: {
14
+ type: "suggestion",
15
+
14
16
  docs: {
15
17
  description: "enforce that class methods utilize `this`",
16
18
  category: "Best Practices",
17
19
  recommended: false,
18
20
  url: "https://eslint.org/docs/rules/class-methods-use-this"
19
21
  },
22
+
20
23
  schema: [{
21
24
  type: "object",
22
25
  properties: {
@@ -76,13 +76,17 @@ function normalizeOptions(optionValue) {
76
76
 
77
77
  module.exports = {
78
78
  meta: {
79
+ type: "layout",
80
+
79
81
  docs: {
80
82
  description: "require or disallow trailing commas",
81
83
  category: "Stylistic Issues",
82
84
  recommended: false,
83
85
  url: "https://eslint.org/docs/rules/comma-dangle"
84
86
  },
87
+
85
88
  fixable: "code",
89
+
86
90
  schema: {
87
91
  definitions: {
88
92
  value: {
@@ -12,6 +12,8 @@ const astUtils = require("../util/ast-utils");
12
12
 
13
13
  module.exports = {
14
14
  meta: {
15
+ type: "layout",
16
+
15
17
  docs: {
16
18
  description: "enforce consistent spacing before and after commas",
17
19
  category: "Stylistic Issues",
@@ -13,13 +13,17 @@ const astUtils = require("../util/ast-utils");
13
13
 
14
14
  module.exports = {
15
15
  meta: {
16
+ type: "layout",
17
+
16
18
  docs: {
17
19
  description: "enforce consistent comma style",
18
20
  category: "Stylistic Issues",
19
21
  recommended: false,
20
22
  url: "https://eslint.org/docs/rules/comma-style"
21
23
  },
24
+
22
25
  fixable: "code",
26
+
23
27
  schema: [
24
28
  {
25
29
  enum: ["first", "last"]
@@ -37,6 +41,7 @@ module.exports = {
37
41
  additionalProperties: false
38
42
  }
39
43
  ],
44
+
40
45
  messages: {
41
46
  unexpectedLineBeforeAndAfterComma: "Bad line breaking before and after ','.",
42
47
  expectedCommaFirst: "',' should be placed first.",
@@ -80,7 +85,7 @@ module.exports = {
80
85
  function getReplacedText(styleType, text) {
81
86
  switch (styleType) {
82
87
  case "between":
83
- return `,${text.replace("\n", "")}`;
88
+ return `,${text.replace(astUtils.LINEBREAK_MATCHER, "")}`;
84
89
 
85
90
  case "first":
86
91
  return `${text},`;
@@ -133,6 +138,11 @@ module.exports = {
133
138
  } else if (!astUtils.isTokenOnSameLine(commaToken, currentItemToken) &&
134
139
  !astUtils.isTokenOnSameLine(previousItemToken, commaToken)) {
135
140
 
141
+ const comment = sourceCode.getCommentsAfter(commaToken)[0];
142
+ const styleType = comment && comment.type === "Block" && astUtils.isTokenOnSameLine(commaToken, comment)
143
+ ? style
144
+ : "between";
145
+
136
146
  // lone comma
137
147
  context.report({
138
148
  node: reportItem,
@@ -141,7 +151,7 @@ module.exports = {
141
151
  column: commaToken.loc.start.column
142
152
  },
143
153
  messageId: "unexpectedLineBeforeAndAfterComma",
144
- fix: getFixerFunction("between", previousItemToken, commaToken, currentItemToken)
154
+ fix: getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken)
145
155
  });
146
156
 
147
157
  } else if (style === "first" && !astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {
@@ -20,6 +20,8 @@ const astUtils = require("../util/ast-utils");
20
20
 
21
21
  module.exports = {
22
22
  meta: {
23
+ type: "suggestion",
24
+
23
25
  docs: {
24
26
  description: "enforce a maximum cyclomatic complexity allowed in a program",
25
27
  category: "Best Practices",
@@ -12,6 +12,8 @@ const astUtils = require("../util/ast-utils");
12
12
 
13
13
  module.exports = {
14
14
  meta: {
15
+ type: "layout",
16
+
15
17
  docs: {
16
18
  description: "enforce consistent spacing inside computed property brackets",
17
19
  category: "Stylistic Issues",
@@ -53,6 +53,8 @@ function isClassConstructor(node) {
53
53
 
54
54
  module.exports = {
55
55
  meta: {
56
+ type: "suggestion",
57
+
56
58
  docs: {
57
59
  description: "require `return` statements to either always or never specify values",
58
60
  category: "Best Practices",