Skip to content

Commit

Permalink
Update: ensure brace-style validates class bodies (fixes #7608) (#7871)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Jan 13, 2017
1 parent 427543a commit bd5e219
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/rules/brace-style.js
Expand Up @@ -140,6 +140,9 @@ module.exports = {
BlockStatement(node) {
validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node));
},
ClassBody(node) {
validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node));
},
SwitchStatement(node) {
const closingCurly = sourceCode.getLastToken(node);
const openingCurly = sourceCode.getTokenBefore(node.cases.length ? node.cases[0] : closingCurly);
Expand Down
86 changes: 85 additions & 1 deletion tests/lib/rules/brace-style.js
Expand Up @@ -22,7 +22,7 @@ const OPEN_MESSAGE = "Opening curly brace does not appear on the same line as co
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });

ruleTester.run("brace-style", rule, {
valid: [
Expand Down Expand Up @@ -50,6 +50,16 @@ ruleTester.run("brace-style", rule, {
"for (foo in bar) { \n baz(); \n }",
"if (a &&\n b &&\n c) { \n }",
"switch(0) {\n}",
"class Foo {\n}",
"(class {\n})",
"class\nFoo {\n}",
`
class Foo {
bar() {
}
}
`,
{ code: "if (foo) {\n}\nelse {\n}", options: ["stroustrup"] },
{ code: "if (foo)\n{\n}\nelse\n{\n}", options: ["allman"] },
{ code: "try { \n bar();\n }\ncatch (e) {\n baz(); \n }", options: ["stroustrup"] },
Expand Down Expand Up @@ -93,6 +103,42 @@ ruleTester.run("brace-style", rule, {
{
code: "switch(x) {}",
options: ["allman", { allowSingleLine: true }]
},
{
code: "class Foo {\n}",
options: ["stroustrup"]
},
{
code: "(class {\n})",
options: ["stroustrup"]
},
{
code: "class Foo\n{\n}",
options: ["allman"]
},
{
code: "(class\n{\n})",
options: ["allman"]
},
{
code: "class\nFoo\n{\n}",
options: ["allman"]
},
{
code: "class Foo {}",
options: ["1tbs", { allowSingleLine: true }]
},
{
code: "class Foo {}",
options: ["allman", { allowSingleLine: true }]
},
{
code: "(class {})",
options: ["1tbs", { allowSingleLine: true }]
},
{
code: "(class {})",
options: ["allman", { allowSingleLine: true }]
}
],

Expand Down Expand Up @@ -494,6 +540,44 @@ ruleTester.run("brace-style", rule, {
output: "switch (x) {\n case 1: foo() \n}",
options: ["1tbs", { allowSingleLine: true }],
errors: [{ message: CLOSE_MESSAGE_SINGLE, type: "Punctuator" }]
},
{
code: "class Foo\n{\n}",
output: "class Foo{\n}",
errors: [{ message: OPEN_MESSAGE, type: "Punctuator" }]
},
{
code: "(class\n{\n})",
output: "(class{\n})",
errors: [{ message: OPEN_MESSAGE, type: "Punctuator" }]
},
{
code: "class Foo{\n}",
output: "class Foo\n{\n}",
options: ["allman"],
errors: [{ message: OPEN_MESSAGE_ALLMAN, type: "Punctuator" }]
},
{
code: "(class {\n})",
output: "(class \n{\n})",
options: ["allman"],
errors: [{ message: OPEN_MESSAGE_ALLMAN, type: "Punctuator" }]
},
{
code: "class Foo {\nbar() {\n}}",
output: "class Foo {\nbar() {\n}\n}",
errors: [{ message: CLOSE_MESSAGE_SINGLE, type: "Punctuator" }]
},
{
code: "(class Foo {\nbar() {\n}})",
output: "(class Foo {\nbar() {\n}\n})",
errors: [{ message: CLOSE_MESSAGE_SINGLE, type: "Punctuator" }]
},
{
code: "class\nFoo{}",
output: "class\nFoo\n{}",
options: ["allman"],
errors: [{ message: OPEN_MESSAGE_ALLMAN, type: "Punctuator" }]
}
]
});

0 comments on commit bd5e219

Please sign in to comment.