const parser = require("../../fixtures/fixture-parser"),
rule = require("../../../lib/rules/keyword-spacing"),
- { RuleTester } = require("../../../lib/rule-tester");
+ { RuleTester } = require("../../../lib/rule-tester"),
+ fixtureParser = require("../../fixtures/fixture-parser");
//------------------------------------------------------------------------------
// Helpers
// not conflict with `space-infix-ops`
{ code: "async function wrap() { a =await a }", parserOptions: { ecmaVersion: 8 } },
{ code: "async function wrap() { a = await a }", options: [NEITHER], parserOptions: { ecmaVersion: 8 } },
+ { code: "async function wrap() { a+await a }", parserOptions: { ecmaVersion: 8 } },
+ { code: "async function wrap() { a + await a }", options: [NEITHER], parserOptions: { ecmaVersion: 8 } },
+ { code: "async function wrap() { a<await a }", parserOptions: { ecmaVersion: 8 } },
+ { code: "async function wrap() { a < await a }", options: [NEITHER], parserOptions: { ecmaVersion: 8 } },
+ { code: "async function wrap() { a>await a }", parserOptions: { ecmaVersion: 8 } },
+ { code: "async function wrap() { a > await a }", options: [NEITHER], parserOptions: { ecmaVersion: 8 } },
// not conflict with `space-unary-ops`
{ code: "async function wrap() { !await'a' }", parserOptions: { ecmaVersion: 8 } },
// not conflict with `space-infix-ops`
{ code: "a =class {}", parserOptions: { ecmaVersion: 6 } },
{ code: "a = class{}", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ { code: "a+class {}", parserOptions: { ecmaVersion: 6 } },
+ { code: "a + class{}", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ { code: "a<class {}", parserOptions: { ecmaVersion: 6 } },
+ { code: "a < class{}", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ { code: "a>class {}", parserOptions: { ecmaVersion: 6 } },
+ { code: "a > class{}", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
// not conflict with `space-unary-ops`
{ code: "!class {}", parserOptions: { ecmaVersion: 6 } },
{ code: "<Foo onClick={class {}} />", parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
{ code: "<Foo onClick={ class{}} />", options: [NEITHER], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
+ // private names
+ { code: "class C {\n#x;\nfoo() {\nfor (this.#x of bar){}}}", options: [{ before: false }], parserOptions: { ecmaVersion: 2022 } },
+ { code: "class C {\n#x;\nfoo() {\nfor (this.#x in bar){}}}", options: [{ before: false }], parserOptions: { ecmaVersion: 2022 } },
+
//----------------------------------------------------------------------
// const
//----------------------------------------------------------------------
// not conflict with `space-infix-ops`
"a =delete foo.a",
{ code: "a = delete foo.a", options: [NEITHER] },
+ "a+delete foo.a",
+ { code: "a + delete foo.a", options: [NEITHER] },
+ "a<delete foo.a",
+ { code: "a < delete foo.a", options: [NEITHER] },
+ "a>delete foo.a",
+ { code: "a > delete foo.a", options: [NEITHER] },
// not conflict with `space-unary-ops`
"!delete(foo.a)",
// not conflict with `space-infix-ops`
"a =function() {}",
{ code: "a = function() {}", options: [NEITHER] },
+ "a+function() {}",
+ { code: "a + function() {}", options: [NEITHER] },
+ "a<function() {}",
+ { code: "a < function() {}", options: [NEITHER] },
+ "a>function() {}",
+ { code: "a > function() {}", options: [NEITHER] },
// not conflict with `space-unary-ops`
"!function() {}",
{ code: "class A { a() {} get [b]() {} }", options: [override("get", BOTH)], parserOptions: { ecmaVersion: 6 } },
{ code: "({ get[b]() {} })", options: [override("get", NEITHER)], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { a() {}get[b]() {} }", options: [override("get", NEITHER)], parserOptions: { ecmaVersion: 6 } },
+ { code: "class A { a; get #b() {} }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a;get#b() {} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
// not conflict with `comma-spacing`
{ code: "({ a,get [b]() {} })", parserOptions: { ecmaVersion: 6 } },
{ code: "({ a, get[b]() {} })", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ // not conflict with `semi-spacing`
+ { code: "class A { ;get #b() {} }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { ; get#b() {} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
+
//----------------------------------------------------------------------
// if
//----------------------------------------------------------------------
// not conflict with `space-infix-ops`
"a =new foo()",
{ code: "a = new foo()", options: [NEITHER] },
+ "a+new foo()",
+ { code: "a + new foo()", options: [NEITHER] },
+ "a<new foo()",
+ { code: "a < new foo()", options: [NEITHER] },
+ "a>new foo()",
+ { code: "a > new foo()", options: [NEITHER] },
// not conflict with `space-unary-ops`
"!new(foo)()",
//----------------------------------------------------------------------
"function foo() { {} return +a }",
+ {
+ code: "function foo() { return <p/>; }",
+ parserOptions: { ecmaFeatures: { jsx: true } }
+ },
{ code: "function foo() { {}return+a }", options: [NEITHER] },
+ {
+ code: "function foo() { return<p/>; }",
+ options: [{ after: false }],
+ parserOptions: { ecmaFeatures: { jsx: true } }
+ },
{ code: "function foo() { {} return +a }", options: [override("return", BOTH)] },
{ code: "function foo() { {}return+a }", options: [override("return", NEITHER)] },
"function foo() {\nreturn\n}",
{ code: "class A { a() {} set [b](value) {} }", options: [override("set", BOTH)], parserOptions: { ecmaVersion: 6 } },
{ code: "({ set[b](value) {} })", options: [override("set", NEITHER)], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { a() {}set[b](value) {} }", options: [override("set", NEITHER)], parserOptions: { ecmaVersion: 6 } },
+ { code: "class A { a; set #b(value) {} }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a;set#b(value) {} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
// not conflict with `comma-spacing`
{ code: "({ a,set [b](value) {} })", parserOptions: { ecmaVersion: 6 } },
{ code: "({ a, set[b](value) {} })", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ // not conflict with `semi-spacing`
+ { code: "class A { ;set #b(value) {} }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { ; set#b(value) {} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
+
//----------------------------------------------------------------------
// static
//----------------------------------------------------------------------
{ code: "class A { a() {}static[b]() {} }", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { a() {} static [b]() {} }", options: [override("static", BOTH)], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { a() {}static[b]() {} }", options: [override("static", NEITHER)], parserOptions: { ecmaVersion: 6 } },
+ { code: "class A { a; static [b]; }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a;static[b]; }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a; static #b; }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a;static#b; }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a() {} static {} }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a() {}static{} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a() {} static {} }", options: [override("static", BOTH)], parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a() {}static{} }", options: [override("static", NEITHER)], parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { a() {}\nstatic\n{} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
// not conflict with `generator-star-spacing`
{ code: "class A { static* [a]() {} }", parserOptions: { ecmaVersion: 6 } },
// not conflict with `semi-spacing`
{ code: "class A { ;static a() {} }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { ; static a() {} }", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ { code: "class A { ;static a; }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { ; static a ; }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { ;static {} }", parserOptions: { ecmaVersion: 2022 } },
+ { code: "class A { ; static{} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
//----------------------------------------------------------------------
// super
// not conflict with `space-infix-ops`
{ code: "class A extends B { constructor() { b =super() } }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A extends B { constructor() { b = super() } }", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ { code: "class A extends B { constructor() { b+super() } }", parserOptions: { ecmaVersion: 6 } },
+ { code: "class A extends B { constructor() { b + super() } }", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ { code: "class A extends B { constructor() { b<super() } }", parserOptions: { ecmaVersion: 6 } },
+ { code: "class A extends B { constructor() { b < super() } }", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
+ { code: "class A extends B { constructor() { b>super() } }", parserOptions: { ecmaVersion: 6 } },
+ { code: "class A extends B { constructor() { b > super() } }", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
// not conflict with `space-unary-ops`
{ code: "class A extends B { constructor() { !super() } }", parserOptions: { ecmaVersion: 6 } },
{ code: "{} this[a]", options: [override("this", BOTH)] },
{ code: "{}this[a]", options: [override("this", NEITHER)] },
+ {
+ code: "<Thing> this.blah",
+ parser: fixtureParser("keyword-spacing", "prefix-cast-operator-space")
+ },
+ {
+ code: "<Thing>this.blah",
+ options: [override("this", { before: false })],
+ parser: fixtureParser("keyword-spacing", "prefix-cast-operator-no-space")
+ },
+
// not conflict with `array-bracket-spacing`
"[this]",
{ code: "[ this ]", options: [NEITHER] },
// not conflict with `space-infix-ops`
"a =this",
{ code: "a = this", options: [NEITHER] },
+ "a+this",
+ { code: "a + this", options: [NEITHER] },
+ "a<this",
+ { code: "a < this", options: [NEITHER] },
+ "a>this",
+ { code: "a > this", options: [NEITHER] },
+ "this+a",
+ { code: "this + a", options: [NEITHER] },
+ "this<a",
+ { code: "this < a", options: [NEITHER] },
+ "this>a",
+ { code: "this > a", options: [NEITHER] },
// not conflict with `space-unary-ops`
"!this",
// not conflict with `space-infix-ops`
"a =typeof foo",
{ code: "a = typeof foo", options: [NEITHER] },
+ "a+typeof foo",
+ { code: "a + typeof foo", options: [NEITHER] },
+ "a<typeof foo",
+ { code: "a < typeof foo", options: [NEITHER] },
+ "a>typeof foo",
+ { code: "a > typeof foo", options: [NEITHER] },
// not conflict with `space-unary-ops`
"!typeof+foo",
// not conflict with `space-infix-ops`
"a =void foo",
{ code: "a = void foo", options: [NEITHER] },
+ "a+void foo",
+ { code: "a + void foo", options: [NEITHER] },
+ "a<void foo",
+ { code: "a < void foo", options: [NEITHER] },
+ "a>void foo",
+ { code: "a > void foo", options: [NEITHER] },
// not conflict with `space-unary-ops`
"!void+foo",
parserOptions: { ecmaVersion: 6 },
errors: unexpectedBeforeAndAfter("get")
},
+ {
+ code: "class A { a;get#b() {} }",
+ output: "class A { a;get #b() {} }",
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedAfter("get")
+ },
+ {
+ code: "class A { a; get #b() {} }",
+ output: "class A { a; get#b() {} }",
+ options: [NEITHER],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedAfter("get")
+ },
//----------------------------------------------------------------------
// if
output: "function foo() { {} return +a }",
errors: expectedBeforeAndAfter("return")
},
+ {
+ code: "function foo() { return<p/>; }",
+ output: "function foo() { return <p/>; }",
+ parserOptions: { ecmaFeatures: { jsx: true } },
+ errors: expectedAfter("return")
+ },
{
code: "function foo() { {} return +a }",
output: "function foo() { {}return+a }",
options: [NEITHER],
errors: unexpectedBeforeAndAfter("return")
},
+ {
+ code: "function foo() { return <p/>; }",
+ output: "function foo() { return<p/>; }",
+ options: [{ after: false }],
+ parserOptions: { ecmaFeatures: { jsx: true } },
+ errors: unexpectedAfter("return")
+ },
{
code: "function foo() { {}return+a }",
output: "function foo() { {} return +a }",
parserOptions: { ecmaVersion: 6 },
errors: unexpectedBeforeAndAfter("set")
},
+ {
+ code: "class A { a;set#b(x) {} }",
+ output: "class A { a;set #b(x) {} }",
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedAfter("set")
+ },
+ {
+ code: "class A { a; set #b(x) {} }",
+ output: "class A { a; set#b(x) {} }",
+ options: [NEITHER],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedAfter("set")
+ },
//----------------------------------------------------------------------
// static
parserOptions: { ecmaVersion: 6 },
errors: unexpectedBeforeAndAfter("static")
},
+ {
+ code: "class A { a;static[b]; }",
+ output: "class A { a;static [b]; }",
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedAfter("static")
+ },
+ {
+ code: "class A { a; static [b]; }",
+ output: "class A { a; static[b]; }",
+ options: [NEITHER],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedAfter("static")
+ },
+ {
+ code: "class A { a;static#b; }",
+ output: "class A { a;static #b; }",
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedAfter("static")
+ },
+ {
+ code: "class A { a; static #b; }",
+ output: "class A { a; static#b; }",
+ options: [NEITHER],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedAfter("static")
+ },
+ {
+ code: "class A { a() {}static{} }",
+ output: "class A { a() {} static {} }",
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedBeforeAndAfter("static")
+ },
+ {
+ code: "class A { a() {}static{} }",
+ output: "class A { a() {} static {} }",
+ options: [override("static", BOTH)],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedBeforeAndAfter("static")
+ },
+ {
+ code: "class A { a() {}static {} }",
+ output: "class A { a() {} static {} }",
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedBefore("static")
+ },
+ {
+ code: "class A { a() {} static{} }",
+ output: "class A { a() {} static {} }",
+ parserOptions: { ecmaVersion: 2022 },
+ errors: expectedAfter("static")
+ },
+ {
+ code: "class A { a() {} static {} }",
+ output: "class A { a() {}static{} }",
+ options: [NEITHER],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedBeforeAndAfter("static")
+ },
+ {
+ code: "class A { a() {} static {} }",
+ output: "class A { a() {}static{} }",
+ options: [override("static", NEITHER)],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedBeforeAndAfter("static")
+ },
+ {
+ code: "class A { a() {} static{} }",
+ output: "class A { a() {}static{} }",
+ options: [NEITHER],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedBefore("static")
+ },
+ {
+ code: "class A { a() {}static {} }",
+ output: "class A { a() {}static{} }",
+ options: [NEITHER],
+ parserOptions: { ecmaVersion: 2022 },
+ errors: unexpectedAfter("static")
+ },
//----------------------------------------------------------------------
// super
options: [override("this", NEITHER)],
errors: unexpectedBefore("this")
},
+ {
+ code: "<Thing> this.blah",
+ output: "<Thing>this.blah",
+ options: [override("this", { before: false })],
+ parser: fixtureParser("keyword-spacing", "prefix-cast-operator-space"),
+ errors: unexpectedBefore("this")
+ },
+ {
+ code: "<Thing>this.blah",
+ output: "<Thing> this.blah",
+ parser: fixtureParser("keyword-spacing", "prefix-cast-operator-no-space"),
+ errors: expectedBefore("this")
+ },
//----------------------------------------------------------------------
// throw