Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TS type information for remark-stringify and remark-parse #383

Merged
merged 21 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9ce1fe9
feat: TS type information for remark-stringify
mike-north Jan 11, 2019
6f6745b
ci: remove unnecessary Travis-CI jobs, and serialization of jobs
mike-north Jan 11, 2019
a53b3a6
ci: travis-ci - fail fast
mike-north Jan 11, 2019
c6e6fd5
ci: run TS type tests in node 10.0 instead of node 0.10
mike-north Jan 11, 2019
b6b70b8
chore: respond to code review feedback
mike-north Jan 15, 2019
b8fd5e7
feat: TS type information for remark-parse
mike-north Jan 11, 2019
55163c9
chore: respond to code review feedback
mike-north Jan 15, 2019
1c3c2c1
Fix type definitions
Rokt33r Jan 21, 2019
888707b
Fix type test of remark-stringify
Rokt33r Jan 21, 2019
5fe120c
Merge remote-tracking branch 'mike-north/parse-types' into stringify-…
Rokt33r Jan 21, 2019
89ee188
Fix remark-parse type definitions
Rokt33r Jan 21, 2019
84c229c
Add type test for the extending Parser example
Rokt33r Jan 21, 2019
7d12f1b
Revise types and add test for the extending Compiler example
Rokt33r Jan 21, 2019
356f2c9
Format tslint
Rokt33r Jan 21, 2019
4ab9dc9
Pull out dtslint and typescript devDependencies to master package.json
Rokt33r Jan 21, 2019
6debdcb
Make locator optional
Rokt33r Jan 21, 2019
33e8712
Discard extending paresr example in test
Rokt33r Jan 21, 2019
822c593
Merge remote-tracking branch 'upstream/master' into stringify-types
Rokt33r Jul 2, 2019
553c4e0
Merge branch 'master' into stringify-types
ChristianMurphy Jul 11, 2019
7ff9082
add typings for remark, leverage generic in stringify and parse
ChristianMurphy Jul 11, 2019
9011bf8
types: add support for type checking remark options
ChristianMurphy Jul 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"bundle-collapser": "^1.3.0",
"camelcase": "^5.0.0",
"clone": "^2.0.0",
"dtslint": "^0.4.2",
"lerna": "^3.0.0",
"mdast-util-assert": "^2.0.0",
"mdast-util-compact": "^1.0.0",
Expand All @@ -20,20 +21,22 @@
"remark-preset-wooorm": "^5.0.0",
"tape": "^4.5.1",
"tinyify": "^2.4.3",
"typescript": "^3.2.4",
"unist-builder": "^1.0.2",
"unist-util-remove-position": "^1.1.0",
"xo": "^0.24.0"
},
"scripts": {
"postinstall": "lerna bootstrap --no-ci",
"format": "packages/remark-cli/cli.js . -qfo && prettier --write \"**/*.js\" && xo --fix",
"format": "packages/remark-cli/cli.js . -qfo && prettier --write \"**/*.{js,ts}\" && xo --fix",
"build-bundle": "browserify packages/remark -s remark > remark.js",
"build-mangle": "browserify packages/remark -s remark -p tinyify > remark.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "tape packages/*/test.js test/index.js",
"test-api-extensive": "TEST_EXTENDED=true npm run test-api",
"test-coverage": "nyc --reporter lcov tape \"test/index.js\" \"packages/*/test.js\"",
"test": "npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint packages/remark-parse/types && dtslint packages/remark-stringify/types && dtslint packages/remark/types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
Expand Down
6 changes: 4 additions & 2 deletions packages/remark-parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ast",
"parse"
],
"types": "types/index.d.ts",
"homepage": "https://remark.js.org",
"repository": "https://github.com/remarkjs/remark/tree/master/packages/remark-parse",
"bugs": "https://github.com/remarkjs/remark/issues",
Expand All @@ -28,7 +29,8 @@
],
"files": [
"index.js",
"lib"
"lib",
"types/index.d.ts"
],
"dependencies": {
"collapse-white-space": "^1.0.2",
Expand All @@ -49,7 +51,7 @@
},
"devDependencies": {
"tape": "^4.9.1",
"unified": "^8.0.0",
"unified": "^8.2.0",
"vfile": "^4.0.0"
},
"scripts": {
Expand Down
53 changes: 53 additions & 0 deletions packages/remark-parse/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// TypeScript Version: 3.0

import {Node, Parent, Position} from 'unist'
import {Parser, Attacher} from 'unified'

declare class RemarkParser implements Parser {
parse(): Node
blockMethods: string[]
inlineTokenizers: {
[key: string]: remarkParse.Tokenizer
}
inlineMethods: string[]
}

declare namespace remarkParse {
interface Parse extends Attacher<[Partial<RemarkParseOptions>]> {
(options: Partial<RemarkParseOptions>): void
Parser: typeof RemarkParser
}

type Parser = RemarkParser

interface RemarkParseOptions {
gfm: boolean
commonmark: boolean
footnotes: boolean
blocks: string[]
pedantic: boolean
}

interface Add {
(node: Node, parent?: Parent): Node
test(): Position
reset(node: Node, parent?: Node): Node
}

type Eat = (value: string) => Add

type Locator = (value: string, fromIndex: number) => number

interface Tokenizer {
(eat: Eat, value: string, silent: true): boolean | void
(eat: Eat, value: string): Node | void
locator?: Locator
onlyAtStart?: boolean
notInBlock?: boolean
notInList?: boolean
notInLink?: boolean
}
}
declare const remarkParse: remarkParse.Parse

export = remarkParse
17 changes: 17 additions & 0 deletions packages/remark-parse/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unified = require('unified')
import * as Unist from 'unist'
import remarkParse = require('remark-parse')

const parseOptions = {
gfm: true,
pedantic: true
}

unified().use(remarkParse, parseOptions)

const badParseOptions = {
gfm: 'true'
}

// $ExpectError
unified().use(remarkParse, badParseOptions)
14 changes: 14 additions & 0 deletions packages/remark-parse/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": ".",
"paths": {
"remark-parse": ["index.d.ts"]
}
}
}
14 changes: 14 additions & 0 deletions packages/remark-parse/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"callable-types": false,
"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
}
}
6 changes: 4 additions & 2 deletions packages/remark-stringify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
],
"files": [
"index.js",
"lib"
"lib",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"ccount": "^1.0.0",
"is-alphanumeric": "^1.0.0",
Expand All @@ -46,7 +48,7 @@
},
"devDependencies": {
"tape": "^4.9.1",
"unified": "^8.0.0",
"unified": "^8.2.0",
"unist-builder": "^1.0.3",
"unist-util-visit": "^1.4.0",
"wcwidth": "^1.0.1"
Expand Down
48 changes: 48 additions & 0 deletions packages/remark-stringify/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// TypeScript Version: 3.0

import {Attacher, Compiler, Processor} from 'unified'
import {Node, Parent} from 'unist'

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

declare namespace remarkStringify {
interface Stringify extends Attacher<[Partial<RemarkStringifyOptions>]> {
Compiler: typeof RemarkCompiler
(this: Processor, options?: Partial<RemarkStringifyOptions>): void
}

type Compiler = RemarkCompiler

interface RemarkStringifyOptions {
gfm: boolean
commonmark: boolean
entities: boolean | 'numbers' | 'escape'
setext: boolean
closeAtx: boolean
looseTable: boolean
spacedTable: boolean
paddedTable: boolean
stringLength: (s: string) => number
fence: '~' | '`'
fences: boolean
bullet: '-' | '*' | '+'
listItemIndent: 'tab' | '1' | 'mixed'
incrementListMarker: boolean
rule: '-' | '_' | '*'
ruleRepetition: number
ruleSpaces: boolean
strong: '_' | '*'
emphasis: '_' | '*'
}

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

declare const remarkStringify: remarkStringify.Stringify

export = remarkStringify
51 changes: 51 additions & 0 deletions packages/remark-stringify/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import remarkStringify = require('remark-stringify')
import unified = require('unified')
import {Node, Parent} from 'unist'

const inferredStringifyOptions = {
gfm: true,
fences: true,
incrementListMarker: false
}

unified().use(remarkStringify, inferredStringifyOptions)

// These cannot be automatically inferred by TypeScript
const nonInferredStringifyOptions: Partial<
remarkStringify.RemarkStringifyOptions
> = {
fence: '~',
bullet: '+',
listItemIndent: 'tab',
rule: '-',
strong: '_',
emphasis: '*'
}

unified().use(remarkStringify, nonInferredStringifyOptions)

const badStringifyOptions = {
gfm: 'true'
}

// $ExpectError
unified().use(remarkStringify, badStringifyOptions)

function gap(this: unified.Processor) {
const Compiler = this.Compiler as typeof remarkStringify.Compiler
const visitors = Compiler.prototype.visitors
const original = visitors.heading

visitors.heading = heading

function heading(this: unified.Processor, node: Node, parent?: Parent) {
// FIXME: remove need for explicit 'as' casting
const headingNode = node as (Node & {depth: number})
return (
(headingNode.depth === 2 ? '\n' : '') +
original.apply(this, [node, parent])
)
}
}

const plugin: unified.Attacher = gap
14 changes: 14 additions & 0 deletions packages/remark-stringify/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
mike-north marked this conversation as resolved.
Show resolved Hide resolved
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": ".",
"paths": {
"remark-stringify": ["index.d.ts"]
}
}
}
14 changes: 14 additions & 0 deletions packages/remark-stringify/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
mike-north marked this conversation as resolved.
Show resolved Hide resolved
"extends": "dtslint/dtslint.json",
"rules": {
"callable-types": false,
"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
}
}
6 changes: 4 additions & 2 deletions packages/remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"remark-parse": "^6.0.0",
"remark-stringify": "^6.0.0",
"unified": "^8.0.0"
"unified": "^8.2.0"
},
"devDependencies": {
"tape": "^4.9.1"
Expand Down
14 changes: 14 additions & 0 deletions packages/remark/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// TypeScript Version: 3.0

import unified = require('unified')
import remarkParse = require('remark-parse')
import remarkStringify = require('remark-stringify')

type RemarkOptions = remarkParse.RemarkParseOptions &
remarkStringify.RemarkStringifyOptions

declare function remark<
P extends Partial<RemarkOptions> = Partial<RemarkOptions>
>(): unified.Processor<P>

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

interface PluginOptions {
example: boolean
}

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

remark().use(plugin)
remark().use(plugin, {example: true})
remark().use({settings: {commonmark: true}})
ChristianMurphy marked this conversation as resolved.
Show resolved Hide resolved
// $ExpectError
remark().use({settings: {doesNotExist: true}})
// $ExpectError
remark().use(plugin, {doesNotExist: 'true'})
14 changes: 14 additions & 0 deletions packages/remark/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": ".",
"paths": {
"remark": ["index.d.ts"]
}
}
}
15 changes: 15 additions & 0 deletions packages/remark/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"callable-types": false,
"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
}
}