Socket
Socket
Sign inDemoInstall

eslint

Package Overview
Dependencies
33
Maintainers
1
Versions
357
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.5 to 0.0.6

.gitattributes

6

bin/eslint.js
#!/usr/bin/env node
/*jshint node:true*/
var cli = require("../lib/cli");
cli.execute(Array.prototype.slice.call(process.argv, 2));
var exitCode = cli.execute(Array.prototype.slice.call(process.argv, 2));
process.exit(exitCode);

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -17,4 +15,3 @@ // Requirements

rules = require("./rules"),
eslint = require("./eslint"),
reporter = require("./reporters/compact"); // TODO: More reporters
eslint = require("./eslint"); // TODO: More formatters

@@ -25,4 +22,3 @@ //------------------------------------------------------------------------------

var DEFAULT_CONFIG = "../config/eslint.json",
DEFAULT_RULES_DIR = "./rules";
var DEFAULT_CONFIG = "../conf/eslint.json";

@@ -62,3 +58,3 @@ //------------------------------------------------------------------------------

if (file[0] == ".") {
if (file[0] === ".") {
return;

@@ -80,6 +76,9 @@ } else if (stat.isFile() && /\.js$/.test(file)){

/**
*
* Processes an individual file using ESLint.
* @param {string} filename The filename of the file being checked.
* @param {Object} config The configuration object for ESLint.
* @param {Function} formatter The formatter to use to output results.
* @returns {int} The total number of errors.
*/
function processFile(filename, config) {
function processFile(filename, config, formatter) {

@@ -89,7 +88,17 @@ // clear all existing settings for a new file

var text = fs.readFileSync(path.resolve(filename), "utf8"),
var filePath = path.resolve(filename),
text,
messages;
if (fs.existsSync(filePath)) {
text = fs.readFileSync(path.resolve(filename), "utf8");
messages = eslint.verify(text, config);
} else {
console.log("Could not find file at '%s'.", filePath);
process.exit(1);
}
console.log(reporter(eslint, messages, filename, config));
console.log(formatter(messages, filename, config));
// count all errors and return the total
return messages.reduce(function(previous, message) {

@@ -105,3 +114,5 @@ if (message.fatal || config.rules[message.ruleId] === 2) {

/**
*
* Processes all files from the command line.
* @param {string[]} files All of the filenames to process.
* @param {Object} config The configuration options for ESLint.
* @returns {int} The total number of errors.

@@ -111,5 +122,15 @@ */

var fullFileList = [];
var fullFileList = [],
formatter;
// just in case an incorrect formatter was passed in
try {
formatter = require("./formatters/" + config.format);
} catch (ex) {
console.log("Could not find formatter '%s'.", config.format);
process.exit(1);
}
files.forEach(function(file) {
if (isDirectory(file)) {

@@ -123,3 +144,3 @@ fullFileList = fullFileList.concat(getFiles(file));

return fullFileList.reduce(function(previous, file) {
return previous + processFile(file, config);
return previous + processFile(file, config, formatter);
}, 0);

@@ -141,7 +162,5 @@ }

* @param {String[]} argv The array of arguments to process.
* @param {Function} [callback] The function to call when all processing is
* complete.
* @returns {void}
* @returns {int} The exit code for the operation.
*/
execute: function(argv, callback) {
execute: function(argv) {

@@ -153,4 +172,10 @@ var currentOptions = options.parse(argv),

if (currentOptions.h || !files.length) {
if (currentOptions.v) { // version from package.json
console.log("v" + require("../package.json").version);
} else if (currentOptions.h || !files.length) {
options.help();
} else {

@@ -166,10 +191,15 @@

// assign format information
if (currentOptions.f) {
config.format = currentOptions.f;
}
result = processFiles(files, config);
// result is the number of errors (not warnings)
if (result > 0) {
process.exit(1);
}
return result > 0 ? 1 : 0;
}
return 0;
}

@@ -181,9 +211,2 @@

module.exports = cli;

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -13,4 +11,3 @@ // Requirements

var util = require("util"),
esprima = require("esprima"),
var esprima = require("esprima"),
estraverse = require("estraverse"),

@@ -30,3 +27,4 @@ rules = require("./rules"),

currentText = null,
currentConfig = null;
currentConfig = null,
currentTokens = null;

@@ -42,2 +40,3 @@ /**

currentText = null;
currentTokens = null;
};

@@ -87,3 +86,4 @@

try {
var ast = esprima.parse(text, { loc: true, range: true });
var ast = esprima.parse(text, { loc: true, range: true, raw: true, tokens: true });
currentTokens = ast.tokens;
estraverse.traverse(ast, {

@@ -105,11 +105,4 @@ enter: function(node) {

// fake AST node to make life easier for reporters
node: {
loc: {
start: {
line: ex.lineNumber,
column: ex.column
}
}
}
line: ex.lineNumber,
column: ex.column
});

@@ -133,3 +126,5 @@ }

node: node,
message: message
message: message,
line: node.loc.start.line,
column: node.loc.start.column
});

@@ -145,20 +140,42 @@ };

api.isNodeJS = function() {
return currentConfig.env ? currentConfig.env.nodejs : false;
return currentConfig.env ? currentConfig.env.nodejs : false;
};
/**
* Returns the text currently being processed.
* @returns {string} The text currently being processed.
* Gets the source code for the given node.
* @param {ASTNode} [node] The AST node to get the text for.
* @param {int} [beforeCount] The number of characters before the node to retrieve.
* @param {int} [afterCount] The number of characters after the node to retrieve.
* @returns {string} The text representing the AST node.
*/
api.getCurrentText = function() {
return currentText;
api.getSource = function(node, beforeCount, afterCount) {
if (node) {
return currentText ? currentText.slice(node.range[0] - (beforeCount || 0),
node.range[1] + (afterCount || 0)) : null;
} else {
return currentText || null;
}
};
/**
* Gets the source code for the given node.
* @param {ASTNode} The AST node to get the text for.
* @returns {string} The text representing the AST node.
* Gets all tokens that are related to the given node.
* @param {ASTNode} [node] The AST node to get the text for.
* @param {int} [beforeCount] The number of characters before the node to retrieve.
* @param {int} [afterCount] The number of characters after the node to retrieve.
* @returns {Object[]} Array of objects representing tokens.
*/
api.getSource = function(node) {
return currentText ? currentText.slice(node.range[0], node.range[1]) : null;
api.getTokens = function(node, beforeCount, afterCount) {
if (node) {
var startLocation = node.range[0] - (beforeCount || 0);
var endLocation = node.range[1] + (afterCount || 0) ;
return currentTokens.filter(function(token) {
return (token.range[0] >= startLocation &&
token.range[0] <= endLocation &&
token.range[1] >= startLocation &&
token.range[1] <= endLocation);
});
} else {
return currentTokens || null;
}
};

@@ -165,0 +182,0 @@

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -33,2 +31,11 @@ // Requirements

// Format
optimist.alias("f", "format");
optimist.default("f", "compact");
optimist.describe("f", "Use a specific output format.");
// Version
optimist.alias("v", "version");
optimist.describe("v", "Outputs the version number.");
//------------------------------------------------------------------------------

@@ -35,0 +42,0 @@ // Public Interface

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -14,3 +12,4 @@ // Constants

var PASSTHROUGHS = [
"getCurrentText",
"getSource",
"getTokens",
"isNodeJS"

@@ -17,0 +16,0 @@ ];

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -10,0 +8,0 @@ // Requirements

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -10,0 +8,0 @@ // Rule Definition

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -29,3 +27,4 @@ // Helpers

if (node.alternate && node.alternate.type !== "BlockStatement") {
if (node.alternate && node.alternate.type !== "BlockStatement" &&
node.alternate.type !== "IfStatement") {
context.report(node, "Expected { after 'else'.");

@@ -32,0 +31,0 @@ }

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -10,0 +8,0 @@ // Rule Definition

@@ -6,5 +6,3 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -18,5 +16,11 @@ // Rule Definition

"NewExpression": function(node) {
var callee = node.callee.name;
var constructorName = "";
if (callee.charAt(0) === callee.charAt(0).toLowerCase()) {
if (node.callee.type === "MemberExpression") {
constructorName = node.callee.property.name;
} else {
constructorName = node.callee.name;
}
if (constructorName.charAt(0) === constructorName.charAt(0).toLowerCase()) {
context.report(node, "A constructor name should start with an uppercase letter.");

@@ -23,0 +27,0 @@ }

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -10,0 +8,0 @@ // Helpers

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -10,0 +8,0 @@ // Rule Definition

@@ -6,5 +6,3 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -11,0 +9,0 @@ // Rule Definition

@@ -6,5 +6,3 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -11,0 +9,0 @@ // Rule Definition

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -21,2 +19,13 @@ // Rule Definition

}
},
"SwitchStatement": function(node) {
if (typeof node.cases === "undefined") {
context.report(node, "Empty switch statement.");
}
},
"EmptyStatement": function(node) {
context.report(node, "Empty statement.");
}

@@ -23,0 +32,0 @@ };

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -10,0 +8,0 @@ // Rule Definition

@@ -6,5 +6,3 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -11,0 +9,0 @@ // Rule Definition

{
"name": "eslint",
"version": "0.0.5",
"version": "0.0.6",
"author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",

@@ -10,5 +10,5 @@ "description": "An Esprima-based pattern checker for JavaScript.",

"scripts": {
"ctest": "istanbul cover --print both vows -- --spec ./tests/*/*/*.js",
"test": "npm run-script lint && vows -- --spec ./tests/*/*.js ./tests/*/*/*.js",
"lint": "node node_modules/jshint/bin/jshint ./config/eslint.json ./lib"
"changelog": "bash ./scripts/changelog-update.sh",
"test": "npm run-script lint && node ./node_modules/istanbul/lib/cli.js cover --print both ./node_modules/vows/bin/vows -- --spec ./tests/*/*.js ./tests/*/*/*.js",
"lint": "node node_modules/jshint/bin/jshint ./conf/eslint.json ./lib"
},

@@ -38,3 +38,6 @@ "repository": {

"preferGlobal": true,
"license": "MIT"
"license": {
"type": "MIT",
"url": "https://github.com/nzakas/eslint/blob/master/LICENSE"
}
}

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -30,2 +28,168 @@ // Requirements

"when calling toSource()": {
topic: TEST_CODE,
"should retrieve all text when used without parameters": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("Program", function() {
var source = eslint.getSource();
assert.equal(source, TEST_CODE);
});
eslint.verify(topic, config, true);
},
"should retrieve all text for root node": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("Program", function(node) {
var source = eslint.getSource(node);
assert.equal(source, TEST_CODE);
});
eslint.verify(topic, config, true);
},
"should retrieve all text for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var source = eslint.getSource(node);
assert.equal(source, "6 * 7");
});
eslint.verify(topic, config, true);
},
"should retrieve all text plus two characters before for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var source = eslint.getSource(node, 2);
assert.equal(source, "= 6 * 7");
});
eslint.verify(topic, config, true);
},
"should retrieve all text plus one character after for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var source = eslint.getSource(node, 0, 1);
assert.equal(source, "6 * 7;");
});
eslint.verify(topic, config, true);
},
"should retrieve all text plus two characters before and one character after for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var source = eslint.getSource(node, 2, 1);
assert.equal(source, "= 6 * 7;");
});
eslint.verify(topic, config, true);
}
},
"when calling getTokens": {
topic: TEST_CODE,
"should retrieve all tokens when used without parameters": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("Program", function(node) {
var tokens = eslint.getTokens();
assert.equal(tokens.length, 7);
});
eslint.verify(topic, config, true);
},
"should retrieve all tokens for root node": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("Program", function(node) {
var tokens = eslint.getTokens(node);
assert.equal(tokens.length, 7);
});
eslint.verify(topic, config, true);
},
"should retrieve all tokens for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var tokens = eslint.getTokens(node);
assert.equal(tokens.length, 3);
});
eslint.verify(topic, config, true);
},
"should retrieve all tokens plus equals sign for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var tokens = eslint.getTokens(node, 2);
assert.equal(tokens.length, 4);
});
eslint.verify(topic, config, true);
},
"should retrieve all tokens plus one character after for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var tokens = eslint.getTokens(node, 0, 1);
assert.equal(tokens.length, 4);
});
eslint.verify(topic, config, true);
},
"should retrieve all tokens plus two characters before and one character after for binary expression": function(topic) {
var config = { rules: {} };
eslint.reset();
eslint.on("BinaryExpression", function(node) {
var tokens = eslint.getTokens(node, 2, 1);
assert.equal(tokens.length, 5);
});
eslint.verify(topic, config, true);
}
},
"when evaluating code": {

@@ -61,3 +225,3 @@

sinon.assert.calledOnce(spyBinaryExpression);
},
}
},

@@ -107,3 +271,3 @@

assert.equal(messages.length, 0);
assert.isNull(eslint.getCurrentText());
assert.isNull(eslint.getSource());
},

@@ -110,0 +274,0 @@

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
options = require("../../lib/options");

@@ -23,2 +20,9 @@

/*
* This may look like it's simply testing optimist under the covers, but really
* it's testing the interface of the options object. I want to make sure the
* interface is solid and tested because I'm not sure I want to use optimist
* long-term.
*/
vows.describe("options").addBatch({

@@ -75,10 +79,57 @@

"should return string for .rulesdir": function(topic) {
"should return a string for .rulesdir": function(topic) {
var currentOptions = options.parse(topic);
assert.iString(currentOptions.rulesdir);
assert.isString(currentOptions.rulesdir);
assert.equal(currentOptions.rulesdir, "/morerules");
}
},
"when passed --format": {
topic: [ "--format", "compact" ],
"should return a string for .f": function(topic) {
var currentOptions = options.parse(topic);
assert.equal(currentOptions.f, "compact");
}
},
"when passed -f": {
topic: [ "-f", "compact" ],
"should return a string for .f": function(topic) {
var currentOptions = options.parse(topic);
assert.equal(currentOptions.f, "compact");
}
},
"when passed -v": {
topic: [ "-v" ],
"should return true for .v": function(topic) {
var currentOptions = options.parse(topic);
assert.isTrue(currentOptions.v);
}
},
"when passed --version": {
topic: [ "--version" ],
"should return true for .v": function(topic) {
var currentOptions = options.parse(topic);
assert.isTrue(currentOptions.v);
}
}
});
}).export(module);

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -18,0 +15,0 @@

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -79,4 +76,17 @@

"when evaluating 'if (foo) { bar() } else if (foo2) { baz() }'": {
topic: "if (foo) { bar() } else if (foo2) { baz() }",
"should not report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 0);
}
},
"when evaluating 'while (foo) bar()'": {

@@ -83,0 +93,0 @@

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -18,0 +15,0 @@

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -58,4 +56,20 @@ // Requirements

}
},
"when evaluating 'var x = new foo.C();'": {
topic: "var x = new foo.C();",
"should not report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 0);
}
}
}).export(module);

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -18,0 +15,0 @@

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -83,4 +80,20 @@

}
},
"when evaluating '+": {
topic: "a + b",
"should not report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 0);
}
}
}).export(module);

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -83,2 +80,16 @@

}
},
"when evaluating 'Console.info(foo)'": {
topic: "Console.info(foo)",
"should not report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 0);
}
}

@@ -88,2 +99,3 @@

}).export(module);

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -18,0 +15,0 @@

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -173,2 +170,50 @@

}
},
"when evaluating 'switch(foo) {}'": {
topic: "switch(foo) {}",
"should report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, RULE_ID);
assert.equal(messages[0].message, "Empty switch statement.");
assert.include(messages[0].node.type, "SwitchStatement");
}
},
"when evaluating 'switch(foo) {case 'foo': break;}'": {
topic: "switch(foo) {case 'foo': break;}",
"should not report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 0);
}
},
"when evaluating ';'": {
topic: ";",
"should report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, RULE_ID);
assert.equal(messages[0].message, "Empty statement.");
assert.include(messages[0].node.type, "EmptyStatement");
}
}

@@ -180,2 +225,4 @@

}).export(module);

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -47,4 +44,19 @@

}
},
"when evaluating 'Eval(foo)": {
topic: "Eval(foo)",
"should not report a violation": function(topic) {
var config = { rules: {} };
config.rules[RULE_ID] = 1;
var messages = eslint.verify(topic, config);
assert.equal(messages.length, 0);
}
}
}).export(module);

@@ -6,4 +6,2 @@ /**

/*jshint node:true*/
//------------------------------------------------------------------------------

@@ -15,3 +13,2 @@ // Requirements

assert = require("assert"),
sinon = require("sinon"),
eslint = require("../../../lib/eslint");

@@ -18,0 +15,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc