eslint 1.9.0 → 1.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +1 -1
  2. package/bin/eslint.js +1 -2
  3. package/lib/ast-utils.js +22 -1
  4. package/lib/cli-engine.js +15 -7
  5. package/lib/cli.js +2 -1
  6. package/lib/config/config-file.js +440 -0
  7. package/lib/config/config-initializer.js +241 -0
  8. package/lib/config/config-ops.js +186 -0
  9. package/lib/{config-validator.js → config/config-validator.js} +10 -2
  10. package/lib/config.js +39 -183
  11. package/lib/eslint.js +49 -41
  12. package/lib/file-finder.js +34 -15
  13. package/lib/ignored-paths.js +1 -1
  14. package/lib/options.js +7 -1
  15. package/lib/rules/block-spacing.js +7 -2
  16. package/lib/rules/brace-style.js +3 -1
  17. package/lib/rules/comma-spacing.js +25 -66
  18. package/lib/rules/consistent-this.js +25 -29
  19. package/lib/rules/curly.js +37 -7
  20. package/lib/rules/eqeqeq.js +17 -2
  21. package/lib/rules/id-length.js +2 -2
  22. package/lib/rules/indent.js +7 -10
  23. package/lib/rules/lines-around-comment.js +32 -12
  24. package/lib/rules/new-cap.js +11 -1
  25. package/lib/rules/no-alert.js +2 -3
  26. package/lib/rules/no-catch-shadow.js +7 -13
  27. package/lib/rules/no-extend-native.js +1 -2
  28. package/lib/rules/no-fallthrough.js +1 -2
  29. package/lib/rules/no-implicit-coercion.js +19 -0
  30. package/lib/rules/no-label-var.js +9 -23
  31. package/lib/rules/no-multiple-empty-lines.js +1 -1
  32. package/lib/rules/no-sequences.js +2 -1
  33. package/lib/rules/no-shadow.js +31 -58
  34. package/lib/rules/no-spaced-func.js +16 -12
  35. package/lib/rules/no-undef-init.js +5 -3
  36. package/lib/rules/no-undef.js +10 -13
  37. package/lib/rules/no-use-before-define.js +7 -21
  38. package/lib/rules/operator-linebreak.js +3 -2
  39. package/lib/rules/quotes.js +34 -15
  40. package/lib/rules/require-jsdoc.js +61 -2
  41. package/lib/rules/space-after-keywords.js +2 -0
  42. package/lib/rules/space-before-function-paren.js +5 -26
  43. package/lib/rules/space-before-keywords.js +5 -2
  44. package/lib/rules/spaced-comment.js +1 -3
  45. package/lib/rules/valid-jsdoc.js +8 -4
  46. package/lib/rules/vars-on-top.js +2 -2
  47. package/lib/testers/rule-tester.js +48 -7
  48. package/lib/util/source-code.js +4 -0
  49. package/lib/util.js +0 -92
  50. package/package.json +4 -6
  51. package/lib/config-initializer.js +0 -146
@@ -0,0 +1,241 @@
1
+ /**
2
+ * @fileoverview Config initialization wizard.
3
+ * @author Ilya Volodin
4
+ * @copyright 2015 Ilya Volodin. All rights reserved.
5
+ */
6
+
7
+ "use strict";
8
+
9
+ //------------------------------------------------------------------------------
10
+ // Requirements
11
+ //------------------------------------------------------------------------------
12
+
13
+ var exec = require("child_process").exec,
14
+ inquirer = require("inquirer"),
15
+ ConfigFile = require("./config-file");
16
+
17
+ //------------------------------------------------------------------------------
18
+ // Private
19
+ //------------------------------------------------------------------------------
20
+
21
+ /* istanbul ignore next: hard to test fs function */
22
+ /**
23
+ * Create .eslintrc file in the current working directory
24
+ * @param {object} config object that contains user's answers
25
+ * @param {string} format The file format to write to.
26
+ * @param {function} callback function to call once the file is written.
27
+ * @returns {void}
28
+ */
29
+ function writeFile(config, format, callback) {
30
+
31
+ // default is .js
32
+ var extname = ".js";
33
+ if (format === "YAML") {
34
+ extname = ".yml";
35
+ } else if (format === "JSON") {
36
+ extname = ".json";
37
+ }
38
+
39
+
40
+ try {
41
+ ConfigFile.write(config, "./.eslintrc" + extname);
42
+ console.log("Successfully created .eslintrc" + extname + " file in " + process.cwd());
43
+ } catch (e) {
44
+ callback(e);
45
+ return;
46
+ }
47
+
48
+ // install any external configs as well as any included plugins
49
+ if (config.extends && config.extends.indexOf("eslint") === -1) {
50
+ console.log("Installing additional dependencies");
51
+ exec("npm i eslint-config-" + config.extends + " --save-dev", function(err) {
52
+
53
+ if (err) {
54
+ return callback(err);
55
+ }
56
+
57
+ // TODO: consider supporting more than 1 plugin though it's required yet.
58
+ exec("npm i eslint-plugin-" + config.plugins[0] + " --save-dev", callback);
59
+ });
60
+ return;
61
+ }
62
+
63
+ // install the react plugin if it was explictly chosen
64
+ if (config.plugins && config.plugins.indexOf("react") >= 0) {
65
+ console.log("Installing React plugin");
66
+ exec("npm i eslint-plugin-react --save-dev", callback);
67
+ return;
68
+ }
69
+ callback();
70
+ }
71
+
72
+ /**
73
+ * process user's answers and create config object
74
+ * @param {object} answers answers received from inquirer
75
+ * @returns {object} config object
76
+ */
77
+ function processAnswers(answers) {
78
+ var config = {rules: {}, env: {}, extends: "eslint:recommended"};
79
+ config.rules.indent = [2, answers.indent];
80
+ config.rules.quotes = [2, answers.quotes];
81
+ config.rules["linebreak-style"] = [2, answers.linebreak];
82
+ config.rules.semi = [2, answers.semi ? "always" : "never"];
83
+ if (answers.es6) {
84
+ config.env.es6 = true;
85
+ }
86
+ answers.env.forEach(function(env) {
87
+ config.env[env] = true;
88
+ });
89
+ if (answers.jsx) {
90
+ config.ecmaFeatures = {jsx: true};
91
+ if (answers.react) {
92
+ config.plugins = ["react"];
93
+ config.ecmaFeatures.experimentalObjectRestSpread = true;
94
+ }
95
+ }
96
+ return config;
97
+ }
98
+
99
+ /**
100
+ * process user's style guide of choice and return an appropriate config object.
101
+ * @param {string} guide name of the chosen style guide
102
+ * @returns {object} config object
103
+ */
104
+ function getConfigForStyleGuide(guide) {
105
+ var guides = {
106
+ google: {extends: "google"},
107
+ airbnb: {extends: "airbnb", plugins: ["react"]},
108
+ standard: {extends: "standard", plugins: ["standard"]}
109
+ };
110
+ if (!guides[guide]) {
111
+ throw new Error("You referenced an unsupported guide.");
112
+ }
113
+ return guides[guide];
114
+ }
115
+
116
+ /* istanbul ignore next: no need to test inquirer*/
117
+ /**
118
+ * Ask use a few questions on command prompt
119
+ * @param {function} callback callback function when file has been written
120
+ * @returns {void}
121
+ */
122
+ function promptUser(callback) {
123
+ inquirer.prompt([
124
+ {
125
+ type: "list",
126
+ name: "source",
127
+ message: "How would you like to configure ESLint?",
128
+ default: "prompt",
129
+ choices: [{name: "Answer questions about your style", value: "prompt"}, {name: "Use a popular style guide", value: "guide"}]
130
+ },
131
+ {
132
+ type: "list",
133
+ name: "styleguide",
134
+ message: "Which style guide do you want to follow?",
135
+ choices: [{name: "Google", value: "google"}, {name: "AirBnB", value: "airbnb"}, {name: "Standard", value: "standard"}],
136
+ when: function(answers) {
137
+ return answers.source === "guide";
138
+ }
139
+ },
140
+ {
141
+ type: "list",
142
+ name: "format",
143
+ message: "What format do you want your config file to be in?",
144
+ default: "JavaScript",
145
+ choices: ["JavaScript", "YAML", "JSON"],
146
+ when: function(answers) {
147
+ return answers.source === "guide";
148
+ }
149
+ }
150
+ ], function(earlyAnswers) {
151
+
152
+ // early exit if you are using a style guide
153
+ if (earlyAnswers.source === "guide") {
154
+ writeFile(getConfigForStyleGuide(earlyAnswers.styleguide), earlyAnswers.format, callback);
155
+ return;
156
+ }
157
+
158
+ // continue with the style questions otherwise...
159
+ inquirer.prompt([
160
+ {
161
+ type: "list",
162
+ name: "indent",
163
+ message: "What style of indentation do you use?",
164
+ default: "tabs",
165
+ choices: [{name: "Tabs", value: "tab"}, {name: "Spaces", value: 4}]
166
+ },
167
+ {
168
+ type: "list",
169
+ name: "quotes",
170
+ message: "What quotes do you use for strings?",
171
+ default: "double",
172
+ choices: [{name: "Double", value: "double"}, {name: "Single", value: "single"}]
173
+ },
174
+ {
175
+ type: "list",
176
+ name: "linebreak",
177
+ message: "What line endings do you use?",
178
+ default: "unix",
179
+ choices: [{name: "Unix", value: "unix"}, {name: "Windows", value: "windows"}]
180
+ },
181
+ {
182
+ type: "confirm",
183
+ name: "semi",
184
+ message: "Do you require semicolons?",
185
+ default: true
186
+ },
187
+ {
188
+ type: "confirm",
189
+ name: "es6",
190
+ message: "Are you using ECMAScript 6 features?",
191
+ default: false
192
+ },
193
+ {
194
+ type: "checkbox",
195
+ name: "env",
196
+ message: "Where will your code run?",
197
+ default: ["browser"],
198
+ choices: [{name: "Node", value: "node"}, {name: "Browser", value: "browser"}]
199
+ },
200
+ {
201
+ type: "confirm",
202
+ name: "jsx",
203
+ message: "Do you use JSX?",
204
+ default: false
205
+ },
206
+ {
207
+ type: "confirm",
208
+ name: "react",
209
+ message: "Do you use React",
210
+ default: false,
211
+ when: function(answers) {
212
+ return answers.jsx;
213
+ }
214
+ },
215
+ {
216
+ type: "list",
217
+ name: "format",
218
+ message: "What format do you want your config file to be in?",
219
+ default: "JavaScript",
220
+ choices: ["JavaScript", "YAML", "JSON"]
221
+ }
222
+ ], function(answers) {
223
+ var config = processAnswers(answers);
224
+ writeFile(config, answers.format, callback);
225
+ });
226
+ });
227
+ }
228
+
229
+ //------------------------------------------------------------------------------
230
+ // Public Interface
231
+ //------------------------------------------------------------------------------
232
+
233
+ var init = {
234
+ getConfigForStyleGuide: getConfigForStyleGuide,
235
+ processAnswers: processAnswers,
236
+ initializeConfig: /* istanbul ignore next */ function(callback) {
237
+ promptUser(callback);
238
+ }
239
+ };
240
+
241
+ module.exports = init;
@@ -0,0 +1,186 @@
1
+ /**
2
+ * @fileoverview Config file operations. This file must be usable in the browser,
3
+ * so no Node-specific code can be here.
4
+ * @author Nicholas C. Zakas
5
+ * @copyright 2015 Nicholas C. Zakas. All rights reserved.
6
+ * See LICENSE file in root directory for full license.
7
+ */
8
+ "use strict";
9
+
10
+ //------------------------------------------------------------------------------
11
+ // Requirements
12
+ //------------------------------------------------------------------------------
13
+
14
+ var debug = require("debug"),
15
+ environments = require("../../conf/environments"),
16
+ assign = require("object-assign");
17
+
18
+ //------------------------------------------------------------------------------
19
+ // Private
20
+ //------------------------------------------------------------------------------
21
+
22
+ debug = debug("eslint:config-ops");
23
+
24
+ //------------------------------------------------------------------------------
25
+ // Public Interface
26
+ //------------------------------------------------------------------------------
27
+
28
+ module.exports = {
29
+
30
+ /**
31
+ * Creates an empty configuration object suitable for merging as a base.
32
+ * @returns {Object} A configuration object.
33
+ */
34
+ createEmptyConfig: function() {
35
+ return {
36
+ globals: {},
37
+ env: {},
38
+ rules: {},
39
+ ecmaFeatures: {}
40
+ };
41
+ },
42
+
43
+ /**
44
+ * Creates an environment config based on the specified environments.
45
+ * @param {Object<string,boolean>} env The environment settings.
46
+ * @returns {Object} A configuration object with the appropriate rules and globals
47
+ * set.
48
+ */
49
+ createEnvironmentConfig: function(env) {
50
+
51
+ var envConfig = this.createEmptyConfig();
52
+
53
+ if (env) {
54
+
55
+ envConfig.env = env;
56
+
57
+ Object.keys(env).filter(function(name) {
58
+ return env[name];
59
+ }).forEach(function(name) {
60
+ var environment = environments[name];
61
+
62
+ if (environment) {
63
+ debug("Creating config for environment " + name);
64
+ if (environment.globals) {
65
+ assign(envConfig.globals, environment.globals);
66
+ }
67
+
68
+ if (environment.ecmaFeatures) {
69
+ assign(envConfig.ecmaFeatures, environment.ecmaFeatures);
70
+ }
71
+ }
72
+ });
73
+ }
74
+
75
+ return envConfig;
76
+ },
77
+
78
+ /**
79
+ * Given a config with environment settings, applies the globals and
80
+ * ecmaFeatures to the configuration and returns the result.
81
+ * @param {Object} config The configuration information.
82
+ * @returns {Object} The updated configuration information.
83
+ */
84
+ applyEnvironments: function(config) {
85
+ if (config.env && typeof config.env === "object") {
86
+ debug("Apply environment settings to config");
87
+ return this.merge(this.createEnvironmentConfig(config.env), config);
88
+ }
89
+
90
+ return config;
91
+ },
92
+
93
+ /**
94
+ * Merges two config objects. This will not only add missing keys, but will also modify values to match.
95
+ * @param {Object} target config object
96
+ * @param {Object} src config object. Overrides in this config object will take priority over base.
97
+ * @param {boolean} [combine] Whether to combine arrays or not
98
+ * @param {boolean} [isRule] Whether its a rule
99
+ * @returns {Object} merged config object.
100
+ */
101
+ merge: function deepmerge(target, src, combine, isRule) {
102
+ /*
103
+ The MIT License (MIT)
104
+
105
+ Copyright (c) 2012 Nicholas Fisher
106
+
107
+ Permission is hereby granted, free of charge, to any person obtaining a copy
108
+ of this software and associated documentation files (the "Software"), to deal
109
+ in the Software without restriction, including without limitation the rights
110
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
111
+ copies of the Software, and to permit persons to whom the Software is
112
+ furnished to do so, subject to the following conditions:
113
+
114
+ The above copyright notice and this permission notice shall be included in
115
+ all copies or substantial portions of the Software.
116
+
117
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
118
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
119
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
120
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
121
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
122
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
123
+ THE SOFTWARE.
124
+ */
125
+ // This code is taken from deepmerge repo (https://github.com/KyleAMathews/deepmerge) and modified to meet our needs.
126
+ var array = Array.isArray(src) || Array.isArray(target);
127
+ var dst = array && [] || {};
128
+
129
+ combine = !!combine;
130
+ isRule = !!isRule;
131
+ if (array) {
132
+ target = target || [];
133
+ if (isRule && src.length > 1) {
134
+ dst = dst.concat(src);
135
+ } else {
136
+ dst = dst.concat(target);
137
+ }
138
+ if (typeof src !== "object" && !Array.isArray(src)) {
139
+ src = [src];
140
+ }
141
+ Object.keys(src).forEach(function(e, i) {
142
+ e = src[i];
143
+ if (typeof dst[i] === "undefined") {
144
+ dst[i] = e;
145
+ } else if (typeof e === "object") {
146
+ if (isRule) {
147
+ dst[i] = e;
148
+ } else {
149
+ dst[i] = deepmerge(target[i], e, combine, isRule);
150
+ }
151
+ } else {
152
+ if (!combine) {
153
+ dst[i] = e;
154
+ } else {
155
+ if (dst.indexOf(e) === -1) {
156
+ dst.push(e);
157
+ }
158
+ }
159
+ }
160
+ });
161
+ } else {
162
+ if (target && typeof target === "object") {
163
+ Object.keys(target).forEach(function(key) {
164
+ dst[key] = target[key];
165
+ });
166
+ }
167
+ Object.keys(src).forEach(function(key) {
168
+ if (Array.isArray(src[key]) || Array.isArray(target[key])) {
169
+ dst[key] = deepmerge(target[key], src[key], key === "plugins", isRule);
170
+ } else if (typeof src[key] !== "object" || !src[key]) {
171
+ dst[key] = src[key];
172
+ } else {
173
+ if (!target[key]) {
174
+ dst[key] = src[key];
175
+ } else {
176
+ dst[key] = deepmerge(target[key], src[key], combine, key === "rules");
177
+ }
178
+ }
179
+ });
180
+ }
181
+
182
+ return dst;
183
+ }
184
+
185
+
186
+ };
@@ -10,14 +10,18 @@
10
10
  // Requirements
11
11
  //------------------------------------------------------------------------------
12
12
 
13
- var rules = require("./rules"),
14
- environments = require("../conf/environments"),
13
+ var rules = require("../rules"),
14
+ environments = require("../../conf/environments"),
15
15
  schemaValidator = require("is-my-json-valid");
16
16
 
17
17
  var validators = {
18
18
  rules: Object.create(null)
19
19
  };
20
20
 
21
+ //------------------------------------------------------------------------------
22
+ // Private
23
+ //------------------------------------------------------------------------------
24
+
21
25
  /**
22
26
  * Gets a complete options schema for a rule.
23
27
  * @param {string} id The rule's unique name.
@@ -148,6 +152,10 @@ function validate(config, source) {
148
152
  validateEnvironment(config.env, source);
149
153
  }
150
154
 
155
+ //------------------------------------------------------------------------------
156
+ // Public Interface
157
+ //------------------------------------------------------------------------------
158
+
151
159
  module.exports = {
152
160
  getRuleOptionsSchema: getRuleOptionsSchema,
153
161
  validate: validate,