From 30d51329a499c8ce1d65809b39717044494c3638 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 15 Oct 2024 13:53:29 +0200 Subject: [PATCH] Refactor to remove some `ts-expect-error`s --- packages/retext-dutch/index.d.ts | 4 ++++ packages/retext-dutch/lib/index.js | 15 ++++++++++----- packages/retext-dutch/package.json | 5 ++++- packages/retext-english/index.d.ts | 4 ++++ packages/retext-english/lib/index.js | 15 ++++++++++----- packages/retext-english/package.json | 5 ++++- packages/retext-latin/index.d.ts | 4 ++++ packages/retext-latin/lib/index.js | 13 +++++++++---- packages/retext-latin/package.json | 5 ++++- packages/retext-stringify/index.d.ts | 4 ++++ packages/retext-stringify/lib/index.js | 20 ++++++++++---------- 11 files changed, 67 insertions(+), 27 deletions(-) diff --git a/packages/retext-dutch/index.d.ts b/packages/retext-dutch/index.d.ts index 9271915..0274223 100644 --- a/packages/retext-dutch/index.d.ts +++ b/packages/retext-dutch/index.d.ts @@ -4,6 +4,10 @@ import type {Plugin} from 'unified' // See `parse-latin`. type Extension = (node: Node) => undefined | void +// Note: we have to use manual types here, +// instead of getting them from `lib/index.js`, +// because TS generates wrong types for functions that use `this`. +// TS makes them into classes which is incorrect. /** * Add support for parsing Dutch natural language. * diff --git a/packages/retext-dutch/lib/index.js b/packages/retext-dutch/lib/index.js index 0d79b24..3887857 100644 --- a/packages/retext-dutch/lib/index.js +++ b/packages/retext-dutch/lib/index.js @@ -1,6 +1,6 @@ /** * @import {Root} from 'nlcst' - * @import {Parser, Processor} from 'unified' + * @import {Processor} from 'unified' */ import {ParseDutch} from 'parse-dutch' @@ -8,17 +8,22 @@ import {ParseDutch} from 'parse-dutch' /** * Add support for parsing Dutch natural language. * + * @this {Processor} + * Processor. * @returns {undefined} * Nothing. */ export default function retextDutch() { - // @ts-expect-error -- TS in JSDoc doesn’t understand `this`. - // eslint-disable-next-line unicorn/no-this-assignment - const self = /** @type {Processor} */ (this) + const self = this self.parser = parser - /** @type {Parser} */ + /** + * @param {string} value + * Document. + * @returns {Root} + * Tree. + */ function parser(value) { const parser = new ParseDutch() add(parser.tokenizeParagraphPlugins, self.data('nlcstParagraphExtensions')) diff --git a/packages/retext-dutch/package.json b/packages/retext-dutch/package.json index e19b7fb..f7d6de5 100644 --- a/packages/retext-dutch/package.json +++ b/packages/retext-dutch/package.json @@ -61,6 +61,9 @@ } } ], - "prettier": true + "prettier": true, + "rules": { + "unicorn/no-this-assignment": "off" + } } } diff --git a/packages/retext-english/index.d.ts b/packages/retext-english/index.d.ts index 288bc5b..4bc5f69 100644 --- a/packages/retext-english/index.d.ts +++ b/packages/retext-english/index.d.ts @@ -4,6 +4,10 @@ import type {Plugin} from 'unified' // See `parse-latin`. type Extension = (node: Node) => undefined | void +// Note: we have to use manual types here, +// instead of getting them from `lib/index.js`, +// because TS generates wrong types for functions that use `this`. +// TS makes them into classes which is incorrect. /** * Add support for parsing English natural language. * diff --git a/packages/retext-english/lib/index.js b/packages/retext-english/lib/index.js index d0bac77..cd1eff2 100644 --- a/packages/retext-english/lib/index.js +++ b/packages/retext-english/lib/index.js @@ -1,6 +1,6 @@ /** * @import {Root} from 'nlcst' - * @import {Parser, Processor} from 'unified' + * @import {Processor} from 'unified' */ import {ParseEnglish} from 'parse-english' @@ -8,17 +8,22 @@ import {ParseEnglish} from 'parse-english' /** * Add support for parsing English natural language. * + * @this {Processor} + * Processor. * @returns {undefined} * Nothing. */ export default function retextEnglish() { - // @ts-expect-error -- TS in JSDoc doesn’t understand `this`. - // eslint-disable-next-line unicorn/no-this-assignment - const self = /** @type {Processor} */ (this) + const self = this self.parser = parser - /** @type {Parser} */ + /** + * @param {string} value + * Document. + * @returns {Root} + * Tree. + */ function parser(value) { const parser = new ParseEnglish() add(parser.tokenizeParagraphPlugins, self.data('nlcstParagraphExtensions')) diff --git a/packages/retext-english/package.json b/packages/retext-english/package.json index cb056af..f80a551 100644 --- a/packages/retext-english/package.json +++ b/packages/retext-english/package.json @@ -61,6 +61,9 @@ } } ], - "prettier": true + "prettier": true, + "rules": { + "unicorn/no-this-assignment": "off" + } } } diff --git a/packages/retext-latin/index.d.ts b/packages/retext-latin/index.d.ts index 91447b5..45c81fd 100644 --- a/packages/retext-latin/index.d.ts +++ b/packages/retext-latin/index.d.ts @@ -4,6 +4,10 @@ import type {Plugin} from 'unified' // See `parse-latin`. type Extension = (node: Node) => undefined | void +// Note: we have to use manual types here, +// instead of getting them from `lib/index.js`, +// because TS generates wrong types for functions that use `this`. +// TS makes them into classes which is incorrect. /** * Add support for parsing Latin-script natural language. * diff --git a/packages/retext-latin/lib/index.js b/packages/retext-latin/lib/index.js index 2f0a99a..3f35596 100644 --- a/packages/retext-latin/lib/index.js +++ b/packages/retext-latin/lib/index.js @@ -8,17 +8,22 @@ import {ParseLatin} from 'parse-latin' /** * Add support for parsing Latin-script natural language. * + * @this {Processor} + * Processor. * @returns {undefined} * Nothing. */ export default function retextLatin() { - // @ts-expect-error -- TS in JSDoc doesn’t understand `this`. - // eslint-disable-next-line unicorn/no-this-assignment - const self = /** @type {Processor} */ (this) + const self = this self.parser = parser - /** @type {Parser} */ + /** + * @param {string} value + * Document. + * @returns {Root} + * Tree. + */ function parser(value) { const parser = new ParseLatin() add(parser.tokenizeParagraphPlugins, self.data('nlcstParagraphExtensions')) diff --git a/packages/retext-latin/package.json b/packages/retext-latin/package.json index e84c8a9..5792647 100644 --- a/packages/retext-latin/package.json +++ b/packages/retext-latin/package.json @@ -61,6 +61,9 @@ } } ], - "prettier": true + "prettier": true, + "rules": { + "unicorn/no-this-assignment": "off" + } } } diff --git a/packages/retext-stringify/index.d.ts b/packages/retext-stringify/index.d.ts index b831f50..537050a 100644 --- a/packages/retext-stringify/index.d.ts +++ b/packages/retext-stringify/index.d.ts @@ -1,6 +1,10 @@ import type {Root} from 'nlcst' import type {Plugin} from 'unified' +// Note: we have to use manual types here, +// instead of getting them from `lib/index.js`, +// because TS generates wrong types for functions that use `this`. +// TS makes them into classes which is incorrect. /** * Add support for serializing natural language. * diff --git a/packages/retext-stringify/lib/index.js b/packages/retext-stringify/lib/index.js index 9fe8314..243c727 100644 --- a/packages/retext-stringify/lib/index.js +++ b/packages/retext-stringify/lib/index.js @@ -1,6 +1,6 @@ /** * @import {Root} from 'nlcst' - * @import {Compiler, Processor} from 'unified' + * @import {Processor} from 'unified' */ import {toString} from 'nlcst-to-string' @@ -8,21 +8,21 @@ import {toString} from 'nlcst-to-string' /** * Add support for serializing natural language. * + * @this {Processor} + * Processor. * @returns {undefined} * Nothing. */ export default function retextStringify() { - // eslint-disable-next-line unicorn/no-this-assignment - const self = - /** @type {Processor} */ ( - // @ts-expect-error -- TS in JSDoc doesn’t understand `this`. - this - ) - - self.compiler = compiler + this.compiler = compiler } -/** @type {Compiler} */ +/** + * @param {Root} tree + * Tree. + * @returns {string} + * Document. + */ function compiler(tree) { return toString(tree) }