mirror of https://github.com/sveltejs/svelte
parent
49880c0930
commit
dfee586567
@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* @template T
|
||||||
|
* @param {Set<T>} a
|
||||||
|
* @param {Set<T> | T[]} b
|
||||||
|
*/
|
||||||
|
export default function add_to_set(a, b) {
|
||||||
|
// @ts-ignore
|
||||||
|
b.forEach((item) => {
|
||||||
|
a.add(item);
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -1,6 +0,0 @@
|
|||||||
export default function add_to_set<T>(a: Set<T>, b: Set<T> | T[]) {
|
|
||||||
// @ts-ignore
|
|
||||||
b.forEach((item) => {
|
|
||||||
a.add(item);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @param {import("../../interfaces.js").EnableSourcemap} enable_sourcemap
|
||||||
|
* @param {keyof Extract<import("../../interfaces.js").EnableSourcemap, object>} namespace
|
||||||
|
*/
|
||||||
|
export default function check_enable_sourcemap(enable_sourcemap, namespace) {
|
||||||
|
return typeof enable_sourcemap === 'boolean' ? enable_sourcemap : enable_sourcemap[namespace];
|
||||||
|
}
|
||||||
@ -1,8 +0,0 @@
|
|||||||
import { EnableSourcemap } from '../../interfaces';
|
|
||||||
|
|
||||||
export default function check_enable_sourcemap(
|
|
||||||
enable_sourcemap: EnableSourcemap,
|
|
||||||
namespace: keyof Extract<EnableSourcemap, object>
|
|
||||||
) {
|
|
||||||
return typeof enable_sourcemap === 'boolean' ? enable_sourcemap : enable_sourcemap[namespace];
|
|
||||||
}
|
|
||||||
@ -1,5 +1,10 @@
|
|||||||
export default function check_graph_for_cycles(edges: Array<[any, any]>): any[] {
|
/**
|
||||||
const graph: Map<any, any[]> = edges.reduce((g, edge) => {
|
* @param {Array<[any, any]>} edges
|
||||||
|
* @returns {any[]}
|
||||||
|
*/
|
||||||
|
export default function check_graph_for_cycles(edges) {
|
||||||
|
/** @type {Map<any, any[]>} */
|
||||||
|
const graph = edges.reduce((g, edge) => {
|
||||||
const [u, v] = edge;
|
const [u, v] = edge;
|
||||||
if (!g.has(u)) g.set(u, []);
|
if (!g.has(u)) g.set(u, []);
|
||||||
if (!g.has(v)) g.set(v, []);
|
if (!g.has(v)) g.set(v, []);
|
||||||
@ -1,10 +1,10 @@
|
|||||||
import { TemplateLiteral } from 'estree';
|
import { escape_template } from './stringify.js';
|
||||||
import { escape_template } from './stringify';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collapse string literals together
|
* Collapse string literals together
|
||||||
|
* @param {import('estree').TemplateLiteral} literal
|
||||||
*/
|
*/
|
||||||
export function collapse_template_literal(literal: TemplateLiteral) {
|
export function collapse_template_literal(literal) {
|
||||||
if (!literal.quasis.length) return;
|
if (!literal.quasis.length) return;
|
||||||
|
|
||||||
const collapsed_quasis = [];
|
const collapsed_quasis = [];
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* @param {import('estree').Node | void} a
|
||||||
|
* @param {import('estree').Node | void} b
|
||||||
|
*/
|
||||||
|
export function compare_node(a, b) {
|
||||||
|
if (a === b) return true;
|
||||||
|
if (!a || !b) return false;
|
||||||
|
if (a.type !== b.type) return false;
|
||||||
|
switch (a.type) {
|
||||||
|
case 'Identifier':
|
||||||
|
return a.name === /** @type {import('estree').Identifier} */ (b).name;
|
||||||
|
case 'MemberExpression':
|
||||||
|
return (
|
||||||
|
compare_node(a.object, /** @type {import('estree').MemberExpression} */ (b).object) &&
|
||||||
|
compare_node(a.property, /** @type {import('estree').MemberExpression} */ (b).property) &&
|
||||||
|
a.computed === /** @type {import('estree').MemberExpression} */ (b).computed
|
||||||
|
);
|
||||||
|
case 'Literal':
|
||||||
|
return a.value === /** @type {import('estree').Literal} */ (b).value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,19 +0,0 @@
|
|||||||
import { Node, Literal, Identifier, MemberExpression } from 'estree';
|
|
||||||
|
|
||||||
export function compare_node(a: Node | void, b: Node | void) {
|
|
||||||
if (a === b) return true;
|
|
||||||
if (!a || !b) return false;
|
|
||||||
if (a.type !== b.type) return false;
|
|
||||||
switch (a.type) {
|
|
||||||
case 'Identifier':
|
|
||||||
return a.name === (b as Identifier).name;
|
|
||||||
case 'MemberExpression':
|
|
||||||
return (
|
|
||||||
compare_node(a.object, (b as MemberExpression).object) &&
|
|
||||||
compare_node(a.property, (b as MemberExpression).property) &&
|
|
||||||
a.computed === (b as MemberExpression).computed
|
|
||||||
);
|
|
||||||
case 'Literal':
|
|
||||||
return a.value === (b as Literal).value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @param {import('estree').Node} node
|
||||||
|
*/
|
||||||
|
export default function get_object(node) {
|
||||||
|
while (node.type === 'MemberExpression') node = node.object;
|
||||||
|
return /** @type {import('estree').Identifier} */ (node);
|
||||||
|
}
|
||||||
@ -1,6 +0,0 @@
|
|||||||
import { Node, Identifier } from 'estree';
|
|
||||||
|
|
||||||
export default function get_object(node: Node): Identifier {
|
|
||||||
while (node.type === 'MemberExpression') node = node.object;
|
|
||||||
return node as Identifier;
|
|
||||||
}
|
|
||||||
@ -1,8 +1,18 @@
|
|||||||
import { Node } from 'estree';
|
import is_reference from 'is-reference';
|
||||||
import is_reference, { NodeWithPropertyDefinition } from 'is-reference';
|
|
||||||
|
|
||||||
export default function is_used_as_reference(node: Node, parent: Node): boolean {
|
/**
|
||||||
if (!is_reference(node as NodeWithPropertyDefinition, parent as NodeWithPropertyDefinition)) {
|
*
|
||||||
|
* @param {import('estree').Node} node
|
||||||
|
* @param {import('estree').Node} parent
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
export default function is_used_as_reference(node, parent) {
|
||||||
|
if (
|
||||||
|
!is_reference(
|
||||||
|
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (node),
|
||||||
|
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (parent)
|
||||||
|
)
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!parent) {
|
if (!parent) {
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* @param {import('estree').Node} node
|
||||||
|
* @param {import('estree').Node} replacement
|
||||||
|
*/
|
||||||
|
export default function replace_object(node, replacement) {
|
||||||
|
if (node.type === 'Identifier') return replacement;
|
||||||
|
|
||||||
|
const ancestor = node;
|
||||||
|
let parent;
|
||||||
|
while (node.type === 'MemberExpression') {
|
||||||
|
parent = node;
|
||||||
|
node = node.object;
|
||||||
|
}
|
||||||
|
parent.object = /** @type {any} */ (replacement);
|
||||||
|
return ancestor;
|
||||||
|
}
|
||||||
@ -1,14 +0,0 @@
|
|||||||
import { Node } from 'estree';
|
|
||||||
|
|
||||||
export default function replace_object(node: Node, replacement: Node) {
|
|
||||||
if (node.type === 'Identifier') return replacement;
|
|
||||||
|
|
||||||
const ancestor = node;
|
|
||||||
let parent;
|
|
||||||
while (node.type === 'MemberExpression') {
|
|
||||||
parent = node;
|
|
||||||
node = node.object;
|
|
||||||
}
|
|
||||||
parent.object = replacement;
|
|
||||||
return ancestor;
|
|
||||||
}
|
|
||||||
@ -1,5 +1,6 @@
|
|||||||
export const reserved_keywords = new Set(['$$props', '$$restProps', '$$slots']);
|
export const reserved_keywords = new Set(['$$props', '$$restProps', '$$slots']);
|
||||||
|
|
||||||
|
/** @param {string} name */
|
||||||
export function is_reserved_keyword(name) {
|
export function is_reserved_keyword(name) {
|
||||||
return reserved_keywords.has(name);
|
return reserved_keywords.has(name);
|
||||||
}
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
import { Node } from 'estree';
|
|
||||||
import { analyze, Scope, extract_names, extract_identifiers } from 'periscopic';
|
import { analyze, Scope, extract_names, extract_identifiers } from 'periscopic';
|
||||||
|
|
||||||
export function create_scopes(expression: Node) {
|
/**
|
||||||
|
* @param {import('estree').Node} expression
|
||||||
|
*/
|
||||||
|
export function create_scopes(expression) {
|
||||||
return analyze(expression);
|
return analyze(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* @param {string} name
|
||||||
|
*/
|
||||||
|
export function string_to_member_expression(name) {
|
||||||
|
const parts = name.split('.');
|
||||||
|
/** @type {import('estree').MemberExpression | import('estree').Identifier} */
|
||||||
|
let node = {
|
||||||
|
type: 'Identifier',
|
||||||
|
name: parts[0]
|
||||||
|
};
|
||||||
|
for (let i = 1; i < parts.length; i++) {
|
||||||
|
node = /** @type {import('estree').MemberExpression} */ ({
|
||||||
|
type: 'MemberExpression',
|
||||||
|
object: node,
|
||||||
|
property: { type: 'Identifier', name: parts[i] }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
@ -1,17 +0,0 @@
|
|||||||
import { MemberExpression, Identifier } from 'estree';
|
|
||||||
|
|
||||||
export function string_to_member_expression(name: string) {
|
|
||||||
const parts = name.split('.');
|
|
||||||
let node: MemberExpression | Identifier = {
|
|
||||||
type: 'Identifier',
|
|
||||||
name: parts[0]
|
|
||||||
};
|
|
||||||
for (let i = 1; i < parts.length; i++) {
|
|
||||||
node = {
|
|
||||||
type: 'MemberExpression',
|
|
||||||
object: node,
|
|
||||||
property: { type: 'Identifier', name: parts[i] }
|
|
||||||
} as MemberExpression;
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
@ -1,13 +1,19 @@
|
|||||||
import { Literal } from 'estree';
|
/**
|
||||||
export function string_literal(data: string): Literal {
|
* @param {string} data
|
||||||
|
* @returns {import('estree').Literal}
|
||||||
|
*/
|
||||||
|
export function string_literal(data) {
|
||||||
return {
|
return {
|
||||||
type: 'Literal',
|
type: 'Literal',
|
||||||
value: data
|
value: data
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
export function escape(data: string, { only_escape_at_symbol = false } = {}) {
|
* @param {string} data
|
||||||
return data.replace(only_escape_at_symbol ? /@+/g : /(@+|#+)/g, (match: string) => {
|
* @param {{ only_escape_at_symbol?: boolean }} [options]
|
||||||
|
*/
|
||||||
|
export function escape(data, { only_escape_at_symbol = false } = {}) {
|
||||||
|
return data.replace(only_escape_at_symbol ? /@+/g : /(@+|#+)/g, (match) => {
|
||||||
return match + match[0];
|
return match + match[0];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Loading…
Reference in new issue