Skip to content

Commit 3addaf0

Browse files
authored
Documentation improvements (lesshint#335)
1 parent f8473d1 commit 3addaf0

File tree

9 files changed

+413
-71
lines changed

9 files changed

+413
-71
lines changed

README.md

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
[![Build Status](https://travis-ci.org/lesshint/lesshint.svg?branch=master)](https://travis-ci.org/lesshint/lesshint)
44
[![Build status](https://ci.appveyor.com/api/projects/status/d1u4477uxtv6dygk/branch/master?svg=true)](https://ci.appveyor.com/project/lesshint/lesshint/branch/master)
55
[![Coverage Status](https://coveralls.io/repos/lesshint/lesshint/badge.svg?branch=master)](https://coveralls.io/r/lesshint/lesshint?branch=master)
6-
[![Dependency Status](https://david-dm.org/lesshint/lesshint.svg?theme=shields.io&style=flat)](https://david-dm.org/lesshint/lesshint)
7-
[![devDependency Status](https://david-dm.org/lesshint/lesshint/dev-status.svg?theme=shields.io&style=flat)](https://david-dm.org/lesshint/lesshint#info=devDependencies)
86

97
`lesshint` is a tool to aid you in writing clean and consistent [Less](http://lesscss.org/).
108

11-
* [Requirements](#requirements)
129
* [Installation](#installation)
1310
* [Configuration](#configuration)
14-
* [Custom linters](#custom-linters)
1511
* [CLI usage](#cli-usage)
12+
* [Linters](#linters)
1613
* [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)
2015

2116
## 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):
2320

2421
```
2522
npm install lesshint
2623
```
2724

2825
## 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+
=======
2931
`lesshint` is customizable and we highly recommend you to look at the [available options](lib/linters/README.md) to tailor it to your needs.
3032

3133
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
193195
* [postcss-less](https://github.com/webschik/postcss-less) - PostCSS Less plugin docs.
194196
* [postcss-selector-parser](https://github.com/postcss/postcss-selector-parser) - PostCSS plugin for working with selectors.
195197
* [postcss-values-parser](https://github.com/lesshint/postcss-values-parser) - PostCSS plugin for working with values.
198+
>>>>>>> master
196199
197200
## CLI usage
198201
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
201204
lesshint src/less/ lib/style.less
202205
```
203206

204-
Available Flags | Description
205-
----------------------|----------------------------------------------
207+
Available Flags | Description
208+
----------------------|---------------------
206209
`-c`/`--config` | Specify the configuration file to use (will be merged with defaults).
207210
`-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.
212215

213216
### Exit status codes
214217
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
224227

225228
These codes were chosen with regards to the [preferable exit codes](http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=sysexits).
226229

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.
237-
238-
### Using reporters from the CLI
239-
```bash
240-
lesshint --reporter my-super-awesome-reporter file.less
241-
lesshint --reporter /path/to/my/super/awesome/reporter.js file.less
242-
```
243-
244-
### Using reporters from code
245-
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.
248232

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).
257234

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
264-
report: function (errors) {
265-
console.log(errors.length ? 'Errors found' : 'No errors');
266-
}
267-
};
268-
```
269-
270-
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.
284237

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).
286239

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.

docs/developer-guide/API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Node API
2+
3+
* [Lesshint class](/docs/developer-guide/api/lesshint.md)
4+
* [Runner class](/docs/developer-guide/api/runner.md)
5+
6+
_Note: Anything not documented here should be considered a internal API which may change at any time._

docs/developer-guide/api/lesshint.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Lesshint class
2+
3+
The `Lesshint` class is the core of `lesshint`, from here all operations can be performed. To use it, start by initalizing a `lesshint` instance.
4+
5+
```js
6+
const Lesshint = require('lesshint').Lesshint;
7+
8+
const lesshint = new Lesshint();
9+
10+
lesshint.configure();
11+
12+
// Do your thing using the methods below
13+
```
14+
15+
## `Lesshint.checkDirectory(checkPath)`
16+
Check a directory recursively. Will respect `fileExtensions` and `excludedFiles` options.
17+
18+
A `Promise` will be returned.
19+
20+
```js
21+
const result = lesshint.checkDirectory('/path/to/my/directory');
22+
23+
result.then((results) => {
24+
25+
});
26+
```
27+
28+
## `Lesshint.checkFile(checkPath)`
29+
Check a single file asynchronously. Will not check `fileExtensions` and `excludedFiles` options.
30+
31+
A `Promise` will be returned.
32+
33+
```js
34+
const result = lesshint.checkFile('/path/to/my/file.less');
35+
36+
result.then((results) => {
37+
38+
});
39+
```
40+
41+
## `Lesshint.checkPath(checkPath)`
42+
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.
43+
44+
A `Promise` will be returned.
45+
46+
```js
47+
const result = lesshint.checkPath('/path/to/my/directory');
48+
49+
result.then((results) => {
50+
51+
});
52+
```
53+
54+
## `Lesshint.checkString(input, checkPath)`
55+
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+
const results = 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+
const config = {
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.
86+
87+
```js
88+
const config = lesshint.getConfig('/path/to/my/config.json');
89+
```
90+
91+
92+
## `Lesshint.getReporter(reporter)`
93+
Load a reporter using `lesshint`'s logic.
94+
95+
The `reporter` argument can be one of the following.
96+
* The name of a module. The normal Node.js loading rules for global/local modules apply.
97+
* Path to a reporter. Relative paths will be resolved against [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd).
98+
* A already initialized reporter object which will just be returned again.
99+
100+
If nothing is passed, the default reporter will be returned. If the reporter loading fails for some reason, `false` will be returned.
101+
102+
```js
103+
const reporter = lesshint.getReporter('/path/to/my/reporter.js');
104+
```

docs/developer-guide/api/runner.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Runner class
2+
3+
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+
const Runner = require('lesshint').Runner;
7+
8+
// Any CLI option is valid
9+
const options = {
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+
const runner = new Runner(options);
15+
16+
const result = runner.run();
17+
```
18+
19+
## `Runner.constructor()`
20+
Set the options for a `Runner` instance.
21+
22+
```js
23+
const runner = new Runner(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).
30+
31+
```js
32+
const result = runner.run();
33+
34+
result.then((output) => {
35+
36+
});
37+
```

docs/developer-guide/linters.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Developing linters
2+
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+
const results = [];
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

Comments
 (0)