Skip to content

Commit

Permalink
Refactor to remove some ts-expect-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 15, 2024
1 parent 79f0306 commit 30d5132
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 27 deletions.
4 changes: 4 additions & 0 deletions packages/retext-dutch/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type {Plugin} from 'unified'
// See `parse-latin`.
type Extension<Node extends Nodes> = (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.
*
Expand Down
15 changes: 10 additions & 5 deletions packages/retext-dutch/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/**
* @import {Root} from 'nlcst'
* @import {Parser, Processor} from 'unified'
* @import {Processor} from 'unified'
*/

import {ParseDutch} from 'parse-dutch'

/**
* Add support for parsing Dutch natural language.
*
* @this {Processor<Root>}
* 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<Root>} */ (this)
const self = this

self.parser = parser

/** @type {Parser<Root>} */
/**
* @param {string} value
* Document.
* @returns {Root}
* Tree.
*/
function parser(value) {
const parser = new ParseDutch()
add(parser.tokenizeParagraphPlugins, self.data('nlcstParagraphExtensions'))
Expand Down
5 changes: 4 additions & 1 deletion packages/retext-dutch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
}
}
],
"prettier": true
"prettier": true,
"rules": {
"unicorn/no-this-assignment": "off"
}
}
}
4 changes: 4 additions & 0 deletions packages/retext-english/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type {Plugin} from 'unified'
// See `parse-latin`.
type Extension<Node extends Nodes> = (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.
*
Expand Down
15 changes: 10 additions & 5 deletions packages/retext-english/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/**
* @import {Root} from 'nlcst'
* @import {Parser, Processor} from 'unified'
* @import {Processor} from 'unified'
*/

import {ParseEnglish} from 'parse-english'

/**
* Add support for parsing English natural language.
*
* @this {Processor<Root>}
* 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<Root>} */ (this)
const self = this

self.parser = parser

/** @type {Parser<Root>} */
/**
* @param {string} value
* Document.
* @returns {Root}
* Tree.
*/
function parser(value) {
const parser = new ParseEnglish()
add(parser.tokenizeParagraphPlugins, self.data('nlcstParagraphExtensions'))
Expand Down
5 changes: 4 additions & 1 deletion packages/retext-english/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
}
}
],
"prettier": true
"prettier": true,
"rules": {
"unicorn/no-this-assignment": "off"
}
}
}
4 changes: 4 additions & 0 deletions packages/retext-latin/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type {Plugin} from 'unified'
// See `parse-latin`.
type Extension<Node extends Nodes> = (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.
*
Expand Down
13 changes: 9 additions & 4 deletions packages/retext-latin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import {ParseLatin} from 'parse-latin'
/**
* Add support for parsing Latin-script natural language.
*
* @this {Processor<Root>}
* 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<Root>} */ (this)
const self = this

self.parser = parser

/** @type {Parser<Root>} */
/**
* @param {string} value
* Document.
* @returns {Root}
* Tree.
*/
function parser(value) {
const parser = new ParseLatin()
add(parser.tokenizeParagraphPlugins, self.data('nlcstParagraphExtensions'))
Expand Down
5 changes: 4 additions & 1 deletion packages/retext-latin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
}
}
],
"prettier": true
"prettier": true,
"rules": {
"unicorn/no-this-assignment": "off"
}
}
}
4 changes: 4 additions & 0 deletions packages/retext-stringify/index.d.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down
20 changes: 10 additions & 10 deletions packages/retext-stringify/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
/**
* @import {Root} from 'nlcst'
* @import {Compiler, Processor} from 'unified'
* @import {Processor} from 'unified'
*/

import {toString} from 'nlcst-to-string'

/**
* Add support for serializing natural language.
*
* @this {Processor<undefined, undefined, undefined, Root, string>}
* Processor.
* @returns {undefined}
* Nothing.
*/
export default function retextStringify() {
// eslint-disable-next-line unicorn/no-this-assignment
const self =
/** @type {Processor<undefined, undefined, undefined, Root, string>} */ (
// @ts-expect-error -- TS in JSDoc doesn’t understand `this`.
this
)

self.compiler = compiler
this.compiler = compiler
}

/** @type {Compiler<Root, string>} */
/**
* @param {Root} tree
* Tree.
* @returns {string}
* Document.
*/
function compiler(tree) {
return toString(tree)
}

0 comments on commit 30d5132

Please sign in to comment.