Files

1304 lines
25 KiB
JavaScript

/**
* @fileoverview Tests for block-spacing rule.
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/block-spacing");
const RuleTester = require("../../../lib/rule-tester/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("block-spacing", rule, {
valid: [
// default/always
{ code: "{ foo(); }", options: ["always"] },
"{ foo(); }",
"{ foo();\n}",
"{\nfoo(); }",
"{\r\nfoo();\r\n}",
"if (a) { foo(); }",
"if (a) {} else { foo(); }",
"switch (a) {}",
"switch (a) { case 0: foo(); }",
"while (a) { foo(); }",
"do { foo(); } while (a);",
"for (;;) { foo(); }",
"for (var a in b) { foo(); }",
{
code: "for (var a of b) { foo(); }",
languageOptions: { ecmaVersion: 6 },
},
"try { foo(); } catch (e) { foo(); }",
"function foo() { bar(); }",
"(function() { bar(); });",
{ code: "(() => { bar(); });", languageOptions: { ecmaVersion: 6 } },
"if (a) { /* comment */ foo(); /* comment */ }",
"if (a) { //comment\n foo(); }",
{
code: "class C { static {} }",
languageOptions: { ecmaVersion: 2022 },
},
{
code: "class C { static { foo; } }",
languageOptions: { ecmaVersion: 2022 },
},
{
code: "class C { static { /* comment */foo;/* comment */ } }",
languageOptions: { ecmaVersion: 2022 },
},
// never
{ code: "{foo();}", options: ["never"] },
{ code: "{foo();\n}", options: ["never"] },
{ code: "{\nfoo();}", options: ["never"] },
{ code: "{\r\nfoo();\r\n}", options: ["never"] },
{ code: "if (a) {foo();}", options: ["never"] },
{ code: "if (a) {} else {foo();}", options: ["never"] },
{ code: "switch (a) {}", options: ["never"] },
{ code: "switch (a) {case 0: foo();}", options: ["never"] },
{ code: "while (a) {foo();}", options: ["never"] },
{ code: "do {foo();} while (a);", options: ["never"] },
{ code: "for (;;) {foo();}", options: ["never"] },
{ code: "for (var a in b) {foo();}", options: ["never"] },
{
code: "for (var a of b) {foo();}",
options: ["never"],
languageOptions: { ecmaVersion: 6 },
},
{ code: "try {foo();} catch (e) {foo();}", options: ["never"] },
{ code: "function foo() {bar();}", options: ["never"] },
{ code: "(function() {bar();});", options: ["never"] },
{
code: "(() => {bar();});",
options: ["never"],
languageOptions: { ecmaVersion: 6 },
},
{
code: "if (a) {/* comment */ foo(); /* comment */}",
options: ["never"],
},
{ code: "if (a) { //comment\n foo();}", options: ["never"] },
{
code: "class C { static { } }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
},
{
code: "class C { static {foo;} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
},
{
code: "class C { static {/* comment */ foo; /* comment */} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
},
{
code: "class C { static { // line comment is allowed\n foo;\n} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
},
{
code: "class C { static {\nfoo;\n} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
},
{
code: "class C { static { \n foo; \n } }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
},
],
invalid: [
// default/always
{
code: "{foo();}",
output: "{ foo(); }",
options: ["always"],
errors: [
{
line: 1,
column: 1,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 8,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "{foo();}",
output: "{ foo(); }",
errors: [
{
line: 1,
column: 1,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 8,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "{ foo();}",
output: "{ foo(); }",
errors: [
{
line: 1,
column: 9,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "{foo(); }",
output: "{ foo(); }",
errors: [
{
line: 1,
column: 1,
messageId: "missing",
data: { location: "after", token: "{" },
},
],
},
{
code: "{\nfoo();}",
output: "{\nfoo(); }",
errors: [
{
line: 2,
column: 7,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "{foo();\n}",
output: "{ foo();\n}",
errors: [
{
line: 1,
column: 1,
messageId: "missing",
data: { location: "after", token: "{" },
},
],
},
{
code: "if (a) {foo();}",
output: "if (a) { foo(); }",
errors: [
{
line: 1,
column: 8,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 15,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "if (a) {} else {foo();}",
output: "if (a) {} else { foo(); }",
errors: [
{
line: 1,
column: 16,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 23,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "switch (a) {case 0: foo();}",
output: "switch (a) { case 0: foo(); }",
errors: [
{
line: 1,
column: 12,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 27,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "while (a) {foo();}",
output: "while (a) { foo(); }",
errors: [
{
line: 1,
column: 11,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 18,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "do {foo();} while (a);",
output: "do { foo(); } while (a);",
errors: [
{
line: 1,
column: 4,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 11,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "for (;;) {foo();}",
output: "for (;;) { foo(); }",
errors: [
{
line: 1,
column: 10,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 17,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "for (var a in b) {foo();}",
output: "for (var a in b) { foo(); }",
errors: [
{
line: 1,
column: 18,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 25,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "for (var a of b) {foo();}",
output: "for (var a of b) { foo(); }",
languageOptions: { ecmaVersion: 6 },
errors: [
{
line: 1,
column: 18,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 25,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "try {foo();} catch (e) {foo();} finally {foo();}",
output: "try { foo(); } catch (e) { foo(); } finally { foo(); }",
errors: [
{
messageId: "missing",
data: { location: "after", token: "{" },
line: 1,
column: 5,
endLine: 1,
endColumn: 6,
},
{
messageId: "missing",
data: { location: "before", token: "}" },
line: 1,
column: 12,
endLine: 1,
endColumn: 13,
},
{
messageId: "missing",
data: { location: "after", token: "{" },
line: 1,
column: 24,
endLine: 1,
endColumn: 25,
},
{
messageId: "missing",
data: { location: "before", token: "}" },
line: 1,
column: 31,
endLine: 1,
endColumn: 32,
},
{
messageId: "missing",
data: { location: "after", token: "{" },
line: 1,
column: 41,
endLine: 1,
endColumn: 42,
},
{
messageId: "missing",
data: { location: "before", token: "}" },
line: 1,
column: 48,
endLine: 1,
endColumn: 49,
},
],
},
{
code: "function foo() {bar();}",
output: "function foo() { bar(); }",
errors: [
{
line: 1,
column: 16,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 23,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "(function() {bar();});",
output: "(function() { bar(); });",
errors: [
{
line: 1,
column: 13,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 20,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "(() => {bar();});",
output: "(() => { bar(); });",
languageOptions: { ecmaVersion: 6 },
errors: [
{
line: 1,
column: 8,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 15,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "if (a) {/* comment */ foo(); /* comment */}",
output: "if (a) { /* comment */ foo(); /* comment */ }",
languageOptions: { ecmaVersion: 6 },
errors: [
{
line: 1,
column: 8,
messageId: "missing",
data: { location: "after", token: "{" },
},
{
line: 1,
column: 43,
messageId: "missing",
data: { location: "before", token: "}" },
},
],
},
{
code: "if (a) {//comment\n foo(); }",
output: "if (a) { //comment\n foo(); }",
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "missing",
data: {
location: "after",
token: "{",
},
line: 1,
column: 8,
endLine: 1,
endColumn: 9,
},
],
},
// class static blocks
{
code: "class C { static {foo; } }",
output: "class C { static { foo; } }",
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "missing",
data: {
location: "after",
token: "{",
},
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
],
},
{
code: "class C { static { foo;} }",
output: "class C { static { foo; } }",
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "missing",
data: {
location: "before",
token: "}",
},
line: 1,
column: 24,
endLine: 1,
endColumn: 25,
},
],
},
{
code: "class C { static {foo;} }",
output: "class C { static { foo; } }",
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "missing",
data: {
location: "after",
token: "{",
},
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
{
messageId: "missing",
data: {
location: "before",
token: "}",
},
line: 1,
column: 23,
endLine: 1,
endColumn: 24,
},
],
},
{
code: "class C { static {/* comment */} }",
output: "class C { static { /* comment */ } }",
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "missing",
data: {
location: "after",
token: "{",
},
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
{
messageId: "missing",
data: {
location: "before",
token: "}",
},
line: 1,
column: 32,
endLine: 1,
endColumn: 33,
},
],
},
{
code: "class C { static {/* comment 1 */ foo; /* comment 2 */} }",
output: "class C { static { /* comment 1 */ foo; /* comment 2 */ } }",
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "missing",
data: {
location: "after",
token: "{",
},
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
{
messageId: "missing",
data: {
location: "before",
token: "}",
},
line: 1,
column: 55,
endLine: 1,
endColumn: 56,
},
],
},
{
code: "class C {\n static {foo()\nbar()} }",
output: "class C {\n static { foo()\nbar() } }",
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "missing",
data: {
location: "after",
token: "{",
},
line: 2,
column: 9,
endLine: 2,
endColumn: 10,
},
{
messageId: "missing",
data: {
location: "before",
token: "}",
},
line: 3,
column: 6,
endLine: 3,
endColumn: 7,
},
],
},
//----------------------------------------------------------------------
// never
{
code: "{ foo(); }",
output: "{foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 2,
endLine: 1,
endColumn: 3,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 9,
endLine: 1,
endColumn: 10,
},
],
},
{
code: "{ foo();}",
output: "{foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: {
location: "after",
token: "{",
},
line: 1,
column: 2,
endLine: 1,
endColumn: 3,
},
],
},
{
code: "{foo(); }",
output: "{foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: {
location: "before",
token: "}",
},
line: 1,
column: 8,
endLine: 1,
endColumn: 9,
},
],
},
{
code: "{\nfoo(); }",
output: "{\nfoo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: {
location: "before",
token: "}",
},
line: 2,
column: 7,
endLine: 2,
endColumn: 8,
},
],
},
{
code: "{ foo();\n}",
output: "{foo();\n}",
options: ["never"],
errors: [
{
messageId: "extra",
data: {
location: "after",
token: "{",
},
line: 1,
column: 2,
endLine: 1,
endColumn: 3,
},
],
},
{
code: "if (a) { foo(); }",
output: "if (a) {foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 9,
endLine: 1,
endColumn: 10,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 16,
endLine: 1,
endColumn: 17,
},
],
},
{
code: "if (a) {} else { foo(); }",
output: "if (a) {} else {foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 17,
endLine: 1,
endColumn: 18,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 24,
endLine: 1,
endColumn: 25,
},
],
},
{
code: "switch (a) { case 0: foo(); }",
output: "switch (a) {case 0: foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 13,
endLine: 1,
endColumn: 14,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 28,
endLine: 1,
endColumn: 29,
},
],
},
{
code: "while (a) { foo(); }",
output: "while (a) {foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 12,
endLine: 1,
endColumn: 13,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
],
},
{
code: "do { foo(); } while (a);",
output: "do {foo();} while (a);",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 5,
endLine: 1,
endColumn: 6,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 12,
endLine: 1,
endColumn: 13,
},
],
},
{
code: "for (;;) { foo(); }",
output: "for (;;) {foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 11,
endLine: 1,
endColumn: 12,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
],
},
{
code: "for (var a in b) { foo(); }",
output: "for (var a in b) {foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 26,
endLine: 1,
endColumn: 27,
},
],
},
{
code: "for (var a of b) { foo(); }",
output: "for (var a of b) {foo();}",
options: ["never"],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 26,
endLine: 1,
endColumn: 27,
},
],
},
{
code: "try { foo(); } catch (e) { foo(); } finally { foo(); }",
output: "try {foo();} catch (e) {foo();} finally {foo();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 6,
endLine: 1,
endColumn: 7,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 13,
endLine: 1,
endColumn: 14,
},
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 27,
endLine: 1,
endColumn: 28,
},
{
line: 1,
column: 34,
messageId: "extra",
data: { location: "before", token: "}" },
endLine: 1,
endColumn: 35,
},
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 46,
endLine: 1,
endColumn: 47,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 53,
endLine: 1,
endColumn: 54,
},
],
},
{
code: "function foo() { bar(); }",
output: "function foo() {bar();}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 17,
endLine: 1,
endColumn: 18,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 24,
endLine: 1,
endColumn: 25,
},
],
},
{
code: "(function() { bar(); });",
output: "(function() {bar();});",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 14,
endLine: 1,
endColumn: 15,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 21,
endLine: 1,
endColumn: 22,
},
],
},
{
code: "(() => { bar(); });",
output: "(() => {bar();});",
options: ["never"],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 9,
endLine: 1,
endColumn: 10,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 16,
endLine: 1,
endColumn: 17,
},
],
},
{
code: "if (a) { /* comment */ foo(); /* comment */ }",
output: "if (a) {/* comment */ foo(); /* comment */}",
options: ["never"],
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 9,
endLine: 1,
endColumn: 10,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 44,
endLine: 1,
endColumn: 45,
},
],
},
{
code: "(() => { bar();});",
output: "(() => {bar();});",
options: ["never"],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 9,
endLine: 1,
endColumn: 12,
},
],
},
{
code: "(() => {bar(); });",
output: "(() => {bar();});",
options: ["never"],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 15,
endLine: 1,
endColumn: 18,
},
],
},
{
code: "(() => { bar(); });",
output: "(() => {bar();});",
options: ["never"],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "extra",
data: { location: "after", token: "{" },
line: 1,
column: 9,
endLine: 1,
endColumn: 12,
},
{
messageId: "extra",
data: { location: "before", token: "}" },
line: 1,
column: 18,
endLine: 1,
endColumn: 21,
},
],
},
// class static blocks
{
code: "class C { static { foo;} }",
output: "class C { static {foo;} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "extra",
data: {
location: "after",
token: "{",
},
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
],
},
{
code: "class C { static {foo; } }",
output: "class C { static {foo;} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "extra",
data: {
location: "before",
token: "}",
},
line: 1,
column: 23,
endLine: 1,
endColumn: 24,
},
],
},
{
code: "class C { static { foo; } }",
output: "class C { static {foo;} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "extra",
data: {
location: "after",
token: "{",
},
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
{
messageId: "extra",
data: {
location: "before",
token: "}",
},
line: 1,
column: 24,
endLine: 1,
endColumn: 25,
},
],
},
{
code: "class C { static { /* comment */ } }",
output: "class C { static {/* comment */} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "extra",
data: {
location: "after",
token: "{",
},
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
{
messageId: "extra",
data: {
location: "before",
token: "}",
},
line: 1,
column: 33,
endLine: 1,
endColumn: 34,
},
],
},
{
code: "class C { static { /* comment 1 */ foo; /* comment 2 */ } }",
output: "class C { static {/* comment 1 */ foo; /* comment 2 */} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "extra",
data: {
location: "after",
token: "{",
},
line: 1,
column: 19,
endLine: 1,
endColumn: 20,
},
{
messageId: "extra",
data: {
location: "before",
token: "}",
},
line: 1,
column: 56,
endLine: 1,
endColumn: 57,
},
],
},
{
code: "class C { static\n{ foo()\nbar() } }",
output: "class C { static\n{foo()\nbar()} }",
options: ["never"],
languageOptions: { ecmaVersion: 2022 },
errors: [
{
messageId: "extra",
data: {
location: "after",
token: "{",
},
line: 2,
column: 2,
endLine: 2,
endColumn: 5,
},
{
messageId: "extra",
data: {
location: "before",
token: "}",
},
line: 3,
column: 6,
endLine: 3,
endColumn: 8,
},
],
},
],
});