From ee195af3c6f0b2a83863ea04d52c6885cdd07353 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 11 May 2023 17:46:38 +0200 Subject: [PATCH] Convert src/compiler/utils/extract_svelte_ignore.ts to JavaScript --- src/compiler/utils/error.js | 1 + src/compiler/utils/extract_svelte_ignore.ts | 120 ++++++++++---------- 2 files changed, 64 insertions(+), 57 deletions(-) diff --git a/src/compiler/utils/error.js b/src/compiler/utils/error.js index f4925e781a..466e89f49a 100644 --- a/src/compiler/utils/error.js +++ b/src/compiler/utils/error.js @@ -34,6 +34,7 @@ class CompileError extends Error { * @type {string} */ frame = undefined; + toString() { return `${this.message} (${this.start.line}:${this.start.column})\n${this.frame}`; } diff --git a/src/compiler/utils/extract_svelte_ignore.ts b/src/compiler/utils/extract_svelte_ignore.ts index c108096099..df52dc2371 100644 --- a/src/compiler/utils/extract_svelte_ignore.ts +++ b/src/compiler/utils/extract_svelte_ignore.ts @@ -1,70 +1,76 @@ -import { TemplateNode } from '../interfaces'; -import { flatten } from './flatten'; +import { flatten } from './flatten.js'; import { regex_whitespace } from './patterns'; -import { INode } from '../compile/nodes/interfaces'; - const regex_svelte_ignore = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m; -export function extract_svelte_ignore(text: string): string[] { - const match = regex_svelte_ignore.exec(text); - return match - ? match[1] - .split(regex_whitespace) - .map((x) => x.trim()) - .filter(Boolean) - : []; +/** + * @param {string} text + * @returns {string[]} + */ +export function extract_svelte_ignore(text) { + const match = regex_svelte_ignore.exec(text); + return match + ? match[1] + .split(regex_whitespace) + .map((x) => x.trim()) + .filter(Boolean) + : []; } -export function extract_svelte_ignore_from_comments< - Node extends { leadingComments?: Array<{ value: string }> } ->(node: Node): string[] { - return flatten( - (node.leadingComments || []).map((comment) => extract_svelte_ignore(comment.value)) - ); +/** + * @param {Node} node + * @returns {string[]} + */ +export function extract_svelte_ignore_from_comments(node) { + return flatten((node.leadingComments || []).map((comment) => extract_svelte_ignore(comment.value))); } -export function extract_ignores_above_position( - position: number, - template_nodes: TemplateNode[] -): string[] { - const previous_node_idx = template_nodes.findIndex((child) => child.end === position); - if (previous_node_idx === -1) { - return []; - } - - for (let i = previous_node_idx; i >= 0; i--) { - const node = template_nodes[i]; - if (node.type !== 'Comment' && node.type !== 'Text') { - return []; - } - if (node.type === 'Comment') { - if (node.ignores.length) { - return node.ignores; - } - } - } +/** + * @param {number} position + * @param {TemplateNode[]} template_nodes + * @returns {string[]} + */ +export function extract_ignores_above_position(position, template_nodes) { + const previous_node_idx = template_nodes.findIndex((child) => child.end === position); + if (previous_node_idx === -1) { + return []; + } + for (let i = previous_node_idx; i >= 0; i--) { + const node = template_nodes[i]; + if (node.type !== 'Comment' && node.type !== 'Text') { + return []; + } + if (node.type === 'Comment') { + if (node.ignores.length) { + return node.ignores; + } + } + } + return []; +} - return []; +/** + * @param {INode} node + * @returns {string[]} + */ +export function extract_ignores_above_node(node) { + /** + * This utilizes the fact that node has a prev and a next attribute + * which means that it can find svelte-ignores along + * the nodes on the same level as itself who share the same parent. + */ + let cur_node = node.prev; + while (cur_node) { + if (cur_node.type !== 'Comment' && cur_node.type !== 'Text') { + return []; + } + if (cur_node.type === 'Comment' && cur_node.ignores.length) { + return cur_node.ignores; + } + cur_node = cur_node.prev; + } + return []; } -export function extract_ignores_above_node(node: INode): string[] { - /** - * This utilizes the fact that node has a prev and a next attribute - * which means that it can find svelte-ignores along - * the nodes on the same level as itself who share the same parent. - */ - let cur_node = node.prev; - while (cur_node) { - if (cur_node.type !== 'Comment' && cur_node.type !== 'Text') { - return []; - } - if (cur_node.type === 'Comment' && cur_node.ignores.length) { - return cur_node.ignores; - } - cur_node = cur_node.prev; - } - return []; -}