mirror of
https://github.com/hanzoai/eslint.git
synced 2026-10-30 19:38:10 +00:00
194 lines
3.9 KiB
JavaScript
194 lines
3.9 KiB
JavaScript
/**
|
|
* @fileoverview Tests for max-classes-per-file rule.
|
|
* @author James Garbutt <https://github.com/43081j>
|
|
*/
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Requirements
|
|
//------------------------------------------------------------------------------
|
|
|
|
const rule = require("../../../lib/rules/max-classes-per-file"),
|
|
RuleTester = require("../../../lib/rule-tester/rule-tester");
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Tests
|
|
//------------------------------------------------------------------------------
|
|
|
|
const ruleTester = new RuleTester({
|
|
languageOptions: { ecmaVersion: 6, sourceType: "script" },
|
|
});
|
|
|
|
ruleTester.run("max-classes-per-file", rule, {
|
|
valid: [
|
|
"class Foo {}",
|
|
"var x = class {};",
|
|
"var x = 5;",
|
|
"/* comment */\n/* eslint-disable rule-to-test/max-classes-per-file */\nclass A {}\nclass B {}",
|
|
{
|
|
code: "class Foo {}",
|
|
options: [1],
|
|
},
|
|
{
|
|
code: "class Foo {}\nclass Bar {}",
|
|
options: [2],
|
|
},
|
|
{
|
|
code: "class Foo {}",
|
|
options: [{ max: 1 }],
|
|
},
|
|
{
|
|
code: "class Foo {}\nclass Bar {}",
|
|
options: [{ max: 2 }],
|
|
},
|
|
{
|
|
code: `
|
|
class Foo {}
|
|
const myExpression = class {}
|
|
`,
|
|
options: [{ ignoreExpressions: true, max: 1 }],
|
|
},
|
|
{
|
|
code: `
|
|
class Foo {}
|
|
class Bar {}
|
|
const myExpression = class {}
|
|
`,
|
|
options: [{ ignoreExpressions: true, max: 2 }],
|
|
},
|
|
],
|
|
|
|
invalid: [
|
|
{
|
|
code: "class Foo {}\nclass Bar {}",
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 2, max: 1 },
|
|
line: 1,
|
|
column: 1,
|
|
endLine: 2,
|
|
endColumn: 13,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "class Foo {}\nconst myExpression = class {}",
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 2, max: 1 },
|
|
line: 1,
|
|
column: 1,
|
|
endLine: 2,
|
|
endColumn: 30,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var x = class {};\nvar y = class {};",
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 2, max: 1 },
|
|
line: 1,
|
|
column: 1,
|
|
endLine: 2,
|
|
endColumn: 18,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "class Foo {}\nvar x = class {};",
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 2, max: 1 },
|
|
line: 1,
|
|
column: 1,
|
|
endLine: 2,
|
|
endColumn: 18,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "class Foo {} class Bar {}",
|
|
options: [1],
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 2, max: 1 },
|
|
line: 1,
|
|
column: 1,
|
|
endLine: 1,
|
|
endColumn: 26,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "class Foo {} class Bar {} class Baz {}",
|
|
options: [2],
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 3, max: 2 },
|
|
line: 1,
|
|
column: 1,
|
|
endLine: 1,
|
|
endColumn: 39,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: `
|
|
class Foo {}
|
|
class Bar {}
|
|
const myExpression = class {}
|
|
`,
|
|
options: [{ ignoreExpressions: true, max: 1 }],
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 2, max: 1 },
|
|
line: 2,
|
|
column: 17,
|
|
endLine: 4,
|
|
endColumn: 46,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: `
|
|
class Foo {}
|
|
class Bar {}
|
|
class Baz {}
|
|
const myExpression = class {}
|
|
`,
|
|
options: [{ ignoreExpressions: true, max: 2 }],
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 3, max: 2 },
|
|
line: 2,
|
|
column: 17,
|
|
endLine: 5,
|
|
endColumn: 46,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "/* comment */\nclass A {}\nclass B {}\n/* comment */",
|
|
errors: [
|
|
{
|
|
messageId: "maximumExceeded",
|
|
data: { classCount: 2, max: 1 },
|
|
line: 2,
|
|
column: 1,
|
|
endLine: 3,
|
|
endColumn: 11,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|