Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
Closes GH-35.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: Junyoung Choi <fluke8259@gmail.com>
Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
remcohaszing authored Jun 8, 2020
1 parent 35d2f90 commit 38bdb01
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
coverage
rehype.js
rehype.min.js
*.html
*.json
*.md
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"devDependencies": {
"bail": "^1.0.0",
"browserify": "^16.0.0",
"dtslint": "^3.5.0",
"hast-util-assert": "^2.0.0",
"lerna": "^3.0.0",
"mdast-zone": "^4.0.0",
Expand All @@ -27,13 +28,14 @@
},
"scripts": {
"postinstall": "lerna bootstrap --no-ci",
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"format": "remark . -qfo && prettier --write . && xo --fix",
"build-bundle": "browserify packages/rehype -s rehype > rehype.js",
"build-mangle": "browserify packages/rehype -p tinyify -s rehype > rehype.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint packages/rehype-parse/types && dtslint packages/rehype-stringify/types && dtslint packages/rehype/types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
Expand All @@ -58,7 +60,8 @@
"unicorn/string-content": "off"
},
"ignores": [
"rehype.js"
"rehype.js",
"**/*.ts"
]
},
"remarkConfig": {
Expand Down
4 changes: 3 additions & 1 deletion packages/rehype-parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
],
"files": [
"index.js",
"types/index.d.ts",
"errors.json"
],
"types": "types/index.d.ts",
"dependencies": {
"hast-util-from-parse5": "^5.0.0",
"hast-util-from-parse5": "^6.0.0",
"parse5": "^5.0.0",
"xtend": "^4.0.0"
},
Expand Down
47 changes: 47 additions & 0 deletions packages/rehype-parse/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// TypeScript Version: 3.5
import {Parser, Plugin} from 'unified'
import {Node, Parent} from 'unist'
import {HastUtilFromParse5Options} from 'hast-util-from-parse5'

declare namespace rehypeParse {
interface Parse extends Plugin<[RehypeParseOptions?]> {
Parser: Parser
}

interface RehypeParseOptions
extends Pick<HastUtilFromParse5Options, 'space' | 'verbose'> {
/**
* Specify whether to parse a fragment, instead of a complete document.
* In document mode, unopened `html`, `head`, and `body` elements are opened in
* just the right places.
*
* @default false
*/
fragment?: boolean

/**
* > ⚠️ Parse errors are currently being added to HTML.
* > Not all errors emitted by parse5 (or rehype-parse) are specced yet.
* > Some documentation may still be missing.
*
* Emit parse errors while parsing on the [vfile](https://github.com/vfile/vfile)
*
* Setting this to true starts emitting
* [HTML parse errors](https://html.spec.whatwg.org/multipage/parsing.html#parse-errors).
*
* Specific rules can be turned off by setting them to `false` (or `0`).
* The default, when `emitParseErrors: true`, is `true` (or `1`), and means that
* rules emit as warnings.
* Rules can also be configured with `2`, to turn them into fatal errors.
*
* @default false
*/
emitParseErrors?: boolean
}

type Visitor = (node: Node, parent?: Parent) => string
}

declare const rehypeParse: rehypeParse.Parse

export = rehypeParse
15 changes: 15 additions & 0 deletions packages/rehype-parse/types/rehype-parse-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unified = require('unified')
import parse = require('rehype-parse')

unified().use(parse)

unified().use(parse, {})

unified().use(parse, {fragment: true})

unified().use(parse, {space: 'html'})
unified().use(parse, {space: 'svg'})

unified().use(parse, {emitParseErrors: true})

unified().use(parse, {verbose: true})
10 changes: 10 additions & 0 deletions packages/rehype-parse/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"rehype-parse": ["index.d.ts"]
}
}
}
8 changes: 8 additions & 0 deletions packages/rehype-parse/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-redundant-jsdoc": false,
"semicolon": false,
"whitespace": false
}
}
6 changes: 4 additions & 2 deletions packages/rehype-stringify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"hast-util-to-html": "^7.0.0",
"hast-util-to-html": "^7.1.1",
"xtend": "^4.0.0"
},
"xo": false
Expand Down
28 changes: 28 additions & 0 deletions packages/rehype-stringify/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TypeScript Version: 3.5
import {Compiler, Processor, Plugin} from 'unified'
import {Node, Parent} from 'unist'
import {HastUtilToHtmlOptions} from 'hast-util-to-html'

declare class RehypeCompiler implements Compiler {
compile(): string
visitors: {
[key: string]: rehypeStringify.Visitor
}
}

declare namespace rehypeStringify {
interface Stringify extends Plugin<[HastUtilToHtmlOptions?]> {
Compiler: typeof RehypeCompiler
(this: Processor, options?: HastUtilToHtmlOptions): void
}

type RehypeStringifyOptions = HastUtilToHtmlOptions

type Compiler = RehypeCompiler

type Visitor = (node: Node, parent?: Parent) => string
}

declare const rehypeStringify: rehypeStringify.Stringify

export = rehypeStringify
10 changes: 10 additions & 0 deletions packages/rehype-stringify/types/rehype-stringify-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import unified = require('unified')
import stringify = require('rehype-stringify')

unified().use(stringify)

unified().use(stringify, {})

unified().use(stringify, {
upperDoctype: true
})
10 changes: 10 additions & 0 deletions packages/rehype-stringify/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"rehype-stringify": ["index.d.ts"]
}
}
}
7 changes: 7 additions & 0 deletions packages/rehype-stringify/types/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
}
}
4 changes: 3 additions & 1 deletion packages/rehype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"scripts": {},
"xo": false
}
13 changes: 13 additions & 0 deletions packages/rehype/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// TypeScript Version: 3.5

import {Processor} from 'unified'
import {RehypeParseOptions} from 'rehype-parse'
import {RehypeStringifyOptions} from 'rehype-stringify'

declare namespace rehype {
type RehypeOptions = RehypeStringifyOptions & RehypeParseOptions
}

declare function rehype(): Processor<rehype.RehypeOptions>

export = rehype
12 changes: 12 additions & 0 deletions packages/rehype/types/rehype-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import rehype = require('rehype')

interface PluginOptions {
example: boolean
}

const plugin = (options?: PluginOptions) => {}

rehype().use(plugin)
rehype().use(plugin, {example: true})
rehype().use({settings: {fragment: true}})
rehype().use({settings: {upperDoctype: true}})
10 changes: 10 additions & 0 deletions packages/rehype/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"rehype": ["index.d.ts"]
}
}
}
15 changes: 15 additions & 0 deletions packages/rehype/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"max-line-length": false,
"no-redundant-jsdoc": false,
"no-void-expression": false,
"only-arrow-functions": false,
"semicolon": false,
"unified-signatures": false,
"whitespace": false,
"interface-over-type-literal": false,
"no-unnecessary-generics": false,
"strict-export-declare-modifiers": false
}
}

0 comments on commit 38bdb01

Please sign in to comment.