Skip to content

Add TypeScript definitions #10

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

Merged
merged 5 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coverage/
hast-util-from-parse5.js
hast-util-from-parse5.min.js
*.html
*.md
17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"ccount": "^1.0.3",
"hastscript": "^5.0.0",
Expand All @@ -34,28 +36,32 @@
"xtend": "^4.0.1"
},
"devDependencies": {
"@types/parse5": "^5.0.2",
"browserify": "^16.0.0",
"dtslint": "^3.5.2",
"is-hidden": "^1.0.0",
"not": "^0.1.0",
"nyc": "^15.0.0",
"parse5": "^5.0.0",
"prettier": "^1.0.0",
"prettier": "^2.0.5",
"remark-cli": "^7.0.0",
"remark-preset-wooorm": "^6.0.0",
"tape": "^4.0.0",
"tinyify": "^2.0.0",
"to-vfile": "^6.0.0",
"unist-util-visit": "^2.0.0",
"vfile": "^4.1.0",
"xo": "^0.27.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"format": "remark . -qfo && prettier --write . && xo --fix",
"build-bundle": "browserify index.js -s hastUtilFromParse5 > hast-util-from-parse5.js",
"build-mangle": "browserify index.js -p tinyify -s hastUtilFromParse5 > hast-util-from-parse5.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test",
"test": "npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -72,7 +78,8 @@
"guard-for-in": "off"
},
"ignores": [
"hast-util-from-parse5.js"
"hast-util-from-parse5.js",
"**/*.ts"
]
},
"nyc": {
Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/attributes/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
"tagName": "p",
"properties": {
"id": "foo",
"className": [
"bar",
"baz"
],
"className": ["bar", "baz"],
"dataQux": "quux"
},
"children": [],
Expand Down
10 changes: 4 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var fromParse5 = require('..')

var join = path.join

test('hast-util-from-parse5', function(t) {
test('hast-util-from-parse5', function (t) {
var file = vfile({contents: '<title>Hello!</title><h1>World!'})

t.deepEqual(
Expand Down Expand Up @@ -346,17 +346,15 @@ test('hast-util-from-parse5', function(t) {
t.end()
})

test('fixtures', function(t) {
test('fixtures', function (t) {
var base = join('test', 'fixtures')

fs.readdirSync(base)
.filter(not(hidden))
.forEach(each)
fs.readdirSync(base).filter(not(hidden)).forEach(each)

t.end()

function each(fixture) {
t.test(fixture, function(st) {
t.test(fixture, function (st) {
var opts = {
file: vfile.readSync(join(base, fixture, 'index.html')),
out: join(base, fixture, 'index.json')
Expand Down
55 changes: 55 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// TypeScript Version: 3.5
import {Node} from 'unist'
import {Document} from 'parse5'
import {VFile} from 'vfile'

declare namespace hastUtilFromParse5 {
interface HastUtilFromParse5Options {
/**
* Whether the [*root*](https://github.com/syntax-tree/unist#root) of the
* [*tree*](https://github.com/syntax-tree/unist#tree) is in the `'html'` or `'svg'`
* space.
*
* If an element in with the SVG namespace is found in `ast`, `fromParse5`
* automatically switches to the SVG space when entering the element, and switches
* back when leaving.
*
* @default 'html'
*/
space?: 'html' | 'svg'

/**
* [`VFile`](https://github.com/vfile/vfile), used to add
* [positional information](https://github.com/syntax-tree/unist#positional-information)
* to [*nodes*](https://github.com/syntax-tree/hast#nodes).
* If given, the [*file*](https://github.com/syntax-tree/unist#file) should have the
* original HTML source as its contents.
*/
file?: VFile
/**
*
* Whether to add extra positional information about starting tags, closing tags,
* and attributes to elements.
*
* Note: not used without `file`.
*
* @default: false
*/
verbose?: boolean
}
}

/**
* Transform [Parse5’s AST](https://github.com/inikulin/parse5/blob/master/packages/parse5/docs/tree-adapter/default/interface-list.md)
* to a [**hast**](https://github.com/syntax-tree/hast)
* [*tree*](https://github.com/syntax-tree/unist#tree).
*
* @param options If `options` is a [`VFile`](https://github.com/vfile/vfile), it’s treated
* as `{file: options}`.
*/
declare function hastUtilFromParse5(
ast: Document,
options?: hastUtilFromParse5.HastUtilFromParse5Options | VFile
): Node

export = hastUtilFromParse5
11 changes: 11 additions & 0 deletions types/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import vfile = require('vfile')
import parse5 = require('parse5')
import fromParse5 = require('hast-util-from-parse5')

const file = vfile()
const ast = parse5.parse('', {sourceCodeLocationInfo: true})
fromParse5(ast, file)
fromParse5(ast, {file})
fromParse5(ast, {space: 'html'})
fromParse5(ast, {space: 'svg'})
fromParse5(ast, {verbose: true})
10 changes: 10 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"hast-util-from-parse5": ["index.d.ts"]
}
}
}
8 changes: 8 additions & 0 deletions 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
}
}