Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(eslint-plugin-putout) single-property.js (https://gist.github…
- Loading branch information
1 parent
ab0adc0
commit e3e38ed
Showing
3 changed files
with
112 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,53 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'Keep curly braces on one line when you have one destructuring property', | ||
category: 'destructuring', | ||
recommended: true, | ||
}, | ||
fixable: 'whitespace', | ||
}, | ||
|
||
create(context) { | ||
return { | ||
'VariableDeclarator[id.type="ObjectPattern"][id.properties.length=1]': (node) => { | ||
const {id} = node; | ||
|
||
const text = context | ||
.getSourceCode() | ||
.getText(node.parent); | ||
|
||
if (!/(const|let|var) \{\n/.test(text)) | ||
return; | ||
|
||
const assignRegExp = /\{\n?.*=.*\n?.*}/; | ||
|
||
if (assignRegExp.test(text)) | ||
return; | ||
|
||
context.report({ | ||
node, | ||
message: 'Keep curly braces on one line when you have one destructuring property', | ||
|
||
fix(fixer) { | ||
const [property] = id.properties; | ||
const {key, value} = property; | ||
|
||
if (key.name === value.name) | ||
return [ | ||
fixer.replaceText(id, `{${key.name}}`), | ||
]; | ||
|
||
return [ | ||
fixer.replaceText(id, `{${key.name}: ${value.name}}`), | ||
]; | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
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,22 @@ | ||
# keep curly bracesin in one line when property is single (single-property-destructuring) | ||
|
||
When `putout` removes unused variables declared as destructured properties, it keeps one property to take 3 lines, instead of 1. | ||
|
||
## Rule Details | ||
|
||
This rule aims to shorten destricturing of one property. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
const { | ||
username | ||
} = user; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
const {username} = user; | ||
``` | ||
|
37 changes: 37 additions & 0 deletions
37
packages/eslint-plugin-putout/rules/single-property.spec.js
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,37 @@ | ||
'use strict'; | ||
|
||
const rule = require('./single-property-destructuring'); | ||
const {RuleTester} = require('eslint'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2019, | ||
}, | ||
}); | ||
|
||
ruleTester.run('single-property-destructuring', rule, { | ||
valid: [ | ||
`const {hello} = world;`, | ||
`const {hello} = get({});`, | ||
`const { | ||
hello = true | ||
} = world;`, | ||
], | ||
|
||
invalid: [{ | ||
code: `const {\n hello\n} = world`, | ||
output: 'const {hello} = world', | ||
errors: [{ | ||
message: 'Keep curly braces on one line when you have one destructuring property', | ||
type: 'VariableDeclarator', | ||
}], | ||
}, { | ||
code: `const {\n hello: h\n} = world`, | ||
output: 'const {hello: h} = world', | ||
errors: [{ | ||
message: 'Keep curly braces on one line when you have one destructuring property', | ||
type: 'VariableDeclarator', | ||
}], | ||
}], | ||
}); | ||
|