Convert src/compiler/utils/extract_svelte_ignore.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent 4f64d08a71
commit ee195af3c6

@ -34,6 +34,7 @@ class CompileError extends Error {
* @type {string} * @type {string}
*/ */
frame = undefined; frame = undefined;
toString() { toString() {
return `${this.message} (${this.start.line}:${this.start.column})\n${this.frame}`; return `${this.message} (${this.start.line}:${this.start.column})\n${this.frame}`;
} }

@ -1,11 +1,12 @@
import { TemplateNode } from '../interfaces'; import { flatten } from './flatten.js';
import { flatten } from './flatten';
import { regex_whitespace } from './patterns'; import { regex_whitespace } from './patterns';
import { INode } from '../compile/nodes/interfaces';
const regex_svelte_ignore = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m; const regex_svelte_ignore = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m;
export function extract_svelte_ignore(text: string): string[] { /**
* @param {string} text
* @returns {string[]}
*/
export function extract_svelte_ignore(text) {
const match = regex_svelte_ignore.exec(text); const match = regex_svelte_ignore.exec(text);
return match return match
? match[1] ? match[1]
@ -15,23 +16,24 @@ export function extract_svelte_ignore(text: string): string[] {
: []; : [];
} }
export function extract_svelte_ignore_from_comments< /**
Node extends { leadingComments?: Array<{ value: string }> } * @param {Node} node
>(node: Node): string[] { * @returns {string[]}
return flatten( */
(node.leadingComments || []).map((comment) => extract_svelte_ignore(comment.value)) 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, * @param {number} position
template_nodes: TemplateNode[] * @param {TemplateNode[]} template_nodes
): string[] { * @returns {string[]}
*/
export function extract_ignores_above_position(position, template_nodes) {
const previous_node_idx = template_nodes.findIndex((child) => child.end === position); const previous_node_idx = template_nodes.findIndex((child) => child.end === position);
if (previous_node_idx === -1) { if (previous_node_idx === -1) {
return []; return [];
} }
for (let i = previous_node_idx; i >= 0; i--) { for (let i = previous_node_idx; i >= 0; i--) {
const node = template_nodes[i]; const node = template_nodes[i];
if (node.type !== 'Comment' && node.type !== 'Text') { if (node.type !== 'Comment' && node.type !== 'Text') {
@ -43,11 +45,14 @@ export function extract_ignores_above_position(
} }
} }
} }
return []; return [];
} }
export function extract_ignores_above_node(node: INode): string[] { /**
* @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 * This utilizes the fact that node has a prev and a next attribute
* which means that it can find svelte-ignores along * which means that it can find svelte-ignores along
@ -58,13 +63,14 @@ export function extract_ignores_above_node(node: INode): string[] {
if (cur_node.type !== 'Comment' && cur_node.type !== 'Text') { if (cur_node.type !== 'Comment' && cur_node.type !== 'Text') {
return []; return [];
} }
if (cur_node.type === 'Comment' && cur_node.ignores.length) { if (cur_node.type === 'Comment' && cur_node.ignores.length) {
return cur_node.ignores; return cur_node.ignores;
} }
cur_node = cur_node.prev; cur_node = cur_node.prev;
} }
return []; return [];
} }

Loading…
Cancel
Save