Skip to content

Commit

Permalink
Add lintText() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Mar 5, 2020
1 parent d5f79e3 commit 00b2bb0
Show file tree
Hide file tree
Showing 3 changed files with 756 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/cli-engine/cli-engine.js
Expand Up @@ -850,6 +850,7 @@ class CLIEngine {
const startTime = Date.now();
const resolvedFilename = filename && path.resolve(cwd, filename);


// Clear the last used config arrays.
lastConfigArrays.length = 0;

Expand Down
56 changes: 34 additions & 22 deletions lib/eslint/eslint.js
Expand Up @@ -81,20 +81,21 @@ const { CLIEngine } = require("../cli-engine");
*/

/**
* Private properties for the `ESLint` instance.
* @typedef {Object} LinterInternalSlots
* Private members for the `ESLint` instance.
* @typedef {Object} ESLintPrivateMembers
* @property {CLIEngine} cliEngine The wrapped CLIEngine instance.
* @property {ESLintOptions} options The options used to instantiate the ESLint instance.
*/

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

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

/**
* Checks if a plugin object is valid.
Expand Down Expand Up @@ -129,19 +130,19 @@ function processOptions({
envs = [],
extensions = null, // TODO: Should we update the CLIEngine check to account for empty arrays?
fix = false,
fixTypes = ["problem", "suggestion", "layout"],
fixTypes = [], // TODO: We don't currently set a default value for this. Doing so changes the behavior.
globals = [],
globInputPaths = true,
ignore = true,
ignorePath = null,
ignorePattern = [],
parser = "espree",
parserOptions = void 0, // TODO: Is this correct? line 11 in cli-engine/cli-engine.js will never be evaluate to true if we use `null`.
parserOptions = {}, // TODO: Is this correct?
plugins = [],
reportUnusedDisableDirectives = false,
resolvePluginsRelativeTo = cwd,
rulePaths = [],
rules = void 0, // TODO: Is this correct? line 19 in cli-engine/cli-engine.js will never be evaluate to true if we use `null`.
rules = {}, // TODO: Is this correct?
useEslintrc = true,
...unknownOptions
}) {
Expand Down Expand Up @@ -169,7 +170,7 @@ function processOptions({
throw new Error("cacheLocation must be a string.");
}

if (typeof configFile !== "string" && cacheLocation !== null) {
if (typeof configFile !== "string" && configFile !== null) {
throw new Error("configFile must be a string or null.");
}

Expand Down Expand Up @@ -217,7 +218,7 @@ function processOptions({
throw new Error("parser must be a string.");
}

if (typeof parserOptions !== "object") {
if (typeof parserOptions !== "object" && parserOptions !== null) {
throw new Error("parserOptions must be an object or null.");
}

Expand Down Expand Up @@ -303,10 +304,11 @@ class ESLint {
* Creates a new instance of the main ESLint API.
* @param {ESLintOptions} options The options for this instance.
*/
constructor(options) {
const cliEngine = new CLIEngine(processOptions(options));
constructor(options = {}) {
const processedOptions = processOptions(options);
const cliEngine = new CLIEngine(processedOptions);

if (options.plugins.length) {
if (options.plugins && options.plugins.length) {
for (const plugin of options.plugins) {
if (isValidPluginObject(plugin)) {
cliEngine.addPlugin(plugin.id, plugin.definition);
Expand All @@ -317,8 +319,9 @@ class ESLint {
}

// Initialize private properties.
internalSlotsMap.set(this, {
cliEngine
privateMembersMap.set(this, {
cliEngine,
options: processedOptions
});
}

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

return processCLIEngineLintReport(cliEngine.executeOnFiles(patterns));
}
Expand All @@ -377,8 +380,8 @@ class ESLint {
* @param {boolean} [options.warnIgnored] When set to true, warn if given filePath is an ignored path.
* @returns {Promise<LintResult[]>} The results of linting the string of code given.
*/
async lintText(code, { filePath = null, warnIgnored = false }) {
const { cliEngine } = internalSlotsMap.get(this);
async lintText(code, { filePath = null, warnIgnored = false } = {}) {
const { cliEngine } = privateMembersMap.get(this);

return processCLIEngineLintReport(cliEngine.executeOnText(code, filePath, warnIgnored));
}
Expand All @@ -391,7 +394,7 @@ class ESLint {
* @returns {Promise<Formatter|null>} A promise resolving to the formatter object or null if not found.
*/
async loadFormatter(name) {
const { cliEngine } = internalSlotsMap.get(this);
const { cliEngine } = privateMembersMap.get(this);
const formatter = cliEngine.getFormatter(name);

if (formatter === null) {
Expand Down Expand Up @@ -431,7 +434,7 @@ class ESLint {
* @returns {Promise<ConfigData>} A configuration object for the file.
*/
async calculateConfigForFile(filePath) {
const { cliEngine } = internalSlotsMap.get(this);
const { cliEngine } = privateMembersMap.get(this);

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

return cliEngine.isPathIgnored(filePath);
}
Expand All @@ -453,5 +456,14 @@ class ESLint {
//------------------------------------------------------------------------------

module.exports = {
ESLint
ESLint,

/**
* Get the private class members of a given ESLint instance for tests.
* @param {ESLint} instance The ESLint instance to get.
* @returns {ESLintPrivateMembers} The instance's private class members.
*/
getESLintPrivateMembers(instance) {
return privateMembersMap.get(instance);
}
};

0 comments on commit 00b2bb0

Please sign in to comment.