-
Notifications
You must be signed in to change notification settings - Fork 5
/
jslint.cpp
171 lines (138 loc) · 5.52 KB
/
jslint.cpp
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
/*
Copyright (c) 2010 Jorge Falcão basead in js-beautify code
*/
#include <v8.h>
#include <string.h>
#include "jslint.h"
#include "v8.h"
using namespace v8;
Handle<String> readFile(const char* name) {
FILE* file = fopen(name, "rb");
if (file == NULL) {
return Handle<String>();
}
fseek(file, 0, SEEK_END);
int len = ftell(file);
rewind(file);
char* buf = new char[len + 1];
buf[len] = '\0';
fread(buf, 1, len, file);
fclose(file);
Handle<String> result = String::New(buf);
delete[] buf;
return result;
}
// Extracts a C string from a V8 Utf8Value.
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
// The callback that is invoked by v8 whenever the JavaScript 'print'
// function is called. Prints its arguments on stdout separated by
// spaces and ending with a newline.
v8::Handle<v8::Value> Print(const v8::Arguments& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
v8::HandleScope handle_scope;
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
const char* cstr = ToCString(str);
printf("%s", cstr);
}
printf("\n");
fflush(stdout);
return v8::Undefined();
}
static void usage(char* progname)
{
printf("Usage: %s [options] source-file\n", progname);
printf("See: http://www.JSLint.com/lint.html\n");
printf("default selected: --bitwise --eqeqeq --immed --newcap --nomen --onevar --regexp --undef --white\n");
printf("[options]\n");
printf(" --adsafe : ADsafe\n");
printf(" --bitwise : Disallow bitwise operator.\n");
printf(" --browser : Assume a browser.\n");
printf(" --cap : Tolerate HTML case.\n");
printf(" --css : Tolerate CSS workarounds.\n");
printf(" --debug : Tolerate debugger statements.\n");
printf(" --devel : Assume console, alert, ...\n");
printf(" --eqeqeq : Disallow == and !=\n");
printf(" --es5 : Tolerate ES5 syntax\n");
printf(" --evil : Tolerate eval\n");
printf(" --forin : Tolerate unfiltered for in.\n");
printf(" --fragment : Tolerate HTML fragments.\n");
printf(" --immed : Require parens around immediate invocations.\n");
printf(" --indent : Strict white space indentation.\n");
printf(" --laxbreak : Tolerate sloppy line breaking.\n");
printf(" --maxerr : Maximum number of errors.\n");
printf(" --maxlen : Maximum line length.\n");
printf(" --nomen : Disallow dangling _ in identifiers.\n");
printf(" --newcap : Require Initial Caps for constructors on Tolerate HTML event handlers.\n");
printf(" --onevar : Allow one var statement per function.\n");
printf(" --passfail : Stop on first error.\n");
printf(" --plusplus : Disallow ++ and --.\n");
printf(" --predef : Predefined ( , separated).\n");
printf(" --regexp : Disallow insecure . and [^...]. in /RegExp/.\n");
printf(" --rhino : Assume Rhino.\n");
printf(" --safe : Safe Subset .\n");
printf(" --strict : Require \"use strict\";.\n");
printf(" --sub : Tolerate inefficient subscripting.\n");
printf(" --undef : Disallow undefined variables.\n");
printf(" --white : Strict white space.\n");
printf(" --widget : Assume a Yahoo Widget.\n");
printf(" --windows : AssumeWindows.\n");
}
int main(int argc, char* argv[])
{
Handle<String> source;
HandleScope handle_scope;
Handle<ObjectTemplate> global = ObjectTemplate::New();
Handle<ObjectTemplate> options = ObjectTemplate::New();
bool overwrite = false;
const char* output = 0;
bool some_parameter = false;
for (int argpos = 1; argpos < argc; ++argpos) {
if (argv[argpos][0] != '-') {
source = readFile(argv[argpos]);
output = argv[argpos];
} else if (strcmp(argv[argpos], "--help") == 0 ||
strcmp(argv[argpos], "-h") == 0) {
usage(argv[0]);
return -1;
} else if (strncmp(argv[argpos], "--", 2) == 0) {
// TODO: check if is valid.
some_parameter = true;
options->Set(argv[argpos]+2, Boolean::New(true));
}
}
if (source.IsEmpty()) {
usage(argv[0]);
return -1;
}
if (!some_parameter) {
options->Set("bitwise", Boolean::New(true));
options->Set("eqeqeq", Boolean::New(true));
options->Set("immed", Boolean::New(true));
options->Set("newcap", Boolean::New(true));
options->Set("nomen", Boolean::New(true));
options->Set("onevar", Boolean::New(true));
options->Set("regexp", Boolean::New(true));
options->Set("undef", Boolean::New(true));
options->Set("white", Boolean::New(true));
}
global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
global->Set("source", source);
global->Set("options", options);
Handle<Context> context = Context::New(NULL, global);
Context::Scope context_scope(context);
Handle<Script> beautifyScript = Script::Compile(String::New(jslint_code));
beautifyScript->Run();
Handle<Script> runnerScript = Script::Compile(String::New("JSLINT(source, options);"));
Handle<Value> result = runnerScript->Run();
Handle<Script> printScript = Script::Compile(String::New(v8_code));
printScript->Run();
return 0;
}