mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.9 KiB
85 lines
2.9 KiB
/**
|
|
* @fileoverview Common utilities.
|
|
*/
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Constants
|
|
//------------------------------------------------------------------------------
|
|
|
|
var PLUGIN_NAME_PREFIX = "eslint-plugin-",
|
|
NAMESPACE_REGEX = /^@.*\//i;
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Public Interface
|
|
//------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Merges two config objects. This will not only add missing keys, but will also modify values to match.
|
|
* @param {Object} base config object
|
|
* @param {Object} custom config object. Overrides in this config object will take priority over base.
|
|
* @returns {Object} merged config object.
|
|
*/
|
|
exports.mergeConfigs = function mergeConfigs(base, custom) {
|
|
|
|
Object.keys(custom).forEach(function (key) {
|
|
var property = custom[key];
|
|
|
|
if (key === "plugins") {
|
|
if (!base[key]) {
|
|
base[key] = [];
|
|
}
|
|
|
|
property.forEach(function (plugin) {
|
|
// skip duplicates
|
|
if (base[key].indexOf(plugin) === -1) {
|
|
base[key].push(plugin);
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (Array.isArray(base[key]) && !Array.isArray(property) && typeof property === "number") {
|
|
// assume that we are just overriding first attribute
|
|
base[key][0] = custom[key];
|
|
return;
|
|
}
|
|
|
|
if (typeof property === "object" && !Array.isArray(property) && property !== null) {
|
|
// base[key] might not exist, so be careful with recursion here
|
|
base[key] = mergeConfigs(base[key] || {}, custom[key]);
|
|
} else {
|
|
base[key] = custom[key];
|
|
}
|
|
});
|
|
|
|
return base;
|
|
};
|
|
|
|
/**
|
|
* Removes the prefix `eslint-plugin-` from a plugin name.
|
|
* @param {string} pluginName The name of the plugin which may have the prefix.
|
|
* @returns {string} The name of the plugin without prefix.
|
|
*/
|
|
exports.removePluginPrefix = function removePluginPrefix(pluginName) {
|
|
return pluginName.indexOf(PLUGIN_NAME_PREFIX) === 0 ? pluginName.substring(PLUGIN_NAME_PREFIX.length) : pluginName;
|
|
};
|
|
|
|
/**
|
|
* @param {string} pluginName The name of the plugin which may have the prefix.
|
|
* @returns {string} The name of the plugins namepace if it has one.
|
|
*/
|
|
exports.getNamespace = function getNamespace(pluginName) {
|
|
return pluginName.match(NAMESPACE_REGEX) ? pluginName.match(NAMESPACE_REGEX)[0] : "";
|
|
};
|
|
|
|
/**
|
|
* Removes the namespace from a plugin name.
|
|
* @param {string} pluginName The name of the plugin which may have the prefix.
|
|
* @returns {string} The name of the plugin without the namespace.
|
|
*/
|
|
exports.removeNameSpace = function removeNameSpace(pluginName) {
|
|
return pluginName.replace(NAMESPACE_REGEX, "");
|
|
};
|
|
|
|
exports.PLUGIN_NAME_PREFIX = PLUGIN_NAME_PREFIX;
|
|
|