Skip to content

Commit

Permalink
Refactor to use @import
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 8, 2024
1 parent 77364ee commit 0f26ecf
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
14 changes: 4 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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'
*/

/**
Expand Down
27 changes: 18 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1129,14 +1129,18 @@ See [`Transformer`][api-transformer] for more info.
`move.js`:
```js
/**
* @import {Plugin} from 'unified'
*/

/**
* @typedef Options
* Configuration (required).
* @property {string} extname
* 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`')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'
*/

/**
Expand All @@ -1505,22 +1514,22 @@ 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`.
}
}

// 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`.
Expand All @@ -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) {}
```

Expand Down
9 changes: 7 additions & 2 deletions test/freeze.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -217,15 +222,15 @@ 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
}

// `this` in JS is buggy in TS.
/**
* @type {import('unified').Plugin<[], import('unist').Node, string>}
* @type {Plugin<[], Node, string>}
*/
function compile() {
this.compiler = simpleCompiler
Expand Down
4 changes: 0 additions & 4 deletions test/parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @typedef {import('unist').Node} Node
*/

import assert from 'node:assert/strict'
import test from 'node:test'
import {unified} from 'unified'
Expand Down
9 changes: 7 additions & 2 deletions test/process-sync.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -61,15 +66,15 @@ 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
}

// `this` in JS is buggy in TS.
/**
* @type {import('unified').Plugin<[], import('unist').Node, string>}
* @type {Plugin<[], Node, string>}
*/
function compile() {
this.compiler = simpleCompiler
Expand Down
2 changes: 1 addition & 1 deletion test/stringify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('unist').Node} Node
* @import {Node} from 'unist'
*/

import assert from 'node:assert/strict'
Expand Down
3 changes: 1 addition & 2 deletions test/util/simple.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 0f26ecf

Please sign in to comment.