From 83f83c5efe11e5aeb5ad486c9e221a704e339b84 Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 18 Mar 2020 21:34:03 +0000 Subject: [PATCH 1/9] Fix: consistencies for blockstmt no-inner-fn (fix #12222) --- lib/rules/no-inner-declarations.js | 2 +- tests/lib/rules/no-inner-declarations.js | 59 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-inner-declarations.js b/lib/rules/no-inner-declarations.js index e1c29e0a3b4..77f1e4ecd62 100644 --- a/lib/rules/no-inner-declarations.js +++ b/lib/rules/no-inner-declarations.js @@ -67,7 +67,7 @@ module.exports = { function check(node) { const body = nearestBody(), valid = ((body.type === "Program" && body.distance === 1) || - body.distance === 2); + (body.type !== "Program" && body.distance === 2)); if (!valid) { context.report({ diff --git a/tests/lib/rules/no-inner-declarations.js b/tests/lib/rules/no-inner-declarations.js index d255824619e..cbb5d45262b 100644 --- a/tests/lib/rules/no-inner-declarations.js +++ b/tests/lib/rules/no-inner-declarations.js @@ -61,6 +61,65 @@ ruleTester.run("no-inner-declarations", rule, { }, type: "FunctionDeclaration" }] + }, { + code: "if (foo) var a; ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] + }, + { + code: "if (foo) function f(){} ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "program" + }, + type: "FunctionDeclaration" + }] + }, + { + code: "function bar() { if (foo) function f(){}; }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "function body" + }, + type: "FunctionDeclaration" + }] + }, + { + code: "function bar() { if (foo) var a; }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] + }, + { + code: "if (foo){ var a; }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] }, { code: "function doSomething() { do { function somethingElse() { } } while (test); }", errors: [{ From 1c813f04ee14276ec28c7ad6ba406b0572ed775b Mon Sep 17 00:00:00 2001 From: Anix Date: Thu, 19 Mar 2020 07:26:52 +0000 Subject: [PATCH 2/9] Chore: added some more tests --- tests/lib/rules/no-inner-declarations.js | 58 ++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/lib/rules/no-inner-declarations.js b/tests/lib/rules/no-inner-declarations.js index cbb5d45262b..be7601e8edf 100644 --- a/tests/lib/rules/no-inner-declarations.js +++ b/tests/lib/rules/no-inner-declarations.js @@ -72,6 +72,64 @@ ruleTester.run("no-inner-declarations", rule, { }, type: "VariableDeclaration" }] + }, { + code: "if (foo) /* some comments */ var a; ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] + }, { + code: "if (foo){ function f(){ if(bar){ var a; } } }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "program" + }, + type: "FunctionDeclaration" + }, { + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] + }, { + code: "if (foo) function f(){ if(bar) var a; } ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "program" + }, + type: "FunctionDeclaration" + }, { + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] + }, { + code: "if (foo) { var fn = function(){} } ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] }, { code: "if (foo) function f(){} ", From 4c9adf8f712daa15852186d9cf1a08c5468d1853 Mon Sep 17 00:00:00 2001 From: Anix Date: Thu, 19 Mar 2020 16:44:57 +0000 Subject: [PATCH 3/9] Chore: allowing named and default exports and tests --- lib/rules/no-inner-declarations.js | 4 +- tests/lib/rules/no-inner-declarations.js | 392 ++++++++++++----------- 2 files changed, 214 insertions(+), 182 deletions(-) diff --git a/lib/rules/no-inner-declarations.js b/lib/rules/no-inner-declarations.js index 77f1e4ecd62..0d3c59ca2f5 100644 --- a/lib/rules/no-inner-declarations.js +++ b/lib/rules/no-inner-declarations.js @@ -43,7 +43,7 @@ module.exports = { generation = 1; while (ancestor && ["Program", "FunctionDeclaration", - "FunctionExpression", "ArrowFunctionExpression" + "FunctionExpression", "ArrowFunctionExpression", "ExportNamedDeclaration", "ExportDefaultDeclaration" ].indexOf(ancestor.type) < 0) { generation += 1; ancestor = ancestors.pop(); @@ -66,7 +66,7 @@ module.exports = { */ function check(node) { const body = nearestBody(), - valid = ((body.type === "Program" && body.distance === 1) || + valid = ((!!~["Program", "ExportNamedDeclaration", "ExportDefaultDeclaration"].indexOf(body.type) && body.distance === 1) || (body.type !== "Program" && body.distance === 2)); if (!valid) { diff --git a/tests/lib/rules/no-inner-declarations.js b/tests/lib/rules/no-inner-declarations.js index be7601e8edf..0c6f58a33c2 100644 --- a/tests/lib/rules/no-inner-declarations.js +++ b/tests/lib/rules/no-inner-declarations.js @@ -45,191 +45,223 @@ ruleTester.run("no-inner-declarations", rule, { code: "var x = {doSomething() {var foo;}}", options: ["both"], parserOptions: { ecmaVersion: 6 } + }, + { + code: "export var foo;", + options: ["both"], + parserOptions: { sourceType: "module", ecmaVersion: 6 } + }, + { + code: "export function bar() {}", + options: ["both"], + parserOptions: { sourceType: "module", ecmaVersion: 6 } + }, + { + code: "export default function baz() {}", + options: ["both"], + parserOptions: { sourceType: "module", ecmaVersion: 6 } + }, + { + code: "exports.foo = () => {}", + options: ["both"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "exports.foo = function(){}", + options: ["both"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "module.exports = function foo(){}", + options: ["both"], + parserOptions: { ecmaVersion: 6 } } ], // Examples of code that should trigger the rule - invalid: [{ - code: "if (test) { function doSomething() { } }", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "function", - body: "program" - }, - type: "FunctionDeclaration" - }] - }, { - code: "if (foo) var a; ", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "program" - }, - type: "VariableDeclaration" - }] - }, { - code: "if (foo) /* some comments */ var a; ", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "program" - }, - type: "VariableDeclaration" - }] - }, { - code: "if (foo){ function f(){ if(bar){ var a; } } }", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "function", - body: "program" - }, - type: "FunctionDeclaration" + invalid: [ + { + code: "if (test) { function doSomething() { } }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "program" + }, + type: "FunctionDeclaration" + }] + }, { + code: "if (foo) var a; ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] + }, { + code: "if (foo) /* some comments */ var a; ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] }, { - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "function body" - }, - type: "VariableDeclaration" - }] - }, { - code: "if (foo) function f(){ if(bar) var a; } ", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "function", - body: "program" - }, - type: "FunctionDeclaration" + code: "if (foo){ function f(){ if(bar){ var a; } } }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "program" + }, + type: "FunctionDeclaration" + }, { + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] + }, { + code: "if (foo) function f(){ if(bar) var a; } ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "program" + }, + type: "FunctionDeclaration" + }, { + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] }, { - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "function body" - }, - type: "VariableDeclaration" - }] - }, { - code: "if (foo) { var fn = function(){} } ", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "program" - }, - type: "VariableDeclaration" - }] - }, - { - code: "if (foo) function f(){} ", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "function", - body: "program" - }, - type: "FunctionDeclaration" - }] - }, - { - code: "function bar() { if (foo) function f(){}; }", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "function", - body: "function body" - }, - type: "FunctionDeclaration" - }] - }, - { - code: "function bar() { if (foo) var a; }", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "function body" - }, - type: "VariableDeclaration" - }] - }, - { - code: "if (foo){ var a; }", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "program" - }, - type: "VariableDeclaration" - }] - }, { - code: "function doSomething() { do { function somethingElse() { } } while (test); }", - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "function", - body: "function body" - }, - type: "FunctionDeclaration" - }] - }, { - code: "(function() { if (test) { function doSomething() { } } }());", - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "function", - body: "function body" - }, - type: "FunctionDeclaration" - }] - }, { - code: "while (test) { var foo; }", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "program" - }, - type: "VariableDeclaration" - }] - }, { - code: "function doSomething() { if (test) { var foo = 42; } }", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "function body" - }, - type: "VariableDeclaration" - }] - }, { - code: "(function() { if (test) { var foo; } }());", - options: ["both"], - errors: [{ - messageId: "moveDeclToRoot", - data: { - type: "variable", - body: "function body" - }, - type: "VariableDeclaration" - }] - }] + code: "if (foo) { var fn = function(){} } ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] + }, + { + code: "if (foo) function f(){} ", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "program" + }, + type: "FunctionDeclaration" + }] + }, + { + code: "function bar() { if (foo) function f(){}; }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "function body" + }, + type: "FunctionDeclaration" + }] + }, + { + code: "function bar() { if (foo) var a; }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] + }, + { + code: "if (foo){ var a; }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] + }, { + code: "function doSomething() { do { function somethingElse() { } } while (test); }", + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "function body" + }, + type: "FunctionDeclaration" + }] + }, { + code: "(function() { if (test) { function doSomething() { } } }());", + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "function", + body: "function body" + }, + type: "FunctionDeclaration" + }] + }, { + code: "while (test) { var foo; }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "program" + }, + type: "VariableDeclaration" + }] + }, { + code: "function doSomething() { if (test) { var foo = 42; } }", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] + }, { + code: "(function() { if (test) { var foo; } }());", + options: ["both"], + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] + } + ] }); From 0727032a391c266fabd05e3eba67e1144122e64e Mon Sep 17 00:00:00 2001 From: Anix Date: Sun, 22 Mar 2020 16:21:09 +0000 Subject: [PATCH 4/9] Chore: simply the check conditions --- lib/rules/no-inner-declarations.js | 72 +++++++++++++++--------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/lib/rules/no-inner-declarations.js b/lib/rules/no-inner-declarations.js index 0d3c59ca2f5..5e804fcfd2f 100644 --- a/lib/rules/no-inner-declarations.js +++ b/lib/rules/no-inner-declarations.js @@ -9,6 +9,9 @@ // Rule Definition //------------------------------------------------------------------------------ +const validParent = ["Program", "ExportNamedDeclaration", "ExportDefaultDeclaration"]; +const validBlockStatementParent = ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]; + module.exports = { meta: { type: "problem", @@ -34,53 +37,48 @@ module.exports = { create(context) { /** - * Find the nearest Program or Function ancestor node. - * @returns {Object} Ancestor's type and distance from node. + * Ensure that a given node is at a program or function body's root. + * @param {ASTNode} node Declaration node to check. + * @returns {void} */ - function nearestBody() { + function check(node) { const ancestors = context.getAncestors(); - let ancestor = ancestors.pop(), - generation = 1; + let ancestor = ancestors.pop(); - while (ancestor && ["Program", "FunctionDeclaration", - "FunctionExpression", "ArrowFunctionExpression", "ExportNamedDeclaration", "ExportDefaultDeclaration" - ].indexOf(ancestor.type) < 0) { - generation += 1; - ancestor = ancestors.pop(); + if ( + ancestor.type === "BlockStatement" && + !!~validBlockStatementParent.indexOf(ancestor.parent.type) + ) { + return; } - return { + if ((~validParent.indexOf(ancestor.type))) { + return; + } - // Type of containing ancestor - type: ancestor.type, + let parentsListToCheck = ancestor.type === "BlockStatement" ? validBlockStatementParent : validParent; - // Separation between ancestor and node - distance: generation - }; - } - - /** - * Ensure that a given node is at a program or function body's root. - * @param {ASTNode} node Declaration node to check. - * @returns {void} - */ - function check(node) { - const body = nearestBody(), - valid = ((!!~["Program", "ExportNamedDeclaration", "ExportDefaultDeclaration"].indexOf(body.type) && body.distance === 1) || - (body.type !== "Program" && body.distance === 2)); - - if (!valid) { - context.report({ - node, - messageId: "moveDeclToRoot", - data: { - type: (node.type === "FunctionDeclaration" ? "function" : "variable"), - body: (body.type === "Program" ? "program" : "function body") - } - }); + while (ancestor && !~parentsListToCheck.indexOf(ancestor.type)) { + ancestor = ancestors.pop(); + if (ancestor.type === "BlockStatement") { + ancestor = ancestor.parent; + parentsListToCheck = validBlockStatementParent; + } else { + parentsListToCheck = validParent; + } } + + context.report({ + node, + messageId: "moveDeclToRoot", + data: { + type: (node.type === "FunctionDeclaration" ? "function" : "variable"), + body: (ancestor.type === "Program" ? "program" : "function body") + } + }); } + return { FunctionDeclaration: check, From d7a8d2b407c912dbeb3cf7b51f503e7c5bb2c3ab Mon Sep 17 00:00:00 2001 From: Anix Date: Mon, 23 Mar 2020 11:46:51 +0000 Subject: [PATCH 5/9] Chore: simplify checks and ancestor loop --- lib/rules/no-inner-declarations.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/rules/no-inner-declarations.js b/lib/rules/no-inner-declarations.js index 5e804fcfd2f..b6b5a92aa03 100644 --- a/lib/rules/no-inner-declarations.js +++ b/lib/rules/no-inner-declarations.js @@ -9,8 +9,8 @@ // Rule Definition //------------------------------------------------------------------------------ -const validParent = ["Program", "ExportNamedDeclaration", "ExportDefaultDeclaration"]; -const validBlockStatementParent = ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]; +const validParent = new Set(["Program", "ExportNamedDeclaration", "ExportDefaultDeclaration"]); +const validBlockStatementParent = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]); module.exports = { meta: { @@ -46,26 +46,20 @@ module.exports = { let ancestor = ancestors.pop(); if ( - ancestor.type === "BlockStatement" && - !!~validBlockStatementParent.indexOf(ancestor.parent.type) + ancestor.type === "BlockStatement" && validBlockStatementParent.has(ancestor.parent.type) ) { return; } - if ((~validParent.indexOf(ancestor.type))) { + if ((validParent.has(ancestor.type))) { return; } - let parentsListToCheck = ancestor.type === "BlockStatement" ? validBlockStatementParent : validParent; - - while (ancestor && !~parentsListToCheck.indexOf(ancestor.type)) { + while (ancestor && + !["Program", "FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"] + .includes(ancestor.type) + ) { ancestor = ancestors.pop(); - if (ancestor.type === "BlockStatement") { - ancestor = ancestor.parent; - parentsListToCheck = validBlockStatementParent; - } else { - parentsListToCheck = validParent; - } } context.report({ From 45c92ee7b344ce4925d3a047e8965b5c295502c5 Mon Sep 17 00:00:00 2001 From: Anix Date: Mon, 23 Mar 2020 11:54:53 +0000 Subject: [PATCH 6/9] Chore: using getUpperFunction to get body --- lib/rules/no-inner-declarations.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/rules/no-inner-declarations.js b/lib/rules/no-inner-declarations.js index b6b5a92aa03..fdf4887c608 100644 --- a/lib/rules/no-inner-declarations.js +++ b/lib/rules/no-inner-declarations.js @@ -5,6 +5,12 @@ "use strict"; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const astUtils = require("./utils/ast-utils"); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -43,7 +49,7 @@ module.exports = { */ function check(node) { const ancestors = context.getAncestors(); - let ancestor = ancestors.pop(); + const ancestor = ancestors.pop(); if ( ancestor.type === "BlockStatement" && validBlockStatementParent.has(ancestor.parent.type) @@ -55,19 +61,14 @@ module.exports = { return; } - while (ancestor && - !["Program", "FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"] - .includes(ancestor.type) - ) { - ancestor = ancestors.pop(); - } + const body = astUtils.getUpperFunction(ancestor); context.report({ node, messageId: "moveDeclToRoot", data: { type: (node.type === "FunctionDeclaration" ? "function" : "variable"), - body: (ancestor.type === "Program" ? "program" : "function body") + body: (body === null ? "program" : "function body") } }); } From bb9fe9510619bac0150460770caf53f1c2d43784 Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 25 Mar 2020 08:26:06 +0000 Subject: [PATCH 7/9] chore: added tests and minor changes --- lib/rules/no-inner-declarations.js | 11 +++++------ tests/lib/rules/no-inner-declarations.js | 14 +++++++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/rules/no-inner-declarations.js b/lib/rules/no-inner-declarations.js index fdf4887c608..0768bc61149 100644 --- a/lib/rules/no-inner-declarations.js +++ b/lib/rules/no-inner-declarations.js @@ -48,27 +48,26 @@ module.exports = { * @returns {void} */ function check(node) { - const ancestors = context.getAncestors(); - const ancestor = ancestors.pop(); + const parent = node.parent; if ( - ancestor.type === "BlockStatement" && validBlockStatementParent.has(ancestor.parent.type) + parent.type === "BlockStatement" && validBlockStatementParent.has(parent.parent.type) ) { return; } - if ((validParent.has(ancestor.type))) { + if (validParent.has(parent.type)) { return; } - const body = astUtils.getUpperFunction(ancestor); + const upperFunction = astUtils.getUpperFunction(parent); context.report({ node, messageId: "moveDeclToRoot", data: { type: (node.type === "FunctionDeclaration" ? "function" : "variable"), - body: (body === null ? "program" : "function body") + body: (upperFunction === null ? "program" : "function body") } }); } diff --git a/tests/lib/rules/no-inner-declarations.js b/tests/lib/rules/no-inner-declarations.js index 0c6f58a33c2..4a44c9e8941 100644 --- a/tests/lib/rules/no-inner-declarations.js +++ b/tests/lib/rules/no-inner-declarations.js @@ -164,7 +164,6 @@ ruleTester.run("no-inner-declarations", rule, { }, { code: "if (foo) function f(){} ", - options: ["both"], errors: [{ messageId: "moveDeclToRoot", data: { @@ -262,6 +261,19 @@ ruleTester.run("no-inner-declarations", rule, { }, type: "VariableDeclaration" }] + }, { + code: "const doSomething = () => { if (test) { var foo = 42; } }", + options: ["both"], + parserOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "moveDeclToRoot", + data: { + type: "variable", + body: "function body" + }, + type: "VariableDeclaration" + }] } + ] }); From 7c772960d2972f774752ae45521eb5031be7421e Mon Sep 17 00:00:00 2001 From: Anix Date: Thu, 26 Mar 2020 14:00:16 +0000 Subject: [PATCH 8/9] Docs: eg for both non-blockstatement --- docs/rules/no-inner-declarations.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/rules/no-inner-declarations.md b/docs/rules/no-inner-declarations.md index 23fd9253b10..0b8558849bd 100644 --- a/docs/rules/no-inner-declarations.md +++ b/docs/rules/no-inner-declarations.md @@ -137,6 +137,10 @@ if (test) { function doAnotherThing() { var baz = 81; } + +if (foo) var a; + +if (foo) function f(){} ``` ## When Not To Use It From cc0e5b3a4e293280395db8f4752b87f1f46de53c Mon Sep 17 00:00:00 2001 From: Anix Date: Thu, 26 Mar 2020 19:20:43 +0000 Subject: [PATCH 9/9] Chore: right docs example and test refactore --- docs/rules/no-inner-declarations.md | 13 +++++++++---- tests/lib/rules/no-inner-declarations.js | 6 ++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/rules/no-inner-declarations.md b/docs/rules/no-inner-declarations.md index 0b8558849bd..0592084bce1 100644 --- a/docs/rules/no-inner-declarations.md +++ b/docs/rules/no-inner-declarations.md @@ -81,6 +81,8 @@ function doSomethingElse() { function doAnotherThing() { } } } + +if (foo) function f(){} ``` Examples of **correct** code for this rule with the default `"functions"` option: @@ -102,6 +104,8 @@ var fn; if (test) { fn = function fnExpression() { }; } + +if (foo) var a; ``` ### both @@ -120,6 +124,11 @@ function doAnotherThing() { var bar = 81; } } + + +if (foo) var a; + +if (foo) function f(){} ``` Examples of **correct** code for this rule with the `"both"` option: @@ -137,10 +146,6 @@ if (test) { function doAnotherThing() { var baz = 81; } - -if (foo) var a; - -if (foo) function f(){} ``` ## When Not To Use It diff --git a/tests/lib/rules/no-inner-declarations.js b/tests/lib/rules/no-inner-declarations.js index 4a44c9e8941..a9f70fd3f5e 100644 --- a/tests/lib/rules/no-inner-declarations.js +++ b/tests/lib/rules/no-inner-declarations.js @@ -68,13 +68,11 @@ ruleTester.run("no-inner-declarations", rule, { }, { code: "exports.foo = function(){}", - options: ["both"], - parserOptions: { ecmaVersion: 6 } + options: ["both"] }, { code: "module.exports = function foo(){}", - options: ["both"], - parserOptions: { ecmaVersion: 6 } + options: ["both"] } ],