Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering PhoneGap Mobile Application Development

You're reading from   Mastering PhoneGap Mobile Application Development Take your PhoneGap experience to the next level and create engaging real-world applications

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781783288434
Length 392 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Kerri Shotts Kerri Shotts
Author Profile Icon Kerri Shotts
Kerri Shotts
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Task Automation FREE CHAPTER 2. ECMAScript 2015 and Browserify 3. Sassy CSS 4. More Responsive Design 5. Hybrid Application Accessibility 6. Testing and UI Automation 7. IndexedDB 8. Web SQL Database 9. Transferring Files 10. Performance 11. Graphical Assets 12. Deployment Index

Linting your code

You could execute gulp emulate --platform ios (or android) right now; the app should work. But how do we know whether our code will work when it is built? Better yet—how can we prevent a build if the code isn't valid?

We do this by adding lint tasks to our Gulp configuration. Linting is a lot like compiling; the linter checks your code for obvious errors and aborts if it finds any. There are various linters available (some better than others), but not all of them support ES2015 syntax yet. The best one that does is ESLint (http://www.eslint.org). Thankfully, there's a very simple Gulp plugin that uses it.

We could stop at linting and be done, but code style is also important, and can catch out potentially serious issues as well. As such, we're also going to be using the JavaScript Code Style checker or JSCS (https://github.com/jscs-dev/node-jscs).

Let's create tasks to lint and check our coding style. First, we need to add some additional configuration to gulp/config.js:

var config = { …,
    lint: "src/www/js/app/**/*.js",
    "code-style": "src/www/js/app/**/*.js"
}

Now, let's create the associated tasks. Let's start with linting—this will live in the gulp/tasks/link.js file:

var gulp = require("gulp"),
    eslint = require("gulp-eslint"),
    config = require ("../config"),
    settings = require("../settings"),
    paths = require("../utils/paths");
function lintCode() {
    return gulp.src(paths.makeFullPath(config.lint))
        .pipe(eslint(paths.makeFullPath("eslint.json", 
          paths.CONFIG)))
        .pipe(eslint.format());
}
module.exports = {
    task: lintCode
}

The task for checking our code style will be named gulp/tasks/code-style.js. It should have the following code:

var gulp = require("gulp"),
    jscs = require("gulp-jscs"),
    config = require ("../config"),
    settings = require("../settings"),
    paths = require("../utils/paths");
function checkCodeStyle() {
    return gulp.src(paths.makeFullPath(config["code-style"]))
        .pipe(jscs({
            configPath: paths.makeFullPath("jscs.json", 
                          paths.CONFIG),
            esnext: true
        }))
        .pipe(jscs.reporter())
        .pipe(jscs.reporter('fail'));
}
module.exports = {
    task: checkCodeStyle
}

Now, before you run either task, you'll need two configuration files to tell each task what should be an error and what shouldn't be. We suggest using the files from the code bundle for this chapter for now (you can find them in the logology-v01/config directory). If you want to change the settings, you can do so; the sites for ESLint and JSCS both have information on how to modify the configuration files.

Tip

config/eslint.json must contain "parser": "babel-eslint" in order to force it to use the ES2015 syntax. This is set for JSCS in the Gulp configuration, however.

config/jscs.json must exist and must not be empty. If you don't need to specify any rules, use an empty JSON object ({}).

Now, if you were to execute gulp lint and our source code had a syntax error, you would receive an error message. The same goes for code styles; gulp code-style would generate an error if it didn't like the look of the code.

Next, you should add these two tasks to our build, emulate, and run tasks. Here's what the module.exports of gulp/tasks/build.js looks like after doing this:

module.exports = {
    deps: ["copy", "lint", "code-style"],
    task: …
}

Now, if you execute gulp build and there is a linting or code style error, the build will fail with an error. This gives a little more assurance that our code is at least syntactically valid prior to distributing or running the code.

Note

Linting and style checks do not guarantee that your code works logically. It just ensures that there are no syntax or style errors. If your program responds incorrectly to a gesture or processes some data incorrectly, a linter won't necessarily catch these issues.

You have been reading a chapter from
Mastering PhoneGap Mobile Application Development
Published in: Feb 2016
Publisher:
ISBN-13: 9781783288434
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image