Back our Angular 4 Book on Kickstarter.

Linting TypeScript - Building Angular 2 Applications with Gulp - Part 2

This is part of an ongoing series about building Angular 2 TypeScript applications with Gulp. Check out Part 1 here.

This section will build a Gulp task for Linting TypeScript. The lint tool is a syntax checker that looks for errors.

Install Dependencies

First, install a NodeJS Plugin, tslint and a gulp plugin, gulp-tslint.

Run this code:


npm install --save-dev tslint gulp-tslint

You should see results like this:

Once the install is complete, you're good to start creating your gulpfile.js

Create the Gulp Lint Task

If you don't already have one, create a file named gulpfile.js in the root directory of the project. Our first task to create is going to lint the TypeScript code. The first step is to import the gulp and gulp-tslint libraries:


var gulp = require("gulp");
var tslint = require('gulp-tslint');

This will make them available for use within our gulp script.

Next, I'm going to define the location of the typeScriptSource:


var sourceRoot = "src/";
var typeScriptSource = [sourceRoot + "**/*.ts"];

This is split up into two variables. The first just points to the source directory and that will be used throughout our script. The second uses the sourceRoot to create a wildcard glob that will cover all existing type script files in the main directory.

Now, create the gulp task to lint the TypeScript:


gulp.task('tslint', function() {
});

This is an empty task that does nothing. Use gulp.src() to tell gulp which items to process:


return gulp.src(typeScriptSource)

Then, run the tslint() task:


.pipe(tslint({
formatter: 'prose'
}))

This will parse the TypeScript, and make a collection of any errors. Then, report the errors:


.pipe(tslint.report());

That is the completed task. Before we run it, we'll need to configure it. Specific configuration options are beyond the scope of this article. Put this tslint.json file, in your root directory and you'll be fine. The file comes from the official Angular 2 Quickstart documentation.

Run the task:


gulp tslint

You'll see something like this:

No issues. What happens when there are issues? I removed a semi-colon and added some random characters to the main.ts file and reran the lint process:

It correctly found the error. The lint process performs a syntax validation; it does not validate the code for accuracy of imports or other bugs; but those will be flagged as part of the compilation process which we'll tackle in part 3.

Get our expanded 38-page version: Building Angular 2 Applications with Gulp.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.