Skip to content

Commit 84325bf

Browse files
chore: use JSDoc type imports in more files (#12239)
* chore: use JSDoc type imports in more files * Location_1 -> Location * merge * fix * oops --------- Co-authored-by: Rich Harris <[email protected]>
1 parent bcf23ca commit 84325bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+818
-742
lines changed

packages/svelte/src/animate/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @import { FlipParams, AnimationConfig } from './public.js' */
12
import { cubicOut } from '../easing/index.js';
23

34
/**
@@ -7,8 +8,8 @@ import { cubicOut } from '../easing/index.js';
78
* https://svelte.dev/docs/svelte-animate#flip
89
* @param {Element} node
910
* @param {{ from: DOMRect; to: DOMRect }} fromTo
10-
* @param {import('./public.js').FlipParams} params
11-
* @returns {import('./public.js').AnimationConfig}
11+
* @param {FlipParams} params
12+
* @returns {AnimationConfig}
1213
*/
1314
export function flip(node, { from, to }, params = {}) {
1415
const style = getComputedStyle(node);

packages/svelte/src/compiler/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @import { LegacyRoot } from './types/legacy-nodes.js' */
2+
/** @import { CompileOptions, CompileResult, ValidatedCompileOptions, ModuleCompileOptions, Root } from '#compiler' */
13
import { walk as zimmerframe_walk } from 'zimmerframe';
24
import { convert } from './legacy.js';
35
import { parse as parse_acorn } from './phases/1-parse/acorn.js';
@@ -14,8 +16,8 @@ export { default as preprocess } from './preprocess/index.js';
1416
*
1517
* https://svelte.dev/docs/svelte-compiler#svelte-compile
1618
* @param {string} source The component source code
17-
* @param {import('#compiler').CompileOptions} options The compiler options
18-
* @returns {import('#compiler').CompileResult}
19+
* @param {CompileOptions} options The compiler options
20+
* @returns {CompileResult}
1921
*/
2022
export function compile(source, options) {
2123
const validated = validate_component_options(options, '');
@@ -25,7 +27,7 @@ export function compile(source, options) {
2527

2628
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
2729

28-
/** @type {import('#compiler').ValidatedCompileOptions} */
30+
/** @type {ValidatedCompileOptions} */
2931
const combined_options = {
3032
...validated,
3133
...parsed_options,
@@ -52,8 +54,8 @@ export function compile(source, options) {
5254
*
5355
* https://svelte.dev/docs/svelte-compiler#svelte-compile
5456
* @param {string} source The component source code
55-
* @param {import('#compiler').ModuleCompileOptions} options
56-
* @returns {import('#compiler').CompileResult}
57+
* @param {ModuleCompileOptions} options
58+
* @returns {CompileResult}
5759
*/
5860
export function compileModule(source, options) {
5961
const validated = validate_module_options(options, '');
@@ -73,7 +75,7 @@ export function compileModule(source, options) {
7375
* @overload
7476
* @param {string} source
7577
* @param {{ filename?: string; modern: true }} options
76-
* @returns {import('#compiler').Root}
78+
* @returns {Root}
7779
*/
7880

7981
/**
@@ -86,7 +88,7 @@ export function compileModule(source, options) {
8688
* @overload
8789
* @param {string} source
8890
* @param {{ filename?: string; modern?: false }} [options]
89-
* @returns {import('./types/legacy-nodes.js').LegacyRoot}
91+
* @returns {LegacyRoot}
9092
*/
9193

9294
/**
@@ -98,7 +100,7 @@ export function compileModule(source, options) {
98100
* https://svelte.dev/docs/svelte-compiler#svelte-parse
99101
* @param {string} source
100102
* @param {{ filename?: string; rootDir?: string; modern?: boolean }} [options]
101-
* @returns {import('#compiler').Root | import('./types/legacy-nodes.js').LegacyRoot}
103+
* @returns {Root | LegacyRoot}
102104
*/
103105
export function parse(source, { filename, rootDir, modern } = {}) {
104106
state.reset(source, { filename, rootDir }); // TODO it's weird to require filename/rootDir here. reconsider the API
@@ -109,7 +111,7 @@ export function parse(source, { filename, rootDir, modern } = {}) {
109111

110112
/**
111113
* @param {string} source
112-
* @param {import('#compiler').Root} ast
114+
* @param {Root} ast
113115
* @param {boolean | undefined} modern
114116
*/
115117
function to_public_ast(source, ast, modern) {

packages/svelte/src/compiler/migrate/index.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/** @import { VariableDeclarator, Node, Identifier } from 'estree' */
2+
/** @import { SvelteNode } from '../types/template.js' */
3+
/** @import { Visitors } from 'zimmerframe' */
4+
/** @import { ComponentAnalysis } from '../phases/types.js' */
5+
/** @import { Scope } from '../phases/scope.js' */
6+
/** @import * as Compiler from '#compiler' */
17
import MagicString from 'magic-string';
28
import { walk } from 'zimmerframe';
39
import { parse } from '../phases/1-parse/index.js';
@@ -24,7 +30,7 @@ export function migrate(source) {
2430

2531
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
2632

27-
/** @type {import('#compiler').ValidatedCompileOptions} */
33+
/** @type {Compiler.ValidatedCompileOptions} */
2834
const combined_options = {
2935
...validate_component_options({}, ''),
3036
...parsed_options,
@@ -160,9 +166,9 @@ export function migrate(source) {
160166

161167
/**
162168
* @typedef {{
163-
* scope: import('../phases/scope.js').Scope;
169+
* scope: Scope;
164170
* str: MagicString;
165-
* analysis: import('../phases/types.js').ComponentAnalysis;
171+
* analysis: ComponentAnalysis;
166172
* indent: string;
167173
* props: Array<{ local: string; exported: string; init: string; bindable: boolean; slot_name?: string; optional: boolean; type: string; comment?: string }>;
168174
* props_insertion_point: number;
@@ -175,7 +181,7 @@ export function migrate(source) {
175181
* }} State
176182
*/
177183

178-
/** @type {import('zimmerframe').Visitors<import('../types/template.js').SvelteNode, State>} */
184+
/** @type {Visitors<SvelteNode, State>} */
179185
const instance_script = {
180186
_(node, { state, next }) {
181187
// @ts-expect-error
@@ -281,9 +287,7 @@ const instance_script = {
281287
// }
282288
}
283289

284-
const binding = /** @type {import('#compiler').Binding} */ (
285-
state.scope.get(declarator.id.name)
286-
);
290+
const binding = /** @type {Compiler.Binding} */ (state.scope.get(declarator.id.name));
287291

288292
if (
289293
state.analysis.uses_props &&
@@ -429,7 +433,7 @@ const instance_script = {
429433
}
430434
};
431435

432-
/** @type {import('zimmerframe').Visitors<import('../types/template.js').SvelteNode, State>} */
436+
/** @type {Visitors<SvelteNode, State>} */
433437
const template = {
434438
Identifier(node, { state, path }) {
435439
handle_identifier(node, state, path);
@@ -543,15 +547,15 @@ const template = {
543547
};
544548

545549
/**
546-
* @param {import('estree').VariableDeclarator} declarator
550+
* @param {VariableDeclarator} declarator
547551
* @param {MagicString} str
548-
* @param {import('#compiler').SvelteNode[]} path
552+
* @param {Compiler.SvelteNode[]} path
549553
*/
550554
function extract_type_and_comment(declarator, str, path) {
551555
const parent = path.at(-1);
552556

553557
// Try to find jsdoc above the declaration
554-
let comment_node = /** @type {import('estree').Node} */ (parent)?.leadingComments?.at(-1);
558+
let comment_node = /** @type {Node} */ (parent)?.leadingComments?.at(-1);
555559
if (comment_node?.type !== 'Block') comment_node = undefined;
556560

557561
const comment_start = /** @type {any} */ (comment_node)?.start;
@@ -590,11 +594,11 @@ function extract_type_and_comment(declarator, str, path) {
590594
}
591595

592596
/**
593-
* @param {import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').SvelteWindow | import('#compiler').SvelteDocument | import('#compiler').SvelteBody} element
597+
* @param {Compiler.RegularElement | Compiler.SvelteElement | Compiler.SvelteWindow | Compiler.SvelteDocument | Compiler.SvelteBody} element
594598
* @param {State} state
595599
*/
596600
function handle_events(element, state) {
597-
/** @type {Map<string, import('#compiler').OnDirective[]>} */
601+
/** @type {Map<string, Compiler.OnDirective[]>} */
598602
const handlers = new Map();
599603
for (const attribute of element.attributes) {
600604
if (attribute.type !== 'OnDirective') continue;
@@ -805,7 +809,7 @@ function get_indent(state, ...nodes) {
805809
}
806810

807811
/**
808-
* @param {import('#compiler').OnDirective} last
812+
* @param {Compiler.OnDirective} last
809813
* @param {State} state
810814
*/
811815
function generate_event_name(last, state) {
@@ -821,7 +825,7 @@ function generate_event_name(last, state) {
821825
}
822826

823827
/**
824-
* @param {import('estree').Identifier} node
828+
* @param {Identifier} node
825829
* @param {State} state
826830
* @param {any[]} path
827831
*/

packages/svelte/src/compiler/phases/1-parse/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @import { TemplateNode, Fragment, Root, SvelteOptionsRaw } from '#compiler' */
12
// @ts-expect-error acorn type definitions are borked in the release we use
23
import { isIdentifierStart, isIdentifierChar } from 'acorn';
34
import fragment from './state/fragment.js';
@@ -26,13 +27,13 @@ export class Parser {
2627
/** Whether we're parsing in TypeScript mode */
2728
ts = false;
2829

29-
/** @type {import('#compiler').TemplateNode[]} */
30+
/** @type {TemplateNode[]} */
3031
stack = [];
3132

32-
/** @type {import('#compiler').Fragment[]} */
33+
/** @type {Fragment[]} */
3334
fragments = [];
3435

35-
/** @type {import('#compiler').Root} */
36+
/** @type {Root} */
3637
root;
3738

3839
/** @type {Record<string, boolean>} */
@@ -120,9 +121,7 @@ export class Parser {
120121
(thing) => thing.type === 'SvelteOptions'
121122
);
122123
if (options_index !== -1) {
123-
const options = /** @type {import('#compiler').SvelteOptionsRaw} */ (
124-
this.root.fragment.nodes[options_index]
125-
);
124+
const options = /** @type {SvelteOptionsRaw} */ (this.root.fragment.nodes[options_index]);
126125
this.root.fragment.nodes.splice(options_index, 1);
127126
this.root.options = read_options(options);
128127
// We need this for the old AST format
@@ -289,7 +288,7 @@ export class Parser {
289288

290289
/**
291290
* @param {string} template
292-
* @returns {import('#compiler').Root}
291+
* @returns {Root}
293292
*/
294293
export function parse(template) {
295294
const parser = new Parser(template);

packages/svelte/src/compiler/phases/1-parse/read/context.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/** @import { Location } from 'locate-character' */
2+
/** @import { Pattern } from 'estree' */
3+
/** @import { Parser } from '../index.js' */
14
// @ts-expect-error acorn type definitions are borked in the release we use
25
import { isIdentifierStart } from 'acorn';
36
import full_char_code_at from '../utils/full_char_code_at.js';
@@ -13,8 +16,8 @@ import * as e from '../../../errors.js';
1316
import { locator } from '../../../state.js';
1417

1518
/**
16-
* @param {import('../index.js').Parser} parser
17-
* @returns {import('estree').Pattern}
19+
* @param {Parser} parser
20+
* @returns {Pattern}
1821
*/
1922
export default function read_pattern(parser) {
2023
const start = parser.index;
@@ -30,8 +33,8 @@ export default function read_pattern(parser) {
3033
name,
3134
start,
3235
loc: {
33-
start: /** @type {import('locate-character').Location} */ (locator(start)),
34-
end: /** @type {import('locate-character').Location} */ (locator(parser.index))
36+
start: /** @type {Location} */ (locator(start)),
37+
end: /** @type {Location} */ (locator(parser.index))
3538
},
3639
end: parser.index,
3740
typeAnnotation: annotation
@@ -95,7 +98,7 @@ export default function read_pattern(parser) {
9598
}
9699

97100
/**
98-
* @param {import('../index.js').Parser} parser
101+
* @param {Parser} parser
99102
* @returns {any}
100103
*/
101104
function read_type_annotation(parser) {

packages/svelte/src/compiler/phases/1-parse/read/options.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
/** @import { ObjectExpression } from 'estree' */
2+
/** @import { SvelteOptionsRaw, Root, SvelteOptions } from '#compiler' */
13
import { namespace_mathml, namespace_svg } from '../../../../constants.js';
24
import * as e from '../../../errors.js';
35

46
const regex_valid_tag_name = /^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/;
57

68
/**
7-
* @param {import('#compiler').SvelteOptionsRaw} node
8-
* @returns {import('#compiler').Root['options']}
9+
* @param {SvelteOptionsRaw} node
10+
* @returns {Root['options']}
911
*/
1012
export default function read_options(node) {
11-
/** @type {import('#compiler').SvelteOptions} */
13+
/** @type {SvelteOptions} */
1214
const component_options = {
1315
start: node.start,
1416
end: node.end,
@@ -37,7 +39,7 @@ export default function read_options(node) {
3739
break; // eslint doesn't know this is unnecessary
3840
}
3941
case 'customElement': {
40-
/** @type {import('#compiler').SvelteOptions['customElement']} */
42+
/** @type {SvelteOptions['customElement']} */
4143
const ce = { tag: '' };
4244

4345
const { value } = attribute;
@@ -86,8 +88,7 @@ export default function read_options(node) {
8688
e.svelte_options_invalid_customelement_props(attribute);
8789
}
8890
ce.props = {};
89-
for (const property of /** @type {import('estree').ObjectExpression} */ (props)
90-
.properties) {
91+
for (const property of /** @type {ObjectExpression} */ (props).properties) {
9192
if (
9293
property.type !== 'Property' ||
9394
property.computed ||

packages/svelte/src/compiler/phases/1-parse/read/script.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/** @import { Program } from 'estree' */
2+
/** @import { Attribute, SpreadAttribute, Directive, Script } from '#compiler' */
3+
/** @import { Parser } from '../index.js' */
14
import * as acorn from '../acorn.js';
25
import { regex_not_newline_characters } from '../../patterns.js';
36
import * as e from '../../../errors.js';
@@ -29,10 +32,10 @@ function get_context(attributes) {
2932
}
3033

3134
/**
32-
* @param {import('../index.js').Parser} parser
35+
* @param {Parser} parser
3336
* @param {number} start
34-
* @param {Array<import('#compiler').Attribute | import('#compiler').SpreadAttribute | import('#compiler').Directive>} attributes
35-
* @returns {import('#compiler').Script}
37+
* @param {Array<Attribute | SpreadAttribute | Directive>} attributes
38+
* @returns {Script}
3639
*/
3740
export function read_script(parser, start, attributes) {
3841
const script_start = parser.index;
@@ -45,7 +48,7 @@ export function read_script(parser, start, attributes) {
4548
parser.template.slice(0, script_start).replace(regex_not_newline_characters, ' ') + data;
4649
parser.read(regex_starts_with_closing_script_tag);
4750

48-
/** @type {import('estree').Program} */
51+
/** @type {Program} */
4952
let ast;
5053

5154
try {

0 commit comments

Comments
 (0)