Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add default-param-last rule (#1418)
- Loading branch information
1 parent
b139540
commit a37ff9f
Showing
6 changed files
with
640 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Enforce default parameters to be last (`default-param-last`) | ||
|
||
## Rule Details | ||
|
||
This rule enforces default or optional parameters to be the last of parameters. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
/* eslint @typescript-eslint/default-param-last: ["error"] */ | ||
|
||
function f(a = 0, b: number) {} | ||
function f(a: number, b = 0, c?: number) {} | ||
function f(a: number, b = 0, c: number) {} | ||
function f(a: number, b?: number, c: number) {} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
/* eslint @typescript-eslint/default-param-last: ["error"] */ | ||
|
||
function f(a = 0) {} | ||
function f(a: number, b = 0) {} | ||
function f(a: number, b?: number) {} | ||
function f(a: number, b?: number, c = 0) {} | ||
``` | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/default-param-last.md)</sup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { createRule } from '../util'; | ||
import { | ||
TSESTree, | ||
AST_NODE_TYPES, | ||
} from '@typescript-eslint/experimental-utils'; | ||
|
||
export default createRule({ | ||
name: 'default-param-last', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce default parameters to be last', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
shouldBeLast: 'Default parameters should be last.', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
/** | ||
* checks if node is optional parameter | ||
* @param node the node to be evaluated | ||
* @private | ||
*/ | ||
function isOptionalParam(node: TSESTree.Parameter): boolean { | ||
return 'optional' in node && node.optional === true; | ||
} | ||
|
||
/** | ||
* checks if node is plain parameter | ||
* @param node the node to be evaluated | ||
* @private | ||
*/ | ||
function isPlainParam(node: TSESTree.Parameter): boolean { | ||
return !( | ||
node.type === AST_NODE_TYPES.AssignmentPattern || | ||
node.type === AST_NODE_TYPES.RestElement || | ||
isOptionalParam(node) | ||
); | ||
} | ||
|
||
function checkDefaultParamLast( | ||
node: | ||
| TSESTree.ArrowFunctionExpression | ||
| TSESTree.FunctionDeclaration | ||
| TSESTree.FunctionExpression, | ||
): void { | ||
let hasSeenPlainParam = false; | ||
for (let i = node.params.length - 1; i >= 0; i--) { | ||
const param = node.params[i]; | ||
|
||
if (isPlainParam(param)) { | ||
hasSeenPlainParam = true; | ||
continue; | ||
} | ||
|
||
if ( | ||
hasSeenPlainParam && | ||
(isOptionalParam(param) || | ||
param.type === AST_NODE_TYPES.AssignmentPattern) | ||
) { | ||
context.report({ node: param, messageId: 'shouldBeLast' }); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
ArrowFunctionExpression: checkDefaultParamLast, | ||
FunctionDeclaration: checkDefaultParamLast, | ||
FunctionExpression: checkDefaultParamLast, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.