Skip to content

Commit aa17c1a

Browse files
authoredMay 21, 2020
Add types
Closes GH-10. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent cdf4822 commit aa17c1a

File tree

7 files changed

+103
-11
lines changed

7 files changed

+103
-11
lines changed
 

‎.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
coverage/
22
hast-util-from-parse5.js
33
hast-util-from-parse5.min.js
4+
*.html
5+
*.json
6+
*.md

‎package.json

+12-5
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,27 @@
2424
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2525
],
2626
"files": [
27-
"index.js"
27+
"index.js",
28+
"types/index.d.ts"
2829
],
30+
"types": "types/index.d.ts",
2931
"dependencies": {
32+
"@types/parse5": "^5.0.2",
3033
"ccount": "^1.0.3",
3134
"hastscript": "^5.0.0",
3235
"property-information": "^5.0.0",
36+
"vfile": "^4.1.0",
3337
"web-namespaces": "^1.1.2",
3438
"xtend": "^4.0.1"
3539
},
3640
"devDependencies": {
3741
"browserify": "^16.0.0",
42+
"dtslint": "^3.5.2",
3843
"is-hidden": "^1.0.0",
3944
"not": "^0.1.0",
4045
"nyc": "^15.0.0",
4146
"parse5": "^5.0.0",
42-
"prettier": "^1.0.0",
47+
"prettier": "^2.0.5",
4348
"remark-cli": "^7.0.0",
4449
"remark-preset-wooorm": "^6.0.0",
4550
"tape": "^4.0.0",
@@ -49,13 +54,14 @@
4954
"xo": "^0.27.0"
5055
},
5156
"scripts": {
52-
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
57+
"format": "remark . -qfo && prettier --write . && xo --fix",
5358
"build-bundle": "browserify index.js -s hastUtilFromParse5 > hast-util-from-parse5.js",
5459
"build-mangle": "browserify index.js -p tinyify -s hastUtilFromParse5 > hast-util-from-parse5.min.js",
5560
"build": "npm run build-bundle && npm run build-mangle",
5661
"test-api": "node test",
5762
"test-coverage": "nyc --reporter lcov tape test",
58-
"test": "npm run format && npm run build && npm run test-coverage"
63+
"test-types": "dtslint types",
64+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
5965
},
6066
"prettier": {
6167
"tabWidth": 2,
@@ -72,7 +78,8 @@
7278
"guard-for-in": "off"
7379
},
7480
"ignores": [
75-
"hast-util-from-parse5.js"
81+
"hast-util-from-parse5.js",
82+
"**/*.ts"
7683
]
7784
},
7885
"nyc": {

‎test/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var fromParse5 = require('..')
1313

1414
var join = path.join
1515

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

1919
t.deepEqual(
@@ -346,17 +346,15 @@ test('hast-util-from-parse5', function(t) {
346346
t.end()
347347
})
348348

349-
test('fixtures', function(t) {
349+
test('fixtures', function (t) {
350350
var base = join('test', 'fixtures')
351351

352-
fs.readdirSync(base)
353-
.filter(not(hidden))
354-
.forEach(each)
352+
fs.readdirSync(base).filter(not(hidden)).forEach(each)
355353

356354
t.end()
357355

358356
function each(fixture) {
359-
t.test(fixture, function(st) {
357+
t.test(fixture, function (st) {
360358
var opts = {
361359
file: vfile.readSync(join(base, fixture, 'index.html')),
362360
out: join(base, fixture, 'index.json')

‎types/index.d.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// TypeScript Version: 3.5
2+
import {Node} from 'unist'
3+
import {Document} from 'parse5'
4+
import {VFile} from 'vfile'
5+
6+
declare namespace hastUtilFromParse5 {
7+
interface HastUtilFromParse5Options {
8+
/**
9+
* Whether the [*root*](https://github.com/syntax-tree/unist#root) of the
10+
* [*tree*](https://github.com/syntax-tree/unist#tree) is in the `'html'` or `'svg'`
11+
* space.
12+
*
13+
* If an element in with the SVG namespace is found in `ast`, `fromParse5`
14+
* automatically switches to the SVG space when entering the element, and switches
15+
* back when leaving.
16+
*
17+
* @default 'html'
18+
*/
19+
space?: 'html' | 'svg'
20+
21+
/**
22+
* [`VFile`](https://github.com/vfile/vfile), used to add
23+
* [positional information](https://github.com/syntax-tree/unist#positional-information)
24+
* to [*nodes*](https://github.com/syntax-tree/hast#nodes).
25+
* If given, the [*file*](https://github.com/syntax-tree/unist#file) should have the
26+
* original HTML source as its contents.
27+
*/
28+
file?: VFile
29+
/**
30+
*
31+
* Whether to add extra positional information about starting tags, closing tags,
32+
* and attributes to elements.
33+
*
34+
* Note: not used without `file`.
35+
*
36+
* @default: false
37+
*/
38+
verbose?: boolean
39+
}
40+
}
41+
42+
/**
43+
* Transform [Parse5’s AST](https://github.com/inikulin/parse5/blob/master/packages/parse5/docs/tree-adapter/default/interface-list.md)
44+
* to a [**hast**](https://github.com/syntax-tree/hast)
45+
* [*tree*](https://github.com/syntax-tree/unist#tree).
46+
*
47+
* @param options If `options` is a [`VFile`](https://github.com/vfile/vfile), it’s treated
48+
* as `{file: options}`.
49+
*/
50+
declare function hastUtilFromParse5(
51+
ast: Document,
52+
options?: hastUtilFromParse5.HastUtilFromParse5Options | VFile
53+
): Node
54+
55+
export = hastUtilFromParse5

‎types/tests.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import vfile = require('vfile')
2+
import parse5 = require('parse5')
3+
import fromParse5 = require('hast-util-from-parse5')
4+
5+
const file = vfile()
6+
const ast = parse5.parse('', {sourceCodeLocationInfo: true})
7+
fromParse5(ast, file)
8+
fromParse5(ast, {file})
9+
fromParse5(ast, {space: 'html'})
10+
fromParse5(ast, {space: 'svg'})
11+
fromParse5(ast, {verbose: true})

‎types/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"hast-util-from-parse5": ["index.d.ts"]
8+
}
9+
}
10+
}

‎types/tslint.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"no-redundant-jsdoc": false,
5+
"semicolon": false,
6+
"whitespace": false
7+
}
8+
}

0 commit comments

Comments
 (0)
Please sign in to comment.