Blame view

node_modules/eslint/lib/config/config-ops.js 6.76 KB
c39994410   Ryan Glover   wip converting to...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  /**
   * @fileoverview Config file operations. This file must be usable in the browser,
   * so no Node-specific code can be here.
   * @author Nicholas C. Zakas
   * @copyright 2015 Nicholas C. Zakas. All rights reserved.
   * See LICENSE file in root directory for full license.
   */
  "use strict";
  
  //------------------------------------------------------------------------------
  // Requirements
  //------------------------------------------------------------------------------
  
  var debug = require("debug"),
      environments = require("../../conf/environments"),
      assign = require("object-assign");
  
  //------------------------------------------------------------------------------
  // Private
  //------------------------------------------------------------------------------
  
  debug = debug("eslint:config-ops");
  
  //------------------------------------------------------------------------------
  // Public Interface
  //------------------------------------------------------------------------------
  
  module.exports = {
  
      /**
       * Creates an empty configuration object suitable for merging as a base.
       * @returns {Object} A configuration object.
       */
      createEmptyConfig: function() {
          return {
              globals: {},
              env: {},
              rules: {},
              ecmaFeatures: {}
          };
      },
  
      /**
       * Creates an environment config based on the specified environments.
       * @param {Object<string,boolean>} env The environment settings.
       * @returns {Object} A configuration object with the appropriate rules and globals
       *      set.
       */
      createEnvironmentConfig: function(env) {
  
          var envConfig = this.createEmptyConfig();
  
          if (env) {
  
              envConfig.env = env;
  
              Object.keys(env).filter(function(name) {
                  return env[name];
              }).forEach(function(name) {
                  var environment = environments[name];
  
                  if (environment) {
                      debug("Creating config for environment " + name);
                      if (environment.globals) {
                          assign(envConfig.globals, environment.globals);
                      }
  
                      if (environment.ecmaFeatures) {
                          assign(envConfig.ecmaFeatures, environment.ecmaFeatures);
                      }
                  }
              });
          }
  
          return envConfig;
      },
  
      /**
       * Given a config with environment settings, applies the globals and
       * ecmaFeatures to the configuration and returns the result.
       * @param {Object} config The configuration information.
       * @returns {Object} The updated configuration information.
       */
      applyEnvironments: function(config) {
          if (config.env && typeof config.env === "object") {
              debug("Apply environment settings to config");
              return this.merge(this.createEnvironmentConfig(config.env), config);
          }
  
          return config;
      },
  
      /**
       * Merges two config objects. This will not only add missing keys, but will also modify values to match.
       * @param {Object} target config object
       * @param {Object} src config object. Overrides in this config object will take priority over base.
       * @param {boolean} [combine] Whether to combine arrays or not
       * @param {boolean} [isRule] Whether its a rule
       * @returns {Object} merged config object.
       */
      merge: function deepmerge(target, src, combine, isRule) {
          /*
           The MIT License (MIT)
  
           Copyright (c) 2012 Nicholas Fisher
  
           Permission is hereby granted, free of charge, to any person obtaining a copy
           of this software and associated documentation files (the "Software"), to deal
           in the Software without restriction, including without limitation the rights
           to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
           copies of the Software, and to permit persons to whom the Software is
           furnished to do so, subject to the following conditions:
  
           The above copyright notice and this permission notice shall be included in
           all copies or substantial portions of the Software.
  
           THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
           IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
           FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
           AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
           LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
           OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
           THE SOFTWARE.
           */
          // This code is taken from deepmerge repo (https://github.com/KyleAMathews/deepmerge) and modified to meet our needs.
          var array = Array.isArray(src) || Array.isArray(target);
          var dst = array && [] || {};
  
          combine = !!combine;
          isRule = !!isRule;
          if (array) {
              target = target || [];
              if (isRule && src.length > 1) {
                  dst = dst.concat(src);
              } else {
                  dst = dst.concat(target);
              }
              if (typeof src !== "object" && !Array.isArray(src)) {
                  src = [src];
              }
              Object.keys(src).forEach(function(e, i) {
                  e = src[i];
                  if (typeof dst[i] === "undefined") {
                      dst[i] = e;
                  } else if (typeof e === "object") {
                      if (isRule) {
                          dst[i] = e;
                      } else {
                          dst[i] = deepmerge(target[i], e, combine, isRule);
                      }
                  } else {
                      if (!combine) {
                          dst[i] = e;
                      } else {
                          if (dst.indexOf(e) === -1) {
                              dst.push(e);
                          }
                      }
                  }
              });
          } else {
              if (target && typeof target === "object") {
                  Object.keys(target).forEach(function(key) {
                      dst[key] = target[key];
                  });
              }
              Object.keys(src).forEach(function(key) {
                  if (Array.isArray(src[key]) || Array.isArray(target[key])) {
                      dst[key] = deepmerge(target[key], src[key], key === "plugins", isRule);
                  } else if (typeof src[key] !== "object" || !src[key]) {
                      dst[key] = src[key];
                  } else {
                      if (!target[key]) {
                          dst[key] = src[key];
                      } else {
                          dst[key] = deepmerge(target[key], src[key], combine, key === "rules");
                      }
                  }
              });
          }
  
          return dst;
      }
  
  
  };