Skip to content

Commit

Permalink
fix: types strictness, enable typecheck in ci, close #64 (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Nov 11, 2023
1 parent 9731ec9 commit 2e44cab
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 95 deletions.
33 changes: 16 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,27 @@ jobs:
- name: Lint
run: nr lint

# The typecheck from the migration is currently broken, need to fix them first.
# typecheck:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

# - name: Install pnpm
# uses: pnpm/action-setup@v2
- name: Install pnpm
uses: pnpm/action-setup@v2

# - name: Set node
# uses: actions/setup-node@v3
# with:
# node-version: lts/*
- name: Set node
uses: actions/setup-node@v3
with:
node-version: lts/*

# - name: Setup
# run: npm i -g @antfu/ni
- name: Setup
run: npm i -g @antfu/ni

# - name: Install
# run: nci
- name: Install
run: nci

# - name: Typecheck
# run: nr typecheck
- name: Typecheck
run: nr typecheck

test:
runs-on: ${{ matrix.os }}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"update": "esno scripts/update.ts",
"release": "bumpp -r && esno scripts/publish.ts",
"test": "vitest",
"typecheck": "tsc --noEmit",
"typecheck": "vue-tsc --noEmit",
"prepare": "simple-git-hooks",
"docs": "pnpm -C docs run docs:dev",
"docs:build": "pnpm -C docs run docs:build"
Expand Down Expand Up @@ -53,7 +53,8 @@
"unbuild": "^2.0.0",
"vite": "^4.5.0",
"vitest": "^0.34.6",
"vue": "^3.3.8"
"vue": "^3.3.8",
"vue-tsc": "^1.8.22"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
Expand Down
15 changes: 11 additions & 4 deletions packages/eslint-plugin-ts/rules/key-spacing/key-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function at<T>(arr: T[], position: number): T | undefined {
return arr[position]
}

type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never

type OptionsUnion = UnionToIntersection<Exclude<RuleOptions[0], undefined>>

export default createRule<RuleOptions, MessageIds>({
name: 'key-spacing',
meta: {
Expand All @@ -43,7 +48,8 @@ export default createRule<RuleOptions, MessageIds>({
messages: baseRule.meta.messages,
},
defaultOptions: [{}],
create(context, [options = {}]) {
create(context, [_options]) {
const options: OptionsUnion = _options || {}
const sourceCode = context.getSourceCode()
const baseRules = baseRule.create(context)

Expand Down Expand Up @@ -335,14 +341,15 @@ export default createRule<RuleOptions, MessageIds>({
node: TSESTree.Node,
{ singleLine }: { singleLine: boolean },
): void {
const beforeColon
= (singleLine
const beforeColon = (
singleLine
? options.singleLine
? options.singleLine.beforeColon
: options.beforeColon
: options.multiLine
? options.multiLine.beforeColon
: options.beforeColon) ?? false
: options.beforeColon
) ?? false
const expectedWhitespaceBeforeColon = beforeColon ? 1 : 0
const afterColon
= (singleLine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import type { TSESLint } from '@typescript-eslint/utils'

import type { MessageIds, Options } from './keyword-spacing'
import type { MessageIds, RuleOptions } from './types'
import rule from './keyword-spacing'

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -32,7 +32,7 @@ const NEITHER = { before: false, after: false }
* @param value A value to override.
* @returns An option object to test an 'overrides' option.
*/
function overrides(keyword: string, value: Options[0]): Options[0] {
function overrides(keyword: string, value: RuleOptions[0] = {}): RuleOptions[0] {
return {
before: value.before === false,
after: value.after === false,
Expand Down
10 changes: 2 additions & 8 deletions packages/eslint-plugin-ts/rules/quotes/quotes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import type { TSESTree } from '@typescript-eslint/utils'
import { AST_NODE_TYPES } from '@typescript-eslint/utils'

import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../../utils'
import { createRule } from '../../utils'
import { getESLintCoreRule } from '../../utils/getESLintCoreRule'
import type { MessageIds, RuleOptions } from './types'

const baseRule = getESLintCoreRule('quotes')

export type Options = InferOptionsTypeFromRule<typeof baseRule>
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>

export default createRule<Options, MessageIds>({
export default createRule<RuleOptions, MessageIds>({
name: 'quotes',
meta: {
type: 'layout',
Expand Down
10 changes: 5 additions & 5 deletions packages/eslint-plugin-ts/rules/semi/semi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import type { TSESLint } from '@typescript-eslint/utils'

import type { MessageIds, Options } from './semi'
import type { MessageIds, RuleOptions } from './types'
import rule from './semi'

const ruleTester = new RuleTester({
Expand All @@ -16,8 +16,8 @@ const ruleTester = new RuleTester({
},
})

const neverOption: Options = ['never']
const neverOptionWithoutContinuationChars: Options = [
const neverOption: RuleOptions = ['never']
const neverOptionWithoutContinuationChars: RuleOptions = [
'never',
{ beforeStatementContinuationChars: 'never' },
]
Expand Down Expand Up @@ -264,7 +264,7 @@ class PanCamera extends FreeCamera {
'export default (foo) => foo.bar();',
'export default foo = 42;',
'export default foo += 42;',
].reduce<TSESLint.ValidTestCase<Options>[]>((acc, code) => {
].reduce<TSESLint.ValidTestCase<RuleOptions>[]>((acc, code) => {
acc.push({
code,
options: ['always'],
Expand Down Expand Up @@ -1077,7 +1077,7 @@ class PanCamera extends FreeCamera {
},
],
},
].reduce<TSESLint.InvalidTestCase<MessageIds, Options>[]>((acc, test) => {
].reduce<TSESLint.InvalidTestCase<MessageIds, RuleOptions>[]>((acc, test) => {
acc.push({
code: test.code.replace(/;/g, ''),
output: test.code,
Expand Down
14 changes: 3 additions & 11 deletions packages/eslint-plugin-ts/rules/semi/semi.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
import { AST_NODE_TYPES } from '@typescript-eslint/utils'

import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../../utils'
import { createRule } from '../../utils'
import { getESLintCoreRule } from '../../utils/getESLintCoreRule'
import type { MessageIds, RuleOptions } from './types'

const baseRule = getESLintCoreRule('semi')

export type Options = InferOptionsTypeFromRule<typeof baseRule>
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>

export default createRule<Options, MessageIds>({
export default createRule<RuleOptions, MessageIds>({
name: 'semi',
meta: {
type: 'layout',
Expand All @@ -33,7 +25,7 @@ export default createRule<Options, MessageIds>({
omitLastInOneLineBlock: false,
beforeStatementContinuationChars: 'any',
},
],
] as unknown as RuleOptions,
create(context) {
const rules = baseRule.create(context)
const checkForSemicolon
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import type { TSESTree } from '@typescript-eslint/utils'

import { isTokenOnSameLine } from '@typescript-eslint/utils/ast-utils'
import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../../utils'

import { createRule } from '../../utils'
import { getESLintCoreRule } from '../../utils/getESLintCoreRule'
import type { MessageIds, RuleOptions } from './types'

const baseRule = getESLintCoreRule('space-before-blocks')

export type Options = InferOptionsTypeFromRule<typeof baseRule>
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>

export default createRule<Options, MessageIds>({
export default createRule<RuleOptions, MessageIds>({
name: 'space-before-blocks',
meta: {
type: 'layout',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'

import { isOpeningParenToken } from '@typescript-eslint/utils/ast-utils'
import { createRule } from '../../utils'
import type { MessageIds, RuleOptions } from './types'

type Option = 'always' | 'never'
type FuncOption = Option | 'ignore'
type FuncOption = 'always' | 'never' | 'ignore'

export type Options = [
| Option
| {
anonymous?: FuncOption
named?: FuncOption
asyncArrow?: FuncOption
},
]
export type MessageIds = 'missing' | 'unexpected'

export default createRule<Options, MessageIds>({
export default createRule<RuleOptions, MessageIds>({
name: 'space-before-function-paren',
meta: {
type: 'layout',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { AST_TOKEN_TYPES, TSESTree } from '@typescript-eslint/utils'

import { isNotOpeningParenToken } from '@typescript-eslint/utils/ast-utils'
import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../../utils'
import { createRule } from '../../utils'
import { getESLintCoreRule } from '../../utils/getESLintCoreRule'
import type { MessageIds, RuleOptions } from './types'

const baseRule = getESLintCoreRule('space-infix-ops')

export type Options = InferOptionsTypeFromRule<typeof baseRule>
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>

const UNIONS = ['|', '&']

export default createRule<Options, MessageIds>({
export default createRule<RuleOptions, MessageIds>({
name: 'space-infix-ops',
meta: {
type: 'layout',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import type { TSESLint } from '@typescript-eslint/utils'

import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../../utils'
import rule from './type-annotation-spacing'

type MessageIds = InferMessageIdsTypeFromRule<typeof rule>
type Options = InferOptionsTypeFromRule<typeof rule>
import type { MessageIds, RuleOptions } from './types'

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down Expand Up @@ -6562,7 +6556,7 @@ type Foo = {
const operators = ['+?:', '-?:']

ruleTester.run('type-annotation-spacing', rule, {
valid: operators.reduce<TSESLint.ValidTestCase<Options>[]>(
valid: operators.reduce<TSESLint.ValidTestCase<RuleOptions>[]>(
(validCases, operator) =>
validCases.concat([
{
Expand Down Expand Up @@ -6604,7 +6598,7 @@ ruleTester.run('type-annotation-spacing', rule, {
]),
[],
),
invalid: operators.reduce<TSESLint.InvalidTestCase<MessageIds, Options>[]>(
invalid: operators.reduce<TSESLint.InvalidTestCase<MessageIds, RuleOptions>[]>(
(invalidCases, operator) =>
invalidCases.concat([
// space before + after cases
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-ts/utils/getESLintCoreRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { UnprefixedRuleOptions } from '@stylistic/eslint-plugin-js'
import jsRules from '@stylistic/eslint-plugin-js'
import type { TSESLint } from '@typescript-eslint/utils'

export function getESLintCoreRule(ruleId: keyof UnprefixedRuleOptions) {
export function getESLintCoreRule(ruleId: Exclude<keyof UnprefixedRuleOptions, symbol>) {
if (ruleId in jsRules.rules)
// TODO: fix this type
return jsRules.rules[ruleId] as TSESLint.RuleModule<any, readonly any[]>
Expand Down
3 changes: 0 additions & 3 deletions packages/eslint-plugin-ts/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ const {
NullThrowsReasons,
} = ESLintUtils

export type InferMessageIdsTypeFromRule<T> = ESLintUtils.InferMessageIdsTypeFromRule<T>
export type InferOptionsTypeFromRule<T> = ESLintUtils.InferOptionsTypeFromRule<T>

export {
applyDefault,
deepMerge,
Expand Down
Loading

0 comments on commit 2e44cab

Please sign in to comment.