Skip to content

Commit 375b4b1

Browse files
authored
feat(no-type): new command (#6)
1 parent 4fef1d3 commit 375b4b1

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

src/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ 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 { noType } from './no-type'
1112

1213
// @keep-sorted
1314
export {
1415
inlineArrow,
1516
keepSorted,
17+
noType,
1618
toArrow,
1719
toDynamicImport,
1820
toForEach,
@@ -27,6 +29,7 @@ export {
2729
export const builtinCommands = [
2830
inlineArrow,
2931
keepSorted,
32+
noType,
3033
toArrow,
3134
toDynamicImport,
3235
toForEach,

src/commands/no-type.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# `no-type`
2+
3+
Removes TypeScript type annotations.
4+
5+
## Triggers
6+
7+
- `/// no-type`
8+
- `/// nt`
9+
10+
## Examples
11+
12+
```js
13+
/// no-type
14+
const foo: string = 'foo'
15+
```
16+
17+
Will be converted to:
18+
19+
```js
20+
const foo = 'foo'
21+
```

src/commands/no-type.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { noType as command } from './no-type'
2+
import { d, run } from './_test-utils'
3+
4+
run(
5+
command,
6+
{
7+
code: d`
8+
/// no-type
9+
let a: string`,
10+
output: d`
11+
let a`,
12+
errors: ['command-removal', 'command-fix'],
13+
},
14+
{
15+
code: d`
16+
/// no-type
17+
function a<T>(arg: A): R {}`,
18+
output: d`
19+
function a(arg) {}`,
20+
errors: ['command-removal', 'command-fix', 'command-fix', 'command-fix'],
21+
},
22+
{
23+
code: d`
24+
/// no-type
25+
declare const a: string`,
26+
output: d`
27+
declare const a`,
28+
errors: ['command-removal', 'command-fix'],
29+
},
30+
{
31+
code: d`
32+
/// no-type
33+
fn(arg as any)`,
34+
output: d`
35+
fn(arg)`,
36+
errors: ['command-removal', 'command-fix'],
37+
},
38+
{
39+
code: d`
40+
/// no-type
41+
fn(arg satisfies any)`,
42+
output: d`
43+
fn(arg)`,
44+
errors: ['command-removal', 'command-fix'],
45+
},
46+
{
47+
code: d`
48+
/// no-type
49+
fn(arg!)`,
50+
output: d`
51+
fn(arg)`,
52+
errors: ['command-removal', 'command-fix'],
53+
},
54+
{
55+
code: d`
56+
/// no-type
57+
fn(<string>arg)`,
58+
output: d`
59+
fn(arg)`,
60+
errors: ['command-removal', 'command-fix'],
61+
},
62+
{
63+
code: d`
64+
/// no-type
65+
const fn = foo<string>`,
66+
output: d`
67+
const fn = foo`,
68+
errors: ['command-removal', 'command-fix'],
69+
},
70+
{
71+
code: d`
72+
/// no-type
73+
type A = string`,
74+
output: '\n',
75+
errors: ['command-removal', 'command-fix'],
76+
},
77+
{
78+
code: d`
79+
/// nt
80+
const a = 1`,
81+
output: null,
82+
errors: 'command-error',
83+
},
84+
)

src/commands/no-type.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Command } from '../types'
2+
3+
export const noType: Command = {
4+
name: 'no-type',
5+
match: /^[\/:@]\s*(no-type|nt)$/,
6+
action(ctx) {
7+
const nodes = ctx.findNodeBelow({
8+
filter: node => node.type.startsWith('TS'),
9+
findAll: true,
10+
shallow: true,
11+
})
12+
if (!nodes || nodes.length === 0)
13+
return ctx.reportError('Unable to find type to remove')
14+
15+
ctx.removeComment()
16+
for (const node of nodes) {
17+
ctx.report({
18+
node,
19+
message: 'Remove type',
20+
fix(fixer) {
21+
if (node.type === 'TSAsExpression' // foo as number
22+
|| node.type === 'TSSatisfiesExpression' // foo satisfies T
23+
|| node.type === 'TSNonNullExpression' // foo!
24+
|| node.type === 'TSInstantiationExpression') // foo<string>
25+
return fixer.removeRange([node.expression.range[1], node.range[1]])
26+
else if (node.type === 'TSTypeAssertion') // <number>foo
27+
return fixer.removeRange([node.range[0], node.expression.range[0]])
28+
else
29+
return fixer.remove(node)
30+
},
31+
})
32+
}
33+
},
34+
}

src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class CommandContext {
149149
*
150150
* Override 3: Find all match with full options (returns an array)
151151
*/
152-
findNodeBelow<T extends Tree.Node['type']>(options: FindNodeOptions<T, true>): Extract<Tree.Node, { type: T }>[]
152+
findNodeBelow<T extends Tree.Node['type']>(options: FindNodeOptions<T, true>): Extract<Tree.Node, { type: T }>[] | undefined
153153
/**
154154
* Find specific node within the line below the comment
155155
*

0 commit comments

Comments
 (0)