Skip to content

Commit

Permalink
feat(no-type): new command (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz authored May 3, 2024
1 parent 4fef1d3 commit 375b4b1
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { toStringLiteral } from './to-string-literal'
import { toTemplateLiteral } from './to-template-literal'
import { inlineArrow } from './inline-arrow'
import { toPromiseAll } from './to-promise-all'
import { noType } from './no-type'

// @keep-sorted
export {
inlineArrow,
keepSorted,
noType,
toArrow,
toDynamicImport,
toForEach,
Expand All @@ -27,6 +29,7 @@ export {
export const builtinCommands = [
inlineArrow,
keepSorted,
noType,
toArrow,
toDynamicImport,
toForEach,
Expand Down
21 changes: 21 additions & 0 deletions src/commands/no-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# `no-type`

Removes TypeScript type annotations.

## Triggers

- `/// no-type`
- `/// nt`

## Examples

```js
/// no-type
const foo: string = 'foo'
```

Will be converted to:

```js
const foo = 'foo'
```
84 changes: 84 additions & 0 deletions src/commands/no-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { noType as command } from './no-type'
import { d, run } from './_test-utils'

run(
command,
{
code: d`
/// no-type
let a: string`,
output: d`
let a`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// no-type
function a<T>(arg: A): R {}`,
output: d`
function a(arg) {}`,
errors: ['command-removal', 'command-fix', 'command-fix', 'command-fix'],
},
{
code: d`
/// no-type
declare const a: string`,
output: d`
declare const a`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// no-type
fn(arg as any)`,
output: d`
fn(arg)`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// no-type
fn(arg satisfies any)`,
output: d`
fn(arg)`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// no-type
fn(arg!)`,
output: d`
fn(arg)`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// no-type
fn(<string>arg)`,
output: d`
fn(arg)`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// no-type
const fn = foo<string>`,
output: d`
const fn = foo`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// no-type
type A = string`,
output: '\n',
errors: ['command-removal', 'command-fix'],
},
{
code: d`
/// nt
const a = 1`,
output: null,
errors: 'command-error',
},
)
34 changes: 34 additions & 0 deletions src/commands/no-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Command } from '../types'

export const noType: Command = {
name: 'no-type',
match: /^[\/:@]\s*(no-type|nt)$/,
action(ctx) {
const nodes = ctx.findNodeBelow({
filter: node => node.type.startsWith('TS'),
findAll: true,
shallow: true,
})
if (!nodes || nodes.length === 0)
return ctx.reportError('Unable to find type to remove')

ctx.removeComment()
for (const node of nodes) {
ctx.report({
node,
message: 'Remove type',
fix(fixer) {
if (node.type === 'TSAsExpression' // foo as number
|| node.type === 'TSSatisfiesExpression' // foo satisfies T
|| node.type === 'TSNonNullExpression' // foo!
|| node.type === 'TSInstantiationExpression') // foo<string>
return fixer.removeRange([node.expression.range[1], node.range[1]])
else if (node.type === 'TSTypeAssertion') // <number>foo
return fixer.removeRange([node.range[0], node.expression.range[0]])
else
return fixer.remove(node)
},
})
}
},
}
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class CommandContext {
*
* Override 3: Find all match with full options (returns an array)
*/
findNodeBelow<T extends Tree.Node['type']>(options: FindNodeOptions<T, true>): Extract<Tree.Node, { type: T }>[]
findNodeBelow<T extends Tree.Node['type']>(options: FindNodeOptions<T, true>): Extract<Tree.Node, { type: T }>[] | undefined
/**
* Find specific node within the line below the comment
*
Expand Down

0 comments on commit 375b4b1

Please sign in to comment.