chore: use JSDoc `@import` (#12581)

* chore: use JSDoc @import

* regenerate types
pull/12588/head
Rich Harris 6 months ago committed by GitHub
parent 37f58cf318
commit 621eb76e1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,3 +1,6 @@
/** @import { ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, MemberExpression, Node, Pattern, VariableDeclarator } from 'estree' */
/** @import { Context, Visitor, Visitors } from 'zimmerframe' */
/** @import { AnimateDirective, Binding, DeclarationKind, EachBlock, ElementLike, LetDirective, SvelteNode, TransitionDirective, UseDirective } from '#compiler' */
import is_reference from 'is-reference'; import is_reference from 'is-reference';
import { walk } from 'zimmerframe'; import { walk } from 'zimmerframe';
import { is_element_node } from './nodes.js'; import { is_element_node } from './nodes.js';
@ -30,20 +33,20 @@ export class Scope {
/** /**
* A map of every identifier declared by this scope, and all the * A map of every identifier declared by this scope, and all the
* identifiers that reference it * identifiers that reference it
* @type {Map<string, import('#compiler').Binding>} * @type {Map<string, Binding>}
*/ */
declarations = new Map(); declarations = new Map();
/** /**
* A map of declarators to the bindings they declare * A map of declarators to the bindings they declare
* @type {Map<import('estree').VariableDeclarator | import('#compiler').LetDirective, import('#compiler').Binding[]>} * @type {Map<VariableDeclarator | LetDirective, Binding[]>}
*/ */
declarators = new Map(); declarators = new Map();
/** /**
* A set of all the names referenced with this scope * A set of all the names referenced with this scope
* useful for generating unique names * useful for generating unique names
* @type {Map<string, { node: import('estree').Identifier; path: import('#compiler').SvelteNode[] }[]>} * @type {Map<string, { node: Identifier; path: SvelteNode[] }[]>}
*/ */
references = new Map(); references = new Map();
@ -67,11 +70,11 @@ export class Scope {
} }
/** /**
* @param {import('estree').Identifier} node * @param {Identifier} node
* @param {import('#compiler').Binding['kind']} kind * @param {Binding['kind']} kind
* @param {import('#compiler').DeclarationKind} declaration_kind * @param {DeclarationKind} declaration_kind
* @param {null | import('estree').Expression | import('estree').FunctionDeclaration | import('estree').ClassDeclaration | import('estree').ImportDeclaration | import('../types/template.js').EachBlock} initial * @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | EachBlock} initial
* @returns {import('#compiler').Binding} * @returns {Binding}
*/ */
declare(node, kind, declaration_kind, initial = null) { declare(node, kind, declaration_kind, initial = null) {
if (node.name === '$') { if (node.name === '$') {
@ -103,7 +106,7 @@ export class Scope {
e.declaration_duplicate(node, node.name); e.declaration_duplicate(node, node.name);
} }
/** @type {import('#compiler').Binding} */ /** @type {Binding} */
const binding = { const binding = {
node, node,
references: [], references: [],
@ -157,15 +160,15 @@ export class Scope {
/** /**
* @param {string} name * @param {string} name
* @returns {import('#compiler').Binding | null} * @returns {Binding | null}
*/ */
get(name) { get(name) {
return this.declarations.get(name) ?? this.parent?.get(name) ?? null; return this.declarations.get(name) ?? this.parent?.get(name) ?? null;
} }
/** /**
* @param {import('estree').VariableDeclarator | import('#compiler').LetDirective} node * @param {VariableDeclarator | LetDirective} node
* @returns {import('#compiler').Binding[]} * @returns {Binding[]}
*/ */
get_bindings(node) { get_bindings(node) {
const bindings = this.declarators.get(node); const bindings = this.declarators.get(node);
@ -184,8 +187,8 @@ export class Scope {
} }
/** /**
* @param {import('estree').Identifier} node * @param {Identifier} node
* @param {import('#compiler').SvelteNode[]} path * @param {SvelteNode[]} path
*/ */
reference(node, path) { reference(node, path) {
path = [...path]; // ensure that mutations to path afterwards don't affect this reference path = [...path]; // ensure that mutations to path afterwards don't affect this reference
@ -231,7 +234,7 @@ export class ScopeRoot {
} }
/** /**
* @param {import('#compiler').SvelteNode} ast * @param {SvelteNode} ast
* @param {ScopeRoot} root * @param {ScopeRoot} root
* @param {boolean} allow_reactive_declarations * @param {boolean} allow_reactive_declarations
* @param {Scope | null} parent * @param {Scope | null} parent
@ -241,7 +244,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
/** /**
* A map of node->associated scope. A node appearing in this map does not necessarily mean that it created a scope * A map of node->associated scope. A node appearing in this map does not necessarily mean that it created a scope
* @type {Map<import('#compiler').SvelteNode, Scope>} * @type {Map<SvelteNode, Scope>}
*/ */
const scopes = new Map(); const scopes = new Map();
const scope = new Scope(root, parent, false); const scope = new Scope(root, parent, false);
@ -250,21 +253,21 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
/** @type {State} */ /** @type {State} */
const state = { scope }; const state = { scope };
/** @type {[Scope, { node: import('estree').Identifier; path: import('#compiler').SvelteNode[] }][]} */ /** @type {[Scope, { node: Identifier; path: SvelteNode[] }][]} */
const references = []; const references = [];
/** @type {[Scope, import('estree').Pattern | import('estree').MemberExpression][]} */ /** @type {[Scope, Pattern | MemberExpression][]} */
const updates = []; const updates = [];
/** /**
* An array of reactive declarations, i.e. the `a` in `$: a = b * 2` * An array of reactive declarations, i.e. the `a` in `$: a = b * 2`
* @type {import('estree').Identifier[]} * @type {Identifier[]}
*/ */
const possible_implicit_declarations = []; const possible_implicit_declarations = [];
/** /**
* @param {Scope} scope * @param {Scope} scope
* @param {import('estree').Pattern[]} params * @param {Pattern[]} params
*/ */
function add_params(scope, params) { function add_params(scope, params) {
for (const param of params) { for (const param of params) {
@ -275,7 +278,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
} }
/** /**
* @type {import('zimmerframe').Visitor<import('estree').Node, State, import('#compiler').SvelteNode>} * @type {Visitor<Node, State, SvelteNode>}
*/ */
const create_block_scope = (node, { state, next }) => { const create_block_scope = (node, { state, next }) => {
const scope = state.scope.child(true); const scope = state.scope.child(true);
@ -285,7 +288,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
}; };
/** /**
* @type {import('zimmerframe').Visitor<import('#compiler').ElementLike, State, import('#compiler').SvelteNode>} * @type {Visitor<ElementLike, State, SvelteNode>}
*/ */
const SvelteFragment = (node, { state, next }) => { const SvelteFragment = (node, { state, next }) => {
const [scope] = analyze_let_directives(node, state.scope); const [scope] = analyze_let_directives(node, state.scope);
@ -294,7 +297,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
}; };
/** /**
* @param {import('#compiler').ElementLike} node * @param {ElementLike} node
* @param {Scope} parent * @param {Scope} parent
*/ */
function analyze_let_directives(node, parent) { function analyze_let_directives(node, parent) {
@ -303,7 +306,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
for (const attribute of node.attributes) { for (const attribute of node.attributes) {
if (attribute.type === 'LetDirective') { if (attribute.type === 'LetDirective') {
/** @type {import('#compiler').Binding[]} */ /** @type {Binding[]} */
const bindings = []; const bindings = [];
scope.declarators.set(attribute, bindings); scope.declarators.set(attribute, bindings);
@ -317,7 +320,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
bindings.push(binding); bindings.push(binding);
} }
} else { } else {
/** @type {import('estree').Identifier} */ /** @type {Identifier} */
const id = { const id = {
name: attribute.name, name: attribute.name,
type: 'Identifier', type: 'Identifier',
@ -336,7 +339,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
} }
/** /**
* @type {import('zimmerframe').Visitor<import('#compiler').AnimateDirective | import('#compiler').TransitionDirective | import('#compiler').UseDirective, State, import('#compiler').SvelteNode>} * @type {Visitor<AnimateDirective | TransitionDirective | UseDirective, State, SvelteNode>}
*/ */
const SvelteDirective = (node, { state, path, visit }) => { const SvelteDirective = (node, { state, path, visit }) => {
state.scope.reference(b.id(node.name.split('.')[0]), path); state.scope.reference(b.id(node.name.split('.')[0]), path);
@ -350,7 +353,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
// references // references
Identifier(node, { path, state }) { Identifier(node, { path, state }) {
const parent = path.at(-1); const parent = path.at(-1);
if (parent && is_reference(node, /** @type {import('estree').Node} */ (parent))) { if (parent && is_reference(node, /** @type {Node} */ (parent))) {
references.push([state.scope, { node, path: path.slice() }]); references.push([state.scope, { node, path: path.slice() }]);
} }
}, },
@ -432,12 +435,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
}, },
UpdateExpression(node, { state, next }) { UpdateExpression(node, { state, next }) {
updates.push([ updates.push([state.scope, /** @type {Identifier | MemberExpression} */ (node.argument)]);
state.scope,
/** @type {import('estree').Identifier | import('estree').MemberExpression} */ (
node.argument
)
]);
next(); next();
}, },
@ -489,7 +487,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
VariableDeclaration(node, { state, path, next }) { VariableDeclaration(node, { state, path, next }) {
const is_parent_const_tag = path.at(-1)?.type === 'ConstTag'; const is_parent_const_tag = path.at(-1)?.type === 'ConstTag';
for (const declarator of node.declarations) { for (const declarator of node.declarations) {
/** @type {import('#compiler').Binding[]} */ /** @type {Binding[]} */
const bindings = []; const bindings = [];
state.scope.declarators.set(declarator, bindings); state.scope.declarators.set(declarator, bindings);
@ -525,7 +523,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
EachBlock(node, { state, visit }) { EachBlock(node, { state, visit }) {
// Array part is still from the scope above // Array part is still from the scope above
/** @type {Set<import('estree').Identifier>} */ /** @type {Set<Identifier>} */
const references_within = new Set(); const references_within = new Set();
const idx = references.length; const idx = references.length;
visit(node.expression); visit(node.expression);
@ -600,7 +598,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
item: node.context.type === 'Identifier' ? node.context : b.id('$$item'), item: node.context.type === 'Identifier' ? node.context : b.id('$$item'),
declarations: scope.declarations, declarations: scope.declarations,
references: [...references_within] references: [...references_within]
.map((id) => /** @type {import('#compiler').Binding} */ (state.scope.get(id.name))) .map((id) => /** @type {Binding} */ (state.scope.get(id.name)))
.filter(Boolean), .filter(Boolean),
is_controlled: false is_controlled: false
}; };
@ -673,9 +671,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
BindDirective(node, context) { BindDirective(node, context) {
updates.push([ updates.push([
context.state.scope, context.state.scope,
/** @type {import('estree').Identifier | import('estree').MemberExpression} */ ( /** @type {Identifier | MemberExpression} */ (node.expression)
node.expression
)
]); ]);
context.next(); context.next();
}, },
@ -718,7 +714,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
object = object.object; object = object.object;
} }
const binding = scope.get(/** @type {import('estree').Identifier} */ (object).name); const binding = scope.get(/** @type {Identifier} */ (object).name);
if (binding) binding.mutated = true; if (binding) binding.mutated = true;
} else { } else {
unwrap_pattern(node).forEach((node) => { unwrap_pattern(node).forEach((node) => {
@ -742,15 +738,15 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
/** /**
* @template {{ scope: Scope }} State * @template {{ scope: Scope }} State
* @param {Map<import('#compiler').SvelteNode, Scope>} scopes * @param {Map<SvelteNode, Scope>} scopes
* @returns {import('zimmerframe').Visitors<import('#compiler').SvelteNode, State>} * @returns {Visitors<SvelteNode, State>}
*/ */
export function set_scope(scopes) { export function set_scope(scopes) {
return { return {
/** /**
* *
* @param {import('#compiler').SvelteNode} node * @param {SvelteNode} node
* @param {import('zimmerframe').Context<import('#compiler').SvelteNode, State>} context * @param {Context<SvelteNode, State>} context
*/ */
_(node, { next, state }) { _(node, { next, state }) {
const scope = scopes.get(node); const scope = scopes.get(node);
@ -761,7 +757,7 @@ export function set_scope(scopes) {
/** /**
* Returns the name of the rune if the given expression is a `CallExpression` using a rune. * Returns the name of the rune if the given expression is a `CallExpression` using a rune.
* @param {import('estree').Node | import('../types/template.js').EachBlock | null | undefined} node * @param {Node | EachBlock | null | undefined} node
* @param {Scope} scope * @param {Scope} scope
* @returns {Runes[number] | null} * @returns {Runes[number] | null}
*/ */

@ -582,7 +582,7 @@ declare module 'svelte/animate' {
} }
declare module 'svelte/compiler' { declare module 'svelte/compiler' {
import type { AssignmentExpression, ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, ArrayExpression, MemberExpression, ObjectExpression, Pattern, ArrowFunctionExpression, VariableDeclaration, VariableDeclarator, FunctionExpression, Node, Program, ChainExpression, SimpleCallExpression } from 'estree'; import type { AssignmentExpression, ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, ArrayExpression, MemberExpression, ObjectExpression, Pattern, Node, VariableDeclarator, ArrowFunctionExpression, VariableDeclaration, FunctionExpression, Program, ChainExpression, SimpleCallExpression } from 'estree';
import type { SourceMap } from 'magic-string'; import type { SourceMap } from 'magic-string';
import type { Context } from 'zimmerframe'; import type { Context } from 'zimmerframe';
import type { Location } from 'locate-character'; import type { Location } from 'locate-character';
@ -1254,13 +1254,13 @@ declare module 'svelte/compiler' {
/** /**
* A map of declarators to the bindings they declare * A map of declarators to the bindings they declare
* */ * */
declarators: Map<import("estree").VariableDeclarator | LetDirective, Binding[]>; declarators: Map<VariableDeclarator | LetDirective, Binding[]>;
/** /**
* A set of all the names referenced with this scope * A set of all the names referenced with this scope
* useful for generating unique names * useful for generating unique names
* */ * */
references: Map<string, { references: Map<string, {
node: import("estree").Identifier; node: Identifier;
path: SvelteNode[]; path: SvelteNode[];
}[]>; }[]>;
/** /**
@ -1269,25 +1269,25 @@ declare module 'svelte/compiler' {
*/ */
function_depth: number; function_depth: number;
declare(node: import("estree").Identifier, kind: Binding["kind"], declaration_kind: DeclarationKind, initial?: null | import("estree").Expression | import("estree").FunctionDeclaration | import("estree").ClassDeclaration | import("estree").ImportDeclaration | EachBlock): Binding; declare(node: Identifier, kind: Binding["kind"], declaration_kind: DeclarationKind, initial?: null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | EachBlock): Binding;
child(porous?: boolean): Scope; child(porous?: boolean): Scope;
generate(preferred_name: string): string; generate(preferred_name: string): string;
get(name: string): Binding | null; get(name: string): Binding | null;
get_bindings(node: import("estree").VariableDeclarator | LetDirective): Binding[]; get_bindings(node: VariableDeclarator | LetDirective): Binding[];
owner(name: string): Scope | null; owner(name: string): Scope | null;
reference(node: import("estree").Identifier, path: SvelteNode[]): void; reference(node: Identifier, path: SvelteNode[]): void;
#private; #private;
} }
class ScopeRoot { class ScopeRoot {
conflicts: Set<string>; conflicts: Set<string>;
unique(preferred_name: string): import("estree").Identifier; unique(preferred_name: string): Identifier;
} }
namespace Css { namespace Css {
export interface BaseNode { export interface BaseNode {

Loading…
Cancel
Save