Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: single- and multiline const, let, var statements (fixes #10721) #10919

Merged
merged 1 commit into from Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/rules/padding-line-between-statements.md
Expand Up @@ -52,7 +52,7 @@ You can supply any number of configurations. If a statement pair matches multipl
- `"cjs-export"` is `export` statements of CommonJS; e.g. `module.exports = 0`, `module.exports.foo = 1`, and `exports.foo = 2`. This is the special cases of assignment.
- `"cjs-import"` is `import` statements of CommonJS; e.g. `const foo = require("foo")`. This is the special cases of variable declarations.
- `"class"` is `class` declarations.
- `"const"` is `const` variable declarations.
- `"const"` is `const` variable declarations, both single-line and multiline.
- `"continue"` is `continue` statements.
- `"debugger"` is `debugger` statements.
- `"default"` is `default` labels.
Expand All @@ -65,14 +65,20 @@ You can supply any number of configurations. If a statement pair matches multipl
- `"function"` is function declarations.
- `"if"` is `if` statements.
- `"import"` is `import` declarations.
- `"let"` is `let` variable declarations.
- `"let"` is `let` variable declarations, both single-line and multiline.
- `"multiline-block-like"` is block like statements. This is the same as `block-like` type, but only if the block is multiline.
- `"multiline-const"` is multiline `const` variable declarations.
- `"multiline-expression"` is expression statements. This is the same as `expression` type, but only if the statement is multiline.
- `"multiline-let"` is multiline `let` variable declarations.
- `"multiline-var"` is multiline `var` variable declarations.
- `"return"` is `return` statements.
- `"singleline-const"` is single-line `const` variable declarations.
- `"singleline-let"` is single-line `let` variable declarations.
- `"singleline-var"` is single-line `var` variable declarations.
- `"switch"` is `switch` statements.
- `"throw"` is `throw` statements.
- `"try"` is `try` statements.
- `"var"` is `var` variable declarations.
- `"var"` is `var` variable declarations, both single-line and multiline.
- `"while"` is `while` loop statements.
- `"with"` is `with` statements.

Expand Down
37 changes: 37 additions & 0 deletions lib/rules/padding-line-between-statements.js
Expand Up @@ -36,6 +36,36 @@ function newKeywordTester(keyword) {
};
}

/**
* Creates tester which check if a node starts with specific keyword and spans a single line.
*
* @param {string} keyword The keyword to test.
* @returns {Object} the created tester.
* @private
*/
function newSinglelineKeywordTester(keyword) {
return {
test: (node, sourceCode) =>
node.loc.start.line === node.loc.end.line &&
sourceCode.getFirstToken(node).value === keyword
};
}

/**
* Creates tester which check if a node starts with specific keyword and spans multiple lines.
*
* @param {string} keyword The keyword to test.
* @returns {Object} the created tester.
* @private
*/
function newMultilineKeywordTester(keyword) {
return {
test: (node, sourceCode) =>
node.loc.start.line !== node.loc.end.line &&
sourceCode.getFirstToken(node).value === keyword
};
}

/**
* Creates tester which check if a node is specific type.
*
Expand Down Expand Up @@ -365,6 +395,13 @@ const StatementTypes = {
!isDirectivePrologue(node, sourceCode)
},

"multiline-const": newMultilineKeywordTester("const"),
"multiline-let": newMultilineKeywordTester("let"),
"multiline-var": newMultilineKeywordTester("var"),
"singleline-const": newSinglelineKeywordTester("const"),
"singleline-let": newSinglelineKeywordTester("let"),
"singleline-var": newSinglelineKeywordTester("var"),

block: newNodeTypeTester("BlockStatement"),
empty: newNodeTypeTester("EmptyStatement"),
function: newNodeTypeTester("FunctionDeclaration"),
Expand Down