Skip to content

Commit

Permalink
Upgrade: single- and multiline const, let, var statements (fixes #10721
Browse files Browse the repository at this point in the history
…) (#10919)
  • Loading branch information
neemzy authored and not-an-aardvark committed Dec 8, 2018
1 parent 9666aba commit 4b0f517
Show file tree
Hide file tree
Showing 3 changed files with 466 additions and 3 deletions.
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 a special case of assignment.
- `"cjs-import"` is `import` statements of CommonJS; e.g. `const foo = require("foo")`. This is a special case 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 @@ -66,14 +66,20 @@ You can supply any number of configurations. If a statement pair matches multipl
- `"if"` is `if` statements.
- `"iife"` is immediately invoked function expression statements. This matches calls on a function expression, optionally prefixed with a unary operator.
- `"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 @@ -368,6 +398,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

0 comments on commit 4b0f517

Please sign in to comment.