UNPKG

3.91 kBJavaScriptView Raw
1/**
2 * @fileoverview Tools for obtaining SourceCode objects.
3 * @author Ian VanSchooten
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const CLIEngine = require("../cli-engine"),
13 globUtils = require("./glob-utils"),
14 baseDefaultOptions = require("../../conf/default-cli-options");
15
16const debug = require("debug")("eslint:source-code-utils");
17
18//------------------------------------------------------------------------------
19// Helpers
20//------------------------------------------------------------------------------
21
22/**
23 * Get the SourceCode object for a single file
24 * @param {string} filename The fully resolved filename to get SourceCode from.
25 * @param {Object} options A CLIEngine options object.
26 * @returns {Array} Array of the SourceCode object representing the file
27 * and fatal error message.
28 */
29function getSourceCodeOfFile(filename, options) {
30 debug("getting sourceCode of", filename);
31 const opts = Object.assign({}, options, { rules: {} });
32 const cli = new CLIEngine(opts);
33 const results = cli.executeOnFiles([filename]);
34
35 if (results && results.results[0] && results.results[0].messages[0] && results.results[0].messages[0].fatal) {
36 const msg = results.results[0].messages[0];
37
38 throw new Error(`(${filename}:${msg.line}:${msg.column}) ${msg.message}`);
39 }
40 const sourceCode = cli.linter.getSourceCode();
41
42 return sourceCode;
43}
44
45//------------------------------------------------------------------------------
46// Public Interface
47//------------------------------------------------------------------------------
48
49
50/**
51 * This callback is used to measure execution status in a progress bar
52 * @callback progressCallback
53 * @param {number} The total number of times the callback will be called.
54 */
55
56/**
57 * Gets the SourceCode of a single file, or set of files.
58 * @param {string[]|string} patterns A filename, directory name, or glob, or an array of them
59 * @param {Object} [providedOptions] A CLIEngine options object. If not provided, the default cli options will be used.
60 * @param {progressCallback} [providedCallback] Callback for reporting execution status
61 * @returns {Object} The SourceCode of all processed files.
62 */
63function getSourceCodeOfFiles(patterns, providedOptions, providedCallback) {
64 const sourceCodes = {};
65 const globPatternsList = typeof patterns === "string" ? [patterns] : patterns;
66 let options, callback;
67
68 const defaultOptions = Object.assign({}, baseDefaultOptions, { cwd: process.cwd() });
69
70 if (typeof providedOptions === "undefined") {
71 options = defaultOptions;
72 callback = null;
73 } else if (typeof providedOptions === "function") {
74 callback = providedOptions;
75 options = defaultOptions;
76 } else if (typeof providedOptions === "object") {
77 options = Object.assign({}, defaultOptions, providedOptions);
78 callback = providedCallback;
79 }
80 debug("constructed options:", options);
81
82 const filenames = globUtils.listFilesToProcess(globPatternsList, options)
83 .filter(fileInfo => !fileInfo.ignored)
84 .reduce((files, fileInfo) => files.concat(fileInfo.filename), []);
85
86 if (filenames.length === 0) {
87 debug(`Did not find any files matching pattern(s): ${globPatternsList}`);
88 }
89 filenames.forEach(filename => {
90 const sourceCode = getSourceCodeOfFile(filename, options);
91
92 if (sourceCode) {
93 debug("got sourceCode of", filename);
94 sourceCodes[filename] = sourceCode;
95 }
96 if (callback) {
97 callback(filenames.length); // eslint-disable-line callback-return
98 }
99 });
100 return sourceCodes;
101}
102
103module.exports = {
104 getSourceCodeOfFiles
105};