From faa0631a05bea924248aee2ec3653682800d57cf Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 12 Jul 2021 18:40:05 +0200 Subject: [PATCH] move svelte-ignore comment extraction to parser phase --- src/compiler/compile/Component.ts | 2 +- src/compiler/compile/css/Stylesheet.ts | 2 +- src/compiler/compile/nodes/Comment.ts | 3 +-- src/compiler/compile/render_dom/index.ts | 2 +- src/compiler/parse/state/tag.ts | 4 +++- .../{compile => }/utils/extract_svelte_ignore.ts | 7 +++---- src/compiler/{compile => }/utils/flatten.ts | 0 .../samples/comment-with-ignores/input.svelte | 1 + .../samples/comment-with-ignores/output.json | 16 ++++++++++++++++ test/parser/samples/comment/output.json | 3 ++- 10 files changed, 29 insertions(+), 11 deletions(-) rename src/compiler/{compile => }/utils/extract_svelte_ignore.ts (86%) rename src/compiler/{compile => }/utils/flatten.ts (100%) create mode 100644 test/parser/samples/comment-with-ignores/input.svelte create mode 100644 test/parser/samples/comment-with-ignores/output.json diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index fdc66261a9..f3a012341d 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -36,7 +36,7 @@ import { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping/dist/types import { clone } from '../utils/clone'; import compiler_warnings from './compiler_warnings'; import compiler_errors from './compiler_errors'; -import { extract_ignores_above_position, extract_svelte_ignore_from_comments } from './utils/extract_svelte_ignore'; +import { extract_ignores_above_position, extract_svelte_ignore_from_comments } from '../utils/extract_svelte_ignore'; interface ComponentOptions { namespace?: string; diff --git a/src/compiler/compile/css/Stylesheet.ts b/src/compiler/compile/css/Stylesheet.ts index f9420f0a5f..ed1682378c 100644 --- a/src/compiler/compile/css/Stylesheet.ts +++ b/src/compiler/compile/css/Stylesheet.ts @@ -7,7 +7,7 @@ import Component from '../Component'; import { CssNode } from './interfaces'; import hash from '../utils/hash'; import compiler_warnings from '../compiler_warnings'; -import { extract_ignores_above_position } from '../utils/extract_svelte_ignore'; +import { extract_ignores_above_position } from '../../utils/extract_svelte_ignore'; function remove_css_prefix(name: string): string { return name.replace(/^-((webkit)|(moz)|(o)|(ms))-/, ''); diff --git a/src/compiler/compile/nodes/Comment.ts b/src/compiler/compile/nodes/Comment.ts index a8c40ff675..79580d0563 100644 --- a/src/compiler/compile/nodes/Comment.ts +++ b/src/compiler/compile/nodes/Comment.ts @@ -1,6 +1,5 @@ import { TemplateNode } from '../../interfaces'; import Component from '../Component'; -import { extract_svelte_ignore } from '../utils/extract_svelte_ignore'; import Node from './shared/Node'; import TemplateScope from './shared/TemplateScope'; @@ -12,6 +11,6 @@ export default class Comment extends Node { constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) { super(component, parent, scope, info); this.data = info.data; - this.ignores = extract_svelte_ignore(this.data); + this.ignores = info.ignores; } } diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 23effa1d5f..10eb8276f1 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -10,7 +10,7 @@ import { ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression import { apply_preprocessor_sourcemap } from '../../utils/mapped_code'; import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types'; import { Node as PeriscopicNode } from 'periscopic'; -import { flatten } from '../utils/flatten'; +import { flatten } from '../../utils/flatten'; export default function dom( component: Component, diff --git a/src/compiler/parse/state/tag.ts b/src/compiler/parse/state/tag.ts index 5aacc3e8b2..4b3e1d046f 100644 --- a/src/compiler/parse/state/tag.ts +++ b/src/compiler/parse/state/tag.ts @@ -7,6 +7,7 @@ import { Parser } from '../index'; import { Directive, DirectiveType, TemplateNode, Text } from '../../interfaces'; import fuzzymatch from '../../utils/fuzzymatch'; import list from '../../utils/list'; +import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore'; // eslint-disable-next-line no-useless-escape const valid_tag_name = /^\!?[a-zA-Z]{1,}:?[a-zA-Z0-9\-]*/; @@ -64,7 +65,8 @@ export default function tag(parser: Parser) { start, end: parser.index, type: 'Comment', - data + data, + ignores: extract_svelte_ignore(data) }); return; diff --git a/src/compiler/compile/utils/extract_svelte_ignore.ts b/src/compiler/utils/extract_svelte_ignore.ts similarity index 86% rename from src/compiler/compile/utils/extract_svelte_ignore.ts rename to src/compiler/utils/extract_svelte_ignore.ts index 88e3b859f7..306ba872d6 100644 --- a/src/compiler/compile/utils/extract_svelte_ignore.ts +++ b/src/compiler/utils/extract_svelte_ignore.ts @@ -1,4 +1,4 @@ -import { TemplateNode } from '../../interfaces'; +import { TemplateNode } from '../interfaces'; import { flatten } from './flatten'; const pattern = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m; @@ -24,9 +24,8 @@ export function extract_ignores_above_position(position: number, template_nodes: return []; } if (node.type === 'Comment') { - const ignores = extract_svelte_ignore(node.data || ''); - if (ignores.length) { - return ignores; + if (node.ignores.length) { + return node.ignores; } } } diff --git a/src/compiler/compile/utils/flatten.ts b/src/compiler/utils/flatten.ts similarity index 100% rename from src/compiler/compile/utils/flatten.ts rename to src/compiler/utils/flatten.ts diff --git a/test/parser/samples/comment-with-ignores/input.svelte b/test/parser/samples/comment-with-ignores/input.svelte new file mode 100644 index 0000000000..c4fcec5e50 --- /dev/null +++ b/test/parser/samples/comment-with-ignores/input.svelte @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/parser/samples/comment-with-ignores/output.json b/test/parser/samples/comment-with-ignores/output.json new file mode 100644 index 0000000000..2705ec6658 --- /dev/null +++ b/test/parser/samples/comment-with-ignores/output.json @@ -0,0 +1,16 @@ +{ + "html": { + "start": 0, + "end": 30, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 30, + "type": "Comment", + "data": " svelte-ignore foo bar ", + "ignores": ["foo", "bar"] + } + ] + } +} \ No newline at end of file diff --git a/test/parser/samples/comment/output.json b/test/parser/samples/comment/output.json index 2a57c4fa5d..097fe994cf 100644 --- a/test/parser/samples/comment/output.json +++ b/test/parser/samples/comment/output.json @@ -8,7 +8,8 @@ "start": 0, "end": 18, "type": "Comment", - "data": " a comment " + "data": " a comment ", + "ignores": [] } ] }