From 0f26ecf2f21621413cf5dab00016de8b7dace04f Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 8 Aug 2024 14:55:47 +0200 Subject: [PATCH] Refactor to use `@import` --- lib/index.js | 14 ++++---------- readme.md | 27 ++++++++++++++++++--------- test/freeze.js | 9 +++++++-- test/parse.js | 4 ---- test/process-sync.js | 9 +++++++-- test/stringify.js | 2 +- test/util/simple.js | 3 +-- 7 files changed, 38 insertions(+), 30 deletions(-) diff --git a/lib/index.js b/lib/index.js index c415714e..5e06134c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,14 +1,8 @@ /** - * @typedef {import('trough').Pipeline} Pipeline - * - * @typedef {import('unist').Node} Node - * - * @typedef {import('vfile').Compatible} Compatible - * @typedef {import('vfile').Value} Value - * - * @typedef {import('../index.js').CompileResultMap} CompileResultMap - * @typedef {import('../index.js').Data} Data - * @typedef {import('../index.js').Settings} Settings + * @import {Pipeline} from 'trough' + * @import {CompileResultMap, Data, Settings} from 'unified' + * @import {Node} from 'unist' + * @import {Compatible, Value} from 'vfile' */ /** diff --git a/readme.md b/readme.md index 2e91aa3d..d6a75abb 100644 --- a/readme.md +++ b/readme.md @@ -1129,6 +1129,10 @@ See [`Transformer`][api-transformer] for more info. `move.js`: ```js +/** + * @import {Plugin} from 'unified' + */ + /** * @typedef Options * Configuration (required). @@ -1136,7 +1140,7 @@ See [`Transformer`][api-transformer] for more info. * File extension to use (must start with `.`). */ -/** @type {import('unified').Plugin<[Options]>} */ +/** @type {Plugin<[Options]>} */ export function move(options) { if (!options || !options.extname) { throw new Error('Missing `options.extname`') @@ -1229,13 +1233,17 @@ They can contain plugins and settings. `preset.js`: ```js +/** + * @import {Preset} from 'unified' + */ + import remarkCommentConfig from 'remark-comment-config' import remarkLicense from 'remark-license' import remarkPresetLintConsistent from 'remark-preset-lint-consistent' import remarkPresetLintRecommended from 'remark-preset-lint-recommended' import remarkToc from 'remark-toc' -/** @type {import('unified').Preset} */ +/** @type {Preset} */ const preset = { plugins: [ remarkPresetLintRecommended, @@ -1493,8 +1501,9 @@ node types for the syntax trees provided by our packages (as in, ```js /** - * @typedef {import('hast').Root} HastRoot - * @typedef {import('mdast').Root} MdastRoot + * @import {Root as HastRoot} from 'hast' + * @import {Root as MdastRoot} from 'mdast' + * @import {Plugin} from 'unified' */ /** @@ -1505,14 +1514,14 @@ node types for the syntax trees provided by our packages (as in, */ // To type options: -/** @type {import('unified').Plugin<[(Options | null | undefined)?]>} */ +/** @type {Plugin<[(Options | null | undefined)?]>} */ export function myPluginAcceptingOptions(options) { const settings = options || {} // `settings` is now `Options`. } // To type a plugin that works on a certain tree, without options: -/** @type {import('unified').Plugin<[], MdastRoot>} */ +/** @type {Plugin<[], MdastRoot>} */ export function myRemarkPlugin() { return function (tree, file) { // `tree` is `MdastRoot`. @@ -1520,7 +1529,7 @@ export function myRemarkPlugin() { } // To type a plugin that transforms one tree into another: -/** @type {import('unified').Plugin<[], MdastRoot, HastRoot>} */ +/** @type {Plugin<[], MdastRoot, HastRoot>} */ export function remarkRehype() { return function (tree) { // `tree` is `MdastRoot`. @@ -1529,11 +1538,11 @@ export function remarkRehype() { } // To type a plugin that defines a parser: -/** @type {import('unified').Plugin<[], string, MdastRoot>} */ +/** @type {Plugin<[], string, MdastRoot>} */ export function remarkParse(options) {} // To type a plugin that defines a compiler: -/** @type {import('unified').Plugin<[], HastRoot, string>} */ +/** @type {Plugin<[], HastRoot, string>} */ export function rehypeStringify(options) {} ``` diff --git a/test/freeze.js b/test/freeze.js index 76ba6756..56296c87 100644 --- a/test/freeze.js +++ b/test/freeze.js @@ -1,3 +1,8 @@ +/** + * @import {Plugin} from 'unified' + * @import {Node} from 'unist' + */ + import assert from 'node:assert/strict' import test from 'node:test' import {unified} from 'unified' @@ -217,7 +222,7 @@ test('`freeze`', async function (t) { // `this` in JS is buggy in TS. /** - * @type {import('unified').Plugin<[], string, import('unist').Node>} + * @type {Plugin<[], string, Node>} */ function parse() { this.parser = simpleParser @@ -225,7 +230,7 @@ function parse() { // `this` in JS is buggy in TS. /** - * @type {import('unified').Plugin<[], import('unist').Node, string>} + * @type {Plugin<[], Node, string>} */ function compile() { this.compiler = simpleCompiler diff --git a/test/parse.js b/test/parse.js index b3f2a326..2d1d1698 100644 --- a/test/parse.js +++ b/test/parse.js @@ -1,7 +1,3 @@ -/** - * @typedef {import('unist').Node} Node - */ - import assert from 'node:assert/strict' import test from 'node:test' import {unified} from 'unified' diff --git a/test/process-sync.js b/test/process-sync.js index 2712d68d..6fbdc9c8 100644 --- a/test/process-sync.js +++ b/test/process-sync.js @@ -1,3 +1,8 @@ +/** + * @import {Plugin} from 'unified' + * @import {Node} from 'unist' + */ + import assert from 'node:assert/strict' import test from 'node:test' import {unified} from 'unified' @@ -61,7 +66,7 @@ test('`processSync`', async function (t) { // `this` in JS is buggy in TS. /** - * @type {import('unified').Plugin<[], string, import('unist').Node>} + * @type {Plugin<[], string, Node>} */ function parse() { this.parser = simpleParser @@ -69,7 +74,7 @@ function parse() { // `this` in JS is buggy in TS. /** - * @type {import('unified').Plugin<[], import('unist').Node, string>} + * @type {Plugin<[], Node, string>} */ function compile() { this.compiler = simpleCompiler diff --git a/test/stringify.js b/test/stringify.js index d4a022c1..efe6c54b 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -1,5 +1,5 @@ /** - * @typedef {import('unist').Node} Node + * @import {Node} from 'unist' */ import assert from 'node:assert/strict' diff --git a/test/util/simple.js b/test/util/simple.js index f0b60f2f..7aed9f2c 100644 --- a/test/util/simple.js +++ b/test/util/simple.js @@ -1,6 +1,5 @@ /** - * @typedef {import('unist').Node} Node - * @typedef {import('unist').Literal} Literal + * @import {Literal, Node} from 'unist' */ // Make references to the above types visible in VS Code.