From 2ce6bed04cad376003f70447ece4b6578c142bfd Mon Sep 17 00:00:00 2001 From: Anix Date: Sat, 25 Apr 2020 01:31:09 +0530 Subject: [PATCH] Chore: added tests for nested arrays (#13145) --- tests/lib/rules/array-bracket-newline.js | 547 ++++++++++++++++++++--- 1 file changed, 486 insertions(+), 61 deletions(-) diff --git a/tests/lib/rules/array-bracket-newline.js b/tests/lib/rules/array-bracket-newline.js index d20c365dbc4..90ee8213b77 100644 --- a/tests/lib/rules/array-bracket-newline.js +++ b/tests/lib/rules/array-bracket-newline.js @@ -39,6 +39,7 @@ ruleTester.run("array-bracket-newline", rule, { "var foo = [\nfunction foo() {\nreturn dosomething();\n}\n];", "var foo = [/* \nany comment\n */];", "var foo = [/* single line multiline comment for no real reason */];", + "var foo = [[1,2]]", // "always" { code: "var foo = [\n];", options: ["always"] }, @@ -47,9 +48,37 @@ ruleTester.run("array-bracket-newline", rule, { { code: "var foo = [\n/* any */\n1\n];", options: ["always"] }, { code: "var foo = [\n1, 2\n];", options: ["always"] }, { code: "var foo = [\n1, 2 // any comment\n];", options: ["always"] }, - { code: "var foo = [\n1, 2 /* any comment */\n];", options: ["always"] }, + { + code: "var foo = [\n1, 2 /* any comment */\n];", + options: ["always"] + }, { code: "var foo = [\n1,\n2\n];", options: ["always"] }, - { code: "var foo = [\nfunction foo() {\ndosomething();\n}\n];", options: ["always"] }, + { + code: "var foo = [\nfunction foo() {\ndosomething();\n}\n];", + options: ["always"] + }, + { + code: ` + var foo = [ + [ + 1,2 + ] + ] + `, + options: ["always"] + }, + { + code: ` + var foo = [ + 0, + [ + 1,2 + ], + 3 + ] + `, + options: ["always"] + }, // "never" { code: "var foo = [];", options: ["never"] }, @@ -58,7 +87,14 @@ ruleTester.run("array-bracket-newline", rule, { { code: "var foo = [1, 2];", options: ["never"] }, { code: "var foo = [1,\n2];", options: ["never"] }, { code: "var foo = [1,\n/* any comment */\n2];", options: ["never"] }, - { code: "var foo = [function foo() {\ndosomething();\n}];", options: ["never"] }, + { + code: "var foo = [function foo() {\ndosomething();\n}];", + options: ["never"] + }, + { + code: "var foo = [[1,2],3];", + options: ["never"] + }, // "consistent" { code: "var a = []", options: ["consistent"] }, @@ -69,67 +105,177 @@ ruleTester.run("array-bracket-newline", rule, { { code: "var a = [/**/\n1\n]", options: ["consistent"] }, { code: "var a = [/*\n*/1\n]", options: ["consistent"] }, { code: "var a = [//\n]", options: ["consistent"] }, + { + code: `var a = [ + [1,2] + ]`, + options: ["consistent"] + }, + { + code: `var a = [ + [[1,2]] + ]`, + options: ["consistent"] + }, // { multiline: true } { code: "var foo = [];", options: [{ multiline: true }] }, { code: "var foo = [1];", options: [{ multiline: true }] }, - { code: "var foo = /* any comment */[1];", options: [{ multiline: true }] }, - { code: "var foo = /* any comment */\n[1];", options: [{ multiline: true }] }, + { + code: "var foo = /* any comment */[1];", + options: [{ multiline: true }] + }, + { + code: "var foo = /* any comment */\n[1];", + options: [{ multiline: true }] + }, { code: "var foo = [1, 2];", options: [{ multiline: true }] }, - { code: "var foo = [ // any comment\n1, 2\n];", options: [{ multiline: true }] }, - { code: "var foo = [\n// any comment\n1, 2\n];", options: [{ multiline: true }] }, - { code: "var foo = [\n1, 2\n// any comment\n];", options: [{ multiline: true }] }, + { + code: "var foo = [ // any comment\n1, 2\n];", + options: [{ multiline: true }] + }, + { + code: "var foo = [\n// any comment\n1, 2\n];", + options: [{ multiline: true }] + }, + { + code: "var foo = [\n1, 2\n// any comment\n];", + options: [{ multiline: true }] + }, { code: "var foo = [\n1,\n2\n];", options: [{ multiline: true }] }, - { code: "var foo = [\nfunction foo() {\nreturn dosomething();\n}\n];", options: [{ multiline: true }] }, - { code: "var foo = [/* \nany comment\n */];", options: [{ multiline: true }] }, + { + code: "var foo = [\nfunction foo() {\nreturn dosomething();\n}\n];", + options: [{ multiline: true }] + }, + { + code: "var foo = [/* \nany comment\n */];", + options: [{ multiline: true }] + }, + { + code: "var foo = [\n1,\n2,\n[3,4],\n];", + options: [{ multiline: true }] + }, + { + code: "var foo = [\n1,\n2,\n[\n3,\n4\n],\n];", + options: [{ multiline: true }] + }, // { multiline: false } { code: "var foo = [];", options: [{ multiline: false }] }, { code: "var foo = [1];", options: [{ multiline: false }] }, - { code: "var foo = [1]/* any comment*/;", options: [{ multiline: false }] }, - { code: "var foo = [1]\n/* any comment*/\n;", options: [{ multiline: false }] }, + { + code: "var foo = [1]/* any comment*/;", + options: [{ multiline: false }] + }, + { + code: "var foo = [1]\n/* any comment*/\n;", + options: [{ multiline: false }] + }, { code: "var foo = [1, 2];", options: [{ multiline: false }] }, { code: "var foo = [1,\n2];", options: [{ multiline: false }] }, - { code: "var foo = [function foo() {\nreturn dosomething();\n}];", options: [{ multiline: false }] }, + { + code: "var foo = [function foo() {\nreturn dosomething();\n}];", + options: [{ multiline: false }] + }, + { code: "var foo = [1,\n2,[3,\n4]];", options: [{ multiline: false }] }, // { minItems: 2 } { code: "var foo = [];", options: [{ minItems: 2 }] }, { code: "var foo = [1];", options: [{ minItems: 2 }] }, { code: "var foo = [\n1, 2\n];", options: [{ minItems: 2 }] }, { code: "var foo = [\n1,\n2\n];", options: [{ minItems: 2 }] }, - { code: "var foo = [function foo() {\ndosomething();\n}];", options: [{ minItems: 2 }] }, + { + code: "var foo = [function foo() {\ndosomething();\n}];", + options: [{ minItems: 2 }] + }, + { + code: `var foo = [ + 1,[ + 2,3 + ] + ];`, + options: [{ minItems: 2 }] + }, + { + code: `var foo = [[ + 1,2 + ]]`, + options: [{ minItems: 2 }] + }, // { minItems: 0 } { code: "var foo = [\n];", options: [{ minItems: 0 }] }, { code: "var foo = [\n1\n];", options: [{ minItems: 0 }] }, { code: "var foo = [\n1, 2\n];", options: [{ minItems: 0 }] }, { code: "var foo = [\n1,\n2\n];", options: [{ minItems: 0 }] }, - { code: "var foo = [\nfunction foo() {\ndosomething();\n}\n];", options: [{ minItems: 0 }] }, + { + code: "var foo = [\nfunction foo() {\ndosomething();\n}\n];", + options: [{ minItems: 0 }] + }, // { minItems: null } { code: "var foo = [];", options: [{ minItems: null }] }, { code: "var foo = [1];", options: [{ minItems: null }] }, { code: "var foo = [1, 2];", options: [{ minItems: null }] }, { code: "var foo = [1,\n2];", options: [{ minItems: null }] }, - { code: "var foo = [function foo() {\ndosomething();\n}];", options: [{ minItems: null }] }, + { + code: "var foo = [function foo() {\ndosomething();\n}];", + options: [{ minItems: null }] + }, // { multiline: true, minItems: null } - { code: "var foo = [];", options: [{ multiline: true, minItems: null }] }, - { code: "var foo = [1];", options: [{ multiline: true, minItems: null }] }, - { code: "var foo = [1, 2];", options: [{ multiline: true, minItems: null }] }, - { code: "var foo = [\n1,\n2\n];", options: [{ multiline: true, minItems: null }] }, - { code: "var foo = [\nfunction foo() {\ndosomething();\n}\n];", options: [{ multiline: true, minItems: null }] }, + { + code: "var foo = [];", + options: [{ multiline: true, minItems: null }] + }, + { + code: "var foo = [1];", + options: [{ multiline: true, minItems: null }] + }, + { + code: "var foo = [1, 2];", + options: [{ multiline: true, minItems: null }] + }, + { + code: "var foo = [\n1,\n2\n];", + options: [{ multiline: true, minItems: null }] + }, + { + code: "var foo = [\nfunction foo() {\ndosomething();\n}\n];", + options: [{ multiline: true, minItems: null }] + }, // { multiline: true, minItems: 2 } { code: "var a = [];", options: [{ multiline: true, minItems: 2 }] }, { code: "var b = [1];", options: [{ multiline: true, minItems: 2 }] }, - { code: "var b = [ // any comment\n1\n];", options: [{ multiline: true, minItems: 2 }] }, - { code: "var b = [ /* any comment */ 1];", options: [{ multiline: true, minItems: 2 }] }, - { code: "var c = [\n1, 2\n];", options: [{ multiline: true, minItems: 2 }] }, - { code: "var c = [\n/* any comment */1, 2\n];", options: [{ multiline: true, minItems: 2 }] }, - { code: "var c = [\n1, /* any comment */ 2\n];", options: [{ multiline: true, minItems: 2 }] }, - { code: "var d = [\n1,\n2\n];", options: [{ multiline: true, minItems: 2 }] }, - { code: "var e = [\nfunction foo() {\ndosomething();\n}\n];", options: [{ multiline: true, minItems: 2 }] }, + { + code: "var b = [ // any comment\n1\n];", + options: [{ multiline: true, minItems: 2 }] + }, + { + code: "var b = [ /* any comment */ 1];", + options: [{ multiline: true, minItems: 2 }] + }, + { + code: "var c = [\n1, 2\n];", + options: [{ multiline: true, minItems: 2 }] + }, + { + code: "var c = [\n/* any comment */1, 2\n];", + options: [{ multiline: true, minItems: 2 }] + }, + { + code: "var c = [\n1, /* any comment */ 2\n];", + options: [{ multiline: true, minItems: 2 }] + }, + { + code: "var d = [\n1,\n2\n];", + options: [{ multiline: true, minItems: 2 }] + }, + { + code: "var e = [\nfunction foo() {\ndosomething();\n}\n];", + options: [{ multiline: true, minItems: 2 }] + }, /* * ArrayPattern @@ -137,53 +283,262 @@ ruleTester.run("array-bracket-newline", rule, { */ { code: "var [] = foo", parserOptions: { ecmaVersion: 6 } }, { code: "var [a] = foo;", parserOptions: { ecmaVersion: 6 } }, - { code: "var /* any comment */[a] = foo;", parserOptions: { ecmaVersion: 6 } }, - { code: "var /* any comment */\n[a] = foo;", parserOptions: { ecmaVersion: 6 } }, + { + code: "var /* any comment */[a] = foo;", + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var /* any comment */\n[a] = foo;", + parserOptions: { ecmaVersion: 6 } + }, { code: "var [a, b] = foo;", parserOptions: { ecmaVersion: 6 } }, - { code: "var [ // any comment\na, b\n] = foo;", parserOptions: { ecmaVersion: 6 } }, - { code: "var [\n// any comment\na, b\n] = foo;", parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na, b\n// any comment\n] = foo;", parserOptions: { ecmaVersion: 6 } }, + { + code: "var [ // any comment\na, b\n] = foo;", + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\n// any comment\na, b\n] = foo;", + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na, b\n// any comment\n] = foo;", + parserOptions: { ecmaVersion: 6 } + }, { code: "var [\na,\nb\n] = foo;", parserOptions: { ecmaVersion: 6 } }, // "always" - { code: "var [\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\n// any\na\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\n/* any */\na\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na, b\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na, b // any comment\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na, b /* any comment */\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na,\nb\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } }, + { + code: "var [\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\n// any\na\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\n/* any */\na\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na, b\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na, b // any comment\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na, b /* any comment */\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na,\nb\n] = foo;", + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, // "consistent" - { code: "var [] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [a] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [//\na\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [/**/\na\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [/*\n*/a\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, - { code: "var [//\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }, + { + code: "var [] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\n] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [a] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na\n] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [//\na\n] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [/**/\na\n] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [/*\n*/a\n] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [//\n] = foo", + options: ["consistent"], + parserOptions: { ecmaVersion: 6 } + }, // { multiline: true } - { code: "var [] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var [a] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var /* any comment */[a] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var /* any comment */\n[a] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var [a, b] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var [ // any comment\na, b\n] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\n// any comment\na, b\n] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na, b\n// any comment\n] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var [\na,\nb\n] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } } - + { + code: "var [] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [a] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var /* any comment */[a] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var /* any comment */\n[a] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [a, b] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [ // any comment\na, b\n] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\n// any comment\na, b\n] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na, b\n// any comment\n] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "var [\na,\nb\n] = foo;", + options: [{ multiline: true }], + parserOptions: { ecmaVersion: 6 } + } ], invalid: [ + // default : { mutliline : true} + { + code: `var foo = [ + [1,2] + ]`, + output: "var foo = [[1,2]]", + errors: [ + { + messageId: "unexpectedOpeningLinebreak", + type: "ArrayExpression", + line: 1, + column: 11, + endLine: 1, + endColumn: 12 + }, + { + messageId: "unexpectedClosingLinebreak", + type: "ArrayExpression", + line: 3, + column: 13, + endLine: 3, + endColumn: 14 + } + ] + }, + { + code: "var foo = [[2,\n3]]", + output: "var foo = [\n[\n2,\n3\n]\n]", + errors: [ + { + line: 1, + column: 11, + messageId: "missingOpeningLinebreak", + endLine: 1, + endColumn: 12 + }, + { + line: 1, + column: 12, + messageId: "missingOpeningLinebreak", + endLine: 1, + endColumn: 13 + }, + { + line: 2, + column: 2, + messageId: "missingClosingLinebreak", + endLine: 2, + endColumn: 3 + }, + { + line: 2, + column: 3, + messageId: "missingClosingLinebreak", + endLine: 2, + endColumn: 4 + } + ] + }, + /* * ArrayExpression * "always" */ + { + code: "var foo = [[1,2]]", + output: "var foo = [\n[\n1,2\n]\n]", + options: ["always"], + errors: [ + { + line: 1, + column: 11, + messageId: "missingOpeningLinebreak", + endLine: 1, + endColumn: 12 + }, + { + line: 1, + column: 12, + messageId: "missingOpeningLinebreak", + endLine: 1, + endColumn: 13 + }, + { + line: 1, + column: 16, + messageId: "missingClosingLinebreak", + endLine: 1, + endColumn: 17 + }, + { + line: 1, + column: 17, + messageId: "missingClosingLinebreak", + endLine: 1, + endColumn: 18 + } + ] + }, { code: "var foo = [];", output: "var foo = [\n];", @@ -349,6 +704,21 @@ ruleTester.run("array-bracket-newline", rule, { }, // "never" + { + code: `var foo = [[ + 1,2],3];`, + output: "var foo = [[1,2],3];", + options: ["never"], + errors: [ + { + line: 1, + column: 12, + messageId: "unexpectedOpeningLinebreak", + endLine: 1, + endColumn: 13 + } + ] + }, { code: "var foo = [\n];", output: "var foo = [];", @@ -507,6 +877,35 @@ ruleTester.run("array-bracket-newline", rule, { }, // "consistent" + { + code: `var a = [[1,2] + ]`, + output: "var a = [[1,2]]", + options: ["consistent"], + errors: [ + { + line: 2, + column: 13, + messageId: "unexpectedClosingLinebreak", + endLine: 2, + endColumn: 14 + } + ] + }, + { + code: "var a = [\n[\n[1,2]]\n]", + output: "var a = [\n[\n[1,2]\n]\n]", + options: ["consistent"], + errors: [ + { + line: 3, + column: 6, + messageId: "missingClosingLinebreak", + endLine: 3, + endColumn: 7 + } + ] + }, { code: "var foo = [\n1]", output: "var foo = [\n1\n]", @@ -670,6 +1069,34 @@ ruleTester.run("array-bracket-newline", rule, { }, // { minItems: 2 } + { + code: "var foo = [1,[\n2,3\n]\n];", + output: "var foo = [\n1,[\n2,3\n]\n];", + options: [{ minItems: 2 }], + errors: [ + { + line: 1, + column: 11, + messageId: "missingOpeningLinebreak", + endLine: 1, + endColumn: 12 + } + ] + }, + { + code: "var foo = [[1,2\n]]", + output: "var foo = [[\n1,2\n]]", + options: [{ minItems: 2 }], + errors: [ + { + line: 1, + column: 12, + messageId: "missingOpeningLinebreak", + endLine: 1, + endColumn: 13 + } + ] + }, { code: "var foo = [\n];", output: "var foo = [];", @@ -841,7 +1268,6 @@ ruleTester.run("array-bracket-newline", rule, { line: 2, column: 2 } - ] }, { @@ -1580,6 +2006,5 @@ ruleTester.run("array-bracket-newline", rule, { } ] } - ] });