Skip to content

Commit 12f7f9b

Browse files
committedApr 30, 2021
Add JSDoc based types
1 parent 1076942 commit 12f7f9b

11 files changed

+299
-398
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

‎index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
/**
2+
* @typedef {import('./lib/index.js').Schema} Schema
3+
*/
4+
15
export {sanitize} from './lib/index.js'
26
export {defaultSchema} from './lib/schema.js'

‎lib/index.js

+217-81
Large diffs are not rendered by default.

‎lib/schema.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @type {import('./index.js').Schema} */
12
export const defaultSchema = {
23
strip: ['script'],
34
clobberPrefix: 'user-content-',

‎package.json

+18-7
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,38 @@
2929
"sideEffects": false,
3030
"type": "module",
3131
"main": "index.js",
32-
"types": "types/index.d.ts",
32+
"types": "index.d.ts",
3333
"files": [
34-
"types/index.d.ts",
3534
"lib/",
35+
"index.d.ts",
3636
"index.js"
3737
],
38+
"dependencies": {
39+
"@types/hast": "^2.0.0"
40+
},
3841
"devDependencies": {
42+
"@types/tape": "^4.0.0",
3943
"c8": "^7.0.0",
4044
"deepmerge": "^4.0.0",
4145
"hast-util-to-html": "^7.0.0",
4246
"hastscript": "^7.0.0",
4347
"prettier": "^2.0.0",
4448
"remark-cli": "^9.0.0",
4549
"remark-preset-wooorm": "^8.0.0",
50+
"rimraf": "^3.0.0",
4651
"tape": "^5.0.0",
52+
"type-coverage": "^2.0.0",
53+
"typescript": "^4.0.0",
4754
"unist-builder": "^3.0.0",
4855
"xo": "^0.39.0"
4956
},
5057
"scripts": {
58+
"prepack": "npm run build && npm run format",
59+
"build": "rimraf \"{lib/**,}*.d.ts\" && tsc && type-coverage",
5160
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5261
"test-api": "node test.js",
5362
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
54-
"test": "npm run format && npm run test-coverage"
63+
"test": "npm run build && npm run format && npm run test-coverage"
5564
},
5665
"prettier": {
5766
"tabWidth": 2,
@@ -67,14 +76,16 @@
6776
"unicorn/no-array-for-each": "off",
6877
"no-var": "off",
6978
"prefer-arrow-callback": "off"
70-
},
71-
"ignore": [
72-
"types/"
73-
]
79+
}
7480
},
7581
"remarkConfig": {
7682
"plugins": [
7783
"preset-wooorm"
7884
]
85+
},
86+
"typeCoverage": {
87+
"atLeast": 100,
88+
"detail": true,
89+
"strict": true
7990
}
8091
}

‎test.js

+43-13
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ import {sanitize, defaultSchema} from './index.js'
99

1010
test('sanitize()', function (t) {
1111
t.test('non-node', function (t) {
12+
// @ts-ignore runtime.
1213
t.equal(html(sanitize(true)), '', 'should ignore non-nodes (#1)')
1314
t.equal(html(sanitize(null)), '', 'should ignore non-nodes (#2)')
15+
// @ts-ignore runtime.
1416
t.equal(html(sanitize(1)), '', 'should ignore non-nodes (#3)')
17+
// @ts-ignore runtime.
1518
t.equal(html(sanitize([])), '', 'should ignore non-nodes (#4)')
1619

1720
t.end()
1821
})
1922

2023
t.test('unknown nodes', function (t) {
2124
t.equal(
25+
// @ts-ignore runtime.
2226
html(sanitize(u('unknown', '<xml></xml>'))),
2327
'',
2428
'should ignore unknown nodes'
@@ -28,21 +32,25 @@ test('sanitize()', function (t) {
2832
})
2933

3034
t.test('ignored nodes', function (t) {
35+
// @ts-ignore runtime.
3136
t.equal(html(sanitize(u('raw', '<xml></xml>'))), '', 'should ignore `raw`')
3237

3338
t.equal(
39+
// @ts-ignore runtime.
3440
html(sanitize(u('directive', {name: '!alpha'}, '!alpha bravo'))),
3541
'',
3642
'should ignore declaration `directive`s'
3743
)
3844

3945
t.equal(
46+
// @ts-ignore runtime.
4047
html(sanitize(u('directive', {name: '?xml'}, '?xml version="1.0"'))),
4148
'',
4249
'should ignore processing instruction `directive`s'
4350
)
4451

4552
t.equal(
53+
// @ts-ignore runtime.
4654
html(sanitize(u('characterData', 'alpha'))),
4755
'',
4856
'should ignore `characterData`s'
@@ -65,6 +73,7 @@ test('sanitize()', function (t) {
6573
)
6674

6775
t.equal(
76+
// @ts-ignore runtime.
6877
html(sanitize(u('comment', {toString}), {allowComments: true})),
6978
'<!---->',
7079
'should ignore non-string `value`s with `allowComments: true`'
@@ -135,6 +144,7 @@ test('sanitize()', function (t) {
135144
)
136145

137146
t.equal(
147+
// @ts-ignore runtime.
138148
html(sanitize(u('text', {toString}))),
139149
'',
140150
'should ignore non-string `value`s'
@@ -191,6 +201,7 @@ test('sanitize()', function (t) {
191201
)
192202

193203
t.deepEqual(
204+
// @ts-ignore runtime.
194205
sanitize({
195206
type: 'element',
196207
properties: {},
@@ -201,6 +212,7 @@ test('sanitize()', function (t) {
201212
)
202213

203214
t.deepEqual(
215+
// @ts-ignore runtime.
204216
sanitize({type: 'element', tagName: 'div'}),
205217
h(''),
206218
'should support elements without children / properties'
@@ -294,12 +306,14 @@ test('sanitize()', function (t) {
294306
)
295307

296308
t.deepEqual(
309+
// @ts-ignore runtime.
297310
sanitize(u('element', {tagName: 'img', properties: {alt: null}})),
298311
h('img'),
299312
'should ignore `null`'
300313
)
301314

302315
t.deepEqual(
316+
// @ts-ignore runtime.
303317
sanitize(u('element', {tagName: 'img', properties: {alt: undefined}})),
304318
h('img'),
305319
'should ignore `undefined`'
@@ -318,18 +332,21 @@ test('sanitize()', function (t) {
318332
)
319333

320334
t.deepEqual(
335+
// @ts-ignore runtime.
321336
sanitize(u('element', {tagName: 'img', properties: {alt: {toString}}})),
322337
h('img'),
323338
'should ignore objects'
324339
)
325340

326341
t.deepEqual(
327342
sanitize(
343+
// @ts-ignore runtime.
328344
u('element', {
329345
tagName: 'img',
330346
properties: {alt: [1, true, 'three', [4], {toString}]}
331347
})
332348
),
349+
// @ts-ignore runtime.
333350
h('img', {alt: [1, true, 'three']}),
334351
'should supports arrays'
335352
)
@@ -341,7 +358,7 @@ test('sanitize()', function (t) {
341358
)
342359

343360
t.test('href`', function (t) {
344-
testAllURLs(t, 'a', 'href', {
361+
testAllUrls(t, 'a', 'href', {
345362
valid: {
346363
anchor: '#heading',
347364
relative: '/file.html',
@@ -367,7 +384,7 @@ test('sanitize()', function (t) {
367384
})
368385

369386
t.test('`cite`', function (t) {
370-
testAllURLs(t, 'blockquote', 'cite', {
387+
testAllUrls(t, 'blockquote', 'cite', {
371388
valid: {
372389
anchor: '#heading',
373390
relative: '/file.html',
@@ -392,7 +409,7 @@ test('sanitize()', function (t) {
392409
})
393410

394411
t.test('`src`', function (t) {
395-
testAllURLs(t, 'img', 'src', {
412+
testAllUrls(t, 'img', 'src', {
396413
valid: {
397414
anchor: '#heading',
398415
relative: '/file.html',
@@ -417,7 +434,7 @@ test('sanitize()', function (t) {
417434
})
418435

419436
t.test('`longDesc`', function (t) {
420-
testAllURLs(t, 'img', 'longDesc', {
437+
testAllUrls(t, 'img', 'longDesc', {
421438
valid: {
422439
anchor: '#heading',
423440
relative: '/file.html',
@@ -664,18 +681,31 @@ function toString() {
664681
return 'alert(1);'
665682
}
666683

667-
// Test `valid` and `invalid` `url`s in `prop` on `tagName`.
668-
function testAllURLs(t, tagName, prop, all) {
669-
testURLs(t, tagName, prop, all.valid, true)
670-
testURLs(t, tagName, prop, all.invalid, false)
684+
/**
685+
* Test `valid` and `invalid` `url`s in `prop` on `tagName`.
686+
*
687+
* @param {import('tape').Test} t
688+
* @param {string} tagName
689+
* @param {string} prop
690+
* @param {{valid: Object.<string, string>, invalid: Object.<string, string>}} all
691+
*/
692+
function testAllUrls(t, tagName, prop, all) {
693+
testUrls(t, tagName, prop, all.valid, true)
694+
testUrls(t, tagName, prop, all.invalid, false)
671695
}
672696

673-
// Test `valid` `url`s in `prop` on `tagName`.
674-
function testURLs(t, tagName, prop, urls, valid) {
697+
/**
698+
* Test `valid` `url`s in `prop` on `tagName`.
699+
*
700+
* @param {import('tape').Test} t
701+
* @param {string} tagName
702+
* @param {string} prop
703+
* @param {Object.<string, string>} urls
704+
* @param {boolean} valid
705+
*/
706+
function testUrls(t, tagName, prop, urls, valid) {
675707
Object.keys(urls).forEach(function (name) {
676-
var props = {}
677-
678-
props[prop] = urls[name]
708+
var props = {[prop]: urls[name]}
679709

680710
t.deepEqual(
681711
sanitize(h(tagName, props)),

‎tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["*.js", "lib/**/*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true
14+
}
15+
}

‎types/hast-util-sanitize-tests.ts

-204
This file was deleted.

‎types/index.d.ts

-75
This file was deleted.

‎types/tsconfig.json

-11
This file was deleted.

‎types/tslint.json

-7
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.