Skip to content

Commit

Permalink
Added support for user-provided JSLint options
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangreenberg committed Dec 30, 2010
1 parent 59bc803 commit e539580
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions src/jslint.cpp
Expand Up @@ -31,69 +31,75 @@ THE SOFTWARE.
using namespace v8;

namespace jslint {
std::string find_and_replace(std::string tInput, std::string tFind, std::string tReplace) {
int uPos = 0;
int uFindLen = tFind.length();
int uReplaceLen = tReplace.length();

if( uFindLen == 0 )
{
return tInput;
std::string default_options() {
std::string options = "{passfail: true, white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true}";
return options;
}

for( ;(uPos = tInput.find( tFind, uPos )) != std::string::npos; )
{
tInput.replace( uPos, uFindLen, tReplace );
uPos += uReplaceLen;
}
return tInput;
};

Handle<String> load_source_js (char* src_file) {
std::string result;
std::string line;
std::string jsline;
std::ifstream myfile (src_file);
if (myfile.is_open()) {
//std::cout << jslint::native_fulljslint << std::endl;
result = std::string(jslint::native_jslint) + "\nvar file_to_lint = [];\n";
while (! myfile.eof() ) {
std::getline (myfile, line);
jsline = "file_to_lint.push(\"" + jslint::find_and_replace(line,"\"", "\\\"") + "\");";
result.append(jsline);
std::string find_and_replace(std::string tInput, std::string tFind, std::string tReplace) {
int uPos = 0;
int uFindLen = tFind.length();
int uReplaceLen = tReplace.length();

if( uFindLen == 0 )
{
return tInput;
}
myfile.close();
result.append("(function () { "
"if (!JSLINT(file_to_lint, "
"{passfail: true, white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true}"
")) {"
"var results = [];"
"for(var i=0; i<JSLINT.errors.length-1; i++){"
"var e = JSLINT.errors[i];"
"results.push('Lint at line ' + e.line + ' character ' + e.character + ': ' + e.reason + '\\n' +(e.evidence || '').replace(/^\\s*(\\S*(\\s+\\S+)*)\\s*$/, '$1'));};"
"return results.join('\\n');}"
"else { return 'jslint: No problems found in "+ std::string(src_file) +"'}"
"}());");
//std::cout << result << std::endl;
return String::New(result.c_str());
} else {
std::cout << "jslint: Couldn't open file " << src_file << std::endl;
throw Exception();

for( ;(uPos = tInput.find( tFind, uPos )) != std::string::npos; )
{
tInput.replace( uPos, uFindLen, tReplace );
uPos += uReplaceLen;
}
}
return tInput;
};

Handle<String> load_source_js (char* src_file, std::string lint_options) {
std::string result;
std::string line;
std::string jsline;
std::ifstream myfile (src_file);
if (myfile.is_open()) {
//std::cout << jslint::native_fulljslint << std::endl;
result = std::string(jslint::native_jslint) + "\nvar file_to_lint = [];\n";
while (! myfile.eof() ) {
std::getline (myfile, line);
jsline = "file_to_lint.push(\"" + jslint::find_and_replace(line,"\"", "\\\"") + "\");";
result.append(jsline);
}
myfile.close();
result.append("(function () { "
"if (!JSLINT(file_to_lint, ");
result.append(lint_options);
result.append(
")) {"
"var results = [];"
"for(var i=0; i<JSLINT.errors.length-1; i++){"
"var e = JSLINT.errors[i];"
"results.push('Lint at line ' + e.line + ' character ' + e.character + ': ' + e.reason + '\\n' +(e.evidence || '').replace(/^\\s*(\\S*(\\s+\\S+)*)\\s*$/, '$1'));};"
"return results.join('\\n');}"
"else { return 'jslint: No problems found in "+ std::string(src_file) +"'}"
"}());");
//std::cout << result << std::endl;
return String::New(result.c_str());
} else {
std::cout << "jslint: Couldn't open file " << src_file << std::endl;
throw Exception();
}
}
}
int main(int argc, char* argv[]) {

try {

// Get user options
TCLAP::CmdLine cmd("JSLint", ' ', "0.1");
TCLAP::ValueArg<std::string> optionsArg("o", "options", "JSLint options", false, "", "string", cmd);
TCLAP::ValueArg<std::string> optionsArg("o", "options", "JSLint options", false, jslint::default_options(), "string", cmd);
TCLAP::UnlabeledMultiArg<std::string> filesArg("files", "file names", true, "string", cmd);

cmd.parse( argc, argv );

std::string options = optionsArg.getValue();
std::string lint_options = optionsArg.getValue();
std::vector<std::string> files = filesArg.getValue();

for (int i=0; i < files.size(); i++) {
Expand All @@ -113,7 +119,7 @@ int main(int argc, char* argv[]) {
try {
// Create a string containing the JavaScript source code.

Handle<String> source = jslint::load_source_js(const_cast<char *>(fileName.c_str()));
Handle<String> source = jslint::load_source_js(const_cast<char *>(fileName.c_str()), lint_options);

// Compile the source code.
Handle<Script> script = Script::Compile(source);
Expand Down

0 comments on commit e539580

Please sign in to comment.