1 | <h1 id="canonical">Canonical</h1>
|
2 |
|
3 | [![Travis build status](http://img.shields.io/travis/gajus/canonical/master.svg?style=flat-square)](https://travis-ci.org/gajus/canonical)
|
4 | [![NPM version](http://img.shields.io/npm/v/canonical.svg?style=flat-square)](https://www.npmjs.com/package/canonical)
|
5 | [![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
|
6 |
|
7 | * [Canonical](#canonical)
|
8 | * [Badge](#canonical-badge)
|
9 | * [Rules](#canonical-rules)
|
10 | * [Usage](#canonical-usage)
|
11 | * [Command Line](#canonical-usage-command-line)
|
12 | * [Gulp](#canonical-usage-gulp)
|
13 | * [Node.js API](#canonical-usage-node-js-api)
|
14 |
|
15 |
|
16 | Canonical code style linter and formatter for JavaScript, SCSS and CSS.
|
17 |
|
18 | <h2 id="canonical-badge">Badge</h2>
|
19 |
|
20 | Use this in one of your projects? Include one of these badges in your README to let people know that your code is using the Canonical style.
|
21 |
|
22 | [![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
|
23 |
|
24 | ```markdown
|
25 | [![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
|
26 | ```
|
27 |
|
28 | <h2 id="canonical-rules">Rules</h2>
|
29 |
|
30 | Canonical rules are composed of the following packages:
|
31 |
|
32 | * [`eslint-config-canonical`](https://github.com/gajus/eslint-config-canonical)
|
33 | * [`eslint-config-canonical-jsdoc`](https://github.com/gajus/eslint-config-canonical-jsdoc)
|
34 | * [`eslint-config-canonical-lodash`](https://github.com/gajus/eslint-config-canonical-lodash)
|
35 | * [`eslint-config-canonical-react`](https://github.com/gajus/eslint-config-canonical-react)
|
36 |
|
37 |
|
38 | <h2 id="canonical-usage">Usage</h2>
|
39 |
|
40 | <h3 id="canonical-usage-command-line">Command Line</h3>
|
41 |
|
42 | The easiest way to use Canonical to check your code style is to install it as a Node command line program.
|
43 |
|
44 | ```sh
|
45 | npm install canonical -g
|
46 | ```
|
47 |
|
48 | After that, you can run `canonical` program on any JavaScript, SCSS, CSS or JSON file.
|
49 |
|
50 | <h4 id="canonical-usage-command-line-linting">Linting</h4>
|
51 |
|
52 | ```sh
|
53 | # Lint all JavaScript in ./src/ directory.
|
54 | canonical lint ./src/**/*.js
|
55 |
|
56 | # Lint all CSS in ./src/ directory.
|
57 | canonical lint ./src/**/*.css
|
58 |
|
59 | # Lint all JavaScript and CSS in ./src/ directory.
|
60 | canonical lint ./src/**/*.js ./src/**/*.css
|
61 |
|
62 | # List all supported formats in ./src/ and the descending directories.
|
63 | canonical lint ./src/
|
64 | ```
|
65 |
|
66 | <h4 id="canonical-usage-command-line-fixing">Fixing</h4>
|
67 |
|
68 | ```sh
|
69 | # Fix all JavaScript in ./src/ directory.
|
70 | canonical fix ./src/**/*.js
|
71 |
|
72 | # Fix all CSS in ./src/ directory.
|
73 | canonical fix ./src/**/*.css
|
74 |
|
75 | # Fix all JavaScript and CSS in ./src/ directory.
|
76 | canonical fix ./src/**/*.js ./src/**/*.css
|
77 |
|
78 | # Fix all supported formats in ./src/ and the descending directories.
|
79 | canonical fix ./src/
|
80 | ```
|
81 |
|
82 | <h4 id="canonical-usage-command-line-reading-from-stdin">Reading from <code>stdin</code></h4>
|
83 |
|
84 | `canonical` program can read from stdin, e.g.
|
85 |
|
86 | ```
|
87 | echo 'var test;' | canonical lint --stdin --syntax js --output-format json
|
88 | ```
|
89 |
|
90 | When reading from `stdin`, it is required to provide `--syntax` option. See [Command Line Options](#command-line-options).
|
91 |
|
92 | <h4 id="canonical-usage-command-line-command-line-options">Command Line Options</h4>
|
93 |
|
94 | ```
|
95 | > canonical --help
|
96 |
|
97 | Commands:
|
98 | fix Fix code format.
|
99 | lint Report code format errors.
|
100 |
|
101 | Options:
|
102 | --help Show help [boolean]
|
103 | ```
|
104 |
|
105 | ```
|
106 | canonical fix --help
|
107 |
|
108 | Options:
|
109 | --help Show help [boolean]
|
110 | --stdin Used to indicate that subject body will be read from stdin.
|
111 | [boolean] [default: false]
|
112 | --syntax Syntax of the input. [choices: "js", "json", "css", "scss"]
|
113 | ```
|
114 |
|
115 | ```
|
116 | canonical lint --help
|
117 |
|
118 | Options:
|
119 | --help Show help [boolean]
|
120 | --file-path Name of the file being linted with stdin (if any). Used in
|
121 | reporting. [string] [default: "<text>"]
|
122 | --stdin Used to indicate that subject body will be read from stdin.
|
123 | [boolean] [default: false]
|
124 | --syntax Syntax of the input. [choices: "js", "json", "css", "scss"]
|
125 | --output-format [choices: "json", "checkstyle", "table"] [default: "table"]
|
126 | ```
|
127 |
|
128 | <h3 id="canonical-usage-gulp">Gulp</h3>
|
129 |
|
130 | Using [Canonical](https://github.com/gajus/canonical) does not require a [Gulp](http://gulpjs.com/) plugin. Canonical [program interface](https://github.com/gajus/canonical#program-interface) gives access to all features.
|
131 |
|
132 | Use Canonical API in combination with a glob pattern matcher (e.g. [globby](https://www.npmjs.com/package/globby)) to lint multiple files, e.g.
|
133 |
|
134 | ```js
|
135 | import gulp from 'gulp';
|
136 | import globby from 'globby';
|
137 |
|
138 | import {
|
139 | lintText,
|
140 | lintFiles,
|
141 | getFormatter
|
142 | } from 'canonical/es';
|
143 |
|
144 | gulp.task('lint-javascript', () => {
|
145 | return globby(['./**/*.js'])
|
146 | .then((paths) => {
|
147 | let formatter,
|
148 | report;
|
149 |
|
150 | formatter = getFormatter();
|
151 | report = lintFiles(paths);
|
152 |
|
153 | if (report.errorCount || report.warningCount) {
|
154 | console.log(formatter(report.results));
|
155 | }
|
156 | });
|
157 | });
|
158 | ```
|
159 |
|
160 | This example is written using ES6 syntax. If you want your `gulpfile.js` to use ES6 syntax, you have to execute it using [Babel](babeljs.io) or an equivalent code-to-code compiler, e.g.
|
161 |
|
162 | ```sh
|
163 | babel-node ./node_modules/.bin/gulp lint-javascript
|
164 | ```
|
165 |
|
166 | <h3 id="canonical-usage-node-js-api">Node.js API</h3>
|
167 |
|
168 | ```js
|
169 | import {
|
170 | fixFiles,
|
171 | fixText,
|
172 | getFormatter,
|
173 | lintFiles,
|
174 | lintText
|
175 | } from 'canonical';
|
176 |
|
177 | /**
|
178 | * @returns {function}
|
179 | */
|
180 | getFormatter;
|
181 |
|
182 | /**
|
183 | * @typedef fixFiles~report
|
184 | * @property {fixText~result[]} results
|
185 | */
|
186 |
|
187 | /**
|
188 | * @param {string[]} filePaths
|
189 | * @return {fixFiles~report}
|
190 | */
|
191 |
|
192 | fixFiles;
|
193 |
|
194 | /**
|
195 | * @typedef fixText~result
|
196 | * @property {string} filePath
|
197 | * @property {string} output
|
198 | */
|
199 |
|
200 | /**
|
201 | * @typedef fixText~options
|
202 | * @property {string} syntax (supported languages: 'css', 'js', 'json', 'scss').
|
203 | */
|
204 |
|
205 | /**
|
206 | * @param {string} text
|
207 | * @param {fixText~options} options
|
208 | * @return {fixText~result}
|
209 | */
|
210 | fixText;
|
211 |
|
212 | /**
|
213 | * @typedef lintFiles~report
|
214 | * @property {lintText~result[]} results
|
215 | * @property {number} errorCount
|
216 | * @property {number} warningCount
|
217 | */
|
218 |
|
219 | /**
|
220 | * @param {string[]} filePaths
|
221 | * @return {lintFiles~report}
|
222 | */
|
223 | lintFiles;
|
224 |
|
225 | /**
|
226 | * @typedef lintText~message
|
227 | * @property {string} ruleId
|
228 | * @property {number} severity
|
229 | * @property {string} message
|
230 | * @property {number} line
|
231 | * @property {number} column
|
232 | * @property {string} nodeType
|
233 | * @property {string} source
|
234 | */
|
235 |
|
236 | /**
|
237 | * @typedef lintText~result
|
238 | * @property {string} filePath
|
239 | * @property {lintFiles~message[]} messages
|
240 | * @property {number} errorCount
|
241 | * @property {number} warningCount
|
242 | */
|
243 |
|
244 | /**
|
245 | * @typedef lintText~options
|
246 | * @property {string} syntax (supported languages: 'css', 'js', 'json', 'scss').
|
247 | */
|
248 |
|
249 | /**
|
250 | * @param {string} text
|
251 | * @param {lintText~options} options
|
252 | * @return {lintText~result}
|
253 | */
|
254 | lintText;
|
255 | ```
|
256 |
|