Skip to content

Commit 5f2ab36

Browse files
authored
feat(no-shorthand): new command (#7)
1 parent c9a3a49 commit 5f2ab36

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

src/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import { toStringLiteral } from './to-string-literal'
88
import { toTemplateLiteral } from './to-template-literal'
99
import { inlineArrow } from './inline-arrow'
1010
import { toPromiseAll } from './to-promise-all'
11+
import { noShorthand } from './no-shorthand'
1112
import { noType } from './no-type'
1213

1314
// @keep-sorted
1415
export {
1516
inlineArrow,
1617
keepSorted,
18+
noShorthand,
1719
noType,
1820
toArrow,
1921
toDynamicImport,
@@ -29,6 +31,7 @@ export {
2931
export const builtinCommands = [
3032
inlineArrow,
3133
keepSorted,
34+
noShorthand,
3235
noType,
3336
toArrow,
3437
toDynamicImport,

src/commands/no-shorthand.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# `no-shorthand`
2+
3+
Expand object shorthand properties to their full form.
4+
5+
## Triggers
6+
7+
- `/// no-shorthand`
8+
- `/// nsh`
9+
10+
## Examples
11+
12+
```js
13+
/// no-shorthand
14+
const obj = { a, b, c: 0 }
15+
```
16+
17+
Will be converted to:
18+
19+
```js
20+
// eslint-disable-next-line object-shorthand
21+
const obj = { a: a, b: b, c: 0 }
22+
```

src/commands/no-shorthand.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { noShorthand as command } from './no-shorthand'
2+
import { d, run } from './_test-utils'
3+
4+
run(
5+
command,
6+
{
7+
code: d`
8+
/// no-shorthand
9+
const obj = fn({ a, b, c: d })`,
10+
output: d`
11+
const obj = fn({ a: a, b: b, c: d })`,
12+
errors: ['command-removal', 'command-fix', 'command-fix'],
13+
},
14+
{
15+
code: d`
16+
/// nsh
17+
const obj = 10`,
18+
output: null,
19+
errors: 'command-error',
20+
},
21+
{
22+
code: d`
23+
/// nsh
24+
const obj = { key: value, key2: value2 }`,
25+
output: null,
26+
errors: 'command-error',
27+
},
28+
)

src/commands/no-shorthand.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { AST_NODE_TYPES } from '@typescript-eslint/utils'
2+
import type { Command } from '../types'
3+
4+
export const noShorthand: Command = {
5+
name: 'no-shorthand',
6+
match: /^[\/:@]\s*(no-shorthand|nsh)$/,
7+
action(ctx) {
8+
const nodes = ctx.findNodeBelow<AST_NODE_TYPES.Property>({
9+
filter: node => node.type === 'Property' && node.shorthand,
10+
findAll: true,
11+
})
12+
if (!nodes || nodes.length === 0)
13+
return ctx.reportError('Unable to find shorthand object property to convert')
14+
15+
ctx.removeComment()
16+
for (const node of nodes) {
17+
ctx.report({
18+
node,
19+
message: 'Expand shorthand',
20+
fix(fixer) {
21+
return fixer.insertTextAfter(node.key, `: ${ctx.getTextOf(node.key)}`)
22+
},
23+
})
24+
}
25+
},
26+
}

0 commit comments

Comments
 (0)