You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`lesshint` is a tool to aid you in writing clean and consistent [Less](http://lesscss.org/).
10
8
11
-
*[Requirements](#requirements)
12
9
*[Installation](#installation)
13
10
*[Configuration](#configuration)
14
-
*[Custom linters](#custom-linters)
15
11
*[CLI usage](#cli-usage)
12
+
*[Linters](#linters)
16
13
*[Reporters](#reporters)
17
-
18
-
## Requirements
19
-
[Node.js](https://nodejs.org/) 0.12 (or later) or [io.js](https://iojs.org/) 1.0 (or later).
14
+
*[Developer resources](#developer-resources)
20
15
21
16
## Installation
22
-
Run the following command from the command line (add `-g` to install globally):
17
+
_[Node.js](https://nodejs.org/) 4 (or later) is required._
18
+
19
+
Run the following from the command line to install `lesshint` (add `-g` to install globally):
23
20
24
21
```
25
22
npm install lesshint
26
23
```
27
24
28
25
## Configuration
26
+
<<<<<<< HEAD
27
+
For information on how to configure `lesshint` and other available options, see the [user guide](/docs/user-guide/configuration.md).
28
+
29
+
Since `lesshint` is highly customizable we recommend you to also take a look at the [available rule options](/lib/linters/README.md) to tailor it to your needs.
30
+
=======
29
31
`lesshint` is customizable and we highly recommend you to look at the [available options](lib/linters/README.md) to tailor it to your needs.
30
32
31
33
Start by creating a `.lesshintrc` file in your project root and add your settings to it. It will be automatically loaded and merged with the default values.
@@ -193,6 +195,7 @@ We highly recommend the following resources which are all included with `lesshin
193
195
*[postcss-less](https://github.com/webschik/postcss-less) - PostCSS Less plugin docs.
194
196
*[postcss-selector-parser](https://github.com/postcss/postcss-selector-parser) - PostCSS plugin for working with selectors.
195
197
*[postcss-values-parser](https://github.com/lesshint/postcss-values-parser) - PostCSS plugin for working with values.
198
+
>>>>>>> master
196
199
197
200
## CLI usage
198
201
Run `lesshint` from the command-line by passing one or more files/directories to recursively scan.
@@ -201,14 +204,14 @@ Run `lesshint` from the command-line by passing one or more files/directories to
`-c`/`--config` | Specify the configuration file to use (will be merged with defaults).
207
210
`-e`/`--exclude` | A [minimatch glob pattern](https://github.com/isaacs/minimatch) or a file to exclude from being linted.
208
-
`-l`/`--linters` | Require paths of custom linters to add to the built-in list.
209
-
`-r`/`--reporter` | The reporter to use. See "Reporters" below for possible values.
210
-
`-V`/`--version` | Show version.
211
-
`-x`/`--max-warnings` | Number of warnings to allow before exiting with a non-zero code. Passing `-1` will allow any number of warnings to pass.
211
+
`-l`/`--linters` | Paths to custom linters to add to the built-in list. See "Linters" below for more information.
212
+
`-r`/`--reporter` | The reporter to use. See "Reporters" below for more information.
213
+
`-V`/`--version` | Show the version.
214
+
`-x`/`--max-warnings` | Number of warnings to allow before exiting with a non-zero code.
212
215
213
216
### Exit status codes
214
217
Depending on the linter results and options supplied, the exit status code returned by the CLI will differ.
@@ -224,64 +227,17 @@ Exit status code | Description
224
227
225
228
These codes were chosen with regards to the [preferable exit codes](http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=sysexits).
226
229
227
-
## Reporters
228
-
Reporters can be used to perform actions with the lint results, for example printing something to the terminal or generate custom reports.
229
-
230
-
### The reporter loading steps
231
-
232
-
1. If nothing is passed, a simple, default reporter will be used. This will just print all the warnings/errors found.
233
-
2. Pass the name of a Node module. If `lesshint` is installed globally only globally installed reporters are available (the normal Node module loading rules apply).
234
-
3. Pass a absolute or relative path to a custom reporter anywhere on the disk. Relative paths will be resolved against [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd).
235
-
236
-
These steps always apply, no matter whether you're using the CLI or calling `lesshint` from code.
If you're writing code which utilizes `lesshint`, for example a Gulp plugin you can use the `getReporter` method on the `lesshint` object to load a reporter using the same logic as `lesshint` does.
246
-
247
-
Pass the name of a module or a path to the `getReporter` method like this:
230
+
## Linters
231
+
In addition to the linters included with `lesshint`, it's also possible to include custom ones. For example to check something team or project specific.
248
232
249
-
```js
250
-
var defaultReporter =lesshint.getReporter(); //Or pass path to your custom reporter in getReporter
251
-
var promise =lesshint.checkFile('my-less-file.less');
252
-
253
-
promise.then(function(result){
254
-
defaultReporter.report(result);
255
-
})
256
-
```
233
+
For more information on using custom linters, see the [user guide](/docs/user-guide/linters.md).
257
234
258
-
### Writing your own reporter
259
-
In its simplest form, a reporter is just a function accepting some input. The most basic reporter possible:
260
-
261
-
```js
262
-
module.exports= {
263
-
name:'my-super-awesome-reporter', // Not required, but recommended
The reporter will be passed an array of objects representing each error:
271
-
272
-
```js
273
-
{
274
-
column:5,
275
-
file:'file.less',
276
-
fullPath:'path/to/file.less',
277
-
line:1,
278
-
linter:'spaceBeforeBrace',
279
-
message:'Opening curly brace should be preceded by one space.',
280
-
severity:'warning',
281
-
source:'.foo{'
282
-
}
283
-
```
235
+
## Reporters
236
+
Reporters are small modules that can be used to perform actions with the lint results, for example printing something to the terminal or generate custom reports.
284
237
285
-
It's then up to the reporter to do something with the errors. No `return`s or anything are needed. If running from the CLI, `lesshint` will handle the setting of correct exit codes.
238
+
For more information on using reporters, see the [user guide](/docs/user-guide/reporters.md).
286
239
287
-
Take a look at the [default reporter](https://github.com/lesshint/lesshint/blob/master/lib/reporters/default.js) for more information.
240
+
## Developer resources
241
+
*[Node API](/docs/developer-guide/API.md) - `lesshint`'s public Node API reference.
242
+
*[Developing linters](/docs/developer-guide/linters.md) - Guide for hacking on linters.
243
+
*[Developing reporters](/docs/developer-guide/reporters.md) - Guide for hacking on reporters.
Check a path asynchronously. If a file is passed it will check that, if a directory is passed it will check that recursively. Will respect `fileExtensions` and `excludedFiles` options.
Check a Less string synchronously and return an array of linter results.
56
+
57
+
The `checkPath` argument can be used to include a file name in lint results.
58
+
59
+
If a error occurs which isn't a Less parse error, an exception will be thrown.
60
+
61
+
```js
62
+
try {
63
+
constresults=lesshint.checkString('<my less code>', '/path/to/file');
64
+
} catch (e) {
65
+
console.error('Something bad happened.');
66
+
}
67
+
```
68
+
69
+
## `Lesshint.configure(config)`
70
+
Setup the options to use. This method must always be called before doing anything else, otherwise no options will be specified. Not even defaults.
71
+
72
+
The `config` argument is optional, but if a object is passed it'll be merged with the defaults.
73
+
74
+
```js
75
+
constconfig= {
76
+
emptyRule:false
77
+
};
78
+
79
+
lesshint.configure(config);
80
+
```
81
+
82
+
## `Lesshint.getConfig(path)`
83
+
Load a config file using `lesshint`'s logic.
84
+
85
+
If `path` is a file, it'll be loaded and its contents returned. If `path` is a directory, it will start looking for a `.lesshintrc` file in that directory, traversing up the directory structure until it finds one. If no config file is found, `undefined` will be returned.
The `Runner` class can be used to access the backbone of the CLI. To use it, start by initializing a `runner` instance.
4
+
5
+
```js
6
+
constRunner=require('lesshint').Runner;
7
+
8
+
// Any CLI option is valid
9
+
constoptions= {
10
+
maxWarnings:10, // Dashes in CLI flags are converted to camel case
11
+
paths: ['file.less'] // An array of path(s) to check
12
+
};
13
+
14
+
construnner=newRunner(options);
15
+
16
+
constresult=runner.run();
17
+
```
18
+
19
+
## `Runner.constructor()`
20
+
Set the options for a `Runner` instance.
21
+
22
+
```js
23
+
construnner=newRunner(options);
24
+
```
25
+
26
+
## `Runner.run()`
27
+
Run `lesshint` as if used through the CLI. All rule checks will be performed and reporters called.
28
+
29
+
A `Promise` will be returned. On success, it'll be resolved with a plain object. On failure, it'll be rejected with a `Runner` error object with a `status` property corresponding to a [CLI exit status code](/README.md#exit-status-codes).
If you wish to create your own linter or work on one of the core ones, this is the place for you!
3
+
4
+
First of all, in order to work properly, all linters are required to expose a few things.
5
+
*`name` - The name of the linter. While we don't enforce namespaces, we recommend it when creating custom linters, to prevent naming collisions.
6
+
*`nodeTypes` - An array of [PostCSS node types](http://api.postcss.org/postcss.html) that the linter wants to check.
7
+
*`lint` - The main lint method which will be called with the following arguments.
8
+
*`config` - The config object for this linter.
9
+
*`node` - The current node to lint.
10
+
11
+
If the linter doesn't find any errors or doesn't need/want to check the passed node for some reason it should return `undefined`. The linter doesn't need to check whether is's enabled or not, this will be checked beforehand by `lesshint`. If it's disabled, it'll never be called.
12
+
13
+
If the linter finds something it should return an array of result objects which looks like this:
14
+
15
+
```js
16
+
{
17
+
column:5,
18
+
file:'file.less',
19
+
fullPath:'/path/to/file.less',
20
+
line:1,
21
+
linter:'spaceBeforeBrace',
22
+
message:'Opening curly brace should be preceded by one space.',
23
+
position:4,
24
+
severity:'warning',
25
+
source:'.foo{'
26
+
}
27
+
```
28
+
29
+
If a linter doesn't set a value for a property, `lesshint` will set it. Most of the time, you'll only want to set `column`, `line`, and `message` while leaving the rest to `lesshint`.
30
+
31
+
A simple linter example:
32
+
```js
33
+
module.exports= {
34
+
name:'my-namespace/my-super-awesome-linter',
35
+
nodeTypes: ['decl'],
36
+
lint:function (config, node) {
37
+
constresults= [];
38
+
39
+
if (true) { // Nothing to lint, return early
40
+
return;
41
+
}
42
+
43
+
// Check some things...
44
+
45
+
// Return the results
46
+
return results;
47
+
}
48
+
```
49
+
50
+
When working with linters, we highly recommend the following resources which are all included with `lesshint`.
51
+
* [postcss](http://api.postcss.org/postcss.html) - Main PostCSS docs.
52
+
* [postcss-less](https://github.com/webschik/postcss-less) - PostCSS Less plugin docs.
53
+
* [postcss-selector-parser](https://github.com/postcss/postcss-selector-parser) - PostCSS plugin for working with selectors.
54
+
* [postcss-values-parser](https://github.com/lesshint/postcss-values-parser) - PostCSS plugin for working with values.
0 commit comments