From 7719c1d5df8e09c1c6e6bbabd399ec966b6d74f5 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 28 Jun 2024 15:49:21 +0200 Subject: [PATCH] Refactor types --- .gitignore | 2 + index.d.ts | 4 ++ index.js | 7 +- lib/get-oniguruma.default.js | 2 +- lib/get-oniguruma.fs.js | 2 +- lib/index.js | 92 +-------------------------- lib/types.d.ts | 120 +++++++++++++++++++++++++++++++++++ lib/types.js | 2 + package.json | 24 ++++++- script/build.js | 2 +- tsconfig.json | 2 +- 11 files changed, 157 insertions(+), 102 deletions(-) create mode 100644 index.d.ts create mode 100644 lib/types.d.ts create mode 100644 lib/types.js diff --git a/.gitignore b/.gitignore index e1d8882..a7cae40 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ script/grammars.yml *.log .DS_Store yarn.lock +!/lib/types.d.ts +!/index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..c028f49 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,4 @@ +export type {GetOnigurumaUrl, Grammar, Options} from './lib/types.js' +export {grammars as all} from './lib/all.js' +export {grammars as common} from './lib/common.js' +export {createStarryNight} from './lib/index.js' diff --git a/index.js b/index.js index ef7147a..25071de 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,4 @@ -/** - * @typedef {import('./lib/index.js').GetOnigurumaUrl} GetOnigurumaUrl - * @typedef {import('./lib/index.js').Grammar} Grammar - * @typedef {import('./lib/index.js').Options} Options - */ - +// Note: types exposed from `index.d.ts`. export {grammars as all} from './lib/all.js' export {grammars as common} from './lib/common.js' export {createStarryNight} from './lib/index.js' diff --git a/lib/get-oniguruma.default.js b/lib/get-oniguruma.default.js index 4b571b0..da70ef2 100644 --- a/lib/get-oniguruma.default.js +++ b/lib/get-oniguruma.default.js @@ -2,7 +2,7 @@ /* eslint-env browser */ /** - * @import {Options} from '@wooorm/starry-night' + * @import {Options} from './types.js' */ /** diff --git a/lib/get-oniguruma.fs.js b/lib/get-oniguruma.fs.js index 4960c45..c49eaa6 100644 --- a/lib/get-oniguruma.fs.js +++ b/lib/get-oniguruma.fs.js @@ -1,5 +1,5 @@ /** - * @import {Options} from '@wooorm/starry-night' + * @import {Options} from './types.js' */ import fs from 'node:fs/promises' diff --git a/lib/index.js b/lib/index.js index 9bb1c8c..f4476e5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,97 +1,7 @@ /** * @import {Root} from 'hast' * @import {IGrammar, IRawGrammar} from 'vscode-textmate' - */ - -/** - * @typedef {Record} Captures - * Rule `captures` field. - * - * @callback GetOnigurumaUrl - * Get a URL to the oniguruma WASM. - * - * > 👉 **Note**: this must currently result in a version 1 URL of - * > `onig.wasm` from `vscode-oniguruma`. - * - * > ⚠️ **Danger**: when you use this functionality, your project might break at - * > any time (when reinstalling dependencies), except when you make sure that - * > the WASM binary you load manually is what our internally used - * > `vscode-oniguruma` dependency expects. - * > To solve this, you could for example use an npm script called `dependencies` - * > (which runs everytime `node_modules` is changed) which copies - * > `vscode-oniguruma/release/onig.wasm` to the place you want to host it. - * @returns {Promise> | Readonly} - * URL object to a WASM binary. - * @example - * ```js - * import {common, createStarryNight} from '@wooorm/starry-night' - * - * const starryNight = await createStarryNight(common, { - * getOnigurumaUrlFetch() { - * return new URL('/onig.wasm', window.location.href); - * } - * }) - * ``` - * - * @typedef Grammar - * TextMate grammar with some extra info. - * @property {Array} [dependencies] - * List of scopes that are needed for this grammar to work (optional, - * example: `['source.tsx']`). - * @property {Array} extensions - * List of extensions (example: `['.mdx']`). - * @property {Array} [extensionsWithDot] - * List of extensions that only match if used w/ a dot (optional, example: - * `['.php']`). - * @property {Record} [injections] - * TextMate injections (optional). - * @property {Array} names - * List of names (example: `['mdx']`). - * @property {Array} patterns - * TextMate patterns. - * @property {Record} [repository] - * TextMate repository (optional). - * @property {string} scopeName - * Scope (example: `source.mdx`). - * - * @typedef Options - * Configuration (optional). - * @property {GetOnigurumaUrl | null | undefined} [getOnigurumaUrlFetch] - * Get a URL to the oniguruma WASM, typically used in browsers (optional). - * @property {GetOnigurumaUrl | null | undefined} [getOnigurumaUrlFs] - * Get a URL to the oniguruma WASM, typically used in Node.js (optional). - * - * @typedef {RuleDefinition | RuleInclude | RuleName} Rule - * TextMate rule. - * - * @typedef RuleDefinition - * @property {boolean} [applyEndPatternLast] - * @property {string} [begin] - * @property {Captures} [beginCaptures] - * @property {Captures} [captures] - * @property {string} [contentName] - * @property {string} [end] - * @property {Captures} [endCaptures] - * @property {Record} [injections] - * @property {string} [match] - * @property {string} [name] - * @property {Array} [patterns] - * @property {Record} [repository] - * @property {string} [while] - * @property {Captures} [whileCaptures] - * - * @typedef RuleInclude - * @property {string} [begin] - * @property {string} [end] - * @property {string} include - * @property {string} [match] - * @property {string} [name] - * - * @typedef RuleName - * @property {never} [begin] - * @property {never} [include] - * @property {never} [match] - * @property {string} name + * @import {Grammar, Options} from './types.js' */ import vscodeOniguruma from 'vscode-oniguruma' diff --git a/lib/types.d.ts b/lib/types.d.ts new file mode 100644 index 0000000..b2145be --- /dev/null +++ b/lib/types.d.ts @@ -0,0 +1,120 @@ +/** + * Get a URL to the oniguruma WASM. + * + * > **Note**: this must currently result in a version 1 URL of + * > `onig.wasm` from `vscode-oniguruma`. + * + * > **Danger**: when you use this functionality, your project might break at + * > any time (when reinstalling dependencies), except when you make sure that + * > the WASM binary you load manually is what our internally used + * > `vscode-oniguruma` dependency expects. + * > To solve this, you could for example use an npm script called `dependencies` + * > (which runs everytime `node_modules` is changed) which copies + * > `vscode-oniguruma/release/onig.wasm` to the place you want to host it. + * + * @returns + * URL object to a WASM binary. + * @example + * ```js + * import {common, createStarryNight} from '@wooorm/starry-night' + * + * const starryNight = await createStarryNight(common, { + * getOnigurumaUrlFetch() { + * return new URL('/onig.wasm', window.location.href); + * } + * }) + * ``` + */ +export type GetOnigurumaUrl = () => Promise> | Readonly + +/** + * TextMate grammar with some extra info. + */ +export interface Grammar { + /** + * List of scopes that are needed for this grammar to work (optional, + * example: `['source.tsx']`). + */ + dependencies?: Array + /** + * List of extensions (example: `['.mdx']`). + */ + extensions: Array + /** + * List of extensions that only match if used w/ a dot (optional, example: + * `['.php']`). + */ + extensionsWithDot?: Array + /** + * TextMate injections (optional). + */ + injections?: Record + /** + * List of names (example: `['mdx']`). + */ + names: Array + /** + * TextMate patterns. + */ + patterns: Array + /** + * TextMate repository (optional). + */ + repository?: Record + /** + * Scope (example: `source.mdx`). + */ + scopeName: string +} + +/** + * Configuration (optional). + */ +export interface Options { + /** + * Get a URL to the oniguruma WASM, typically used in browsers (optional). + */ + getOnigurumaUrlFetch?: GetOnigurumaUrl | null | undefined + + /** + * Get a URL to the oniguruma WASM, typically used in Node.js (optional). + */ + getOnigurumaUrlFs?: GetOnigurumaUrl | null | undefined +} + +/** + * TextMate rule. + */ +export type Rule = RuleDefinition | RuleInclude | RuleName + +export interface RuleDefinition { + applyEndPatternLast?: boolean + begin?: string + beginCaptures?: Record + captures?: Record + contentName?: string + end?: string + endCaptures?: Record + injections?: Record + match?: string + name?: string + patterns?: Array + repository?: Record + while?: string + whileCaptures?: Record +} + +export interface RuleInclude { + begin?: string + end?: string + include: string + match?: string + name?: string +} + +export interface RuleName { + begin?: never + include?: never + match?: never + name: string +} diff --git a/lib/types.js b/lib/types.js new file mode 100644 index 0000000..c172e67 --- /dev/null +++ b/lib/types.js @@ -0,0 +1,2 @@ +// Note: types exposed from `types.d.ts`. +export {} diff --git a/package.json b/package.json index fb6cd93..6c9513d 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "lang/", "lib/", "style/", - "index.d.ts.map", "index.d.ts", "index.js", "notice" @@ -138,6 +137,29 @@ "lang/" ], "overrides": [ + { + "files": [ + "**/*.d.ts" + ], + "rules": { + "@typescript-eslint/array-type": [ + "error", + { + "default": "generic" + } + ], + "@typescript-eslint/ban-types": [ + "error", + { + "extendDefaults": true + } + ], + "@typescript-eslint/consistent-type-definitions": [ + "error", + "interface" + ] + } + }, { "files": [ "script/**/*.js" diff --git a/script/build.js b/script/build.js index 2c6ddd8..bd49a5d 100644 --- a/script/build.js +++ b/script/build.js @@ -1,5 +1,5 @@ /** - * @import {Grammar, Rule} from '../lib/index.js' + * @import {Grammar, Rule} from '../lib/types.js' */ /** diff --git a/tsconfig.json b/tsconfig.json index 4d9161a..0fac7b4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ "target": "es2022" }, "exclude": ["coverage/", "node_modules/"], - "include": ["**/*.js"] + "include": ["**/*.js", "lib/types.d.ts", "index.d.ts"] }