Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Implement FlatConfigArray (refs #13481) #14321

Merged
merged 35 commits into from Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0d6db1b
Update: Implement FlatConfigArray (refs #13481)
nzakas Mar 19, 2021
003aa0a
Upgrade config-array package
nzakas Mar 20, 2021
f596b2b
Add schemas for linterOptions, processor, plugins
nzakas Mar 22, 2021
919d8c8
Continue implementing config schemas
nzakas Mar 31, 2021
07aac9a
RulesSchema start
nzakas Apr 3, 2021
5e9521a
Add initial finalization step
nzakas Apr 5, 2021
99b6591
Default config
nzakas Apr 5, 2021
516bd79
Strict mode
nzakas Apr 5, 2021
72eddee
Start rule validation
nzakas Apr 6, 2021
d50b2dd
Finish FlatConfigArray implementation
nzakas Apr 13, 2021
3189abc
Remove too-new syntax
nzakas Apr 13, 2021
ede77e6
Fix default config
nzakas Apr 16, 2021
0bfe125
fix test
nzakas Apr 17, 2021
12df97b
Update tests/lib/config/flat-config-array.js
nzakas Apr 19, 2021
c8ce1aa
Update tests/lib/config/flat-config-array.js
nzakas Apr 19, 2021
e7f7dbc
Update tests/lib/config/flat-config-array.js
nzakas Apr 19, 2021
48025f2
Update tests/lib/config/flat-config-array.js
nzakas Apr 19, 2021
905a5d4
Update tests
nzakas Apr 19, 2021
b99f3af
fix test
nzakas Apr 19, 2021
1117b31
Allow old-style plugin names
nzakas Apr 21, 2021
10343ad
Fix reportUnusedDisableDirectives and add JSDoc
nzakas Apr 21, 2021
40fe618
Add more tests
nzakas Apr 23, 2021
70af1ec
address review comments
nzakas Apr 28, 2021
acabd01
Ignore only .git directory
nzakas Apr 30, 2021
c0bbff6
Allow null for global settings
nzakas Apr 30, 2021
210e792
writeable -> writable
nzakas Apr 30, 2021
6ba32a6
Remove incorrect comment
nzakas Apr 30, 2021
b2dcefa
Validate severity-only rule options
nzakas Apr 30, 2021
0ce35cf
Add key to global error message
nzakas Apr 30, 2021
553b931
deeply merge parserOptions and settings
nzakas Apr 30, 2021
adf4f2f
Rename defaultResultConfig
nzakas May 3, 2021
751352d
Normalize and fix rule validations
nzakas May 3, 2021
b260002
Fix rule options merging
nzakas May 3, 2021
9acd1c2
Fix various errors
nzakas May 7, 2021
7b66408
Rebase onto master
nzakas Jun 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/config/default-config.js
@@ -0,0 +1,52 @@
/**
* @fileoverview Default configuration
* @author Nicholas C. Zakas
*/

"use strict";

//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

const Rules = require("../rules");

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


exports.defaultConfig = [
{
plugins: {
"@": {
btmills marked this conversation as resolved.
Show resolved Hide resolved
parsers: {
espree: require("espree")
},

/*
* Because we try to delay loading rules until absolutely
* necessary, a proxy allows us to hook into the lazy-loading
* aspect of the rules map while still keeping all of the
* relevant configuration inside of the config array.
*/
rules: new Proxy({}, {
get(target, property) {
return Rules.get(property);
},

has(target, property) {
return Rules.has(property);
}
})
}
},
ignores: [
"**/node_modules/**",
".git/**"
],
languageOptions: {
parser: "@/espree"
}
}
];
125 changes: 125 additions & 0 deletions lib/config/flat-config-array.js
@@ -0,0 +1,125 @@
/**
* @fileoverview Flat Config Array
* @author Nicholas C. Zakas
*/

"use strict";

//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

const { ConfigArray, ConfigArraySymbol } = require("@humanwhocodes/config-array");
nzakas marked this conversation as resolved.
Show resolved Hide resolved
const { flatConfigSchema } = require("./flat-config-schema");
const { RuleValidator } = require("./rule-validator");
const { defaultConfig } = require("./default-config");
const recommendedConfig = require("../../conf/eslint-recommended");
const allConfig = require("../../conf/eslint-all");

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

const ruleValidator = new RuleValidator();

/**
* Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
* @param {string} identifier The identifier to parse.
* @returns {{objectName: string, pluginName: string}} The parts of the plugin
* name.
*/
function splitPluginIdentifier(identifier) {
const parts = identifier.split("/");

return {
objectName: parts.pop(),
pluginName: parts.join("/")
};
}
nzakas marked this conversation as resolved.
Show resolved Hide resolved

//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------

/**
* Represents an array containing configuration information for ESLint.
*/
class FlatConfigArray extends ConfigArray {

/**
* Creates a new instance.
* @param {*[]} configs An array of configuration information.
* @param {{basePath: string, baseConfig: FlatConfig}} options The options
* to use for the config array instance.
*/
constructor(configs, { basePath, baseConfig = defaultConfig }) {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
super(configs, {
basePath,
schema: flatConfigSchema
});

this.unshift(baseConfig);
}

/* eslint-disable class-methods-use-this */
/**
* Replaces a config with another config to allow us to put strings
* in the config array that will be replaced by objects before
* normalization.
* @param {Object} config The config to preprocess.
* @returns {Object} The preprocessed config.
*/
[ConfigArraySymbol.preprocessConfig](config) {
if (config === "eslint:recommended") {
return recommendedConfig;
}

if (config === "eslint:all") {
return allConfig;
}

return config;
}
nzakas marked this conversation as resolved.
Show resolved Hide resolved

/**
* Finalizes the config by replacing plugin references with their objects
* and validating rule option schemas.
* @param {Object} config The config to finalize.
* @returns {Object} The finalized config.
* @throws {TypeError} If the config is invalid.
*/
[ConfigArraySymbol.finalizeConfig](config) {

const { plugins, languageOptions, processor } = config;

// Check parser value
if (languageOptions && languageOptions.parser && typeof languageOptions.parser === "string") {
const { pluginName, objectName: parserName } = splitPluginIdentifier(languageOptions.parser);

if (!plugins || !plugins[pluginName] || !plugins[pluginName].parsers || !plugins[pluginName].parsers[parserName]) {
throw new TypeError(`Key "parser": Could not find "${parserName}" in plugin "${pluginName}".`);
}

languageOptions.parser = plugins[pluginName].parsers[parserName];
}

// Check processor value
if (processor && typeof processor === "string") {
const { pluginName, objectName: processorName } = splitPluginIdentifier(processor);

if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[processorName]) {
throw new TypeError(`Key "processor": Could not find "${processorName}" in plugin "${pluginName}".`);
nzakas marked this conversation as resolved.
Show resolved Hide resolved
}

config.processor = plugins[pluginName].processors[processorName];
}

ruleValidator.validate(config);

return config;
}
/* eslint-enable class-methods-use-this */

}

exports.FlatConfigArray = FlatConfigArray;