Skip to content

Commit dc80487

Browse files
Update: space-unary-ops uses astUtils.canTokensBeAdjacent (fixes #9907) (#9906)
* Update: Use astUtils.canTokensBeAdjacent in space-unary-ops (fixes #9907) * Docs: Added some await examples to space-unary-ops
1 parent 084351b commit dc80487

File tree

3 files changed

+138
-13
lines changed

3 files changed

+138
-13
lines changed

docs/rules/space-unary-ops.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ This rule has three options:
5959

6060
In this case, spacing will be disallowed after a `new` operator and required before/after a `++` operator.
6161

62-
Examples of **incorrect** code for this rule with the `{"words": true, "nonwords": false}` option:
62+
Examples of **incorrect** code for this rule with the default `{"words": true, "nonwords": false}` option:
6363

6464
```js
6565
/*eslint space-unary-ops: "error"*/
@@ -90,6 +90,14 @@ function *foo() {
9090
}
9191
```
9292

93+
```js
94+
/*eslint space-unary-ops: "error"*/
95+
96+
async function foo() {
97+
await(bar);
98+
}
99+
```
100+
93101
Examples of **correct** code for this rule with the `{"words": true, "nonwords": false}` option:
94102

95103
```js
@@ -125,3 +133,11 @@ function *foo() {
125133
yield (0)
126134
}
127135
```
136+
137+
```js
138+
/*eslint space-unary-ops: "error"*/
139+
140+
async function foo() {
141+
await (bar);
142+
}
143+
```

lib/rules/space-unary-ops.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,6 @@ module.exports = {
6666
node.argument && node.argument.type === "UnaryExpression" && node.argument.operator === "!";
6767
}
6868

69-
/**
70-
* Check if the node's child argument is an "ObjectExpression"
71-
* @param {ASTnode} node AST node
72-
* @returns {boolean} Whether or not the argument's type is "ObjectExpression"
73-
*/
74-
function isArgumentObjectExpression(node) {
75-
return node.argument && node.argument.type && node.argument.type === "ObjectExpression";
76-
}
77-
7869
/**
7970
* Checks if an override exists for a given operator.
8071
* @param {string} operator Operator
@@ -125,7 +116,7 @@ module.exports = {
125116
* @returns {void}
126117
*/
127118
function verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word) {
128-
if (isArgumentObjectExpression(node)) {
119+
if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) {
129120
if (secondToken.range[0] > firstToken.range[1]) {
130121
context.report({
131122
node,

tests/lib/rules/space-unary-ops.js

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ruleTester.run("space-unary-ops", rule, {
4141
code: "foo.bar --",
4242
options: [{ nonwords: true }]
4343
},
44+
4445
{
4546
code: "delete foo.bar",
4647
options: [{ words: true }]
@@ -49,6 +50,14 @@ ruleTester.run("space-unary-ops", rule, {
4950
code: "delete foo[\"bar\"]",
5051
options: [{ words: true }]
5152
},
53+
{
54+
code: "delete foo.bar",
55+
options: [{ words: false }]
56+
},
57+
{
58+
code: "delete(foo.bar)",
59+
options: [{ words: false }]
60+
},
5261

5362
{
5463
code: "new Foo",
@@ -79,6 +88,14 @@ ruleTester.run("space-unary-ops", rule, {
7988
code: "typeof {foo:true}",
8089
options: [{ words: true }]
8190
},
91+
{
92+
code: "typeof (foo)",
93+
options: [{ words: true }]
94+
},
95+
{
96+
code: "typeof(foo)",
97+
options: [{ words: false }]
98+
},
8299
{
83100
code: "typeof!foo",
84101
options: [{ words: false }]
@@ -100,6 +117,14 @@ ruleTester.run("space-unary-ops", rule, {
100117
code: "void foo",
101118
options: [{ words: true }]
102119
},
120+
{
121+
code: "void foo",
122+
options: [{ words: false }]
123+
},
124+
{
125+
code: "void(foo)",
126+
options: [{ words: false }]
127+
},
103128

104129
{
105130
code: "-1",
@@ -217,12 +242,12 @@ ruleTester.run("space-unary-ops", rule, {
217242
options: [{ words: false, overrides: { new: false } }]
218243
},
219244
{
220-
code: "function *foo () { yield (0) }",
245+
code: "function *foo () { yield(0) }",
221246
options: [{ words: true, overrides: { yield: false } }],
222247
parserOptions: { ecmaVersion: 6 }
223248
},
224249
{
225-
code: "function *foo () { yield (0) }",
250+
code: "function *foo () { yield(0) }",
226251
options: [{ words: false, overrides: { yield: false } }],
227252
parserOptions: { ecmaVersion: 6 }
228253
}
@@ -247,6 +272,15 @@ ruleTester.run("space-unary-ops", rule, {
247272
type: "UnaryExpression"
248273
}]
249274
},
275+
{
276+
code: "delete (foo.bar)",
277+
output: "delete(foo.bar)",
278+
options: [{ words: false }],
279+
errors: [{
280+
message: "Unexpected space after unary word operator 'delete'.",
281+
type: "UnaryExpression"
282+
}]
283+
},
250284
{
251285
code: "new(Foo)",
252286
output: "new (Foo)",
@@ -256,6 +290,15 @@ ruleTester.run("space-unary-ops", rule, {
256290
type: "NewExpression"
257291
}]
258292
},
293+
{
294+
code: "new (Foo)",
295+
output: "new(Foo)",
296+
options: [{ words: false }],
297+
errors: [{
298+
message: "Unexpected space after unary word operator 'new'.",
299+
type: "NewExpression"
300+
}]
301+
},
259302
{
260303
code: "new(Foo())",
261304
output: "new (Foo())",
@@ -265,6 +308,15 @@ ruleTester.run("space-unary-ops", rule, {
265308
type: "NewExpression"
266309
}]
267310
},
311+
{
312+
code: "new [foo][0]",
313+
output: "new[foo][0]",
314+
options: [{ words: false }],
315+
errors: [{
316+
message: "Unexpected space after unary word operator 'new'.",
317+
type: "NewExpression"
318+
}]
319+
},
268320

269321
{
270322
code: "typeof(foo)",
@@ -275,6 +327,33 @@ ruleTester.run("space-unary-ops", rule, {
275327
type: "UnaryExpression"
276328
}]
277329
},
330+
{
331+
code: "typeof (foo)",
332+
output: "typeof(foo)",
333+
options: [{ words: false }],
334+
errors: [{
335+
message: "Unexpected space after unary word operator 'typeof'.",
336+
type: "UnaryExpression"
337+
}]
338+
},
339+
{
340+
code: "typeof[foo]",
341+
output: "typeof [foo]",
342+
options: [{ words: true }],
343+
errors: [{
344+
message: "Unary word operator 'typeof' must be followed by whitespace.",
345+
type: "UnaryExpression"
346+
}]
347+
},
348+
{
349+
code: "typeof [foo]",
350+
output: "typeof[foo]",
351+
options: [{ words: false }],
352+
errors: [{
353+
message: "Unexpected space after unary word operator 'typeof'.",
354+
type: "UnaryExpression"
355+
}]
356+
},
278357
{
279358
code: "typeof{foo:true}",
280359
output: "typeof {foo:true}",
@@ -321,6 +400,15 @@ ruleTester.run("space-unary-ops", rule, {
321400
type: "UnaryExpression"
322401
}]
323402
},
403+
{
404+
code: "void[foo];",
405+
output: "void [foo];",
406+
options: [{ words: true }],
407+
errors: [{
408+
message: "Unary word operator 'void' must be followed by whitespace.",
409+
type: "UnaryExpression"
410+
}]
411+
},
324412
{
325413
code: "void{a:0};",
326414
output: "void {a:0};",
@@ -330,6 +418,24 @@ ruleTester.run("space-unary-ops", rule, {
330418
type: "UnaryExpression"
331419
}]
332420
},
421+
{
422+
code: "void (foo)",
423+
output: "void(foo)",
424+
options: [{ words: false }],
425+
errors: [{
426+
message: "Unexpected space after unary word operator 'void'.",
427+
type: "UnaryExpression"
428+
}]
429+
},
430+
{
431+
code: "void [foo]",
432+
output: "void[foo]",
433+
options: [{ words: false }],
434+
errors: [{
435+
message: "Unexpected space after unary word operator 'void'.",
436+
type: "UnaryExpression"
437+
}]
438+
},
333439

334440
{
335441
code: "! foo",
@@ -488,6 +594,18 @@ ruleTester.run("space-unary-ops", rule, {
488594
column: 19
489595
}]
490596
},
597+
{
598+
code: "function *foo() { yield (0) }",
599+
output: "function *foo() { yield(0) }",
600+
options: [{ words: false }],
601+
parserOptions: { ecmaVersion: 6 },
602+
errors: [{
603+
message: "Unexpected space after unary word operator 'yield'.",
604+
type: "YieldExpression",
605+
line: 1,
606+
column: 19
607+
}]
608+
},
491609
{
492610
code: "function *foo() { yield+0 }",
493611
output: "function *foo() { yield +0 }",

0 commit comments

Comments
 (0)