Skip to content

Commit

Permalink
Make CLIEngine instance a private property
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Feb 19, 2020
1 parent 8fb5b71 commit ae3b417
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions lib/eslint/eslint.js
Expand Up @@ -80,10 +80,22 @@ const { CLIEngine } = require("../cli-engine");
* @property {(results: LintResult[]) => string} format The main formatter method.
*/

/**
* Private properties for the `ESLint` instance.
* @typedef {Object} LinterInternalSlots
* @property {CLIEngine} cliEngine The wrapped CLIEngine instance.
*/

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* The map with which to store private properties.
* @type {WeakMap<ESLint, ESLintInternalSlots>}
*/
const internalSlotsMap = new WeakMap();

/**
* Normalizes an array of plugins to their respective IDs.
* @param {string[]|PluginElement[]} plugins An array of plugins to normalize.
Expand Down Expand Up @@ -283,17 +295,22 @@ class ESLint {
* @param {ESLintOptions} options The options for this instance.
*/
constructor(options) {
this._cliEngine = new CLIEngine(processOptions(options));
const cliEngine = new CLIEngine(processOptions(options));

if (options.plugins.length) {
for (const plugin of options.plugins) {
if (typeof plugin === "object" && plugin !== null) {
this._cliEngine.addPlugin(plugin.id, plugin.definition);
if (typeof plugin === "object" && !Array.isArray(plugin) && plugin !== null) {
cliEngine.addPlugin(plugin.id, plugin.definition);
} else if (typeof plugin !== "string") {
throw new Error("Invalid plugin. Plugins must be specified as a string (e.g., \"eslint-plugin-example\") or as an object (e.g., { id: string; definition: Object }).");
}
}
}

// Initialize private properties.
internalSlotsMap.set(this, {
cliEngine
});
}

/**
Expand Down Expand Up @@ -338,7 +355,9 @@ class ESLint {
* @returns {Promise<LintResult[]>} The results of linting the file patterns given.
*/
async lintFiles(patterns) {
return processCLIEngineLintReport(this._cliEngine.executeOnFiles(patterns));
const { cliEngine } = internalSlotsMap.get(this);

return processCLIEngineLintReport(cliEngine.executeOnFiles(patterns));
}

/**
Expand All @@ -350,7 +369,9 @@ class ESLint {
* @returns {Promise<LintResult[]>} The results of linting the string of code given.
*/
async lintText(code, { filePath = null, warnIgnored = false }) {
return processCLIEngineLintReport(this._cliEngine.executeOnText(code, filePath, warnIgnored));
const { cliEngine } = internalSlotsMap.get(this);

return processCLIEngineLintReport(cliEngine.executeOnText(code, filePath, warnIgnored));
}

/**
Expand All @@ -361,7 +382,8 @@ class ESLint {
* @returns {Promise<Formatter|null>} A promise resolving to the formatter object or null if not found.
*/
async loadFormatter(name) {
const formatter = this._cliEngine.getFormatter(name);
const { cliEngine } = internalSlotsMap.get(this);
const formatter = cliEngine.getFormatter(name);

if (formatter === null) {
return null;
Expand All @@ -382,7 +404,7 @@ class ESLint {
return formatter(results, {
get rulesMeta() {
if (!rulesMeta) {
rulesMeta = createRulesMeta(this._cliEngine.getRules());
rulesMeta = createRulesMeta(cliEngine.getRules());
}

return rulesMeta;
Expand All @@ -400,7 +422,9 @@ class ESLint {
* @returns {Promise<ConfigData>} A configuration object for the file.
*/
async calculateConfigForFile(filePath) {
return this._cliEngine.getConfigForFile(filePath);
const { cliEngine } = internalSlotsMap.get(this);

return cliEngine.getConfigForFile(filePath);
}

/**
Expand All @@ -409,7 +433,9 @@ class ESLint {
* @returns {boolean} Whether or not the given path is ignored.
*/
async isPathIgnored(filePath) {
return this._cliEngine.isPathIgnored(filePath);
const { cliEngine } = internalSlotsMap.get(this);

return cliEngine.isPathIgnored(filePath);
}
}

Expand Down

0 comments on commit ae3b417

Please sign in to comment.