aboutsummaryrefslogtreecommitdiff
path: root/tools/eslint/lib/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/util.js')
-rw-r--r--tools/eslint/lib/util.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/tools/eslint/lib/util.js b/tools/eslint/lib/util.js
new file mode 100644
index 0000000000..9fe594a4ae
--- /dev/null
+++ b/tools/eslint/lib/util.js
@@ -0,0 +1,85 @@
+/**
+ * @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;