Skip to content

Commit

Permalink
Merge branch 'main' into pr/hyoban/10
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 6, 2024
2 parents 1c48a01 + 8c69efb commit b6db79c
Show file tree
Hide file tree
Showing 22 changed files with 502 additions and 272 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"bumpp": "^9.4.1",
"eslint": "^9.2.0",
"eslint-define-config": "^2.1.0",
"eslint-vitest-rule-tester": "^0.1.1",
"esno": "^4.7.0",
"fast-glob": "^3.3.2",
"lint-staged": "^15.2.2",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 18 additions & 43 deletions src/commands/_test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import { RuleTester } from 'eslint'
import * as tsParser from '@typescript-eslint/parser'
import type { TestCase } from 'eslint-vitest-rule-tester'
import { createRuleTester } from 'eslint-vitest-rule-tester'
import { createRuleWithCommands } from '../rule'
import type { Command } from '../types'

export function d(str: TemplateStringsArray) {
const lines = str[0].split('\n')
const commonIndent = lines.slice(1).reduce((min, line) => {
if (/^\s*$/.test(line))
return min
const indent = line.match(/^\s*/)?.[0].length
return indent === undefined ? min : Math.min(min, indent)
}, Number.POSITIVE_INFINITY)
return lines.map(line => line.slice(commonIndent)).join('\n')
}

type Arrayable<T> = T | T[]
export { unindent as $ } from 'eslint-vitest-rule-tester'

export interface TestCase extends RuleTester.ValidTestCase {
output?: string | null
errors?: Arrayable<string | RuleTester.TestCaseError>
}
export function run(command: Command | Command[], ...cases: (TestCase | string)[]) {
const commands = Array.isArray(command) ? command : [command]

export function run(command: Command, ...cases: (TestCase | string)[]) {
const ruleTester: RuleTester = new RuleTester({
languageOptions: {
parser: tsParser,
const ruleTester = createRuleTester({
name: commands[0].name,
rule: createRuleWithCommands(commands) as any,
configs: {
languageOptions: {
parser: tsParser,
},
files: ['**/*.ts', '**/*.js'],
},
})

Expand All @@ -34,29 +26,12 @@ export function run(command: Command, ...cases: (TestCase | string)[]) {
for (const c of cases) {
if (typeof c === 'string')
validCases.push(c)
else if (c.errors)
invalidCases.push(c)
else
validCases.push(c)
invalidCases.push(c)
}

ruleTester.run(
command.name,
createRuleWithCommands([command]) as any,
{
valid: validCases,
invalid: invalidCases.map(i => ({
...i,
code: i.code,
output: i.output ?? null,
errors: (Array.isArray(i.errors) ? i.errors : [i.errors])
.filter(notFalsy)
.map(id => typeof id === 'string' ? ({ messageId: id }) : id),
})),
},
)
}

function notFalsy<T>(value: T): value is Exclude<T, null | undefined | false> {
return !!value
ruleTester.run({
valid: validCases,
invalid: invalidCases,
})
}
3 changes: 3 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import { inlineArrow } from './inline-arrow'
import { toPromiseAll } from './to-promise-all'
import { noShorthand } from './no-shorthand'
import { noType } from './no-type'
import { keepUnique } from './keep-unique'

// @keep-sorted
export {
inlineArrow,
keepSorted,
keepUnique,
noShorthand,
noType,
toArrow,
Expand All @@ -31,6 +33,7 @@ export {
export const builtinCommands = [
inlineArrow,
keepSorted,
keepUnique,
noShorthand,
noType,
toArrow,
Expand Down
18 changes: 9 additions & 9 deletions src/commands/inline-arrow.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { inlineArrow as command } from './inline-arrow'
import { d, run } from './_test-utils'
import { $, run } from './_test-utils'

run(
command,
// no arrow function
{
code: d`
code: $`
///inline-arrow
const a = 1`,
output: null,

Check failure on line 11 in src/commands/inline-arrow.test.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type 'null' is not assignable to type 'string | ((output: string) => void) | undefined'.
errors: 'command-error',

Check failure on line 12 in src/commands/inline-arrow.test.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type 'string' is not assignable to type 'number | (string | Partial<LintMessage>)[] | ((errors: LintMessage[]) => void) | undefined'.
},
// multi statement
{
code: d`
code: $`
/// inline-arrow
export const foo = arg => {
const a = 1
Expand All @@ -23,30 +23,30 @@ run(
errors: 'command-error',
},
{
code: d`
code: $`
/// inline-arrow
export const foo = <T = 1>(arg: Z): Bar => {
return arg
}`,
output: d`
output: $`
export const foo = <T = 1>(arg: Z): Bar => arg`,
errors: ['command-removal', 'command-fix'],
},
// no return statement
{
code: d`
code: $`
///inline-arrow
const foo = () => {}`,
output: d`
output: $`
const foo = () => undefined`,
errors: ['command-removal', 'command-fix'],
},
// without return argument
{
code: d`
code: $`
// /ia
export default <T = 1>(arg: Z): Bar => { return }`,
output: d`
output: $`
export default <T = 1>(arg: Z): Bar => undefined`,
errors: ['command-removal', 'command-fix'],
},
Expand Down
Loading

0 comments on commit b6db79c

Please sign in to comment.