Skip to content

Commit fbfdd5c

Browse files
authored
refactor: update jsdoc syntax (#656)
1 parent 69272c8 commit fbfdd5c

29 files changed

+159
-212
lines changed

packages/vite-plugin-svelte/src/handle-hot-update.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { toRollupError } from './utils/error.js';
88
* @param {import('vite').HmrContext} ctx
99
* @param {import('./types/id.d.ts').SvelteRequest} svelteRequest
1010
* @param {import('./utils/vite-plugin-svelte-cache').VitePluginSvelteCache} cache
11-
* @param options {import('./types/options.d.ts').ResolvedOptions}
11+
* @param {import('./types/options.d.ts').ResolvedOptions} options
1212
* @returns {Promise<import('vite').ModuleNode[] | void>}
1313
*/
1414
export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options) {
@@ -71,18 +71,18 @@ export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache,
7171
}
7272

7373
/**
74-
* @param {import('./types/compile.d.ts').Code=} prev
75-
* @param {import('./types/compile.d.ts').Code=} next
74+
* @param {import('./types/compile.d.ts').Code} [prev]
75+
* @param {import('./types/compile.d.ts').Code} [next]
7676
* @returns {boolean}
7777
*/
7878
function cssChanged(prev, next) {
7979
return !isCodeEqual(prev?.code, next?.code);
8080
}
8181

8282
/**
83-
* @param {import('./types/compile.d.ts').Code=} prev
84-
* @param {import('./types/compile.d.ts').Code=} next
85-
* @param {string=} filename
83+
* @param {import('./types/compile.d.ts').Code} [prev]
84+
* @param {import('./types/compile.d.ts').Code} [next]
85+
* @param {string} [filename]
8686
* @returns {boolean}
8787
*/
8888
function jsChanged(prev, next, filename) {
@@ -102,8 +102,8 @@ function jsChanged(prev, next, filename) {
102102
}
103103

104104
/**
105-
* @param {string=} prev
106-
* @param {string=} next
105+
* @param {string} [prev]
106+
* @param {string} [next]
107107
* @returns {boolean}
108108
*/
109109
function isCodeEqual(prev, next) {
@@ -121,7 +121,8 @@ function isCodeEqual(prev, next) {
121121
*
122122
* 1) add_location() calls. These add location metadata to elements, only used by some dev tools
123123
* 2) ... maybe more (or less) in the future
124-
* @param {string=} code
124+
*
125+
* @param {string} [code]
125126
* @returns {string | undefined}
126127
*/
127128
function normalizeJsCode(code) {

packages/vite-plugin-svelte/src/index.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { InlineConfig, ResolvedConfig, UserConfig, Plugin } from 'vite';
2-
3-
import { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
4-
5-
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
6-
1+
import type { InlineConfig, ResolvedConfig, UserConfig, Plugin } from 'vite';
2+
import type { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
3+
import type { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
74
import type { Options as InspectorOptions } from '@sveltejs/vite-plugin-svelte-inspector';
85

96
type Options = Omit<SvelteOptions, 'vitePlugin'> & PluginOptionsInline;
7+
108
interface PluginOptionsInline extends PluginOptions {
119
/**
1210
* Path to a svelte config file, either absolute or relative to Vite root
@@ -17,6 +15,7 @@ interface PluginOptionsInline extends PluginOptions {
1715
*/
1816
configFile?: string | false;
1917
}
18+
2019
interface PluginOptions {
2120
/**
2221
* A `picomatch` pattern, or array of patterns, which specifies the files the plugin should
@@ -100,6 +99,7 @@ interface PluginOptions {
10099
*/
101100
experimental?: ExperimentalOptions;
102101
}
102+
103103
interface SvelteOptions {
104104
/**
105105
* A list of file extensions to be compiled by Svelte
@@ -130,6 +130,7 @@ interface SvelteOptions {
130130
*/
131131
vitePlugin?: PluginOptions;
132132
}
133+
133134
/**
134135
* These options are considered experimental and breaking changes to them can occur in any release
135136
*/
@@ -170,6 +171,7 @@ interface ExperimentalOptions {
170171
*/
171172
disableSvelteResolveWarnings?: boolean;
172173
}
174+
173175
type ModuleFormat = NonNullable<CompileOptions['format']>;
174176
type CssHashGetter = NonNullable<CompileOptions['cssHash']>;
175177
type Arrayable<T> = T | T[];

packages/vite-plugin-svelte/src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ import { FAQ_LINK_CONFLICTS_IN_SVELTE_RESOLVE } from './utils/constants.js';
2929
const isVite4_0 = viteVersion.startsWith('4.0');
3030
const isSvelte3 = svelteVersion.startsWith('3');
3131

32-
/**
33-
*
34-
* @param {Partial<import('./index.d.ts').Options>=} inlineOptions
35-
* @returns {import('vite').Plugin[]}
36-
*/
32+
/** @type {import('./index.d.ts').svelte} */
3733
export function svelte(inlineOptions) {
3834
if (process.env.DEBUG != null) {
3935
log.setLevel('debug');

packages/vite-plugin-svelte/src/preprocess.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
22
import { mapToRelative, removeLangSuffix } from './utils/sourcemaps.js';
33

4+
/**
5+
* @typedef {(code: string, filename: string) => Promise<{ code: string; map?: any; deps?: Set<string> }>} CssTransform
6+
*/
7+
48
const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss', 'sss'];
59
const supportedScriptLangs = ['ts'];
610

711
export const lang_sep = '.vite-preprocess.';
812

9-
/**
10-
*
11-
* @param {import('./index.d.ts').VitePreprocessOptions=} opts
12-
* @returns {import('svelte/types/compiler/preprocess').PreprocessorGroup}
13-
*/
13+
/** @type {import('./index.d.ts').vitePreprocess} */
1414
export function vitePreprocess(opts) {
15-
/**@type {import('svelte/types/compiler/preprocess').PreprocessorGroup} */
15+
/** @type {import('svelte/types/compiler/preprocess').PreprocessorGroup} */
1616
const preprocessor = {};
1717
if (opts?.script !== false) {
1818
preprocessor.script = viteScript().script;
@@ -25,8 +25,7 @@ export function vitePreprocess(opts) {
2525
}
2626

2727
/**
28-
*
29-
* @returns {{script:import('svelte/types/compiler/preprocess').Preprocessor}}
28+
* @returns {{ script: import('svelte/types/compiler/preprocess').Preprocessor }}
3029
*/
3130
function viteScript() {
3231
return {
@@ -57,12 +56,12 @@ function viteScript() {
5756

5857
/**
5958
* @param {import('vite').ResolvedConfig | import('vite').InlineConfig} config
60-
* @returns {{style:import('svelte/types/compiler/preprocess').Preprocessor}}
59+
* @returns {{ style: import('svelte/types/compiler/preprocess').Preprocessor }}
6160
*/
6261
function viteStyle(config = {}) {
63-
/** @type import('./types/preprocess.d.ts').CssTransform */
62+
/** @type {CssTransform} */
6463
let transform;
65-
/** @type import('svelte/types/compiler/preprocess').Preprocessor */
64+
/** @type {import('svelte/types/compiler/preprocess').Preprocessor} */
6665
const style = async ({ attributes, content, filename = '' }) => {
6766
const lang = /** @type {string} */ (attributes.lang);
6867
if (!supportedStyleLangs.includes(lang)) return;
@@ -102,13 +101,14 @@ function viteStyle(config = {}) {
102101

103102
/**
104103
* @param {import('vite').ResolvedConfig} config
105-
* @returns {import('./types/preprocess.d.ts').CssTransform}
104+
* @returns {CssTransform}
106105
*/
107106
function getCssTransformFn(config) {
108107
return async (code, filename) => {
109108
return preprocessCSS(code, filename, config);
110109
};
111110
}
111+
112112
/**
113113
* @param {any} config
114114
* @returns {config is import('vite').ResolvedConfig}

packages/vite-plugin-svelte/src/types/compile.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Processed } from 'svelte/types/compiler/preprocess';
22
import type { SvelteRequest } from './id.d.ts';
33
import type { ResolvedOptions } from './options.d.ts';
4+
45
export type CompileSvelte = (
56
svelteRequest: SvelteRequest,
67
code: string,

packages/vite-plugin-svelte/src/types/dependencies.d.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/vite-plugin-svelte/src/types/esbuild.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/vite-plugin-svelte/src/types/options.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { CompileOptions } from 'svelte/types/compiler/interfaces';
22
import type { ViteDevServer } from 'vite';
33
import { VitePluginSvelteStats } from '../utils/vite-plugin-svelte-stats.js';
4-
5-
import type { Options as InspectorOptions } from '@sveltejs/vite-plugin-svelte-inspector';
64
import type { Options } from '../index.d.ts';
75

86
export interface PreResolvedOptions extends Options {

packages/vite-plugin-svelte/src/types/preprocess.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/vite-plugin-svelte/src/types/sourcemaps.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/vite-plugin-svelte/src/types/vite-plugin-svelte-cache.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/vite-plugin-svelte/src/types/vite-plugin-svelte-stats.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ export interface PackageStats {
2626

2727
export interface CollectionOptions {
2828
logInProgress: (collection: StatCollection, now: number) => boolean;
29-
3029
logResult: (collection: StatCollection) => boolean;
3130
}

packages/vite-plugin-svelte/src/utils/compile.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import { mapToRelative } from './sourcemaps.js';
1010
const scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
1111

1212
/**
13-
*
14-
* @param {Function=} makeHot
13+
* @param {Function} [makeHot]
1514
* @returns {import('../types/compile.d.ts').CompileSvelte}
1615
*/
1716
export const _createCompileSvelte = (makeHot) => {
1817
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined} */
1918
let stats;
2019
const devStylePreprocessor = createInjectScopeEverythingRulePreprocessorGroup();
21-
/**@type {import('../types/compile.d.ts').CompileSvelte} */
20+
/** @type {import('../types/compile.d.ts').CompileSvelte} */
2221
return async function compileSvelte(svelteRequest, code, options) {
2322
const { filename, normalizedFilename, cssId, ssr, raw } = svelteRequest;
2423
const { emitCss = true } = options;
@@ -95,8 +94,8 @@ export const _createCompileSvelte = (makeHot) => {
9594
mapToRelative(preprocessed?.map, filename);
9695
}
9796
if (raw && svelteRequest.query.type === 'preprocessed') {
98-
// shortcut
99-
return /**@type {import('../types/compile.d.ts').CompileData} */ {
97+
// @ts-expect-error shortcut
98+
return /** @type {import('../types/compile.d.ts').CompileData} */ {
10099
preprocessed: preprocessed ?? { code }
101100
};
102101
}
@@ -168,10 +167,10 @@ export const _createCompileSvelte = (makeHot) => {
168167
};
169168
};
170169
};
170+
171171
/**
172-
*
173172
* @param {import('../types/options.d.ts').ResolvedOptions} options
174-
* @returns {Function|undefined}
173+
* @returns {Function | undefined}
175174
*/
176175
function buildMakeHot(options) {
177176
const needsMakeHot = options.hot !== false && options.isServe && !options.isProduction;
@@ -184,12 +183,12 @@ function buildMakeHot(options) {
184183
walk,
185184
hotApi,
186185
adapter,
187-
hotOptions: { noOverlay: true, .../** @type {object} */ options.hot }
186+
hotOptions: { noOverlay: true, .../** @type {object} */ (options.hot) }
188187
});
189188
}
190189
}
190+
191191
/**
192-
*
193192
* @param {import('../types/options.d.ts').ResolvedOptions} options
194193
* @returns {import('../types/compile.d.ts').CompileSvelte}
195194
*/

packages/vite-plugin-svelte/src/utils/dependencies.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import path from 'path';
22
import fs from 'fs/promises';
3-
43
import { findDepPkgJsonPath } from 'vitefu';
54

65
/**
7-
*
6+
* @typedef {{
7+
* dir: string;
8+
* pkg: Record<string, any>;
9+
* }} DependencyData
10+
*/
11+
12+
/**
813
* @param {string} dep
914
* @param {string} parent
10-
* @returns {Promise<import('../types/dependencies.d.ts').DependencyData|undefined>}
15+
* @returns {Promise<DependencyData | undefined>}
1116
*/
1217
export async function resolveDependencyData(dep, parent) {
1318
const depDataPath = await findDepPkgJsonPath(dep, parent);

packages/vite-plugin-svelte/src/utils/error.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { buildExtendedLogMessage } from './log.js';
22

33
/**
44
* convert an error thrown by svelte.compile to a RollupError so that vite displays it in a user friendly way
5-
* @param {import('../index.d.ts').Warning & Error} error a svelte compiler error, which is a mix of Warning and an error
5+
* @param {import('svelte/types/compiler/interfaces').Warning & Error} error a svelte compiler error, which is a mix of Warning and an error
66
* @param {import('../types/options.d.ts').ResolvedOptions} options
77
* @returns {import('rollup').RollupError} the converted error
88
*/
99
export function toRollupError(error, options) {
1010
const { filename, frame, start, code, name, stack } = error;
11-
/** @type {import('rollup').RollupError} */
11+
/** @type {import('rollup').RollupError} */
1212
const rollupError = {
1313
name, // needed otherwise sveltekit coalesce_to_error turns it into a string
1414
id: filename,
@@ -29,7 +29,7 @@ export function toRollupError(error, options) {
2929

3030
/**
3131
* convert an error thrown by svelte.compile to an esbuild PartialMessage
32-
* @param {import('../index.d.ts').Warning & Error} error a svelte compiler error, which is a mix of Warning and an error
32+
* @param {import('svelte/types/compiler/interfaces').Warning & Error} error a svelte compiler error, which is a mix of Warning and an error
3333
* @param {import('../types/options.d.ts').ResolvedOptions} options
3434
* @returns {import('esbuild').PartialMessage} the converted error
3535
*/
@@ -57,7 +57,7 @@ export function toESBuildError(error, options) {
5757
* extract line with number from codeframe
5858
*
5959
* @param {number} lineNo
60-
* @param {string=} frame
60+
* @param {string} [frame]
6161
* @returns {string}
6262
*/
6363
function lineFromFrame(lineNo, frame) {
@@ -88,8 +88,8 @@ function lineFromFrame(lineNo, frame) {
8888
* 3 | baz
8989
* ```
9090
* @see https://github.com/vitejs/vite/blob/96591bf9989529de839ba89958755eafe4c445ae/packages/vite/src/client/overlay.ts#L116
91-
* @param {string=} frame
92-
* @returns string
91+
* @param {string} [frame]
92+
* @returns {string}
9393
*/
9494
function formatFrameForVite(frame) {
9595
if (!frame) {

0 commit comments

Comments
 (0)