| @@ -0,0 +1,73 @@ | ||
| # Working with Custom Parsers | ||
|
|
||
| If you want to use your own parser and provide additional capabilities for your rules, you can specify your own custom parser. If a `parseForESLint` method is exposed on the parser, this method will be used to parse the code. Otherwise, the `parse` method will be used. Both methods should take in the source code as the first argument, and an optional configuration object as the second argument (provided as `parserOptions` in a config file). The `parse` method should simply return the AST. The `parseForESLint` method should return an object that contains the required property `ast` and optional properties `services`, `scopeManager`, and `visitorKeys`. | ||
|
|
||
| * `ast` should contain the AST. | ||
| * `services` can contain any parser-dependent services (such as type checkers for nodes). The value of the `services` property is available to rules as `context.parserServices`. Default is an empty object. | ||
| * `scopeManager` can be a [ScopeManager](./scope-manager-interface.md) object. Custom parsers can use customized scope analysis for experimental/enhancement syntaxes. Default is the `ScopeManager` object which is created by [eslint-scope](https://github.com/eslint/eslint-scope). | ||
| * Support for `scopeManager` was added in ESLint v4.14.0. ESLint versions which support `scopeManager` will provide an `eslintScopeManager: true` property in `parserOptions`, which can be used for feature detection. | ||
| * `visitorKeys` can be an object to customize AST traversal. The keys of the object are the type of AST nodes. Each value is an array of the property names which should be traversed. Default is [KEYS of `eslint-visitor-keys`](https://github.com/eslint/eslint-visitor-keys#evkkeys). | ||
| * Support for `visitorKeys` was added in ESLint v4.14.0. ESLint versions which support `visitorKeys` will provide an `eslintVisitorKeys: true` property in `parserOptions`, which can be used for feature detection. | ||
|
|
||
| You can find an ESLint parser project [here](https://github.com/typescript-eslint/typescript-eslint). | ||
|
|
||
| ```json | ||
| { | ||
| "parser": "./path/to/awesome-custom-parser.js" | ||
| } | ||
| ``` | ||
|
|
||
| ```javascript | ||
| var espree = require("espree"); | ||
| // awesome-custom-parser.js | ||
| exports.parseForESLint = function(code, options) { | ||
| return { | ||
| ast: espree.parse(code, options), | ||
| services: { | ||
| foo: function() { | ||
| console.log("foo"); | ||
| } | ||
| }, | ||
| scopeManager: null, | ||
| visitorKeys: null | ||
| }; | ||
| }; | ||
| ``` | ||
|
|
||
| ## The AST specification | ||
|
|
||
| The AST that custom parsers should create is based on [ESTree](https://github.com/estree/estree). The AST requires some additional properties about detail information of the source code. | ||
|
|
||
| ### All nodes: | ||
|
|
||
| All nodes must have `range` property. | ||
|
|
||
| * `range` (`number[]`) is an array of two numbers. Both numbers are a 0-based index which is the position in the array of source code characters. The first is the start position of the node, the second is the end position of the node. `code.slice(node.range[0], node.range[1])` must be the text of the node. This range does not include spaces/parentheses which are around the node. | ||
| * `loc` (`SourceLocation`) must not be `null`. [The `loc` property is defined as nullable by ESTree](https://github.com/estree/estree/blob/25834f7247d44d3156030f8e8a2d07644d771fdb/es5.md#node-objects), but ESLint requires this property. On the other hand, `SourceLocation#source` property can be `undefined`. ESLint does not use the `SourceLocation#source` property. | ||
|
|
||
| The `parent` property of all nodes must be rewriteable. ESLint sets each node's `parent` property to its parent node while traversing, before any rules have access to the AST. | ||
|
|
||
| ### The `Program` node: | ||
|
|
||
| The `Program` node must have `tokens` and `comments` properties. Both properties are an array of the below Token interface. | ||
|
|
||
| ```ts | ||
| interface Token { | ||
| type: string; | ||
| loc: SourceLocation; | ||
| range: [number, number]; // See "All nodes:" section for details of `range` property. | ||
| value: string; | ||
| } | ||
| ``` | ||
|
|
||
| * `tokens` (`Token[]`) is the array of tokens which affect the behavior of programs. Arbitrary spaces can exist between tokens, so rules check the `Token#range` to detect spaces between tokens. This must be sorted by `Token#range[0]`. | ||
| * `comments` (`Token[]`) is the array of comment tokens. This must be sorted by `Token#range[0]`. | ||
|
|
||
| The range indexes of all tokens and comments must not overlap with the range of other tokens and comments. | ||
|
|
||
| ### The `Literal` node: | ||
|
|
||
| The `Literal` node must have `raw` property. | ||
|
|
||
| * `raw` (`string`) is the source code of this literal. This is the same as `code.slice(node.range[0], node.range[1])`. |
| @@ -0,0 +1,16 @@ | ||
| # npm two-factor authentication | ||
|
|
||
| The `eslint` npm account has two-factor authentication (2FA) enabled. The 2FA secret is distributed using a team on [Keybase](https://keybase.io). Anyone doing a release of a package from the Jenkins server needs to have access to the 2FA secret. | ||
|
|
||
| If you're on ESLint's TSC, you should perform the following steps to obtain the 2FA secret: | ||
|
|
||
| 1. Download the [Keybase app](https://keybase.io/download) on a smartphone. | ||
| 1. Open the app and create an account. | ||
| 1. From the app, link your Keybase username with your GitHub username. (At the time of writing, the UI for this is to tap the face icon in the bottom-left of the app, then the profile picture in the top-right, then tap "Prove your GitHub" and follow the instructions.) | ||
| 1. Mention your Keybase username in the team chatroom, and wait for someone to add you to the Keybase team. | ||
| 1. Download an authenticator app like [Google Authenticator](https://support.google.com/accounts/answer/1066447) or [Authy](https://authy.com/), if you don't have one installed already. | ||
| 1. In the Keybase app, navigate to the Keybase filesystem (at the time of writing, the UI for this is to tap the hamburger icon in the bottom-right, then tap "Files") and then navigate to `/team/eslint/auth`. | ||
| * If your authenticator app is downloaded on the same device as your Keybase app (this will usually be the case if you're using the Keybase mobile app), then open `npm_2fa_code.txt` and copy the contents to the clipboard. Open your authenticator app, and paste the contents as a new key (by selecting something like "Enter a provided key" or "Enter key manually"). | ||
| * If your authenticator app is downloaded on a *different* device from your Keybase app (e.g. if you're using a Keybase desktop app), then open `npm_2fa_code.png` and scan it as a QR code from your authenticator app. | ||
|
|
||
| You should now be able to generate 6-digit 2FA codes for the `eslint` npm account using your authenticator app. |
| @@ -0,0 +1,22 @@ | ||
| # Working Groups | ||
|
|
||
| The ESLint TSC may form working groups to focus on a specific area of the project. | ||
|
|
||
| ## Creating a Working Group | ||
|
|
||
| Working groups are created by sending an email to the team mailing list. Each working group: | ||
|
|
||
| 1. Must have a GitHub team under the "ESLint Team" top-level GitHub team. (The GitHub team name should end with "WG" to distinguish it from other types of teams.) | ||
| 1. Must have at least one TSC member. | ||
| 1. May have any number of Committers and Reviewers. | ||
| 1. Must have at least two members. | ||
|
|
||
| Active working groups are listed on the [team page](https://eslint.org/team). | ||
|
|
||
| ## How Working Groups Work | ||
|
|
||
| Each working group is responsible for its own inner working. Working groups can decide how large or small they should be (so long as there is at least two members), when and who to add or remove from the working group, and how to accomplish their objectives. | ||
|
|
||
| Working groups may be temporary or permanent. | ||
|
|
||
| If working groups intend to make a significant change to the ESLint project, the proposal must still be approved by the TSC. |
| @@ -0,0 +1,281 @@ | ||
| # enforce line breaks after opening and before closing array brackets (array-bracket-newline) | ||
|
|
||
| A number of style guides require or disallow line breaks inside of array brackets. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces line breaks after opening and before closing array brackets. | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has either a string option: | ||
|
|
||
| * `"always"` requires line breaks inside brackets | ||
| * `"never"` disallows line breaks inside brackets | ||
| * `"consistent"` requires consistent usage of linebreaks for each pair of brackets. It reports an error if one bracket in the pair has a linebreak inside it and the other bracket does not. | ||
|
|
||
| Or an object option (Requires line breaks if any of properties is satisfied. Otherwise, disallows line breaks): | ||
|
|
||
| * `"multiline": true` (default) requires line breaks if there are line breaks inside elements or between elements. If this is false, this condition is disabled. | ||
| * `"minItems": null` (default) requires line breaks if the number of elements is at least the given integer. If this is 0, this condition will act the same as the option `"always"`. If this is `null` (the default), this condition is disabled. | ||
|
|
||
| ### always | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"always"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", "always"]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [1, | ||
| 2]; | ||
| var e = [function foo() { | ||
| dosomething(); | ||
| }]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"always"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", "always"]*/ | ||
| var a = [ | ||
| ]; | ||
| var b = [ | ||
| 1 | ||
| ]; | ||
| var c = [ | ||
| 1, 2 | ||
| ]; | ||
| var d = [ | ||
| 1, | ||
| 2 | ||
| ]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### never | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"never"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", "never"]*/ | ||
| var a = [ | ||
| ]; | ||
| var b = [ | ||
| 1 | ||
| ]; | ||
| var c = [ | ||
| 1, 2 | ||
| ]; | ||
| var d = [ | ||
| 1, | ||
| 2 | ||
| ]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"never"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", "never"]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [1, | ||
| 2]; | ||
| var e = [function foo() { | ||
| dosomething(); | ||
| }]; | ||
| ``` | ||
|
|
||
| ### consistent | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"consistent"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", "consistent"]*/ | ||
| var a = [1 | ||
| ]; | ||
| var b = [ | ||
| 1]; | ||
| var c = [function foo() { | ||
| dosomething(); | ||
| } | ||
| ] | ||
| var d = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }] | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"consistent"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", "consistent"]*/ | ||
| var a = []; | ||
| var b = [ | ||
| ]; | ||
| var c = [1]; | ||
| var d = [ | ||
| 1 | ||
| ]; | ||
| var e = [function foo() { | ||
| dosomething(); | ||
| }]; | ||
| var f = [ | ||
| function foo() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### multiline | ||
|
|
||
| Examples of **incorrect** code for this rule with the default `{ "multiline": true }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", { "multiline": true }]*/ | ||
| var a = [ | ||
| ]; | ||
| var b = [ | ||
| 1 | ||
| ]; | ||
| var c = [ | ||
| 1, 2 | ||
| ]; | ||
| var d = [1, | ||
| 2]; | ||
| var e = [function foo() { | ||
| dosomething(); | ||
| }]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the default `{ "multiline": true }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", { "multiline": true }]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [ | ||
| 1, | ||
| 2 | ||
| ]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### minItems | ||
|
|
||
| Examples of **incorrect** code for this rule with the `{ "minItems": 2 }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/ | ||
| var a = [ | ||
| ]; | ||
| var b = [ | ||
| 1 | ||
| ]; | ||
| var c = [1, 2]; | ||
| var d = [1, | ||
| 2]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `{ "minItems": 2 }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [ | ||
| 1, 2 | ||
| ]; | ||
| var d = [ | ||
| 1, | ||
| 2 | ||
| ]; | ||
| var e = [function foo() { | ||
| dosomething(); | ||
| }]; | ||
| ``` | ||
|
|
||
| ### multiline and minItems | ||
|
|
||
| Examples of **incorrect** code for this rule with the `{ "multiline": true, "minItems": 2 }` options: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/ | ||
| var a = [ | ||
| ]; | ||
| var b = [ | ||
| 1 | ||
| ]; | ||
| var c = [1, 2]; | ||
| var d = [1, | ||
| 2]; | ||
| var e = [function foo() { | ||
| dosomething(); | ||
| }]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `{ "multiline": true, "minItems": 2 }` options: | ||
|
|
||
| ```js | ||
| /*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [ | ||
| 1, 2 | ||
| ]; | ||
| var d = [ | ||
| 1, | ||
| 2 | ||
| ]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you don't want to enforce line breaks after opening and before closing array brackets, don't enable this rule. | ||
|
|
||
| ## Compatibility | ||
|
|
||
| * **JSCS:** [validateNewlineAfterArrayElements](https://jscs-dev.github.io/rule/validateNewlineAfterArrayElements) | ||
|
|
||
| ## Related Rules | ||
|
|
||
| * [array-bracket-spacing](array-bracket-spacing.md) |
| @@ -0,0 +1,304 @@ | ||
| # enforce line breaks between array elements (array-element-newline) | ||
|
|
||
| A number of style guides require or disallow line breaks between array elements. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces line breaks between array elements. | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has either a string option: | ||
|
|
||
| * `"always"` (default) requires line breaks between array elements | ||
| * `"never"` disallows line breaks between array elements | ||
| * `"consistent"` requires consistent usage of linebreaks between array elements | ||
|
|
||
| Or an object option (Requires line breaks if any of properties is satisfied. Otherwise, disallows line breaks): | ||
|
|
||
| * `"multiline": <boolean>` requires line breaks if there are line breaks inside elements. If this is false, this condition is disabled. | ||
| * `"minItems": <number>` requires line breaks if the number of elements is at least the given integer. If this is 0, this condition will act the same as the option `"always"`. If this is `null` (the default), this condition is disabled. | ||
|
|
||
| ### always | ||
|
|
||
| Examples of **incorrect** code for this rule with the default `"always"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", "always"]*/ | ||
| var c = [1, 2]; | ||
| var d = [1, 2, 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the default `"always"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", "always"]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, | ||
| 2]; | ||
| var d = [1, | ||
| 2, | ||
| 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, | ||
| function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### never | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"never"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", "never"]*/ | ||
| var c = [ | ||
| 1, | ||
| 2 | ||
| ]; | ||
| var d = [ | ||
| 1, | ||
| 2, | ||
| 3 | ||
| ]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, | ||
| function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"never"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", "never"]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [1, 2, 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### consistent | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"consistent"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", "consistent"]*/ | ||
| var a = [ | ||
| 1, 2, | ||
| 3 | ||
| ]; | ||
| var b = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, function bar() { | ||
| dosomething(); | ||
| }, | ||
| function baz() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"consistent"` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", "consistent"]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [1, 2, 3]; | ||
| var e = [ | ||
| 1, | ||
| 2 | ||
| ]; | ||
| var f = [ | ||
| 1, | ||
| 2, | ||
| 3 | ||
| ]; | ||
| var g = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, function bar() { | ||
| dosomething(); | ||
| }, function baz() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| var h = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, | ||
| function bar() { | ||
| dosomething(); | ||
| }, | ||
| function baz() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### multiline | ||
|
|
||
| Examples of **incorrect** code for this rule with the `{ "multiline": true }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", { "multiline": true }]*/ | ||
| var d = [1, | ||
| 2, 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `{ "multiline": true }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", { "multiline": true }]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [1, 2, 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, | ||
| function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### minItems | ||
|
|
||
| Examples of **incorrect** code for this rule with the `{ "minItems": 3 }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", { "minItems": 3 }]*/ | ||
| var c = [1, | ||
| 2]; | ||
| var d = [1, 2, 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, | ||
| function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `{ "minItems": 3 }` option: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", { "minItems": 3 }]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [1, | ||
| 2, | ||
| 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| ### multiline and minItems | ||
|
|
||
| Examples of **incorrect** code for this rule with the `{ "multiline": true, "minItems": 3 }` options: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", { "multiline": true, "minItems": 3 }]*/ | ||
| var c = [1, | ||
| 2]; | ||
| var d = [1, 2, 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `{ "multiline": true, "minItems": 3 }` options: | ||
|
|
||
| ```js | ||
| /*eslint array-element-newline: ["error", { "multiline": true, "minItems": 3 }]*/ | ||
| var a = []; | ||
| var b = [1]; | ||
| var c = [1, 2]; | ||
| var d = [1, | ||
| 2, | ||
| 3]; | ||
| var e = [ | ||
| function foo() { | ||
| dosomething(); | ||
| }, | ||
| function bar() { | ||
| dosomething(); | ||
| } | ||
| ]; | ||
| ``` | ||
|
|
||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you don't want to enforce linebreaks between array elements, don't enable this rule. | ||
|
|
||
| ## Compatibility | ||
|
|
||
| * **JSCS:** [validateNewlineAfterArrayElements](https://jscs-dev.github.io/rule/validateNewlineAfterArrayElements) | ||
|
|
||
| ## Related Rules | ||
|
|
||
| * [array-bracket-spacing](array-bracket-spacing.md) | ||
| * [array-bracket-newline](array-bracket-newline.md) | ||
| * [object-property-newline](object-property-newline.md) | ||
| * [object-curly-spacing](object-curly-spacing.md) | ||
| * [object-curly-newline](object-curly-newline.md) | ||
| * [max-statements-per-line](max-statements-per-line.md) | ||
| * [block-spacing](block-spacing.md) | ||
| * [brace-style](brace-style.md) |
| @@ -0,0 +1,35 @@ | ||
| # enforce default parameters to be last (default-param-last) | ||
|
|
||
| Putting default parameter at last allows function calls to omit optional tail arguments. | ||
|
|
||
| ```js | ||
| // Correct: optional argument can be omitted | ||
| function createUser(id, isAdmin = false) {} | ||
| createUser("tabby") | ||
| // Incorrect: optional argument can **not** be omitted | ||
| function createUser(isAdmin = false, id) {} | ||
| createUser(undefined, "tabby") | ||
| ``` | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces default parameters to be the last of parameters. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| /* eslint default-param-last: ["error"] */ | ||
| function f(a = 0, b) {} | ||
| function f(a, b = 0, c) {} | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| /* eslint default-param-last: ["error"] */ | ||
| function f(a, b = 0) {} | ||
| ``` |
| @@ -0,0 +1,24 @@ | ||
| # Enforce "for" loop update clause moving the counter in the right direction. (for-direction) | ||
|
|
||
| ## Rule Details | ||
|
|
||
| A `for` loop with a stop condition that can never be reached, such as one with a counter that moves in the wrong direction, will run infinitely. While there are occasions when an infinite loop is intended, the convention is to construct such loops as `while` loops. More typically, an infinite for loop is a bug. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| /*eslint for-direction: "error"*/ | ||
| for (var i = 0; i < 10; i--) { | ||
| } | ||
| for (var i = 10; i >= 0; i++) { | ||
| } | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| /*eslint for-direction: "error"*/ | ||
| for (var i = 0; i < 10; i++) { | ||
| } | ||
| ``` |
| @@ -0,0 +1,202 @@ | ||
| # enforce line breaks between arguments of a function call (function-call-argument-newline) | ||
|
|
||
| A number of style guides require or disallow line breaks between arguments of a function call. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces line breaks between arguments of a function call. | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has a string option: | ||
|
|
||
| * `"always"` (default) requires line breaks between arguments | ||
| * `"never"` disallows line breaks between arguments | ||
| * `"consistent"` requires consistent usage of line breaks between arguments | ||
|
|
||
|
|
||
| ### always | ||
|
|
||
| Examples of **incorrect** code for this rule with the default `"always"` option: | ||
|
|
||
| ```js | ||
| /*eslint function-call-argument-newline: ["error", "always"]*/ | ||
| foo("one", "two", "three"); | ||
| bar("one", "two", { | ||
| one: 1, | ||
| two: 2 | ||
| }); | ||
| baz("one", "two", (x) => { | ||
| console.log(x); | ||
| }); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the default `"always"` option: | ||
|
|
||
| ```js | ||
| /*eslint function-call-argument-newline: ["error", "always"]*/ | ||
| foo( | ||
| "one", | ||
| "two", | ||
| "three" | ||
| ); | ||
| bar( | ||
| "one", | ||
| "two", | ||
| { one: 1, two: 2 } | ||
| ); | ||
| // or | ||
| bar( | ||
| "one", | ||
| "two", | ||
| { | ||
| one: 1, | ||
| two: 2 | ||
| } | ||
| ); | ||
| baz( | ||
| "one", | ||
| "two", | ||
| (x) => { | ||
| console.log(x); | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
|
|
||
| ### never | ||
|
|
||
| Examples of **incorrect** code for this rule with the default `"never"` option: | ||
|
|
||
| ```js | ||
| /*eslint function-call-argument-newline: ["error", "never"]*/ | ||
| foo( | ||
| "one", | ||
| "two", "three" | ||
| ); | ||
| bar( | ||
| "one", | ||
| "two", { | ||
| one: 1, | ||
| two: 2 | ||
| } | ||
| ); | ||
| baz( | ||
| "one", | ||
| "two", (x) => { | ||
| console.log(x); | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"never"` option: | ||
|
|
||
| ```js | ||
| /*eslint function-call-argument-newline: ["error", "never"]*/ | ||
| foo("one", "two", "three"); | ||
| // or | ||
| foo( | ||
| "one", "two", "three" | ||
| ); | ||
| bar("one", "two", { one: 1, two: 2 }); | ||
| // or | ||
| bar("one", "two", { | ||
| one: 1, | ||
| two: 2 | ||
| }); | ||
| baz("one", "two", (x) => { | ||
| console.log(x); | ||
| }); | ||
| ``` | ||
|
|
||
| ### consistent | ||
|
|
||
| Examples of **incorrect** code for this rule with the default `"consistent"` option: | ||
|
|
||
| ```js | ||
| /*eslint function-call-argument-newline: ["error", "consistent"]*/ | ||
| foo("one", "two", | ||
| "three"); | ||
| //or | ||
| foo("one", | ||
| "two", "three"); | ||
| bar("one", "two", | ||
| { one: 1, two: 2} | ||
| ); | ||
| baz("one", "two", | ||
| (x) => { console.log(x); } | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the default `"consistent"` option: | ||
|
|
||
| ```js | ||
| /*eslint function-call-argument-newline: ["error", "consistent"]*/ | ||
| foo("one", "two", "three"); | ||
| // or | ||
| foo( | ||
| "one", | ||
| "two", | ||
| "three" | ||
| ); | ||
| bar("one", "two", { | ||
| one: 1, | ||
| two: 2 | ||
| }); | ||
| // or | ||
| bar( | ||
| "one", | ||
| "two", | ||
| { one: 1, two: 2 } | ||
| ); | ||
| // or | ||
| bar( | ||
| "one", | ||
| "two", | ||
| { | ||
| one: 1, | ||
| two: 2 | ||
| } | ||
| ); | ||
| baz("one", "two", (x) => { | ||
| console.log(x); | ||
| }); | ||
| // or | ||
| baz( | ||
| "one", | ||
| "two", | ||
| (x) => { | ||
| console.log(x); | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you don't want to enforce line breaks between arguments, don't enable this rule. | ||
|
|
||
| ## Related Rules | ||
|
|
||
| * [function-paren-newline](function-paren-newline.md) | ||
| * [func-call-spacing](func-call-spacing.md) | ||
| * [object-property-newline](object-property-newline.md) | ||
| * [array-element-newline](array-element-newline.md) |
| @@ -0,0 +1,328 @@ | ||
| # enforce consistent line breaks inside function parentheses (function-paren-newline) | ||
|
|
||
| Many style guides require or disallow newlines inside of function parentheses. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces consistent line breaks inside parentheses of function parameters or arguments. | ||
|
|
||
| ### Options | ||
|
|
||
| This rule has a single option, which can either be a string or an object. | ||
|
|
||
| * `"always"` requires line breaks inside all function parentheses. | ||
| * `"never"` disallows line breaks inside all function parentheses. | ||
| * `"multiline"` (default) requires linebreaks inside function parentheses if any of the parameters/arguments have a line break between them. Otherwise, it disallows linebreaks. | ||
| * `"multiline-arguments"` works like `multiline` but allows linebreaks inside function parentheses if there is only one parameter/argument. | ||
| * `"consistent"` requires consistent usage of linebreaks for each pair of parentheses. It reports an error if one parenthesis in the pair has a linebreak inside it and the other parenthesis does not. | ||
| * `{ "minItems": value }` requires linebreaks inside function parentheses if the number of parameters/arguments is at least `value`. Otherwise, it disallows linebreaks. | ||
|
|
||
| Example configurations: | ||
|
|
||
| ```json | ||
| { | ||
| "rules": { | ||
| "function-paren-newline": ["error", "never"] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| "rules": { | ||
| "function-paren-newline": ["error", { "minItems": 3 }] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"always"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "always"] */ | ||
| function foo(bar, baz) {} | ||
| var foo = function(bar, baz) {}; | ||
| var foo = (bar, baz) => {}; | ||
| foo(bar, baz); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"always"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "always"] */ | ||
| function foo( | ||
| bar, | ||
| baz | ||
| ) {} | ||
| var foo = function( | ||
| bar, baz | ||
| ) {}; | ||
| var foo = ( | ||
| bar, | ||
| baz | ||
| ) => {}; | ||
| foo( | ||
| bar, | ||
| baz | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"never"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "never"] */ | ||
| function foo( | ||
| bar, | ||
| baz | ||
| ) {} | ||
| var foo = function( | ||
| bar, baz | ||
| ) {}; | ||
| var foo = ( | ||
| bar, | ||
| baz | ||
| ) => {}; | ||
| foo( | ||
| bar, | ||
| baz | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"never"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "never"] */ | ||
| function foo(bar, baz) {} | ||
| function foo(bar, | ||
| baz) {} | ||
| var foo = function(bar, baz) {}; | ||
| var foo = (bar, baz) => {}; | ||
| foo(bar, baz); | ||
| foo(bar, | ||
| baz); | ||
| ``` | ||
|
|
||
| Examples of **incorrect** code for this rule with the default `"multiline"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "multiline"] */ | ||
| function foo(bar, | ||
| baz | ||
| ) {} | ||
| var foo = function( | ||
| bar, baz | ||
| ) {}; | ||
| var foo = ( | ||
| bar, | ||
| baz) => {}; | ||
| foo(bar, | ||
| baz); | ||
| foo( | ||
| function() { | ||
| return baz; | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the default `"multiline"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "multiline"] */ | ||
| function foo(bar, baz) {} | ||
| var foo = function( | ||
| bar, | ||
| baz | ||
| ) {}; | ||
| var foo = (bar, baz) => {}; | ||
| foo(bar, baz, qux); | ||
| foo( | ||
| bar, | ||
| baz, | ||
| qux | ||
| ); | ||
| foo(function() { | ||
| return baz; | ||
| }); | ||
| ``` | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"consistent"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "consistent"] */ | ||
| function foo(bar, | ||
| baz | ||
| ) {} | ||
| var foo = function(bar, | ||
| baz | ||
| ) {}; | ||
| var foo = ( | ||
| bar, | ||
| baz) => {}; | ||
| foo( | ||
| bar, | ||
| baz); | ||
| foo( | ||
| function() { | ||
| return baz; | ||
| }); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `"consistent"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "consistent"] */ | ||
| function foo(bar, | ||
| baz) {} | ||
| var foo = function(bar, baz) {}; | ||
| var foo = ( | ||
| bar, | ||
| baz | ||
| ) => {}; | ||
| foo( | ||
| bar, baz | ||
| ); | ||
| foo( | ||
| function() { | ||
| return baz; | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **incorrect** code for this rule with the `"multiline-arguments"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "multiline-arguments"] */ | ||
| function foo(bar, | ||
| baz | ||
| ) {} | ||
| var foo = function(bar, | ||
| baz | ||
| ) {}; | ||
| var foo = ( | ||
| bar, | ||
| baz) => {}; | ||
| foo( | ||
| bar, | ||
| baz); | ||
| foo( | ||
| bar, qux, | ||
| baz | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the consistent `"multiline-arguments"` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", "multiline-arguments"] */ | ||
| function foo( | ||
| bar, | ||
| baz | ||
| ) {} | ||
| var foo = function(bar, baz) {}; | ||
| var foo = ( | ||
| bar | ||
| ) => {}; | ||
| foo( | ||
| function() { | ||
| return baz; | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
| Examples of **incorrect** code for this rule with the `{ "minItems": 3 }` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", { "minItems": 3 }] */ | ||
| function foo( | ||
| bar, | ||
| baz | ||
| ) {} | ||
| function foo(bar, baz, qux) {} | ||
| var foo = function( | ||
| bar, baz | ||
| ) {}; | ||
| var foo = (bar, | ||
| baz) => {}; | ||
| foo(bar, | ||
| baz); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule with the `{ "minItems": 3 }` option: | ||
|
|
||
| ```js | ||
| /* eslint function-paren-newline: ["error", { "minItems": 3 }] */ | ||
| function foo(bar, baz) {} | ||
| var foo = function( | ||
| bar, | ||
| baz, | ||
| qux | ||
| ) {}; | ||
| var foo = ( | ||
| bar, baz, qux | ||
| ) => {}; | ||
| foo(bar, baz); | ||
| foo( | ||
| bar, baz, qux | ||
| ); | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If don't want to enforce consistent linebreaks inside function parentheses, do not turn on this rule. |
| @@ -0,0 +1,97 @@ | ||
| # Enforces that a return statement is present in property getters (getter-return) | ||
|
|
||
| The get syntax binds an object property to a function that will be called when that property is looked up. It was first introduced in ECMAScript 5: | ||
|
|
||
| ```js | ||
| var p = { | ||
| get name(){ | ||
| return "nicholas"; | ||
| } | ||
| }; | ||
| Object.defineProperty(p, "age", { | ||
| get: function (){ | ||
| return 17; | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| Note that every `getter` is expected to return a value. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces that a return statement is present in property getters. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| /*eslint getter-return: "error"*/ | ||
| p = { | ||
| get name(){ | ||
| // no returns. | ||
| } | ||
| }; | ||
| Object.defineProperty(p, "age", { | ||
| get: function (){ | ||
| // no returns. | ||
| } | ||
| }); | ||
| class P{ | ||
| get name(){ | ||
| // no returns. | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| /*eslint getter-return: "error"*/ | ||
| p = { | ||
| get name(){ | ||
| return "nicholas"; | ||
| } | ||
| }; | ||
| Object.defineProperty(p, "age", { | ||
| get: function (){ | ||
| return 18; | ||
| } | ||
| }); | ||
| class P{ | ||
| get name(){ | ||
| return "nicholas"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has an object option: | ||
|
|
||
| * `"allowImplicit": false` (default) disallows implicitly returning `undefined` with a `return` statement. | ||
|
|
||
| Examples of **correct** code for the `{ "allowImplicit": true }` option: | ||
|
|
||
| ```js | ||
| /*eslint getter-return: ["error", { allowImplicit: true }]*/ | ||
| p = { | ||
| get name(){ | ||
| return; // return undefined implicitly. | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If your project will not be using ES5 property getters you do not need this rule. | ||
|
|
||
| ## Further Reading | ||
|
|
||
| * [MDN: Functions getter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) | ||
| * [Understanding ES6: Accessor Properties](https://leanpub.com/understandinges6/read/#leanpub-auto-accessor-properties) |