2 * @fileoverview Tools for obtaining SourceCode objects.
3 * @author Ian VanSchooten
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const { CLIEngine } = require("../cli-engine");
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.
23 * TODO1: Expose the API that enumerates target files.
24 * TODO2: Extract the creation logic of `SourceCode` from `Linter` class.
26 const { getCLIEngineInternalSlots } = require("../cli-engine/cli-engine"); // eslint-disable-line no-restricted-modules
28 const debug = require("debug")("eslint:source-code-utils");
30 //------------------------------------------------------------------------------
32 //------------------------------------------------------------------------------
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 * @returns {Array} Array of the SourceCode object representing the file
39 * and fatal error message.
41 function getSourceCodeOfFile(filename, engine) {
42 debug("getting sourceCode of", filename);
43 const results = engine.executeOnFiles([filename]);
45 if (results && results.results[0] && results.results[0].messages[0] && results.results[0].messages[0].fatal) {
46 const msg = results.results[0].messages[0];
48 throw new Error(`(${filename}:${msg.line}:${msg.column}) ${msg.message}`);
51 // TODO: extract the logic that creates source code objects to `SourceCode#parse(text, options)` or something like.
52 const { linter } = getCLIEngineInternalSlots(engine);
53 const sourceCode = linter.getSourceCode();
58 //------------------------------------------------------------------------------
60 //------------------------------------------------------------------------------
64 * This callback is used to measure execution status in a progress bar
65 * @callback progressCallback
66 * @param {number} The total number of times the callback will be called.
70 * Gets the SourceCode of a single file, or set of files.
71 * @param {string[]|string} patterns A filename, directory name, or glob, or an array of them
72 * @param {Object} options A CLIEngine options object. If not provided, the default cli options will be used.
73 * @param {progressCallback} callback Callback for reporting execution status
74 * @returns {Object} The SourceCode of all processed files.
76 function getSourceCodeOfFiles(patterns, options, callback) {
77 const sourceCodes = {};
78 const globPatternsList = typeof patterns === "string" ? [patterns] : patterns;
79 const engine = new CLIEngine({ ...options, rules: {} });
81 // TODO: make file iteration as a public API and use it.
82 const { fileEnumerator } = getCLIEngineInternalSlots(engine);
84 Array.from(fileEnumerator.iterateFiles(globPatternsList))
85 .filter(entry => !entry.ignored)
86 .map(entry => entry.filePath);
88 if (filenames.length === 0) {
89 debug(`Did not find any files matching pattern(s): ${globPatternsList}`);
92 filenames.forEach(filename => {
93 const sourceCode = getSourceCodeOfFile(filename, engine);
96 debug("got sourceCode of", filename);
97 sourceCodes[filename] = sourceCode;
100 callback(filenames.length); // eslint-disable-line callback-return