Skip to content

Commit

Permalink
types: add type definitions for micromark
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMurphy committed Sep 11, 2020
1 parent 005ad17 commit 9aca9bc
Show file tree
Hide file tree
Showing 27 changed files with 290 additions and 1 deletion.
5 changes: 5 additions & 0 deletions buffer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TypeScript Version: 3.0

import buffer = require('./lib')

export = buffer
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TypeScript Version: 3.0

import buffer = require('./buffer')

export = buffer
3 changes: 3 additions & 0 deletions lib/compile/html.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function compileHTML(): void

export = compileHTML
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {ParserOptions} from './parse'

declare function buffer(options?: ParserOptions): void

export = buffer
11 changes: 11 additions & 0 deletions lib/parse.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Parser} from '../shared-types'

declare namespace createParser {
interface ParserOptions {
extensions: unknown[]
}
}

declare function createParser(options?: createParser.ParserOptions): Parser

export = createParser
3 changes: 3 additions & 0 deletions lib/postprocess.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function postprocess(): void

export = postprocess
3 changes: 3 additions & 0 deletions lib/preprocess.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function preprocess(): void

export = preprocess
5 changes: 5 additions & 0 deletions lib/steam.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {ParserOptions} from './parse'

declare function stream(options?: ParserOptions): void

export = stream
3 changes: 3 additions & 0 deletions lib/util/classify-character.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function classifyCharacter(code: number): number

export = classifyCharacter
9 changes: 9 additions & 0 deletions lib/util/create-tokenizer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Parser, Point, Tokenizer} from '../../shared-types'

declare function createTokenizer(
parser: Parser,
initialize: unknown,
from: Point
): Tokenizer

export = createTokenizer
15 changes: 15 additions & 0 deletions lib/util/flat-map.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param array array to flatten
* @param map mapping function
* @param a passed to map function
* @typeParam T shape of item input to flatMap
* @typeParam U shape of item returned by flatMap
* @typeParam A shape of additional attribute passed
*/
declare function flatMap<T, U, A>(
array: T[][],
map: (array: T[], a: A) => U[],
a?: A
): U[]

export = flatMap
3 changes: 3 additions & 0 deletions lib/util/lowercase.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function lowercase(code: number): number

export = lowercase
5 changes: 5 additions & 0 deletions lib/util/move-point.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Point} from 'shared-types'

declare function movePoint(point: Point, offset: number): Point

export = movePoint
3 changes: 3 additions & 0 deletions lib/util/normalize-identifier.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function normalizeIdentifier(value: string): string

export = normalizeIdentifier
3 changes: 3 additions & 0 deletions lib/util/normalize-uri.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function normalizeUri(url: string): string

export = normalizeUri
5 changes: 5 additions & 0 deletions lib/util/prefix-size.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Event, Type} from '../../shared-types'

declare function prefixSize(events: Event[], type: Type): number

export = prefixSize
3 changes: 3 additions & 0 deletions lib/util/safe-from-int.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function safeFromInt(value: string, base?: number): string

export = safeFromInt
3 changes: 3 additions & 0 deletions lib/util/serialize-chunks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function serializeChunks(chunks: Array<string | number>): string

export = serializeChunks
3 changes: 3 additions & 0 deletions lib/util/shallow.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function shallow<T>(object: T): T

export = shallow
5 changes: 5 additions & 0 deletions lib/util/slice-chunks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Token} from '../../shared-types'

declare function sliceChunks(chunks: string[], token: Token): string[]

export = sliceChunks
5 changes: 5 additions & 0 deletions lib/util/subtokenize.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Event} from '../../shared-types'

declare function subtokenize(events: Event[]): {done: boolean; events: Event[]}

export = subtokenize
5 changes: 5 additions & 0 deletions micromark.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import micromarkBuffer = require('micromark')
import micromarkStream = require('micromark/stream')

micromarkBuffer()
micromarkStream()
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"browserify": "^16.0.0",
"commonmark.json": "^0.29.0",
"concat-stream": "^2.0.0",
"dtslint": "^4.0.0",
"gzip-size-cli": "^3.0.0",
"ms": "^2.0.0",
"nyc": "^15.0.0",
Expand All @@ -85,7 +86,8 @@
"format": "remark . -qfo && prettier . --write && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run generate && npm run format && npm run test-coverage"
"test-types": "dtslint .",
"test": "npm run generate && npm run format && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
Expand Down
147 changes: 147 additions & 0 deletions shared-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// TypeScript Version: 3.0

/**
* A location in a string or buffer
*/
export interface Point {
line: number
column: number
offset: number
_index?: number
_bufferIndex?: number
}

/**
* A token type
*
* TODO: enumerate token types
*/
export type Type = string

/**
*
*/
export interface Token {
start: Point
end: Point
type: Type
contentType: 'flow' | 'content' | 'string' | 'text'
previous: Token
next: Token

// TODO move these to interfaces extending Token, or move these to a `data` property similar to unist (would require code refactor)
/**
* Used for whitespace in several places which needs to account for tab stops
*/
_size?: number

/**
* ends with a CR, LF, or CRLF.
*/
_break?: boolean

/**
* Used when dealing with linked tokens. A child tokenizer is needed to tokenize them, which is stored on those tokens
*/
_tokenizer?: Tokenizer

/**
* Used for attention (emphasis, strong).
*
* could be (enter emphasis, enter emphasisMarker, exit emphasisMarker, enter strong, enter strongMarker, exit strongMarker, enter data, exit data)
*/
_events?: Event[]
_subevents?: Event[]
_contentTokenized?: unknown
_close?: unknown
_marker?: unknown
_open?: unknown
_side?: number
_tabs?: unknown
}

/**
*
*/
export type Event = [string, Token, unknown]

/**
* These these are transitions to update the CommonMark State Machine (CSMS)
*/
export interface Effects {
enter: (type: Type) => Token
consume: (code: number) => void
exit: (type: Type) => Token
attempt: (
one: unknown,
two: unknown,
three?: unknown
) => (code: number) => unknown
interrupt: (
one: unknown,
two: unknown,
three: unknown
) => (code: number) => unknown
check: (
one: unknown,
two: unknown,
three?: unknown
) => (code: number) => unknown
lazy: (one: unknown, ok: Okay, nok: NotOkay) => void
}

/**
*
*/
export interface Okay {
(code: number): void
}

/**
*
*/
export interface NotOkay {
(code: number): void
}

/**
*
*/
export interface Tokenizer {
previous: Token
events: Event[]
parser: Parser
sliceStream: (token: Token) => string[]
sliceSerialize: (token: Token) => string
now: () => Point
defineSkip: (value: Point) => void
write: (value: number) => Event[]
}

/**
*
*/
export interface Parser {
hooks: {
[key: string]: unknown
}
flow: (something: unknown) => unknown
defined: unknown[]
}

/**
*
*/
export interface TokenizerThis {
events: Event[]
interrupt: unknown
lazy: unknown
containerState: {
marker: number
type: Type
initialBlankLine: unknown
size: number
_closeFlow: unknown
furtherBlankLines: unknown
}
}
5 changes: 5 additions & 0 deletions stream.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TypeScript Version: 3.0

import stream = require('./lib/steam')

export = stream
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"moduleResolution": "node",
"lib": [
"ES5"
],
"strict": true,
"baseUrl": ".",
"paths": {
"micromark": [
"index.d.ts"
],
"micromark/stream": [
"stream.d.ts"
]
}
}
}
7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"whitespace": false
}
}

0 comments on commit 9aca9bc

Please sign in to comment.