forked from eslint/eslint
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: rule lines-between-class-methods(fixes eslint#5949).
- Loading branch information
1 parent
3c41a05
commit 15eaef4
Showing
4 changed files
with
147 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,5 @@ | ||
# enforce lines between class methods (lines-between-class-methods) | ||
|
||
## Rule Details | ||
|
||
## Options |
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,106 @@ | ||
/** | ||
* @fileoverview Rule to enforce lines between class methods | ||
* @author 薛定谔的猫<hh_2013@foxmail.com> | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
// const astUtils = require("../ast-utils"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "enforce lines between class methods", | ||
category: "Stylistic Issues", | ||
recommended: false | ||
}, | ||
|
||
fixable: null, | ||
|
||
schema: [ | ||
{ | ||
oneOf: [ | ||
{ | ||
enum: ["always", "never"] | ||
}, | ||
{ | ||
type: "object", | ||
properties: { | ||
multiLine: { | ||
enum: ["always", "never"] | ||
}, | ||
singleLine: { | ||
enum: ["always", "never"] | ||
} | ||
}, | ||
additionalProperties: false, | ||
minProperties: 1 | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
|
||
create(context) { | ||
|
||
// const options = {}; | ||
const config = context.options[0] || "always"; | ||
|
||
const ALWAYS_MESSAGE = "Methods must be padded by blank lines."; | ||
const NEVER_MESSAGE = "Methods must not be padded by blank lines."; | ||
|
||
const sourceCode = context.getSourceCode(); | ||
|
||
/** | ||
* Checks if there is padding between two tokens | ||
* @param {Token} first The first token | ||
* @param {Token} second The second token | ||
* @returns {boolean} True if there is at least a line between the tokens | ||
*/ | ||
function isPaddingBetweenTokens(first, second) { | ||
return second.loc.start.line - first.loc.end.line >= 2; | ||
} | ||
|
||
/** | ||
* Checks the given BlockStatement node to be padded if the block is not empty. | ||
* @param {ASTNode} node The AST node of a BlockStatement. | ||
* @returns {void} undefined. | ||
*/ | ||
function checkPadding(node) { | ||
for (let i = 0; i < node.body.length - 1; i++) { | ||
const prev = sourceCode.getLastToken(node.body[i]); | ||
const next = sourceCode.getFirstToken(node.body[i + 1]); | ||
const padded = isPaddingBetweenTokens(prev, next); | ||
|
||
if (config === "always" && !padded) { | ||
context.report({ | ||
node, | ||
message: ALWAYS_MESSAGE | ||
}); | ||
} else if (config === "never" && padded) { | ||
context.report({ | ||
node, | ||
message: NEVER_MESSAGE | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
ClassBody: checkPadding | ||
}; | ||
|
||
} | ||
}; |
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,35 @@ | ||
/** | ||
* @fileoverview Tests for lines-between-class-methods rule. | ||
* @author 薛定谔的猫<hh_2013@foxmail.com> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/lines-between-class-methods"); | ||
const RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
const ALWAYS_MESSAGE = "Methods must be padded by blank lines."; | ||
const NEVER_MESSAGE = "Methods must not be padded by blank lines."; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | ||
|
||
ruleTester.run("lines-between-class-methods", rule, { | ||
valid: [ | ||
"class foo{ bar(){}\n\nbaz(){}}" | ||
], | ||
invalid: [ | ||
{ code: "class foo{ bar(){}baz(){}}", output: null, errors: [{ message: ALWAYS_MESSAGE }] }, | ||
{ code: "class foo{ bar(){}\n\nbaz(){}}", output: null, options: ["never"], errors: [{ message: NEVER_MESSAGE }] } | ||
] | ||
}); |