Skip to content

Commit 1455153

Browse files
committed
refactor(types): introduce ValueOf utility type and update type definitions
1 parent 874d40b commit 1455153

7 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/rules/example/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Text } from 'mdast'
2+
import type { ValueOf } from '@/types'
23
import { createRule } from '@/utils'
34
import { getNodePosition } from '@/utils/ast'
45

@@ -7,7 +8,7 @@ const MESSAGE_IDS = {
78
exampleMsgId: 'exampleMsgId',
89
} as const
910

10-
type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
11+
type MessageIds = ValueOf<typeof MESSAGE_IDS>
1112
type Options = []
1213

1314
export default createRule<Options, MessageIds>({

src/rules/space-around-inline-element/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Emphasis, Image, InlineCode, Link, Strong } from 'mdast'
2-
import type { RuleContext } from '@/types'
2+
import type { RuleContext, ValueOf } from '@/types'
33
import type { InlineElement } from '@/types/inline-element'
44
import { createRule } from '@/utils'
55
import { getNodeContext, getNodePosition, isNestedInlineElement } from '@/utils/ast'
@@ -8,7 +8,7 @@ import { getSpaceContext } from '@/utils/space'
88

99
export const RULE_NAME = 'space-around-inline-element'
1010

11-
type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
11+
type MessageIds = ValueOf<typeof MESSAGE_IDS>
1212
type Options = []
1313

1414
const BEFORE_INLINE_ELEMENT_MESSAGE_IDS = new Set<MessageIds>([

src/rules/space-around-word/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Text } from 'mdast'
2+
import type { ValueOf } from '@/types'
23
import { TEXT_TYPE } from '@/types/text-tokenizer'
34
import { createRule } from '@/utils'
45
import { getNodeContextByParent } from '@/utils/ast'
@@ -14,7 +15,8 @@ export const MESSAGE_IDS = {
1415
unexpectedSpaceAround: 'unexpectedSpaceAround',
1516
} as const
1617

17-
type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
18+
type MessageIds = ValueOf<typeof MESSAGE_IDS>
19+
1820
type Options = []
1921

2022
export default createRule<Options, MessageIds>({

src/rules/valid-heading-anchor/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ValueOf } from '@/types'
12
import { createRule } from '@/utils'
23
import { calcAnchorPositionCompensate, getLikeAnchor, hasChinese, isStrictAnchor, normalizeAnchor } from '@/utils/anchor'
34
import { getNodeContext, getNodePosition } from '@/utils/ast'
@@ -9,7 +10,7 @@ const MESSAGE_IDS = {
910
invalidHeadingAnchor: 'invalidHeadingAnchor',
1011
} as const
1112
type Options = []
12-
type MessageIds = typeof MESSAGE_IDS[keyof typeof MESSAGE_IDS]
13+
type MessageIds = ValueOf<typeof MESSAGE_IDS>
1314

1415
export default createRule<Options, MessageIds>({
1516
name: RULE_NAME,

src/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ export type RuleContext<MessageIds extends string, Options extends unknown[]> =
4242
>[0]
4343

4444
export type RuleListener = MarkdownRuleVisitor
45+
46+
export type ValueOf<T> = T[keyof T]

src/types/inline-element.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Emphasis, Image, InlineCode, Link, Strong } from 'mdast'
2+
import type { ValueOf } from '@/types/index'
23
import type { INLINE_SPACE_MESSAGE_IDS } from '@/types/inline-element'
34

45
/**
@@ -9,7 +10,7 @@ export type InlineElement = Link | Image | InlineCode | Emphasis | Strong
910
/**
1011
* The allowed issue ids for inline element spacing rules.
1112
*/
12-
export type InlineElementSpaceIssue = typeof INLINE_SPACE_MESSAGE_IDS[keyof typeof INLINE_SPACE_MESSAGE_IDS]
13+
export type InlineElementSpaceIssue = ValueOf<typeof INLINE_SPACE_MESSAGE_IDS>
1314

1415
/**
1516
* The relative position to check for spacing.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Text } from 'mdast'
2+
import type { ValueOf } from '@/types/index'
23

34
export const TEXT_TYPE = {
45
'cjk': 'cjk',
@@ -15,7 +16,7 @@ export const TEXT_TYPE = {
1516
'other': 'other',
1617
} as const
1718

18-
export type TextType = typeof TEXT_TYPE[keyof typeof TEXT_TYPE]
19+
export type TextType = ValueOf<typeof TEXT_TYPE>
1920

2021
/**
2122
* Source location point compatible with mdast position points

0 commit comments

Comments
 (0)