eslint 8.4.0 → 8.7.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.
- package/README.md +3 -3
- package/bin/eslint.js +7 -1
- package/lib/cli-engine/cli-engine.js +4 -1
- package/lib/eslint/eslint.js +1 -0
- package/lib/linter/apply-disable-directives.js +20 -16
- package/lib/linter/linter.js +8 -6
- package/lib/rule-tester/rule-tester.js +19 -5
- package/lib/rules/camelcase.js +7 -1
- package/lib/rules/id-match.js +35 -1
- package/lib/rules/index.js +1 -0
- package/lib/rules/keyword-spacing.js +32 -0
- package/lib/rules/max-lines-per-function.js +2 -20
- package/lib/rules/no-constant-condition.js +23 -4
- package/lib/rules/no-invalid-this.js +50 -53
- package/lib/rules/no-restricted-exports.js +9 -3
- package/lib/rules/no-restricted-imports.js +24 -7
- package/lib/rules/no-restricted-modules.js +2 -1
- package/lib/rules/no-self-assign.js +1 -1
- package/lib/rules/no-useless-rename.js +8 -4
- package/lib/rules/prefer-object-has-own.js +112 -0
- package/lib/rules/prefer-regex-literals.js +217 -1
- package/lib/rules/prefer-template.js +1 -1
- package/lib/rules/quotes.js +12 -1
- package/lib/rules/utils/ast-utils.js +21 -1
- package/messages/no-config-found.js +1 -1
- package/package.json +7 -7
- package/lib/init/autoconfig.js +0 -351
- package/lib/init/config-file.js +0 -144
- package/lib/init/config-initializer.js +0 -709
- package/lib/init/config-rule.js +0 -316
- package/lib/init/npm-utils.js +0 -179
- package/lib/init/source-code-utils.js +0 -110
package/lib/init/config-rule.js
DELETED
@@ -1,316 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @fileoverview Create configurations for a rule
|
3
|
-
* @author Ian VanSchooten
|
4
|
-
*/
|
5
|
-
|
6
|
-
"use strict";
|
7
|
-
|
8
|
-
//------------------------------------------------------------------------------
|
9
|
-
// Requirements
|
10
|
-
//------------------------------------------------------------------------------
|
11
|
-
|
12
|
-
const builtInRules = require("../rules");
|
13
|
-
|
14
|
-
//------------------------------------------------------------------------------
|
15
|
-
// Helpers
|
16
|
-
//------------------------------------------------------------------------------
|
17
|
-
|
18
|
-
/**
|
19
|
-
* Wrap all of the elements of an array into arrays.
|
20
|
-
* @param {*[]} xs Any array.
|
21
|
-
* @returns {Array[]} An array of arrays.
|
22
|
-
*/
|
23
|
-
function explodeArray(xs) {
|
24
|
-
return xs.reduce((accumulator, x) => {
|
25
|
-
accumulator.push([x]);
|
26
|
-
return accumulator;
|
27
|
-
}, []);
|
28
|
-
}
|
29
|
-
|
30
|
-
/**
|
31
|
-
* Mix two arrays such that each element of the second array is concatenated
|
32
|
-
* onto each element of the first array.
|
33
|
-
*
|
34
|
-
* For example:
|
35
|
-
* combineArrays([a, [b, c]], [x, y]); // -> [[a, x], [a, y], [b, c, x], [b, c, y]]
|
36
|
-
* @param {Array} arr1 The first array to combine.
|
37
|
-
* @param {Array} arr2 The second array to combine.
|
38
|
-
* @returns {Array} A mixture of the elements of the first and second arrays.
|
39
|
-
*/
|
40
|
-
function combineArrays(arr1, arr2) {
|
41
|
-
const res = [];
|
42
|
-
|
43
|
-
if (arr1.length === 0) {
|
44
|
-
return explodeArray(arr2);
|
45
|
-
}
|
46
|
-
if (arr2.length === 0) {
|
47
|
-
return explodeArray(arr1);
|
48
|
-
}
|
49
|
-
arr1.forEach(x1 => {
|
50
|
-
arr2.forEach(x2 => {
|
51
|
-
res.push([].concat(x1, x2));
|
52
|
-
});
|
53
|
-
});
|
54
|
-
return res;
|
55
|
-
}
|
56
|
-
|
57
|
-
/**
|
58
|
-
* Group together valid rule configurations based on object properties
|
59
|
-
*
|
60
|
-
* e.g.:
|
61
|
-
* groupByProperty([
|
62
|
-
* {before: true},
|
63
|
-
* {before: false},
|
64
|
-
* {after: true},
|
65
|
-
* {after: false}
|
66
|
-
* ]);
|
67
|
-
*
|
68
|
-
* will return:
|
69
|
-
* [
|
70
|
-
* [{before: true}, {before: false}],
|
71
|
-
* [{after: true}, {after: false}]
|
72
|
-
* ]
|
73
|
-
* @param {Object[]} objects Array of objects, each with one property/value pair
|
74
|
-
* @returns {Array[]} Array of arrays of objects grouped by property
|
75
|
-
*/
|
76
|
-
function groupByProperty(objects) {
|
77
|
-
const groupedObj = objects.reduce((accumulator, obj) => {
|
78
|
-
const prop = Object.keys(obj)[0];
|
79
|
-
|
80
|
-
accumulator[prop] = accumulator[prop] ? accumulator[prop].concat(obj) : [obj];
|
81
|
-
return accumulator;
|
82
|
-
}, {});
|
83
|
-
|
84
|
-
return Object.keys(groupedObj).map(prop => groupedObj[prop]);
|
85
|
-
}
|
86
|
-
|
87
|
-
|
88
|
-
//------------------------------------------------------------------------------
|
89
|
-
// Private
|
90
|
-
//------------------------------------------------------------------------------
|
91
|
-
|
92
|
-
/**
|
93
|
-
* Configuration settings for a rule.
|
94
|
-
*
|
95
|
-
* A configuration can be a single number (severity), or an array where the first
|
96
|
-
* element in the array is the severity, and is the only required element.
|
97
|
-
* Configs may also have one or more additional elements to specify rule
|
98
|
-
* configuration or options.
|
99
|
-
* @typedef {Array|number} ruleConfig
|
100
|
-
* @param {number} 0 The rule's severity (0, 1, 2).
|
101
|
-
*/
|
102
|
-
|
103
|
-
/**
|
104
|
-
* Object whose keys are rule names and values are arrays of valid ruleConfig items
|
105
|
-
* which should be linted against the target source code to determine error counts.
|
106
|
-
* (a ruleConfigSet.ruleConfigs).
|
107
|
-
*
|
108
|
-
* e.g. rulesConfig = {
|
109
|
-
* "comma-dangle": [2, [2, "always"], [2, "always-multiline"], [2, "never"]],
|
110
|
-
* "no-console": [2]
|
111
|
-
* }
|
112
|
-
* @typedef rulesConfig
|
113
|
-
*/
|
114
|
-
|
115
|
-
|
116
|
-
/**
|
117
|
-
* Create valid rule configurations by combining two arrays,
|
118
|
-
* with each array containing multiple objects each with a
|
119
|
-
* single property/value pair and matching properties.
|
120
|
-
*
|
121
|
-
* e.g.:
|
122
|
-
* combinePropertyObjects(
|
123
|
-
* [{before: true}, {before: false}],
|
124
|
-
* [{after: true}, {after: false}]
|
125
|
-
* );
|
126
|
-
*
|
127
|
-
* will return:
|
128
|
-
* [
|
129
|
-
* {before: true, after: true},
|
130
|
-
* {before: true, after: false},
|
131
|
-
* {before: false, after: true},
|
132
|
-
* {before: false, after: false}
|
133
|
-
* ]
|
134
|
-
* @param {Object[]} objArr1 Single key/value objects, all with the same key
|
135
|
-
* @param {Object[]} objArr2 Single key/value objects, all with another key
|
136
|
-
* @returns {Object[]} Combined objects for each combination of input properties and values
|
137
|
-
*/
|
138
|
-
function combinePropertyObjects(objArr1, objArr2) {
|
139
|
-
const res = [];
|
140
|
-
|
141
|
-
if (objArr1.length === 0) {
|
142
|
-
return objArr2;
|
143
|
-
}
|
144
|
-
if (objArr2.length === 0) {
|
145
|
-
return objArr1;
|
146
|
-
}
|
147
|
-
objArr1.forEach(obj1 => {
|
148
|
-
objArr2.forEach(obj2 => {
|
149
|
-
const combinedObj = {};
|
150
|
-
const obj1Props = Object.keys(obj1);
|
151
|
-
const obj2Props = Object.keys(obj2);
|
152
|
-
|
153
|
-
obj1Props.forEach(prop1 => {
|
154
|
-
combinedObj[prop1] = obj1[prop1];
|
155
|
-
});
|
156
|
-
obj2Props.forEach(prop2 => {
|
157
|
-
combinedObj[prop2] = obj2[prop2];
|
158
|
-
});
|
159
|
-
res.push(combinedObj);
|
160
|
-
});
|
161
|
-
});
|
162
|
-
return res;
|
163
|
-
}
|
164
|
-
|
165
|
-
/**
|
166
|
-
* Creates a new instance of a rule configuration set
|
167
|
-
*
|
168
|
-
* A rule configuration set is an array of configurations that are valid for a
|
169
|
-
* given rule. For example, the configuration set for the "semi" rule could be:
|
170
|
-
*
|
171
|
-
* ruleConfigSet.ruleConfigs // -> [[2], [2, "always"], [2, "never"]]
|
172
|
-
*
|
173
|
-
* Rule configuration set class
|
174
|
-
*/
|
175
|
-
class RuleConfigSet {
|
176
|
-
|
177
|
-
/**
|
178
|
-
* @param {ruleConfig[]} configs Valid rule configurations
|
179
|
-
*/
|
180
|
-
constructor(configs) {
|
181
|
-
|
182
|
-
/**
|
183
|
-
* Stored valid rule configurations for this instance
|
184
|
-
* @type {Array}
|
185
|
-
*/
|
186
|
-
this.ruleConfigs = configs || [];
|
187
|
-
}
|
188
|
-
|
189
|
-
/**
|
190
|
-
* Add a severity level to the front of all configs in the instance.
|
191
|
-
* This should only be called after all configs have been added to the instance.
|
192
|
-
* @returns {void}
|
193
|
-
*/
|
194
|
-
addErrorSeverity() {
|
195
|
-
const severity = 2;
|
196
|
-
|
197
|
-
this.ruleConfigs = this.ruleConfigs.map(config => {
|
198
|
-
config.unshift(severity);
|
199
|
-
return config;
|
200
|
-
});
|
201
|
-
|
202
|
-
// Add a single config at the beginning consisting of only the severity
|
203
|
-
this.ruleConfigs.unshift(severity);
|
204
|
-
}
|
205
|
-
|
206
|
-
/**
|
207
|
-
* Add rule configs from an array of strings (schema enums)
|
208
|
-
* @param {string[]} enums Array of valid rule options (e.g. ["always", "never"])
|
209
|
-
* @returns {void}
|
210
|
-
*/
|
211
|
-
addEnums(enums) {
|
212
|
-
this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, enums));
|
213
|
-
}
|
214
|
-
|
215
|
-
/**
|
216
|
-
* Add rule configurations from a schema object
|
217
|
-
* @param {Object} obj Schema item with type === "object"
|
218
|
-
* @returns {boolean} true if at least one schema for the object could be generated, false otherwise
|
219
|
-
*/
|
220
|
-
addObject(obj) {
|
221
|
-
const objectConfigSet = {
|
222
|
-
objectConfigs: [],
|
223
|
-
add(property, values) {
|
224
|
-
for (let idx = 0; idx < values.length; idx++) {
|
225
|
-
const optionObj = {};
|
226
|
-
|
227
|
-
optionObj[property] = values[idx];
|
228
|
-
this.objectConfigs.push(optionObj);
|
229
|
-
}
|
230
|
-
},
|
231
|
-
|
232
|
-
combine() {
|
233
|
-
this.objectConfigs = groupByProperty(this.objectConfigs).reduce((accumulator, objArr) => combinePropertyObjects(accumulator, objArr), []);
|
234
|
-
}
|
235
|
-
};
|
236
|
-
|
237
|
-
/*
|
238
|
-
* The object schema could have multiple independent properties.
|
239
|
-
* If any contain enums or booleans, they can be added and then combined
|
240
|
-
*/
|
241
|
-
Object.keys(obj.properties).forEach(prop => {
|
242
|
-
if (obj.properties[prop].enum) {
|
243
|
-
objectConfigSet.add(prop, obj.properties[prop].enum);
|
244
|
-
}
|
245
|
-
if (obj.properties[prop].type && obj.properties[prop].type === "boolean") {
|
246
|
-
objectConfigSet.add(prop, [true, false]);
|
247
|
-
}
|
248
|
-
});
|
249
|
-
objectConfigSet.combine();
|
250
|
-
|
251
|
-
if (objectConfigSet.objectConfigs.length > 0) {
|
252
|
-
this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, objectConfigSet.objectConfigs));
|
253
|
-
return true;
|
254
|
-
}
|
255
|
-
|
256
|
-
return false;
|
257
|
-
}
|
258
|
-
}
|
259
|
-
|
260
|
-
/**
|
261
|
-
* Generate valid rule configurations based on a schema object
|
262
|
-
* @param {Object} schema A rule's schema object
|
263
|
-
* @returns {Array[]} Valid rule configurations
|
264
|
-
*/
|
265
|
-
function generateConfigsFromSchema(schema) {
|
266
|
-
const configSet = new RuleConfigSet();
|
267
|
-
|
268
|
-
if (Array.isArray(schema)) {
|
269
|
-
for (const opt of schema) {
|
270
|
-
if (opt.enum) {
|
271
|
-
configSet.addEnums(opt.enum);
|
272
|
-
} else if (opt.type && opt.type === "object") {
|
273
|
-
if (!configSet.addObject(opt)) {
|
274
|
-
break;
|
275
|
-
}
|
276
|
-
|
277
|
-
// TODO (IanVS): support oneOf
|
278
|
-
} else {
|
279
|
-
|
280
|
-
// If we don't know how to fill in this option, don't fill in any of the following options.
|
281
|
-
break;
|
282
|
-
}
|
283
|
-
}
|
284
|
-
}
|
285
|
-
configSet.addErrorSeverity();
|
286
|
-
return configSet.ruleConfigs;
|
287
|
-
}
|
288
|
-
|
289
|
-
/**
|
290
|
-
* Generate possible rule configurations for all of the core rules
|
291
|
-
* @param {boolean} noDeprecated Indicates whether ignores deprecated rules or not.
|
292
|
-
* @returns {rulesConfig} Hash of rule names and arrays of possible configurations
|
293
|
-
*/
|
294
|
-
function createCoreRuleConfigs(noDeprecated = false) {
|
295
|
-
return Array.from(builtInRules).reduce((accumulator, [id, rule]) => {
|
296
|
-
const schema = (typeof rule === "function") ? rule.schema : rule.meta.schema;
|
297
|
-
const isDeprecated = (typeof rule === "function") ? rule.deprecated : rule.meta.deprecated;
|
298
|
-
|
299
|
-
if (noDeprecated && isDeprecated) {
|
300
|
-
return accumulator;
|
301
|
-
}
|
302
|
-
|
303
|
-
accumulator[id] = generateConfigsFromSchema(schema);
|
304
|
-
return accumulator;
|
305
|
-
}, {});
|
306
|
-
}
|
307
|
-
|
308
|
-
|
309
|
-
//------------------------------------------------------------------------------
|
310
|
-
// Public Interface
|
311
|
-
//------------------------------------------------------------------------------
|
312
|
-
|
313
|
-
module.exports = {
|
314
|
-
generateConfigsFromSchema,
|
315
|
-
createCoreRuleConfigs
|
316
|
-
};
|
package/lib/init/npm-utils.js
DELETED
@@ -1,179 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @fileoverview Utility for executing npm commands.
|
3
|
-
* @author Ian VanSchooten
|
4
|
-
*/
|
5
|
-
|
6
|
-
"use strict";
|
7
|
-
|
8
|
-
//------------------------------------------------------------------------------
|
9
|
-
// Requirements
|
10
|
-
//------------------------------------------------------------------------------
|
11
|
-
|
12
|
-
const fs = require("fs"),
|
13
|
-
spawn = require("cross-spawn"),
|
14
|
-
path = require("path"),
|
15
|
-
log = require("../shared/logging");
|
16
|
-
|
17
|
-
//------------------------------------------------------------------------------
|
18
|
-
// Helpers
|
19
|
-
//------------------------------------------------------------------------------
|
20
|
-
|
21
|
-
/**
|
22
|
-
* Find the closest package.json file, starting at process.cwd (by default),
|
23
|
-
* and working up to root.
|
24
|
-
* @param {string} [startDir=process.cwd()] Starting directory
|
25
|
-
* @returns {string} Absolute path to closest package.json file
|
26
|
-
*/
|
27
|
-
function findPackageJson(startDir) {
|
28
|
-
let dir = path.resolve(startDir || process.cwd());
|
29
|
-
|
30
|
-
do {
|
31
|
-
const pkgFile = path.join(dir, "package.json");
|
32
|
-
|
33
|
-
if (!fs.existsSync(pkgFile) || !fs.statSync(pkgFile).isFile()) {
|
34
|
-
dir = path.join(dir, "..");
|
35
|
-
continue;
|
36
|
-
}
|
37
|
-
return pkgFile;
|
38
|
-
} while (dir !== path.resolve(dir, ".."));
|
39
|
-
return null;
|
40
|
-
}
|
41
|
-
|
42
|
-
//------------------------------------------------------------------------------
|
43
|
-
// Private
|
44
|
-
//------------------------------------------------------------------------------
|
45
|
-
|
46
|
-
/**
|
47
|
-
* Install node modules synchronously and save to devDependencies in package.json
|
48
|
-
* @param {string|string[]} packages Node module or modules to install
|
49
|
-
* @returns {void}
|
50
|
-
*/
|
51
|
-
function installSyncSaveDev(packages) {
|
52
|
-
const packageList = Array.isArray(packages) ? packages : [packages];
|
53
|
-
const npmProcess = spawn.sync("npm", ["i", "--save-dev"].concat(packageList), { stdio: "inherit" });
|
54
|
-
const error = npmProcess.error;
|
55
|
-
|
56
|
-
if (error && error.code === "ENOENT") {
|
57
|
-
const pluralS = packageList.length > 1 ? "s" : "";
|
58
|
-
|
59
|
-
log.error(`Could not execute npm. Please install the following package${pluralS} with a package manager of your choice: ${packageList.join(", ")}`);
|
60
|
-
}
|
61
|
-
}
|
62
|
-
|
63
|
-
/**
|
64
|
-
* Fetch `peerDependencies` of the given package by `npm show` command.
|
65
|
-
* @param {string} packageName The package name to fetch peerDependencies.
|
66
|
-
* @returns {Object} Gotten peerDependencies. Returns null if npm was not found.
|
67
|
-
*/
|
68
|
-
function fetchPeerDependencies(packageName) {
|
69
|
-
const npmProcess = spawn.sync(
|
70
|
-
"npm",
|
71
|
-
["show", "--json", packageName, "peerDependencies"],
|
72
|
-
{ encoding: "utf8" }
|
73
|
-
);
|
74
|
-
|
75
|
-
const error = npmProcess.error;
|
76
|
-
|
77
|
-
if (error && error.code === "ENOENT") {
|
78
|
-
return null;
|
79
|
-
}
|
80
|
-
const fetchedText = npmProcess.stdout.trim();
|
81
|
-
|
82
|
-
return JSON.parse(fetchedText || "{}");
|
83
|
-
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
/**
|
88
|
-
* Check whether node modules are include in a project's package.json.
|
89
|
-
* @param {string[]} packages Array of node module names
|
90
|
-
* @param {Object} opt Options Object
|
91
|
-
* @param {boolean} opt.dependencies Set to true to check for direct dependencies
|
92
|
-
* @param {boolean} opt.devDependencies Set to true to check for development dependencies
|
93
|
-
* @param {boolean} opt.startdir Directory to begin searching from
|
94
|
-
* @throws {Error} If cannot find valid `package.json` file.
|
95
|
-
* @returns {Object} An object whose keys are the module names
|
96
|
-
* and values are booleans indicating installation.
|
97
|
-
*/
|
98
|
-
function check(packages, opt) {
|
99
|
-
const deps = new Set();
|
100
|
-
const pkgJson = (opt) ? findPackageJson(opt.startDir) : findPackageJson();
|
101
|
-
let fileJson;
|
102
|
-
|
103
|
-
if (!pkgJson) {
|
104
|
-
throw new Error("Could not find a package.json file. Run 'npm init' to create one.");
|
105
|
-
}
|
106
|
-
|
107
|
-
try {
|
108
|
-
fileJson = JSON.parse(fs.readFileSync(pkgJson, "utf8"));
|
109
|
-
} catch (e) {
|
110
|
-
const error = new Error(e);
|
111
|
-
|
112
|
-
error.messageTemplate = "failed-to-read-json";
|
113
|
-
error.messageData = {
|
114
|
-
path: pkgJson,
|
115
|
-
message: e.message
|
116
|
-
};
|
117
|
-
throw error;
|
118
|
-
}
|
119
|
-
|
120
|
-
["dependencies", "devDependencies"].forEach(key => {
|
121
|
-
if (opt[key] && typeof fileJson[key] === "object") {
|
122
|
-
Object.keys(fileJson[key]).forEach(dep => deps.add(dep));
|
123
|
-
}
|
124
|
-
});
|
125
|
-
|
126
|
-
return packages.reduce((status, pkg) => {
|
127
|
-
status[pkg] = deps.has(pkg);
|
128
|
-
return status;
|
129
|
-
}, {});
|
130
|
-
}
|
131
|
-
|
132
|
-
/**
|
133
|
-
* Check whether node modules are included in the dependencies of a project's
|
134
|
-
* package.json.
|
135
|
-
*
|
136
|
-
* Convenience wrapper around check().
|
137
|
-
* @param {string[]} packages Array of node modules to check.
|
138
|
-
* @param {string} rootDir The directory containing a package.json
|
139
|
-
* @returns {Object} An object whose keys are the module names
|
140
|
-
* and values are booleans indicating installation.
|
141
|
-
*/
|
142
|
-
function checkDeps(packages, rootDir) {
|
143
|
-
return check(packages, { dependencies: true, startDir: rootDir });
|
144
|
-
}
|
145
|
-
|
146
|
-
/**
|
147
|
-
* Check whether node modules are included in the devDependencies of a project's
|
148
|
-
* package.json.
|
149
|
-
*
|
150
|
-
* Convenience wrapper around check().
|
151
|
-
* @param {string[]} packages Array of node modules to check.
|
152
|
-
* @returns {Object} An object whose keys are the module names
|
153
|
-
* and values are booleans indicating installation.
|
154
|
-
*/
|
155
|
-
function checkDevDeps(packages) {
|
156
|
-
return check(packages, { devDependencies: true });
|
157
|
-
}
|
158
|
-
|
159
|
-
/**
|
160
|
-
* Check whether package.json is found in current path.
|
161
|
-
* @param {string} [startDir] Starting directory
|
162
|
-
* @returns {boolean} Whether a package.json is found in current path.
|
163
|
-
*/
|
164
|
-
function checkPackageJson(startDir) {
|
165
|
-
return !!findPackageJson(startDir);
|
166
|
-
}
|
167
|
-
|
168
|
-
//------------------------------------------------------------------------------
|
169
|
-
// Public Interface
|
170
|
-
//------------------------------------------------------------------------------
|
171
|
-
|
172
|
-
module.exports = {
|
173
|
-
installSyncSaveDev,
|
174
|
-
fetchPeerDependencies,
|
175
|
-
findPackageJson,
|
176
|
-
checkDeps,
|
177
|
-
checkDevDeps,
|
178
|
-
checkPackageJson
|
179
|
-
};
|
@@ -1,110 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @fileoverview Tools for obtaining SourceCode objects.
|
3
|
-
* @author Ian VanSchooten
|
4
|
-
*/
|
5
|
-
|
6
|
-
"use strict";
|
7
|
-
|
8
|
-
//------------------------------------------------------------------------------
|
9
|
-
// Requirements
|
10
|
-
//------------------------------------------------------------------------------
|
11
|
-
|
12
|
-
const { CLIEngine } = require("../cli-engine");
|
13
|
-
|
14
|
-
/*
|
15
|
-
* This is used for:
|
16
|
-
*
|
17
|
-
* 1. Enumerate target file because we have not expose such a API on `CLIEngine`
|
18
|
-
* (https://github.com/eslint/eslint/issues/11222).
|
19
|
-
* 2. Create `SourceCode` instances. Because we don't have any function which
|
20
|
-
* instantiate `SourceCode` so it needs to take the created `SourceCode`
|
21
|
-
* instance out after linting.
|
22
|
-
*
|
23
|
-
* TODO1: Expose the API that enumerates target files.
|
24
|
-
* TODO2: Extract the creation logic of `SourceCode` from `Linter` class.
|
25
|
-
*/
|
26
|
-
const { getCLIEngineInternalSlots } = require("../cli-engine/cli-engine"); // eslint-disable-line node/no-restricted-require -- Todo
|
27
|
-
|
28
|
-
const debug = require("debug")("eslint:source-code-utils");
|
29
|
-
|
30
|
-
//------------------------------------------------------------------------------
|
31
|
-
// Helpers
|
32
|
-
//------------------------------------------------------------------------------
|
33
|
-
|
34
|
-
/**
|
35
|
-
* Get the SourceCode object for a single file
|
36
|
-
* @param {string} filename The fully resolved filename to get SourceCode from.
|
37
|
-
* @param {Object} engine A CLIEngine.
|
38
|
-
* @throws {Error} Upon fatal errors from execution.
|
39
|
-
* @returns {Array} Array of the SourceCode object representing the file
|
40
|
-
* and fatal error message.
|
41
|
-
*/
|
42
|
-
function getSourceCodeOfFile(filename, engine) {
|
43
|
-
debug("getting sourceCode of", filename);
|
44
|
-
const results = engine.executeOnFiles([filename]);
|
45
|
-
|
46
|
-
if (results && results.results[0] && results.results[0].messages[0] && results.results[0].messages[0].fatal) {
|
47
|
-
const msg = results.results[0].messages[0];
|
48
|
-
|
49
|
-
throw new Error(`(${filename}:${msg.line}:${msg.column}) ${msg.message}`);
|
50
|
-
}
|
51
|
-
|
52
|
-
// TODO: extract the logic that creates source code objects to `SourceCode#parse(text, options)` or something like.
|
53
|
-
const { linter } = getCLIEngineInternalSlots(engine);
|
54
|
-
const sourceCode = linter.getSourceCode();
|
55
|
-
|
56
|
-
return sourceCode;
|
57
|
-
}
|
58
|
-
|
59
|
-
//------------------------------------------------------------------------------
|
60
|
-
// Public Interface
|
61
|
-
//------------------------------------------------------------------------------
|
62
|
-
|
63
|
-
|
64
|
-
/**
|
65
|
-
* This callback is used to measure execution status in a progress bar
|
66
|
-
* @callback progressCallback
|
67
|
-
* @param {number} The total number of times the callback will be called.
|
68
|
-
*/
|
69
|
-
|
70
|
-
/**
|
71
|
-
* Gets the SourceCode of a single file, or set of files.
|
72
|
-
* @param {string[]|string} patterns A filename, directory name, or glob, or an array of them
|
73
|
-
* @param {Object} options A CLIEngine options object. If not provided, the default cli options will be used.
|
74
|
-
* @param {progressCallback} callback Callback for reporting execution status
|
75
|
-
* @returns {Object} The SourceCode of all processed files.
|
76
|
-
*/
|
77
|
-
function getSourceCodeOfFiles(patterns, options, callback) {
|
78
|
-
const sourceCodes = {};
|
79
|
-
const globPatternsList = typeof patterns === "string" ? [patterns] : patterns;
|
80
|
-
const engine = new CLIEngine({ ...options, rules: {} });
|
81
|
-
|
82
|
-
// TODO: make file iteration as a public API and use it.
|
83
|
-
const { fileEnumerator } = getCLIEngineInternalSlots(engine);
|
84
|
-
const filenames =
|
85
|
-
Array.from(fileEnumerator.iterateFiles(globPatternsList))
|
86
|
-
.filter(entry => !entry.ignored)
|
87
|
-
.map(entry => entry.filePath);
|
88
|
-
|
89
|
-
if (filenames.length === 0) {
|
90
|
-
debug(`Did not find any files matching pattern(s): ${globPatternsList}`);
|
91
|
-
}
|
92
|
-
|
93
|
-
filenames.forEach(filename => {
|
94
|
-
const sourceCode = getSourceCodeOfFile(filename, engine);
|
95
|
-
|
96
|
-
if (sourceCode) {
|
97
|
-
debug("got sourceCode of", filename);
|
98
|
-
sourceCodes[filename] = sourceCode;
|
99
|
-
}
|
100
|
-
if (callback) {
|
101
|
-
callback(filenames.length); // eslint-disable-line node/callback-return -- End of function
|
102
|
-
}
|
103
|
-
});
|
104
|
-
|
105
|
-
return sourceCodes;
|
106
|
-
}
|
107
|
-
|
108
|
-
module.exports = {
|
109
|
-
getSourceCodeOfFiles
|
110
|
-
};
|