diff --git a/.eslintrc.json b/.eslintrc.json index b5f95a7f88..b274c5c0a4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -94,7 +94,6 @@ "@typescript-eslint/prefer-string-starts-ends-with": 2, "@typescript-eslint/prefer-readonly": 2, "@typescript-eslint/prefer-includes": 2, - "@typescript-eslint/no-unnecessary-condition": 0, // TODO "@typescript-eslint/switch-exhaustiveness-check": 2, "@typescript-eslint/prefer-nullish-coalescing": 2, @@ -105,8 +104,7 @@ "files": "*.spec.ts", "extends": "plugin:jest/recommended", "rules": { - "@typescript-eslint/no-explicit-any": 0, - "@typescript-eslint/ban-ts-comment": 0 + "@typescript-eslint/no-explicit-any": 0 } } ] diff --git a/Readme.md b/Readme.md index f431daaea4..449d371684 100644 --- a/Readme.md +++ b/Readme.md @@ -246,6 +246,16 @@ $.prototype.logHtml = function () { $('body').logHtml(); // logs "Hello, world!" to the console ``` +If you're using TypeScript, you should also add a type definition for your new method: + +```ts +declare module 'cheerio' { + interface Cheerio { + logHtml(this: Cheerio): void; + } +} +``` + ### The "DOM Node" object 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: diff --git a/src/cheerio.spec.ts b/src/cheerio.spec.ts index 237725129c..89d6e0b2ca 100644 --- a/src/cheerio.spec.ts +++ b/src/cheerio.spec.ts @@ -6,6 +6,16 @@ import { Cheerio } from './cheerio'; import type { Element } from 'domhandler'; import type { CheerioOptions } from './options'; +declare module '.' { + interface Cheerio { + myPlugin(...args: unknown[]): { + context: Cheerio; + args: unknown[]; + }; + foo(): void; + } +} + // HTML const script = ''; const multiclass = '

Save

'; @@ -355,7 +365,6 @@ describe('cheerio', () => { it('should honor extensions defined on `prototype` property', () => { const $ = cheerio.load('
'); - // @ts-ignore $.prototype.myPlugin = function (...args: unknown[]) { return { context: this, @@ -365,17 +374,13 @@ describe('cheerio', () => { const $div = $('div'); - // @ts-ignore expect(typeof $div.myPlugin).toBe('function'); - // @ts-ignore expect($div.myPlugin().context).toBe($div); - // @ts-ignore expect($div.myPlugin(1, 2, 3).args).toStrictEqual([1, 2, 3]); }); it('should honor extensions defined on `fn` property', () => { const $ = cheerio.load('
'); - // @ts-ignore $.fn.myPlugin = function (...args: unknown[]) { return { context: this, @@ -385,11 +390,8 @@ describe('cheerio', () => { const $div = $('div'); - // @ts-ignore expect(typeof $div.myPlugin).toBe('function'); - // @ts-ignore expect($div.myPlugin().context).toBe($div); - // @ts-ignore expect($div.myPlugin(1, 2, 3).args).toStrictEqual([1, 2, 3]); }); @@ -397,12 +399,10 @@ describe('cheerio', () => { const $a = cheerio.load('
'); const $b = cheerio.load('
'); - // @ts-ignore $a.prototype.foo = function () { /* Ignore */ }; - // @ts-ignore expect($b('div').foo).toBeUndefined(); }); });