Skip to content

Commit 880fd2c

Browse files
fb55n1xx1
andauthored
chore(plugins): Document & use TS type definition (#1915)
As proposed by @n1xx1 in #1778 (comment) Co-authored-by: n1xx1 <[email protected]>
1 parent 58e090a commit 880fd2c

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

.eslintrc.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"@typescript-eslint/prefer-string-starts-ends-with": 2,
9595
"@typescript-eslint/prefer-readonly": 2,
9696
"@typescript-eslint/prefer-includes": 2,
97-
"@typescript-eslint/no-unnecessary-condition": 0, // TODO
9897
"@typescript-eslint/switch-exhaustiveness-check": 2,
9998
"@typescript-eslint/prefer-nullish-coalescing": 2,
10099

@@ -105,8 +104,7 @@
105104
"files": "*.spec.ts",
106105
"extends": "plugin:jest/recommended",
107106
"rules": {
108-
"@typescript-eslint/no-explicit-any": 0,
109-
"@typescript-eslint/ban-ts-comment": 0
107+
"@typescript-eslint/no-explicit-any": 0
110108
}
111109
}
112110
]

Readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ $.prototype.logHtml = function () {
246246
$('body').logHtml(); // logs "Hello, <b>world</b>!" to the console
247247
```
248248

249+
If you're using TypeScript, you should also add a type definition for your new method:
250+
251+
```ts
252+
declare module 'cheerio' {
253+
interface Cheerio<T> {
254+
logHtml(this: Cheerio<T>): void;
255+
}
256+
}
257+
```
258+
249259
### The "DOM Node" object
250260

251261
Cheerio collections are made up of objects that bear some resemblance to [browser-based DOM nodes](https://developer.mozilla.org/en-US/docs/Web/API/Node). You can expect them to define the following properties:

src/cheerio.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import { Cheerio } from './cheerio';
66
import type { Element } from 'domhandler';
77
import type { CheerioOptions } from './options';
88

9+
declare module '.' {
10+
interface Cheerio<T> {
11+
myPlugin(...args: unknown[]): {
12+
context: Cheerio<T>;
13+
args: unknown[];
14+
};
15+
foo(): void;
16+
}
17+
}
18+
919
// HTML
1020
const script = '<script src="script.js" type="text/javascript"></script>';
1121
const multiclass = '<p><a class="btn primary" href="#">Save</a></p>';
@@ -355,7 +365,6 @@ describe('cheerio', () => {
355365
it('should honor extensions defined on `prototype` property', () => {
356366
const $ = cheerio.load('<div>');
357367

358-
// @ts-ignore
359368
$.prototype.myPlugin = function (...args: unknown[]) {
360369
return {
361370
context: this,
@@ -365,17 +374,13 @@ describe('cheerio', () => {
365374

366375
const $div = $('div');
367376

368-
// @ts-ignore
369377
expect(typeof $div.myPlugin).toBe('function');
370-
// @ts-ignore
371378
expect($div.myPlugin().context).toBe($div);
372-
// @ts-ignore
373379
expect($div.myPlugin(1, 2, 3).args).toStrictEqual([1, 2, 3]);
374380
});
375381

376382
it('should honor extensions defined on `fn` property', () => {
377383
const $ = cheerio.load('<div>');
378-
// @ts-ignore
379384
$.fn.myPlugin = function (...args: unknown[]) {
380385
return {
381386
context: this,
@@ -385,24 +390,19 @@ describe('cheerio', () => {
385390

386391
const $div = $('div');
387392

388-
// @ts-ignore
389393
expect(typeof $div.myPlugin).toBe('function');
390-
// @ts-ignore
391394
expect($div.myPlugin().context).toBe($div);
392-
// @ts-ignore
393395
expect($div.myPlugin(1, 2, 3).args).toStrictEqual([1, 2, 3]);
394396
});
395397

396398
it('should isolate extensions between loaded functions', () => {
397399
const $a = cheerio.load('<div>');
398400
const $b = cheerio.load('<div>');
399401

400-
// @ts-ignore
401402
$a.prototype.foo = function () {
402403
/* Ignore */
403404
};
404405

405-
// @ts-ignore
406406
expect($b('div').foo).toBeUndefined();
407407
});
408408
});

0 commit comments

Comments
 (0)