Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Enforce no spaces between braces. | ||
|
||
<!-- More detailed description. Remove this comment. --> | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const foo = 'unicorn'; | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const foo = '🦄'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-space'; | ||
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; | ||
start = start + 1; | ||
end = 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 = []; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'whitespace', | ||
schema, | ||
messages | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {outdent} from 'outdent'; | ||
import {test} from './utils/test'; | ||
|
||
const MESSAGE_ID = 'empty-brace-space'; | ||
|
||
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() {/* */}}' | ||
]; | ||
const classBodyCases = [ | ||
'class Foo {/* */}', | ||
'foo = class {/* */}', | ||
]; | ||
const allCases = [...cases, ...classBodyCases]; | ||
|
||
const casesIgnored = [ | ||
'switch (foo) {/* */}', | ||
'const {/* */} = foo', | ||
'import {/* */} from "foo"' | ||
]; | ||
|
||
test({ | ||
valid: [ | ||
// We don't check these cases | ||
...casesIgnored.map(code => code.replace(SPACES_PLACEHOLDER, ' ')), | ||
// No space | ||
...allCases.map(code => code.replace(SPACES_PLACEHOLDER, '')), | ||
// Comments | ||
...allCases.map(code => code.replace(SPACES_PLACEHOLDER, '/* comment */')), | ||
...allCases.map(code => code.replace(SPACES_PLACEHOLDER, '\n\t// comment \n')), | ||
// 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'} | ||
} | ||
], | ||
invalid: [ | ||
...[' ', '\t', ' \t \t ', '\n\n', '\r\n'].map(space => | ||
allCases.map(code => ({ | ||
code: code.replace(SPACES_PLACEHOLDER, space), | ||
output: code.replace(SPACES_PLACEHOLDER, ''), | ||
errors: 1 | ||
})) | ||
).flat(), | ||
// `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 | ||
} | ||
` | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Snapshot report for `test/empty-brace-space.js` | ||
|
||
The actual snapshot is saved in `empty-brace-space.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## empty-brace-space - #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.␊ | ||
` |
Binary file not shown.