jshint configuration, code cleaning
Showing
11 changed files
with
124 additions
and
61 deletions
.jshintconfig
0 → 100644
1 | { | ||
2 | "asi": true, | ||
3 | "browser": true, | ||
4 | "debug": true, | ||
5 | "devel": true, | ||
6 | "eqeqeq": true, | ||
7 | "evil": true, | ||
8 | "maxparams": 5, | ||
9 | "maxdepth": 3, | ||
10 | "maxstatements": 15, | ||
11 | "maxcomplexity": 7, | ||
12 | "regexdash": true, | ||
13 | "strict": true, | ||
14 | "sub": true, | ||
15 | "trailing": true, | ||
16 | "undef": true, | ||
17 | |||
18 | "predef" : [ | ||
19 | "exports", | ||
20 | "phantom", | ||
21 | "require", | ||
22 | "window" | ||
23 | ] | ||
24 | } |
.jshintignore
0 → 100644
... | @@ -208,12 +208,6 @@ function bootstrap(global) { | ... | @@ -208,12 +208,6 @@ function bootstrap(global) { |
208 | // custom global CasperError | 208 | // custom global CasperError |
209 | global.CasperError = function CasperError(msg) { | 209 | global.CasperError = function CasperError(msg) { |
210 | Error.call(this); | 210 | Error.call(this); |
211 | try { | ||
212 | // let's get where this error has been thrown from, if we can | ||
213 | this._from = arguments.callee.caller.name; | ||
214 | } catch (e) { | ||
215 | this._from = "anonymous"; | ||
216 | } | ||
217 | this.message = msg; | 211 | this.message = msg; |
218 | this.name = 'CasperError'; | 212 | this.name = 'CasperError'; |
219 | }; | 213 | }; | ... | ... |
This diff is collapsed.
Click to expand it.
... | @@ -75,6 +75,7 @@ | ... | @@ -75,6 +75,7 @@ |
75 | * @return string | 75 | * @return string |
76 | */ | 76 | */ |
77 | this.decode = function decode(str) { | 77 | this.decode = function decode(str) { |
78 | /*jshint maxstatements:30 maxcomplexity:30 */ | ||
78 | var c1, c2, c3, c4, i = 0, len = str.length, out = ""; | 79 | var c1, c2, c3, c4, i = 0, len = str.length, out = ""; |
79 | while (i < len) { | 80 | while (i < len) { |
80 | do { | 81 | do { |
... | @@ -123,6 +124,7 @@ | ... | @@ -123,6 +124,7 @@ |
123 | * @return string | 124 | * @return string |
124 | */ | 125 | */ |
125 | this.encode = function encode(str) { | 126 | this.encode = function encode(str) { |
127 | /*jshint maxstatements:30 */ | ||
126 | var out = "", i = 0, len = str.length, c1, c2, c3; | 128 | var out = "", i = 0, len = str.length, c1, c2, c3; |
127 | while (i < len) { | 129 | while (i < len) { |
128 | c1 = str.charCodeAt(i++) & 0xff; | 130 | c1 = str.charCodeAt(i++) & 0xff; |
... | @@ -295,37 +297,14 @@ | ... | @@ -295,37 +297,14 @@ |
295 | * Retrieves string contents from a binary file behind an url. Silently | 297 | * Retrieves string contents from a binary file behind an url. Silently |
296 | * fails but log errors. | 298 | * fails but log errors. |
297 | * | 299 | * |
298 | * @param String url | 300 | * @param String url Url. |
299 | * @param String method | 301 | * @param String method HTTP method. |
300 | * @param Object data | 302 | * @param Object data Request parameters. |
301 | * @return string | 303 | * @return String |
302 | */ | 304 | */ |
303 | this.getBinary = function getBinary(url, method, data) { | 305 | this.getBinary = function getBinary(url, method, data) { |
304 | try { | 306 | try { |
305 | var xhr = new XMLHttpRequest(), dataString = ""; | 307 | return this.sendAJAX(url, method, data, false); |
306 | if (typeof method !== "string" || ["GET", "POST"].indexOf(method.toUpperCase()) === -1) { | ||
307 | method = "GET"; | ||
308 | } else { | ||
309 | method = method.toUpperCase(); | ||
310 | } | ||
311 | xhr.open(method, url, false); | ||
312 | this.log("getBinary(): Using HTTP method: '" + method + "'", "debug"); | ||
313 | xhr.overrideMimeType("text/plain; charset=x-user-defined"); | ||
314 | if (method === "POST") { | ||
315 | if (typeof data === "object") { | ||
316 | var dataList = []; | ||
317 | for (var k in data) { | ||
318 | dataList.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k].toString())); | ||
319 | } | ||
320 | dataString = dataList.join('&'); | ||
321 | this.log("getBinary(): Using request data: '" + dataString + "'", "debug"); | ||
322 | } else if (typeof data === "string") { | ||
323 | dataString = data; | ||
324 | } | ||
325 | xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | ||
326 | } | ||
327 | xhr.send(method === "POST" ? dataString : null); | ||
328 | return xhr.responseText; | ||
329 | } catch (e) { | 308 | } catch (e) { |
330 | if (e.name === "NETWORK_ERR" && e.code === 101) { | 309 | if (e.name === "NETWORK_ERR" && e.code === 101) { |
331 | this.log("getBinary(): Unfortunately, casperjs cannot make cross domain ajax requests", "warning"); | 310 | this.log("getBinary(): Unfortunately, casperjs cannot make cross domain ajax requests", "warning"); |
... | @@ -529,6 +508,42 @@ | ... | @@ -529,6 +508,42 @@ |
529 | }; | 508 | }; |
530 | 509 | ||
531 | /** | 510 | /** |
511 | * Performs an AJAX request. | ||
512 | * | ||
513 | * @param String url Url. | ||
514 | * @param String method HTTP method. | ||
515 | * @param Object data Request parameters. | ||
516 | * @param Boolean async Asynchroneous request? (default: false) | ||
517 | * @return String Response text. | ||
518 | */ | ||
519 | this.sendAJAX = function sendAJAX(url, method, data, async) { | ||
520 | var xhr = new XMLHttpRequest(), dataString = ""; | ||
521 | if (typeof method !== "string" || ["GET", "POST"].indexOf(method.toUpperCase()) === -1) { | ||
522 | method = "GET"; | ||
523 | } else { | ||
524 | method = method.toUpperCase(); | ||
525 | } | ||
526 | xhr.open(method, url, !!async); | ||
527 | this.log("getBinary(): Using HTTP method: '" + method + "'", "debug"); | ||
528 | xhr.overrideMimeType("text/plain; charset=x-user-defined"); | ||
529 | if (method === "POST") { | ||
530 | if (typeof data === "object") { | ||
531 | var dataList = []; | ||
532 | for (var k in data) { | ||
533 | dataList.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k].toString())); | ||
534 | } | ||
535 | dataString = dataList.join('&'); | ||
536 | this.log("sendAJAX(): Using request data: '" + dataString + "'", "debug"); | ||
537 | } else if (typeof data === "string") { | ||
538 | dataString = data; | ||
539 | } | ||
540 | xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | ||
541 | } | ||
542 | xhr.send(method === "POST" ? dataString : null); | ||
543 | return xhr.responseText; | ||
544 | }; | ||
545 | |||
546 | /** | ||
532 | * Sets a field (or a set of fields) value. Fails silently, but log | 547 | * Sets a field (or a set of fields) value. Fails silently, but log |
533 | * error messages. | 548 | * error messages. |
534 | * | 549 | * | ... | ... |
... | @@ -28,6 +28,8 @@ | ... | @@ -28,6 +28,8 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | var utils = require('utils'); | ||
32 | |||
31 | /* | 33 | /* |
32 | * Building an Array subclass | 34 | * Building an Array subclass |
33 | */ | 35 | */ |
... | @@ -59,7 +61,8 @@ responseHeaders.prototype.get = function get(name){ | ... | @@ -59,7 +61,8 @@ responseHeaders.prototype.get = function get(name){ |
59 | * @param mixed response Phantom response or undefined (generally with local files) | 61 | * @param mixed response Phantom response or undefined (generally with local files) |
60 | */ | 62 | */ |
61 | exports.augmentResponse = function(response) { | 63 | exports.augmentResponse = function(response) { |
62 | if (response === undefined) { | 64 | "use strict"; |
65 | if (!utils.isHTTPResource(response)) { | ||
63 | return; | 66 | return; |
64 | } | 67 | } |
65 | response.headers.__proto__ = responseHeaders.prototype; | 68 | response.headers.__proto__ = responseHeaders.prototype; | ... | ... |
This diff is collapsed.
Click to expand it.
This diff is collapsed.
Click to expand it.
... | @@ -33,6 +33,36 @@ | ... | @@ -33,6 +33,36 @@ |
33 | var utils = require('utils'); | 33 | var utils = require('utils'); |
34 | var fs = require('fs'); | 34 | var fs = require('fs'); |
35 | 35 | ||
36 | /** | ||
37 | * Generates a value for 'classname' attribute of the JUnit XML report. | ||
38 | * | ||
39 | * Uses the (relative) file name of the current casper script without file | ||
40 | * extension as classname. | ||
41 | * | ||
42 | * @param String classname | ||
43 | * @return String | ||
44 | */ | ||
45 | function generateClassName(classname) { | ||
46 | "use strict"; | ||
47 | classname = classname.replace(phantom.casperPath, "").trim(); | ||
48 | var script = classname || phantom.casperScript; | ||
49 | if (script.indexOf(fs.workingDirectory) === 0) { | ||
50 | script = script.substring(fs.workingDirectory.length + 1); | ||
51 | } | ||
52 | if (script.indexOf('/') === 0) { | ||
53 | script = script.substring(1, script.length); | ||
54 | } | ||
55 | if (~script.indexOf('.')) { | ||
56 | script = script.substring(0, script.lastIndexOf('.')); | ||
57 | } | ||
58 | return script || "unknown"; | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Creates a XUnit instance | ||
63 | * | ||
64 | * @return XUnit | ||
65 | */ | ||
36 | exports.create = function create() { | 66 | exports.create = function create() { |
37 | "use strict"; | 67 | "use strict"; |
38 | return new XUnitExporter(); | 68 | return new XUnitExporter(); |
... | @@ -88,31 +118,6 @@ XUnitExporter.prototype.addFailure = function addFailure(classname, name, messag | ... | @@ -88,31 +118,6 @@ XUnitExporter.prototype.addFailure = function addFailure(classname, name, messag |
88 | }; | 118 | }; |
89 | 119 | ||
90 | /** | 120 | /** |
91 | * Generates a value for 'classname' attribute of the JUnit XML report. | ||
92 | * | ||
93 | * Uses the (relative) file name of the current casper script without file | ||
94 | * extension as classname. | ||
95 | * | ||
96 | * @param String classname | ||
97 | * @return String | ||
98 | */ | ||
99 | function generateClassName(classname) { | ||
100 | "use strict"; | ||
101 | classname = classname.replace(phantom.casperPath, "").trim(); | ||
102 | var script = classname || phantom.casperScript; | ||
103 | if (script.indexOf(fs.workingDirectory) === 0) { | ||
104 | script = script.substring(fs.workingDirectory.length + 1); | ||
105 | } | ||
106 | if (script.indexOf('/') === 0) { | ||
107 | script = script.substring(1, script.length); | ||
108 | } | ||
109 | if (~script.indexOf('.')) { | ||
110 | script = script.substring(0, script.lastIndexOf('.')); | ||
111 | } | ||
112 | return script || "unknown"; | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Retrieves generated XML object - actually an HTMLElement. | 121 | * Retrieves generated XML object - actually an HTMLElement. |
117 | * | 122 | * |
118 | * @return HTMLElement | 123 | * @return HTMLElement | ... | ... |
1 | /*global phantom*/ | ||
2 | |||
1 | if (!phantom.casperLoaded) { | 3 | if (!phantom.casperLoaded) { |
2 | console.log('This script must be invoked using the casperjs executable'); | 4 | console.log('This script must be invoked using the casperjs executable'); |
3 | phantom.exit(1); | 5 | phantom.exit(1); |
... | @@ -15,6 +17,7 @@ var casper = require('casper').create({ | ... | @@ -15,6 +17,7 @@ var casper = require('casper').create({ |
15 | 17 | ||
16 | // local utils | 18 | // local utils |
17 | function checkSelfTest(tests) { | 19 | function checkSelfTest(tests) { |
20 | "use strict"; | ||
18 | var isCasperTest = false; | 21 | var isCasperTest = false; |
19 | tests.forEach(function(test) { | 22 | tests.forEach(function(test) { |
20 | var testDir = fs.absolute(fs.dirname(test)); | 23 | var testDir = fs.absolute(fs.dirname(test)); |
... | @@ -28,6 +31,7 @@ function checkSelfTest(tests) { | ... | @@ -28,6 +31,7 @@ function checkSelfTest(tests) { |
28 | } | 31 | } |
29 | 32 | ||
30 | function checkIncludeFile(include) { | 33 | function checkIncludeFile(include) { |
34 | "use strict"; | ||
31 | var absInclude = fs.absolute(include.trim()); | 35 | var absInclude = fs.absolute(include.trim()); |
32 | if (!fs.exists(absInclude)) { | 36 | if (!fs.exists(absInclude)) { |
33 | casper.warn("%s file not found, can't be included", absInclude); | 37 | casper.warn("%s file not found, can't be included", absInclude); |
... | @@ -60,6 +64,7 @@ if (casper.cli.get('no-colors') === true) { | ... | @@ -60,6 +64,7 @@ if (casper.cli.get('no-colors') === true) { |
60 | // test paths are passed as args | 64 | // test paths are passed as args |
61 | if (casper.cli.args.length) { | 65 | if (casper.cli.args.length) { |
62 | tests = casper.cli.args.filter(function(path) { | 66 | tests = casper.cli.args.filter(function(path) { |
67 | "use strict"; | ||
63 | return fs.isFile(path) || fs.isDirectory(path); | 68 | return fs.isFile(path) || fs.isDirectory(path); |
64 | }); | 69 | }); |
65 | } else { | 70 | } else { |
... | @@ -75,6 +80,7 @@ if (!phantom.casperSelfTest && checkSelfTest(tests)) { | ... | @@ -75,6 +80,7 @@ if (!phantom.casperSelfTest && checkSelfTest(tests)) { |
75 | 80 | ||
76 | // includes handling | 81 | // includes handling |
77 | this.loadIncludes.forEach(function(include){ | 82 | this.loadIncludes.forEach(function(include){ |
83 | "use strict"; | ||
78 | var container; | 84 | var container; |
79 | if (casper.cli.has(include)) { | 85 | if (casper.cli.has(include)) { |
80 | container = casper.cli.get(include).split(',').map(function(file) { | 86 | container = casper.cli.get(include).split(',').map(function(file) { |
... | @@ -89,6 +95,7 @@ this.loadIncludes.forEach(function(include){ | ... | @@ -89,6 +95,7 @@ this.loadIncludes.forEach(function(include){ |
89 | 95 | ||
90 | // test suites completion listener | 96 | // test suites completion listener |
91 | casper.test.on('tests.complete', function() { | 97 | casper.test.on('tests.complete', function() { |
98 | "use strict"; | ||
92 | this.renderResults(true, undefined, casper.cli.get('xunit') || undefined); | 99 | this.renderResults(true, undefined, casper.cli.get('xunit') || undefined); |
93 | }); | 100 | }); |
94 | 101 | ... | ... |
1 | /** | 1 | /** |
2 | * CasperJS local HTTP test server | 2 | * CasperJS local HTTP test server |
3 | */ | 3 | */ |
4 | |||
5 | /*global phantom casper require*/ | ||
6 | |||
4 | var colorizer = require('colorizer').create('Colorizer'); | 7 | var colorizer = require('colorizer').create('Colorizer'); |
5 | var fs = require('fs'); | 8 | var fs = require('fs'); |
6 | var utils = require('utils'); | 9 | var utils = require('utils'); |
... | @@ -9,10 +12,12 @@ var service; | ... | @@ -9,10 +12,12 @@ var service; |
9 | var testServerPort = 54321; | 12 | var testServerPort = 54321; |
10 | 13 | ||
11 | function info(message) { | 14 | function info(message) { |
15 | "use strict"; | ||
12 | console.log(colorizer.colorize('INFO', 'INFO_BAR') + ' ' + message); | 16 | console.log(colorizer.colorize('INFO', 'INFO_BAR') + ' ' + message); |
13 | } | 17 | } |
14 | 18 | ||
15 | service = server.listen(testServerPort, function(request, response) { | 19 | service = server.listen(testServerPort, function(request, response) { |
20 | "use strict"; | ||
16 | var pageFile = fs.pathJoin(phantom.casperPath, request.url); | 21 | var pageFile = fs.pathJoin(phantom.casperPath, request.url); |
17 | if (!fs.exists(pageFile) || !fs.isFile(pageFile)) { | 22 | if (!fs.exists(pageFile) || !fs.isFile(pageFile)) { |
18 | response.statusCode = 404; | 23 | response.statusCode = 404; |
... | @@ -26,16 +31,18 @@ service = server.listen(testServerPort, function(request, response) { | ... | @@ -26,16 +31,18 @@ service = server.listen(testServerPort, function(request, response) { |
26 | 31 | ||
27 | // overriding Casper.open to prefix all test urls | 32 | // overriding Casper.open to prefix all test urls |
28 | casper.setFilter('open.location', function(location) { | 33 | casper.setFilter('open.location', function(location) { |
34 | "use strict"; | ||
29 | if (/^file/.test(location)) { | 35 | if (/^file/.test(location)) { |
30 | return location; | 36 | return location; |
31 | } | 37 | } |
32 | if (!/^http/.test(location)) { | 38 | if (!/^http/.test(location)) { |
33 | return f('http://localhost:%d/%s', testServerPort, location); | 39 | return utils.format('http://localhost:%d/%s', testServerPort, location); |
34 | } | 40 | } |
35 | return location; | 41 | return location; |
36 | }); | 42 | }); |
37 | 43 | ||
38 | // test suites completion listener | 44 | // test suites completion listener |
39 | casper.test.on('tests.complete', function() { | 45 | casper.test.on('tests.complete', function() { |
46 | "use strict"; | ||
40 | server.close(); | 47 | server.close(); |
41 | }); | 48 | }); | ... | ... |
-
Please register or sign in to post a comment