Skip to content
Browse files

add closure compiler and tests

  • Loading branch information...
1 parent 5965661 commit 4b6083e76c0452f76bb6b90146eb7a4800b3fd51 @kof committed Dec 28, 2010
View
1 bin/cli.js
@@ -70,6 +70,7 @@ var start = Date.now();
o.callback = function(errors) {
errors.forEach(function(err) {
util.puts(
+ '--------------------------------------------------------------------------',
colorize(err.file, 'red'),
'Line: ' + err.line,
err.linter + ' says: ' + err.message
View
31 conf/server.json
@@ -33,5 +33,36 @@
"closure-linter": {
"nojsdoc": false,
"strict": false
+ },
+ "closure-compiler": {
+ // Input and output charset for all files. By default, we accept UTF-8 as input and output US_ASCII
+ "charset": "utf-8",
+ // possible values: WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS
+ "compilation_level": "ADVANCED_OPTIMIZATIONS",
+ // The file containing javascript externs. You may specify multiple
+ "externs": null,
+ // Turns on extra sanity checks. Possible values: OFF, START, START_AND_END, EVERY_PASS.
+ "dev_mode": "EVERY_PASS",
+ // Make the named class of warnings an error:
+ // accessControls, checkRegExp,checkTypes, checkVars, deprecated,
+ // fileoverviewTags, invalidCasts, missingProperties, nonStandardJsDocs,
+ // strictModuleDepCheck, undefinedVars, unknownDefines, visibility
+ "jscomp_error": null,
+ // Turn off the named class of warnings:
+ "jscomp_off": "undefinedVars", // don't need to check undefined vars if using jslint which is already doing it
+ // Make the named class of warnings a normal warning.
+ "jscomp_warning": null,
+ // The logging level (standard java.util. logging.Level values) for Compiler
+ // progress. Does not control errors or warnings for the JavaScript code under compilation
+ "logging_level": null,
+ // Controls how detailed the compilation summary is. Values: 0 (never print summary), 1 (print summary only if
+ // there are errors or warnings), 2 (print summary if type checking is on, see --check_types), 3 (always print summary). The default level is 1
+ "summary_detail_level": 0,
+ // Check source validity but do not enforce Closure style rules and conventions
+ "third_party": false,
+ // Specifies whether the default externs should be excluded
+ "use_only_custom_externs": null,
+ // Specifies the warning level to use: QUIET, DEFAULT, VERBOSE
+ "warning_level": "VERBOSE"
}
}
View
31 lib/codenazi.js
@@ -15,7 +15,8 @@ exports.options = {
recursive: false,
linter: {
jslint: join(__dirname, 'linter', 'jslint'),
- 'closure-linter': join(__dirname, 'linter', 'closure-linter')
+ 'closure-linter': join(__dirname, 'linter', 'closure-linter'),
+ 'closure-compiler': join(__dirname, 'linter', 'closure-compiler')
}
};
@@ -45,7 +46,6 @@ function readdir(path, r) {
exports.run = function(opts) {
var o = exports.options,
sandbox = {},
- linter,
errors = [],
done = 0,
linters = 0,
@@ -73,24 +73,21 @@ exports.run = function(opts) {
o.config = sandbox.o;
}
- function lint(file) {
- require(o.linter[linter])(file, o.config[linter], function(err) {
+ Object.keys(o.config).forEach(function(linter) {
+ linters++;
+ o.files.forEach(function(file) {
+ require(o.linter[linter])(file, o.config[linter], function(err) {
- if (err && err.length > 0) {
- errors = errors.concat(err);
- }
+ if (err && err.length > 0) {
+ errors = errors.concat(err);
+ }
- done++;
+ done++;
- if (done === o.files.length * linters) {
- o.callback(errors);
- }
+ if (done === o.files.length * linters) {
+ o.callback(errors);
+ }
+ });
});
- }
-
- Object.keys(o.config).forEach(function(name) {
- linter = name;
- linters++;
- o.files.forEach(lint);
});
};
View
66 lib/linter/closure-compiler.js
@@ -0,0 +1,66 @@
+/**
+ * closure-compiler wrapper for nodejs
+ *
+ * @author Oleg Slobodskoi aka Kof
+ */
+
+var fs = require('fs'),
+ join = require('path').join,
+ spawn = require('child_process').spawn,
+ root = join(__dirname, '..', '..');
+
+/**
+ * Run closure-compiler
+ * @param {String} file path to file.
+ * @param {Object} options linter options.
+ * @param {Function} callback callback function.
+ * @export
+ */
+module.exports = function(file, options, callback) {
+ var linter,
+ errors = [],
+ name,
+ opts = [];
+
+ // convert options to args
+ for (name in options) {
+ if (options[name]) {
+ opts.push('--' + name);
+ if (typeof options[name] !== 'boolean') {
+ opts.push(options[name]);
+ }
+ }
+ }
+
+ opts.push(
+ '-jar', join(root, 'deps', 'closure-compiler', 'compiler.jar'),
+ '--js', file,
+ '--js_output_file', '/dev/null'
+ );
+
+ linter = spawn('java', opts);
+
+ linter.stderr.on('data', function(data) {
+ data = data.toString().split('\n');
+ data.forEach(function(str) {
+ if (!/ERROR|WARNING/.test(str)) {
+ return;
+ }
+
+ str = str.split(':');
+
+ var msg = str[2] + (str[3] ? ':' + str[3] : '');
+
+ errors.push({
+ linter: 'closure-compiler',
+ file: file,
+ message: msg || 'no message found',
+ line: str[1] || 'no line found'
+ });
+ });
+ });
+
+ linter.on('exit', function(code) {
+ callback(errors);
+ });
+};
View
5 lib/linter/closure-linter.js
@@ -4,15 +4,14 @@
* @author Oleg Slobodskoi aka Kof
*/
-var fs = require('fs'),
- path = require('path'),
- spawn = require('child_process').spawn;
+var spawn = require('child_process').spawn;
/**
* Run closure-linter
* @param {String} file path to file.
* @param {Object} options linter options.
* @param {Function} callback callback function.
+ * @export
*/
module.exports = function(file, options, callback) {
var linter,
View
3 lib/linter/jslint.js
@@ -18,6 +18,7 @@ require('vm').runInThisContext(fs.readFileSync(jslintPath), jslintPath);
* @param {String} file path to a file.
* @param {Object} options jslint options.
* @param {Function} callback callback function.
+ * @export
*/
module.exports = function jslint(file, options, callback) {
fs.readFile(file, 'utf-8', function(err, code) {
@@ -35,7 +36,7 @@ module.exports = function jslint(file, options, callback) {
errors.push({
linter: 'JSLINT',
file: file,
- message: err.reason + '. ' + err.evidence,
+ message: err.reason + ': ' + err.evidence,
line: err.line + ':' + err.character
});
}
View
4 package.json
@@ -18,8 +18,10 @@
{
"type": "MIT",
"url" : "http://www.opensource.org/licenses/mit-license.php"
- },
+ },
+ {
"type": "Apache License, Version 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
+ }
]
}
View
37 test/fixtures/all.json
@@ -1,37 +0,0 @@
-{
- "closure-linter": {
- "strict": false,
- "nojsdoc": false
- },
- "jslint": {
- "bitwise": true,
- "browser": false,
- "cap": false,
- "debug": false,
- "devel": false,
- "eqeqeq": true,
- "es5": true,
- "evil": true,
- "forin": true,
- "fragment": false,
- "immed": true,
- "indent": 4,
- "laxbreak": false,
- "maxlen": 90,
- "nomen": false,
- "newcap": true,
- "on": false,
- "onevar": true,
- "passfail": false,
- "plusplus": false,
- "predef": ["global", "process", "require", "__dirname", "__filename", "module", "exports", "Buffer"],
- "regexp": true,
- "rhino": false,
- "safe": false,
- "strict": false,
- "sub": false,
- "undef": true,
- "white": false,
- "windows": false
- }
-}
View
1 test/fixtures/closure-compiler.js
@@ -0,0 +1 @@
+var x = -{foo: 'bar'};
View
33 test/fixtures/closure-compiler.json
@@ -0,0 +1,33 @@
+{
+ "closure-compiler": {
+ // Input and output charset for all files. By default, we accept UTF-8 as input and output US_ASCII
+ "charset": "utf-8",
+ // possible values: WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS
+ "compilation_level": "ADVANCED_OPTIMIZATIONS",
+ // The file containing javascript externs. You may specify multiple
+ "externs": null,
+ // Turns on extra sanity checks. Possible values: OFF, START, START_AND_END, EVERY_PASS.
+ "dev_mode": "EVERY_PASS",
+ // Make the named class of warnings an error:
+ // accessControls, checkRegExp,checkTypes, checkVars, deprecated,
+ // fileoverviewTags, invalidCasts, missingProperties, nonStandardJsDocs,
+ // strictModuleDepCheck, undefinedVars, unknownDefines, visibility
+ "jscomp_error": null,
+ // Turn off the named class of warnings:
+ "jscomp_off": null,
+ // Make the named class of warnings a normal warning.
+ "jscomp_warning": null,
+ // The logging level (standard java.util. logging.Level values) for Compiler
+ // progress. Does not control errors or warnings for the JavaScript code under compilation
+ "logging_level": null,
+ // Controls how detailed the compilation summary is. Values: 0 (never print summary), 1 (print summary only if
+ // there are errors or warnings), 2 (print summary if type checking is on, see --check_types), 3 (always print summary). The default level is 1
+ "summary_detail_level": 0,
+ // Check source validity but do not enforce Closure style rules and conventions
+ "third_party": false,
+ // Specifies whether the default externs should be excluded
+ "use_only_custom_externs": null,
+ // Specifies the warning level to use: QUIET, DEFAULT, VERBOSE
+ "warning_level": "VERBOSE"
+ }
+}
View
0 test/fixtures/rec/jslint.js → test/fixtures/subfolder/jslint.js
File renamed without changes.
View
23 test/test.js
@@ -7,6 +7,7 @@ var root = join(__dirname, '..'),
options.linter.jslint = join(lib, 'linter', 'jslint');
options.linter['closure-linter'] = join(lib, 'linter', 'closure-linter');
+options.linter['closure-compiler'] = join(lib, 'linter', 'closure-compiler');
test('jslint', 1, function() {
stop();
@@ -32,13 +33,25 @@ test('closure-linter', 1, function() {
});
});
+test('closure-compiler', 1, function() {
+ stop();
+ run({
+ files: join(fixtures, 'closure-compiler.js'),
+ config: join(fixtures, 'closure-compiler.json'),
+ callback: function(err) {
+ equal(err.length, 2, 'errors count');
+ start();
+ }
+ });
+});
+
test('dir lint with all linters', 1, function() {
stop();
run({
files: fixtures,
- config: join(fixtures, 'all.json'),
+ config: join(root, 'conf', 'server.json'),
callback: function(err) {
- equal(err.length, 7, 'errors count');
+ equal(err.length, 12, 'errors count');
start();
}
});
@@ -48,11 +61,11 @@ test('dir lint with all linters recursively', 1, function() {
stop();
run({
files: fixtures,
- config: join(fixtures, 'all.json'),
+ config: join(root, 'conf', 'server.json'),
recursive: true,
callback: function(err) {
- equal(err.length, 9, 'errors count');
+ equal(err.length, 15, 'errors count');
start();
}
});
-});
+});

0 comments on commit 4b6083e

Please sign in to comment.
Something went wrong with that request. Please try again.