Skip to content

Commit

Permalink
chore: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Aug 23, 2023
1 parent 72107ef commit e8e3263
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/from-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Version: 2.1.0
* License: MIT (https://github.com/syntax-tree/mdast-util-directive/blob/main/license)
*/
import type { Token } from 'micromark-util-types'
import type { Token } from './micromark-extension/types'

Check failure on line 7 in src/from-markdown.ts

View workflow job for this annotation

GitHub Actions / ci

`./micromark-extension/types` type import should occur after import of `scule`
import { parseEntities } from 'parse-entities'
import { kebabCase } from 'scule'

Expand Down
34 changes: 17 additions & 17 deletions src/micromark-extension/factory-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Effects, State } from 'micromark-util-types'
import type { Effects, State, TokenTypeMap } from './types'

Check failure on line 1 in src/micromark-extension/factory-attributes.ts

View workflow job for this annotation

GitHub Actions / ci

`./types` type import should occur after import of `micromark-util-character`
import { factorySpace } from 'micromark-factory-space'
import { factoryWhitespace } from 'micromark-factory-whitespace'

Expand All @@ -15,20 +15,20 @@ export default function createAttributes (
effects: Effects,
ok: State,
nok: State,
attributesType: string,
attributesMarkerType: string,
attributeType: string,
attributeIdType: string,
attributeClassType: string,
attributeNameType: string,
attributeInitializerType: string,
attributeValueLiteralType: string,
attributeValueType: string,
attributeValueMarker: string,
attributeValueData: string,
attributesType: keyof TokenTypeMap,
attributesMarkerType: keyof TokenTypeMap,
attributeType: keyof TokenTypeMap,
attributeIdType: keyof TokenTypeMap,
attributeClassType: keyof TokenTypeMap,
attributeNameType: keyof TokenTypeMap,
attributeInitializerType: keyof TokenTypeMap,
attributeValueLiteralType: keyof TokenTypeMap,
attributeValueType: keyof TokenTypeMap,
attributeValueMarker: keyof TokenTypeMap,
attributeValueData: keyof TokenTypeMap,
disallowEol?: boolean
) {
let type: string
let type: keyof TokenTypeMap
let marker: number | undefined

return start
Expand Down Expand Up @@ -74,9 +74,9 @@ export default function createAttributes (
function shortcutStart (code: number) {
effects.enter(attributeType)
effects.enter(type)
effects.enter(type + 'Marker')
effects.enter(type + 'Marker' as keyof TokenTypeMap)
effects.consume(code)
effects.exit(type + 'Marker')
effects.exit(type + 'Marker' as keyof TokenTypeMap)
return shortcutStartAfter
}

Expand All @@ -97,7 +97,7 @@ export default function createAttributes (
return nok(code)
}

effects.enter(type + 'Value')
effects.enter(type + 'Value' as keyof TokenTypeMap)
effects.consume(code)
return shortcut
}
Expand All @@ -116,7 +116,7 @@ export default function createAttributes (
}

if (code === Codes.hash || code === Codes.dot || code === Codes.closingCurlyBracket || markdownLineEndingOrSpace(code)) {
effects.exit(type + 'Value')
effects.exit(type + 'Value' as keyof TokenTypeMap)
effects.exit(type)
effects.exit(attributeType)
return between(code)
Expand Down
8 changes: 4 additions & 4 deletions src/micromark-extension/factory-label.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Effects, State } from 'micromark-util-types'
import type { Effects, State, TokenTypeMap } from './types'
import { markdownLineEnding } from 'micromark-util-character'

Check failure on line 2 in src/micromark-extension/factory-label.ts

View workflow job for this annotation

GitHub Actions / ci

`micromark-util-character` import should occur before type import of `./types`
import { Codes } from './constants'

Expand All @@ -11,9 +11,9 @@ export default function createLabel (
effects: Effects,
ok: State,
nok: State,
type: string,
markerType: string,
stringType: string,
type: keyof TokenTypeMap,
markerType: keyof TokenTypeMap,
stringType: keyof TokenTypeMap,
disallowEol?: boolean
) {
let size = 0
Expand Down
10 changes: 5 additions & 5 deletions src/micromark-extension/factory-name.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types'
import type { Effects, State, Code, TokenizeContext, TokenTypeMap } from './types'
import { asciiAlpha, asciiAlphanumeric } from 'micromark-util-character'

Check failure on line 2 in src/micromark-extension/factory-name.ts

View workflow job for this annotation

GitHub Actions / ci

`micromark-util-character` import should occur before type import of `./types`
import { Codes } from './constants'

export default function createName (this: TokenizeContext, effects: Effects, ok: State, nok: State, nameType: string) {
export default function createName (this: TokenizeContext, effects: Effects, ok: State, nok: State, nameType: keyof TokenTypeMap) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this

return start

function start (code: Code): State | void {
function start (code: Code): State | undefined {
if (asciiAlpha(code)) {
effects.enter(nameType)
effects.consume(code)
Expand All @@ -18,14 +18,14 @@ export default function createName (this: TokenizeContext, effects: Effects, ok:
return nok(code)
}

function name (code: Code): State | void {
function name (code: Code): State {
if (code === Codes.dash || code === Codes.underscore || asciiAlphanumeric(code)) {
effects.consume(code)
return name
}

effects.exit(nameType)
// To do next major: disallow `-` at end of name too, for consistency.
return self.previous === Codes.underscore ? nok(code) : ok(code)
return self.previous === Codes.underscore ? nok(code)! : ok(code)!
}
}
2 changes: 1 addition & 1 deletion src/micromark-extension/tokenize-attribute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types'
import type { Effects, State, Code, TokenizeContext } from './types'
import { markdownLineEnding } from 'micromark-util-character'

Check failure on line 2 in src/micromark-extension/tokenize-attribute.ts

View workflow job for this annotation

GitHub Actions / ci

`micromark-util-character` import should occur before type import of `./types`
import { Codes } from './constants'
import createAttributes from './factory-attributes'
Expand Down
8 changes: 4 additions & 4 deletions src/micromark-extension/tokenize-binding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types'
import type { Effects, State, Code, TokenizeContext } from './types'
import { Codes } from './constants'

function attempClose (this: TokenizeContext, effects: Effects, ok: State, nok: State) {
Expand Down Expand Up @@ -38,7 +38,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return secondBracket
}

function secondBracket (code: Code): void | State {
function secondBracket (code: Code): undefined | State {
if (code !== Codes.openingCurlyBracket) {
return nok(code)
}
Expand All @@ -49,7 +49,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return content
}

function content (code: Code): void | State {
function content (code: Code): undefined | State {
if (code === Codes.closingCurlyBracket) {
return effects.attempt({ tokenize: attempClose, partial: true }, close, (code) => {
effects.consume(code)
Expand All @@ -61,7 +61,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return content
}

function close (code: Code): void | State {
function close (code: Code): undefined | State {
return ok(code)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/micromark-extension/tokenize-container-indented.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Effects, State, TokenizeContext, Code } from 'micromark-util-types'
import type { Effects, State, TokenizeContext, Code } from './types'

Check failure on line 1 in src/micromark-extension/tokenize-container-indented.ts

View workflow job for this annotation

GitHub Actions / ci

`./types` type import should occur after import of `micromark-factory-space`
import { codeFenced } from 'micromark-core-commonmark'
import { factorySpace } from 'micromark-factory-space'
import { prefixSize } from './utils'
Expand Down
2 changes: 1 addition & 1 deletion src/micromark-extension/tokenize-container-suger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Effects, State, TokenizeContext, Code } from 'micromark-util-types'
import type { Effects, State, TokenizeContext, Code } from './types'

Check failure on line 1 in src/micromark-extension/tokenize-container-suger.ts

View workflow job for this annotation

GitHub Actions / ci

`./types` type import should occur after import of `micromark-factory-space`
import { markdownLineEnding } from 'micromark-util-character'
import { factorySpace } from 'micromark-factory-space'
import componentContainer from './tokenize-inline'
Expand Down
40 changes: 20 additions & 20 deletions src/micromark-extension/tokenize-container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types'
import type { Effects, State, Code, TokenizeContext } from './types'

Check failure on line 1 in src/micromark-extension/tokenize-container.ts

View workflow job for this annotation

GitHub Actions / ci

`./types` type import should occur after import of `micromark-util-character`
import { factorySpace } from 'micromark-factory-space'
import { markdownLineEnding, asciiAlpha, markdownSpace } from 'micromark-util-character'
import { linePrefixSize, tokenizeCodeFence, useTokenState } from './utils'
Expand Down Expand Up @@ -33,7 +33,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
*/
return start

function start (code: Code): State | void {
function start (code: Code): State | undefined {
/* istanbul ignore if - handled by mm */
if (code !== Codes.colon) { throw new Error('expected `:`') }
effects.enter('componentContainer')
Expand All @@ -48,7 +48,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
let revertSectionState: () => void
return closingPrefixAfter

function closingPrefixAfter (code: Code): State | void {
function closingPrefixAfter (code: Code): State | undefined {
sectionIndentSize = linePrefixSize(self.events)

// Close section
Expand All @@ -58,7 +58,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return closingSectionSequence(code)
}

function closingSectionSequence (code: Code): State | void {
function closingSectionSequence (code: Code): State | undefined {
if (code === slotSeparatorCode) {
effects.consume(code)
size++
Expand Down Expand Up @@ -88,7 +88,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
}
}

function sectionOpen (code: Code): void | State {
function sectionOpen (code: Code): undefined | State {
// Open new Section
section.enter(effects)

Expand All @@ -100,7 +100,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return sectionTitle(code)
}

function sectionTitle (code: Code): State | void {
function sectionTitle (code: Code): State | undefined {
if (markdownLineEnding(code)) {
effects.exit('componentContainerSectionTitle')
return factorySpace(effects, lineStart, 'linePrefix', 4)(code)
Expand All @@ -109,7 +109,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return sectionTitle
}

function sequenceOpen (code: Code): State | void {
function sequenceOpen (code: Code): State | undefined {
if (code === Codes.colon) {
effects.consume(code)
sizeOpen++
Expand All @@ -124,23 +124,23 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return createName.call(self, effects, afterName, nok, 'componentContainerName')(code)
}

function afterName (code: Code): State | void {
function afterName (code: Code): State | undefined {
return code === Codes.openingSquareBracket
? effects.attempt(label, afterLabel, afterLabel)(code)
: afterLabel(code)
}

function afterLabel (code: Code): State | void {
function afterLabel (code: Code): State | undefined {
return code === Codes.openingCurlyBracket
? effects.attempt(attributes, afterAttributes, afterAttributes)(code)
: afterAttributes(code)
}

function afterAttributes (code: Code): State | void {
function afterAttributes (code: Code): State | undefined {
return factorySpace(effects, openAfter, 'whitespace')(code)
}

function openAfter (code: Code): State | void {
function openAfter (code: Code): State | undefined {
effects.exit('componentContainerFence')

if (code === null) {
Expand All @@ -159,7 +159,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return nok(code)
}

function contentStart (code: Code): State | void {
function contentStart (code: Code): State | undefined {
if (code === null) {
effects.exit('componentContainer')
return ok(code)
Expand All @@ -173,7 +173,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return lineStart(code)
}

function lineStartAfterPrefix (code: Code): State | void {
function lineStartAfterPrefix (code: Code): State | undefined {
if (code === null) {
return after(code)
}
Expand Down Expand Up @@ -217,7 +217,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return chunkStart(code)
}

function lineStart (code: Code): State | void {
function lineStart (code: Code): State | undefined {
if (code === null) {
return after(code)
}
Expand All @@ -227,7 +227,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
: lineStartAfterPrefix(code)
}

function chunkStart (code: Code): State | void {
function chunkStart (code: Code): State | undefined {
if (code === null) {
return after(code)
}
Expand All @@ -245,7 +245,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return contentContinue(code)
}

function contentContinue (code: Code): State | void {
function contentContinue (code: Code): State | undefined {
if (code === null) {
effects.exit('chunkDocument')
return after(code)
Expand All @@ -261,7 +261,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return contentContinue
}

function after (code: Code): State | void {
function after (code: Code): State | undefined {
// Close section
section.exit(effects)
effects.exit('componentContainerContent')
Expand All @@ -274,13 +274,13 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat

return factorySpace(effects, closingPrefixAfter, 'linePrefix', 4)

function closingPrefixAfter (code: Code): State | void {
function closingPrefixAfter (code: Code): State | undefined {
effects.enter('componentContainerFence')
effects.enter('componentContainerSequence')
return closingSequence(code)
}

function closingSequence (code: Code): State | void {
function closingSequence (code: Code): State | undefined {
if (code === Codes.colon) {
effects.consume(code)
size++
Expand All @@ -300,7 +300,7 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
return factorySpace(effects, closingSequenceEnd, 'whitespace')(code)
}

function closingSequenceEnd (code: Code): State | void {
function closingSequenceEnd (code: Code): State | undefined {
if (code === null || markdownLineEnding(code)) {
effects.exit('componentContainerFence')
return ok(code)
Expand Down
Loading

0 comments on commit e8e3263

Please sign in to comment.