eslint 6.6.0 → 6.8.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 (62) hide show
  1. package/CHANGELOG.md +94 -0
  2. package/README.md +7 -8
  3. package/conf/config-schema.js +2 -0
  4. package/conf/default-cli-options.js +1 -1
  5. package/conf/eslint-recommended.js +0 -1
  6. package/lib/cli-engine/cascading-config-array-factory.js +38 -13
  7. package/lib/cli-engine/cli-engine.js +41 -14
  8. package/lib/cli-engine/config-array/config-array.js +13 -0
  9. package/lib/cli-engine/config-array/extracted-config.js +27 -0
  10. package/lib/cli-engine/config-array/ignore-pattern.js +231 -0
  11. package/lib/cli-engine/config-array/index.js +2 -0
  12. package/lib/cli-engine/config-array-factory.js +115 -1
  13. package/lib/cli-engine/file-enumerator.js +73 -40
  14. package/lib/cli-engine/lint-result-cache.js +2 -1
  15. package/lib/cli.js +2 -1
  16. package/lib/init/config-initializer.js +4 -3
  17. package/lib/linter/config-comment-parser.js +1 -1
  18. package/lib/linter/report-translator.js +73 -7
  19. package/lib/options.js +6 -0
  20. package/lib/rule-tester/rule-tester.js +42 -6
  21. package/lib/rules/array-bracket-spacing.js +8 -8
  22. package/lib/rules/camelcase.js +19 -6
  23. package/lib/rules/comma-dangle.js +5 -2
  24. package/lib/rules/computed-property-spacing.js +4 -4
  25. package/lib/rules/curly.js +9 -4
  26. package/lib/rules/function-call-argument-newline.js +3 -1
  27. package/lib/rules/grouped-accessor-pairs.js +224 -0
  28. package/lib/rules/indent.js +11 -0
  29. package/lib/rules/index.js +5 -0
  30. package/lib/rules/key-spacing.js +34 -15
  31. package/lib/rules/lines-between-class-members.js +42 -53
  32. package/lib/rules/multiline-comment-style.js +237 -106
  33. package/lib/rules/no-cond-assign.js +14 -4
  34. package/lib/rules/no-constructor-return.js +62 -0
  35. package/lib/rules/no-dupe-else-if.js +122 -0
  36. package/lib/rules/no-implicit-globals.js +90 -8
  37. package/lib/rules/no-inline-comments.js +25 -11
  38. package/lib/rules/no-invalid-this.js +16 -2
  39. package/lib/rules/no-multiple-empty-lines.js +1 -1
  40. package/lib/rules/no-octal-escape.js +1 -1
  41. package/lib/rules/no-restricted-imports.js +2 -2
  42. package/lib/rules/no-setter-return.js +227 -0
  43. package/lib/rules/no-underscore-dangle.js +23 -4
  44. package/lib/rules/no-unexpected-multiline.js +8 -0
  45. package/lib/rules/no-unsafe-negation.js +30 -5
  46. package/lib/rules/no-useless-computed-key.js +60 -33
  47. package/lib/rules/no-useless-escape.js +26 -3
  48. package/lib/rules/object-curly-spacing.js +8 -8
  49. package/lib/rules/operator-assignment.js +11 -2
  50. package/lib/rules/prefer-const.js +14 -7
  51. package/lib/rules/prefer-exponentiation-operator.js +189 -0
  52. package/lib/rules/prefer-numeric-literals.js +29 -28
  53. package/lib/rules/require-atomic-updates.js +1 -1
  54. package/lib/rules/require-await.js +8 -0
  55. package/lib/rules/semi.js +6 -3
  56. package/lib/rules/space-infix-ops.js +1 -1
  57. package/lib/rules/spaced-comment.js +5 -4
  58. package/lib/rules/utils/ast-utils.js +31 -4
  59. package/lib/shared/types.js +9 -0
  60. package/lib/source-code/source-code.js +87 -10
  61. package/package.json +4 -3
  62. package/lib/cli-engine/ignored-paths.js +0 -363
@@ -1,363 +0,0 @@
1
- /**
2
- * @fileoverview Responsible for loading ignore config files and managing ignore patterns
3
- * @author Jonathan Rajavuori
4
- */
5
-
6
- "use strict";
7
-
8
- //------------------------------------------------------------------------------
9
- // Requirements
10
- //------------------------------------------------------------------------------
11
-
12
- const fs = require("fs"),
13
- path = require("path"),
14
- ignore = require("ignore");
15
-
16
- const debug = require("debug")("eslint:ignored-paths");
17
-
18
- //------------------------------------------------------------------------------
19
- // Constants
20
- //------------------------------------------------------------------------------
21
-
22
- const ESLINT_IGNORE_FILENAME = ".eslintignore";
23
-
24
- /**
25
- * Adds `"*"` at the end of `"node_modules/"`,
26
- * so that subtle directories could be re-included by .gitignore patterns
27
- * such as `"!node_modules/should_not_ignored"`
28
- */
29
- const DEFAULT_IGNORE_DIRS = [
30
- "/node_modules/*",
31
- "/bower_components/*"
32
- ];
33
- const DEFAULT_OPTIONS = {
34
- dotfiles: false,
35
- cwd: process.cwd()
36
- };
37
-
38
- //------------------------------------------------------------------------------
39
- // Helpers
40
- //------------------------------------------------------------------------------
41
-
42
- /**
43
- * Find a file in the current directory.
44
- * @param {string} cwd Current working directory
45
- * @param {string} name File name
46
- * @returns {string} Path of ignore file or an empty string.
47
- */
48
- function findFile(cwd, name) {
49
- const ignoreFilePath = path.resolve(cwd, name);
50
-
51
- return fs.existsSync(ignoreFilePath) && fs.statSync(ignoreFilePath).isFile() ? ignoreFilePath : "";
52
- }
53
-
54
- /**
55
- * Find an ignore file in the current directory.
56
- * @param {string} cwd Current working directory
57
- * @returns {string} Path of ignore file or an empty string.
58
- */
59
- function findIgnoreFile(cwd) {
60
- return findFile(cwd, ESLINT_IGNORE_FILENAME);
61
- }
62
-
63
- /**
64
- * Find an package.json file in the current directory.
65
- * @param {string} cwd Current working directory
66
- * @returns {string} Path of package.json file or an empty string.
67
- */
68
- function findPackageJSONFile(cwd) {
69
- return findFile(cwd, "package.json");
70
- }
71
-
72
- /**
73
- * Merge options with defaults
74
- * @param {Object} options Options to merge with DEFAULT_OPTIONS constant
75
- * @returns {Object} Merged options
76
- */
77
- function mergeDefaultOptions(options) {
78
- return Object.assign({}, DEFAULT_OPTIONS, options);
79
- }
80
-
81
- /* eslint-disable jsdoc/check-param-names, jsdoc/require-param */
82
- /**
83
- * Normalize the path separators in a given string.
84
- * On Windows environment, this replaces `\` by `/`.
85
- * Otherwrise, this does nothing.
86
- * @param {string} str The path string to normalize.
87
- * @returns {string} The normalized path.
88
- */
89
- const normalizePathSeps = path.sep === "/"
90
- ? (str => str)
91
- : ((seps, str) => str.replace(seps, "/")).bind(null, new RegExp(`\\${path.sep}`, "gu"));
92
- /* eslint-enable jsdoc/check-param-names, jsdoc/require-param */
93
-
94
- /**
95
- * Converts a glob pattern to a new glob pattern relative to a different directory
96
- * @param {string} globPattern The glob pattern, relative the the old base directory
97
- * @param {string} relativePathToOldBaseDir A relative path from the new base directory to the old one
98
- * @returns {string} A glob pattern relative to the new base directory
99
- */
100
- function relativize(globPattern, relativePathToOldBaseDir) {
101
- if (relativePathToOldBaseDir === "") {
102
- return globPattern;
103
- }
104
-
105
- const prefix = globPattern.startsWith("!") ? "!" : "";
106
- const globWithoutPrefix = globPattern.replace(/^!/u, "");
107
-
108
- if (globWithoutPrefix.startsWith("/")) {
109
- return `${prefix}/${normalizePathSeps(relativePathToOldBaseDir)}${globWithoutPrefix}`;
110
- }
111
-
112
- return globPattern;
113
- }
114
-
115
- //------------------------------------------------------------------------------
116
- // Public Interface
117
- //------------------------------------------------------------------------------
118
-
119
- /**
120
- * IgnoredPaths class
121
- */
122
- class IgnoredPaths {
123
-
124
- // eslint-disable-next-line jsdoc/require-description
125
- /**
126
- * @param {Object} providedOptions object containing 'ignore', 'ignorePath' and 'patterns' properties
127
- */
128
- constructor(providedOptions) {
129
- const options = mergeDefaultOptions(providedOptions);
130
-
131
- this.cache = {};
132
-
133
- this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []);
134
-
135
- this.ignoreFileDir = options.ignore !== false && options.ignorePath
136
- ? path.dirname(path.resolve(options.cwd, options.ignorePath))
137
- : options.cwd;
138
- this.options = options;
139
- this._baseDir = null;
140
-
141
- this.ig = {
142
- custom: ignore(),
143
- default: ignore()
144
- };
145
-
146
- this.defaultPatterns.forEach(pattern => this.addPatternRelativeToCwd(this.ig.default, pattern));
147
- if (options.dotfiles !== true) {
148
-
149
- /*
150
- * ignore files beginning with a dot, but not files in a parent or
151
- * ancestor directory (which in relative format will begin with `../`).
152
- */
153
- this.addPatternRelativeToCwd(this.ig.default, ".*");
154
- this.addPatternRelativeToCwd(this.ig.default, "!../");
155
- }
156
-
157
- /*
158
- * Add a way to keep track of ignored files. This was present in node-ignore
159
- * 2.x, but dropped for now as of 3.0.10.
160
- */
161
- this.ig.custom.ignoreFiles = [];
162
- this.ig.default.ignoreFiles = [];
163
-
164
- if (options.ignore !== false) {
165
- let ignorePath;
166
-
167
- if (options.ignorePath) {
168
- debug("Using specific ignore file");
169
-
170
- try {
171
- const stat = fs.statSync(options.ignorePath);
172
-
173
- if (!stat.isFile()) {
174
- throw new Error(`${options.ignorePath} is not a file`);
175
- }
176
- ignorePath = options.ignorePath;
177
- } catch (e) {
178
- e.message = `Cannot read ignore file: ${options.ignorePath}\nError: ${e.message}`;
179
- throw e;
180
- }
181
- } else {
182
- debug(`Looking for ignore file in ${options.cwd}`);
183
- ignorePath = findIgnoreFile(options.cwd);
184
-
185
- try {
186
- fs.statSync(ignorePath);
187
- debug(`Loaded ignore file ${ignorePath}`);
188
- } catch (e) {
189
- debug("Could not find ignore file in cwd");
190
- }
191
- }
192
-
193
- if (ignorePath) {
194
- debug(`Adding ${ignorePath}`);
195
- this.addIgnoreFile(this.ig.custom, ignorePath);
196
- this.addIgnoreFile(this.ig.default, ignorePath);
197
- } else {
198
- try {
199
-
200
- // if the ignoreFile does not exist, check package.json for eslintIgnore
201
- const packageJSONPath = findPackageJSONFile(options.cwd);
202
-
203
- if (packageJSONPath) {
204
- let packageJSONOptions;
205
-
206
- try {
207
- packageJSONOptions = JSON.parse(fs.readFileSync(packageJSONPath, "utf8"));
208
- } catch (e) {
209
- debug("Could not read package.json file to check eslintIgnore property");
210
- e.messageTemplate = "failed-to-read-json";
211
- e.messageData = {
212
- path: packageJSONPath,
213
- message: e.message
214
- };
215
- throw e;
216
- }
217
-
218
- if (packageJSONOptions.eslintIgnore) {
219
- if (Array.isArray(packageJSONOptions.eslintIgnore)) {
220
- packageJSONOptions.eslintIgnore.forEach(pattern => {
221
- this.addPatternRelativeToIgnoreFile(this.ig.custom, pattern);
222
- this.addPatternRelativeToIgnoreFile(this.ig.default, pattern);
223
- });
224
- } else {
225
- throw new TypeError("Package.json eslintIgnore property requires an array of paths");
226
- }
227
- }
228
- }
229
- } catch (e) {
230
- debug("Could not find package.json to check eslintIgnore property");
231
- throw e;
232
- }
233
- }
234
-
235
- if (options.ignorePattern) {
236
- this.addPatternRelativeToCwd(this.ig.custom, options.ignorePattern);
237
- this.addPatternRelativeToCwd(this.ig.default, options.ignorePattern);
238
- }
239
- }
240
- }
241
-
242
- /*
243
- * If `ignoreFileDir` is a subdirectory of `cwd`, all paths will be normalized to be relative to `cwd`.
244
- * Otherwise, all paths will be normalized to be relative to `ignoreFileDir`.
245
- * This ensures that the final normalized ignore rule will not contain `..`, which is forbidden in
246
- * ignore rules.
247
- */
248
-
249
- addPatternRelativeToCwd(ig, pattern) {
250
- const baseDir = this.getBaseDir();
251
- const cookedPattern = baseDir === this.options.cwd
252
- ? pattern
253
- : relativize(pattern, path.relative(baseDir, this.options.cwd));
254
-
255
- ig.addPattern(cookedPattern);
256
- debug("addPatternRelativeToCwd:\n original = %j\n cooked = %j", pattern, cookedPattern);
257
- }
258
-
259
- addPatternRelativeToIgnoreFile(ig, pattern) {
260
- const baseDir = this.getBaseDir();
261
- const cookedPattern = baseDir === this.ignoreFileDir
262
- ? pattern
263
- : relativize(pattern, path.relative(baseDir, this.ignoreFileDir));
264
-
265
- ig.addPattern(cookedPattern);
266
- debug("addPatternRelativeToIgnoreFile:\n original = %j\n cooked = %j", pattern, cookedPattern);
267
- }
268
-
269
- // Detect the common ancestor
270
- getBaseDir() {
271
- if (!this._baseDir) {
272
- const a = path.resolve(this.options.cwd);
273
- const b = path.resolve(this.ignoreFileDir);
274
- let lastSepPos = 0;
275
-
276
- // Set the shorter one (it's the common ancestor if one includes the other).
277
- this._baseDir = a.length < b.length ? a : b;
278
-
279
- // Set the common ancestor.
280
- for (let i = 0; i < a.length && i < b.length; ++i) {
281
- if (a[i] !== b[i]) {
282
- this._baseDir = a.slice(0, lastSepPos);
283
- break;
284
- }
285
- if (a[i] === path.sep) {
286
- lastSepPos = i;
287
- }
288
- }
289
-
290
- // If it's only Windows drive letter, it needs \
291
- if (/^[A-Z]:$/u.test(this._baseDir)) {
292
- this._baseDir += "\\";
293
- }
294
-
295
- debug("baseDir = %j", this._baseDir);
296
- }
297
- return this._baseDir;
298
- }
299
-
300
- /**
301
- * read ignore filepath
302
- * @param {string} filePath file to add to ig
303
- * @returns {Array} raw ignore rules
304
- */
305
- readIgnoreFile(filePath) {
306
- if (typeof this.cache[filePath] === "undefined") {
307
- this.cache[filePath] = fs.readFileSync(filePath, "utf8").split(/\r?\n/gu).filter(Boolean);
308
- }
309
- return this.cache[filePath];
310
- }
311
-
312
- /**
313
- * add ignore file to node-ignore instance
314
- * @param {Object} ig instance of node-ignore
315
- * @param {string} filePath file to add to ig
316
- * @returns {void}
317
- */
318
- addIgnoreFile(ig, filePath) {
319
- ig.ignoreFiles.push(filePath);
320
- this
321
- .readIgnoreFile(filePath)
322
- .forEach(ignoreRule => this.addPatternRelativeToIgnoreFile(ig, ignoreRule));
323
- }
324
-
325
- /**
326
- * Determine whether a file path is included in the default or custom ignore patterns
327
- * @param {string} filepath Path to check
328
- * @param {string} [category=undefined] check 'default', 'custom' or both (undefined)
329
- * @returns {boolean} true if the file path matches one or more patterns, false otherwise
330
- */
331
- contains(filepath, category) {
332
- const isDir = filepath.endsWith(path.sep) ||
333
- (path.sep === "\\" && filepath.endsWith("/"));
334
- let result = false;
335
- const basePath = this.getBaseDir();
336
- const absolutePath = path.resolve(this.options.cwd, filepath);
337
- let relativePath = path.relative(basePath, absolutePath);
338
-
339
- if (relativePath) {
340
- if (isDir) {
341
- relativePath += path.sep;
342
- }
343
- if (typeof category === "undefined") {
344
- result =
345
- (this.ig.default.filter([relativePath]).length === 0) ||
346
- (this.ig.custom.filter([relativePath]).length === 0);
347
- } else {
348
- result =
349
- (this.ig[category].filter([relativePath]).length === 0);
350
- }
351
- }
352
- debug("contains:");
353
- debug(" target = %j", filepath);
354
- debug(" base = %j", basePath);
355
- debug(" relative = %j", relativePath);
356
- debug(" result = %j", result);
357
-
358
- return result;
359
-
360
- }
361
- }
362
-
363
- module.exports = { IgnoredPaths };