UNPKG

1.49 kBJavaScriptView Raw
1/**
2 * @fileoverview A rule to ensure the use of a single variable declaration.
3 * @author Ian Christian Myers
4 */
5
6//------------------------------------------------------------------------------
7// Rule Definition
8//------------------------------------------------------------------------------
9
10module.exports = function(context) {
11
12 "use strict";
13
14 //--------------------------------------------------------------------------
15 // Helpers
16 //--------------------------------------------------------------------------
17
18 var functionStack = [];
19
20 function startFunction() {
21 functionStack.push(false);
22 }
23
24 function endFunction() {
25 functionStack.pop();
26 }
27
28 function checkDeclarations(node) {
29 if (functionStack[functionStack.length - 1]) {
30 context.report(node, "Combine this with the previous 'var' statement.");
31 } else {
32 functionStack[functionStack.length - 1] = true;
33 }
34 }
35
36 //--------------------------------------------------------------------------
37 // Public API
38 //--------------------------------------------------------------------------
39
40 return {
41 "Program": startFunction,
42 "FunctionDeclaration": startFunction,
43 "FunctionExpression": startFunction,
44
45 "VariableDeclaration": checkDeclarations,
46
47 "Program:exit": endFunction,
48 "FunctionDeclaration:exit": endFunction,
49 "FunctionExpression:exit": endFunction
50 };
51
52};