New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add empty-brace-spaces
rule
#933
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Enforce no spaces between braces | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
class Unicorn { | ||
} | ||
``` | ||
|
||
```js | ||
try { | ||
foo(); | ||
} catch { } | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
class Unicorn {} | ||
``` | ||
|
||
```js | ||
try { | ||
foo(); | ||
} catch {} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
|
||
const MESSAGE_ID = 'empty-brace-spaces'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Do not add spaces between braces.' | ||
}; | ||
|
||
const selector = `:matches(${ | ||
[ | ||
'BlockStatement[body.length=0]', | ||
'ClassBody[body.length=0]', | ||
'ObjectExpression[properties.length=0]' | ||
].join(', ') | ||
})`; | ||
|
||
const create = context => { | ||
const sourceCode = context.getSourceCode(); | ||
return { | ||
[selector](node) { | ||
let [start, end] = node.range; | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start += 1; | ||
end -= 1; | ||
|
||
if (!/^\s+$/.test(sourceCode.text.slice(start, end))) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
loc: { | ||
start: sourceCode.getLocFromIndex(start), | ||
end: sourceCode.getLocFromIndex(end) | ||
}, | ||
messageId: MESSAGE_ID, | ||
fix: fixer => fixer.replaceTextRange([start, end], '') | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
const schema = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the point of adding empty schema. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's in template, forgot to remove. BTW: Do you know
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why it didn't catch this. It has caught issues in the past though. |
||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'whitespace', | ||
schema, | ||
messages | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import {outdent} from 'outdent'; | ||
import {flatten} from 'lodash'; | ||
import {test} from './utils/test'; | ||
|
||
const SPACES_PLACEHOLDER = '/* */'; | ||
const cases = [ | ||
'{/* */}', | ||
'function foo(){/* */}', | ||
'if(foo) {/* */}', | ||
'if(foo) {} else if (bar) {/* */}', | ||
'if(foo) {} else {/* */}', | ||
'for(;;){/* */}', | ||
'for(foo in bar){/* */}', | ||
'for(foo of bar){/* */}', | ||
'switch (foo) {case bar: {/* */}}', | ||
'switch (foo) {default: {/* */}}', | ||
'try {/* */} catch(foo){}', | ||
'try {} catch(foo){/* */}', | ||
'try {} catch(foo){} finally {/* */}', | ||
'do {/* */} while (foo)', | ||
'while (foo){/* */}', | ||
'foo = () => {/* */}', | ||
'foo = function (){/* */}', | ||
'function foo(){/* */}', | ||
'foo = {/* */}', | ||
'class Foo {bar() {/* */}}', | ||
'foo = class {bar() {/* */}}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need to test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
]; | ||
const classBodyCases = [ | ||
'class Foo {/* */}', | ||
'foo = class {/* */}' | ||
]; | ||
const allCases = [...cases, ...classBodyCases]; | ||
|
||
const ignoredCases = [ | ||
'switch (foo) {/* */}', | ||
'const {/* */} = foo', | ||
'import {/* */} from "foo"' | ||
]; | ||
|
||
test({ | ||
valid: [ | ||
...flatten([ | ||
'', | ||
'/* comment */', | ||
'\n\t// comment \n' | ||
].map(body => allCases.map(code => code.replace(SPACES_PLACEHOLDER, body)))), | ||
// Not empty | ||
...cases.map(code => code.replace(SPACES_PLACEHOLDER, 'unicorn')), | ||
...classBodyCases.map(code => code.replace(SPACES_PLACEHOLDER, 'bar() {}')), | ||
// `with` | ||
{ | ||
code: 'with (foo) {}', | ||
parserOptions: {ecmaVersion: 5, sourceType: 'script'} | ||
}, | ||
// We don't check these cases | ||
...ignoredCases.map(code => code.replace(SPACES_PLACEHOLDER, ' ')) | ||
], | ||
invalid: [ | ||
...flatten([ | ||
' ', | ||
'\t', | ||
' \t \t ', | ||
'\n\n', | ||
'\r\n' | ||
].map(spaces => allCases.map(code => ({ | ||
code: code.replace(SPACES_PLACEHOLDER, spaces), | ||
output: code.replace(SPACES_PLACEHOLDER, ''), | ||
errors: 1 | ||
})))), | ||
// `with` | ||
{ | ||
code: 'with (foo) { }', | ||
output: 'with (foo) {}', | ||
errors: 1, | ||
parserOptions: {ecmaVersion: 5, sourceType: 'script'} | ||
} | ||
] | ||
}); | ||
|
||
test.visualize([ | ||
outdent` | ||
try { | ||
foo(); | ||
} catch (error) { | ||
\u0020\u0020\u0020\u0020\u0020\u0020\u0020 | ||
} | ||
` | ||
]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Snapshot report for `test/empty-brace-spaces.js` | ||
|
||
The actual snapshot is saved in `empty-brace-spaces.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## empty-brace-spaces - #1 | ||
|
||
> Snapshot 1 | ||
|
||
`␊ | ||
Input:␊ | ||
1 | try {␊ | ||
2 | foo();␊ | ||
3 | } catch (error) {␊ | ||
4 | ␊ | ||
5 | }␊ | ||
␊ | ||
Output:␊ | ||
1 | try {␊ | ||
2 | foo();␊ | ||
3 | } catch (error) {}␊ | ||
␊ | ||
Error 1/1:␊ | ||
1 | try {␊ | ||
2 | foo();␊ | ||
> 3 | } catch (error) {␊ | ||
| ^␊ | ||
> 4 | ␊ | ||
| ^^^^^^^^␊ | ||
> 5 | }␊ | ||
| ^ Do not add spaces between braces.␊ | ||
` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it easier to just do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, same. But I prefer current one, it's more readable add diff friendly, if we want add more.
const selector = `:matches(${ [ 'BlockStatement[body.length=0]', 'ClassBody[body.length=0]', + 'ImportNamespaceSpecifier[assertions.length=0]', 'ObjectExpression[properties.length=0]' ].join(', ') })`;
VS