One of the great features of PHP was php -l, which checked all the code for syntax without executing it. Is there a similar way to do so for JavaScript in AngularJS? That is, in addition to my test suite coverage (which may not be 100%, and won't be instant if it is), is there a way to (perhaps using Node) check that all of my code compiles properly etc.
-
2look at Gulp, gulp jshintCallum Linington– Callum Linington2015-05-19 09:55:23 +00:00Commented May 19, 2015 at 9:55
-
Use CoffeeScript :) It compiles to JSlint-proof javascript code, and the compiler will stop and tell you if there's any syntax errors.Jeremy Thille– Jeremy Thille2015-05-19 10:13:39 +00:00Commented May 19, 2015 at 10:13
-
Don't confuse "syntactically correct" for "compiles properly", or syntax-checking for static-analysis.Ira Baxter– Ira Baxter2015-05-19 10:31:58 +00:00Commented May 19, 2015 at 10:31
Add a comment
|
1 Answer
By using node.js and a build tool called Gulp, you can easily set up and manage an automatic code validation. In fact, you can automate linting, minification and copying of JavaScript files, bundling/concat, renaming, testing, test code coverage etc etc.. List goes on.
How to install and use gulp is documented on their Github repository, and there is even a sample file to get you started. Smahing Magazine have a detailed tutorial about using Gulp as a build/test tool aswell.
Lint JavaScript files example:
// Gulpfile.js
var gulp = require('gulp'),
jshint = require('gulp-jshint');
gulp.task('js-lint', function () { // Task name
return gulp.src('js/*.js') // source files
.pipe(jshint()) // library
.pipe(jshint.reporter('default')) // reporter
});
1 Comment
jpoveda
You may also want to look at ESLint as an alternative to JSLint. You can find the gulp plugin here: github.com/adametry/gulp-eslint