Angular CLI - ng lint Command



This chapter explains the syntax, argument and options of ng lint command along with an example.

Syntax

The syntax for ng lint command is as follows −

ng lint <project> [options]
ng l <project> [options]

ng lint run the linting tool on angular app code. It checks the code quality of angular project specified. It uses TSLint as default linting tool and uses the default configuration available in tslint.json file.

Arguments

The argument for ng lint command is as follows −

Sr.No. Argument & Syntax Description
1 <project> The name of the project to lint.

Options

Options are optional parameters.

Sr.No. Option & Syntax Description
1 --configuration=configuration

The linting configuration to use.

Aliases: -c

2 --exclude Files to exclude from linting.
3 --files Files to include in linting.
4 --fix=true|false Fixes linting errors (may overwrite linted files).

Default: false

5 --force=true|false

Succeeds even if there was linting errors.

Default: false

6 --format=format

Output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist).

Default: prose

7 --help=true|false|json|JSON

Shows a help message for this command in the console.

Default: false

8 --silent=true|false

Show output text.

Default: false

9 --tsConfig=tsConfig The name of the TypeScript configuration file.
10 --tslintConfig=tslintConfig The name of the TSLint configuration file.
11 --typeCheck=true|false

Controls the type check for linting.

Default: false

First move to an angular project updated using ng build command.which is available at https://www.tutorialspoint.com/angular_cli/angular_cli_ng_build.htm.

Update goals.component.html and goals.component.ts as following −

goals.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-goals',
   templateUrl: './goals.component.html',
   styleUrls: ['./goals.component.css']
})
export class GoalsComponent implements OnInit {
   title = 'Goal Component'
   constructor() { }
   ngOnInit(): void {
   }
}

goals.component.html

<p>{{title}}</p>

Now run the linting command.

Example

An example for ng lint command is given below −

\>Node\>TutorialsPoint> ng lint
Linting "TutorialsPoint"...
ERROR: D:/Node/TutorialsPoint/src/app/goals/goals.component.ts:9:27 - Missing semicolon
ERROR: D:/Node/TutorialsPoint/src/app/goals/goals.component.ts:13:2 - file should end with a newline
Lint errors found in the listed files.

Here ng lint command has checked the code quality of application and prints linting status.

Now correct the errors in goals.component.ts.

goals.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-goals',
   templateUrl: './goals.component.html',
   styleUrls: ['./goals.component.css']
})
export class GoalsComponent implements OnInit {
   title = 'Goal Component';
   constructor() { }
   ngOnInit(): void {
   }
}

Now run the linting command.

Example

An example is stated below −

\>Node\>TutorialsPoint> ng lint
Linting "TutorialsPoint"...
All files pass linting.
Advertisements