From e85138f3e3680925c4a46412f2a4810e61a8943e Mon Sep 17 00:00:00 2001 From: Mikhail Korepanov Date: Sat, 23 Mar 2019 19:25:08 +0300 Subject: [PATCH 1/3] Don't remove digits and _ from slot names --- src/utils/names.ts | 4 +- test/js/samples/slot-names/expected.js | 121 ++++++++++++++++++++++++ test/js/samples/slot-names/input.svelte | 5 + 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 test/js/samples/slot-names/expected.js create mode 100644 test/js/samples/slot-names/input.svelte diff --git a/src/utils/names.ts b/src/utils/names.ts index 5047fe850c..317379198f 100644 --- a/src/utils/names.ts +++ b/src/utils/names.ts @@ -111,5 +111,5 @@ export function quote_prop_if_necessary(name: string) { } export function sanitize(name: string) { - return name.replace(/[^a-zA-Z]+/g, '_').replace(/^_/, '').replace(/_$/, ''); -} \ No newline at end of file + return name.replace(/[^a-zA-Z0-9_]+/g, '_').replace(/^_/, '').replace(/_$/, ''); +} diff --git a/test/js/samples/slot-names/expected.js b/test/js/samples/slot-names/expected.js new file mode 100644 index 0000000000..a538888651 --- /dev/null +++ b/test/js/samples/slot-names/expected.js @@ -0,0 +1,121 @@ +/* generated by Svelte vX.Y.Z */ +import { + SvelteComponent as SvelteComponent_1, + append, + create_slot, + detach, + element, + get_slot_changes, + get_slot_context, + init, + insert, + noop, + safe_not_equal, + space +} from "svelte/internal"; + +function create_fragment(ctx) { + var div, t0, t1; + + const header_1_slot_1 = ctx.$$slot_header_1; + const header_1_slot = create_slot(header_1_slot_1, ctx, null); + + const header_2__slot_1 = ctx.$$slot_header_2_; + const header_2__slot = create_slot(header_2__slot_1, ctx, null); + + const header_3_slot_1 = ctx.$$slot_header_3; + const header_3_slot = create_slot(header_3_slot_1, ctx, null); + + return { + c() { + div = element("div"); + + if (header_1_slot) header_1_slot.c(); + t0 = space(); + + if (header_2__slot) header_2__slot.c(); + t1 = space(); + + if (header_3_slot) header_3_slot.c(); + }, + + l(nodes) { + if (header_1_slot) header_1_slot.l(div_nodes); + if (header_2__slot) header_2__slot.l(div_nodes); + if (header_3_slot) header_3_slot.l(div_nodes); + }, + + m(target, anchor) { + insert(target, div, anchor); + + if (header_1_slot) { + header_1_slot.m(div, null); + } + + append(div, t0); + + if (header_2__slot) { + header_2__slot.m(div, null); + } + + append(div, t1); + + if (header_3_slot) { + header_3_slot.m(div, null); + } + }, + + p(changed, ctx) { + if (header_1_slot && header_1_slot.p && changed.$$scope) { + header_1_slot.p(get_slot_changes(header_1_slot_1, ctx, changed,), get_slot_context(header_1_slot_1, ctx, null)); + } + + if (header_2__slot && header_2__slot.p && changed.$$scope) { + header_2__slot.p(get_slot_changes(header_2__slot_1, ctx, changed,), get_slot_context(header_2__slot_1, ctx, null)); + } + + if (header_3_slot && header_3_slot.p && changed.$$scope) { + header_3_slot.p(get_slot_changes(header_3_slot_1, ctx, changed,), get_slot_context(header_3_slot_1, ctx, null)); + } + }, + + i: noop, + o: noop, + + d(detaching) { + if (detaching) { + detach(div); + } + + if (header_1_slot) header_1_slot.d(detaching); + + if (header_2__slot) header_2__slot.d(detaching); + + if (header_3_slot) header_3_slot.d(detaching); + } + }; +} + +function instance($$self, $$props, $$invalidate) { + let { $$slot_header_1, $$slot_header_2_, $$slot_header_3, $$scope } = $$props; + + $$self.$set = $$props => { + if ('$$scope' in $$props) $$invalidate('$$scope', $$scope = $$props.$$scope); + }; + + return { + $$slot_header_1, + $$slot_header_2_, + $$slot_header_3, + $$scope + }; +} + +class SvelteComponent extends SvelteComponent_1 { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal, []); + } +} + +export default SvelteComponent; diff --git a/test/js/samples/slot-names/input.svelte b/test/js/samples/slot-names/input.svelte new file mode 100644 index 0000000000..986fb798f4 --- /dev/null +++ b/test/js/samples/slot-names/input.svelte @@ -0,0 +1,5 @@ +
+ + + +
From 658d2353508ba1d6b79e9e919de01946aff1b8ea Mon Sep 17 00:00:00 2001 From: Mikhail Korepanov Date: Sat, 23 Mar 2019 21:50:11 +0300 Subject: [PATCH 2/3] Add an error for a slot name starts with a digit --- src/compile/nodes/Slot.ts | 9 ++++++++- .../slot-name-starts-with-a-digit/errors.json | 15 +++++++++++++++ .../slot-name-starts-with-a-digit/input.svelte | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/validator/samples/slot-name-starts-with-a-digit/errors.json create mode 100644 test/validator/samples/slot-name-starts-with-a-digit/input.svelte diff --git a/src/compile/nodes/Slot.ts b/src/compile/nodes/Slot.ts index 0a4f4af848..fe9e1ff0e8 100644 --- a/src/compile/nodes/Slot.ts +++ b/src/compile/nodes/Slot.ts @@ -34,6 +34,13 @@ export default class Slot extends Element { message: `default is a reserved word — it cannot be used as a slot name` }); } + + if (/^[0-9]/.test(slot_name)) { + component.error(attr, { + code: `invalid-slot-name`, + message: ` name cannot start with a digit` + }); + } } // TODO should duplicate slots be disallowed? Feels like it's more likely to be a @@ -70,4 +77,4 @@ export default class Slot extends Element { return null; } -} \ No newline at end of file +} diff --git a/test/validator/samples/slot-name-starts-with-a-digit/errors.json b/test/validator/samples/slot-name-starts-with-a-digit/errors.json new file mode 100644 index 0000000000..a898e6e282 --- /dev/null +++ b/test/validator/samples/slot-name-starts-with-a-digit/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "invalid-slot-name", + "message": " name cannot start with a digit", + "start": { + "line": 2, + "column": 7, + "character": 13 + }, + "end": { + "line": 2, + "column": 21, + "character": 27 + }, + "pos": 13 +}] diff --git a/test/validator/samples/slot-name-starts-with-a-digit/input.svelte b/test/validator/samples/slot-name-starts-with-a-digit/input.svelte new file mode 100644 index 0000000000..ae9ede75ba --- /dev/null +++ b/test/validator/samples/slot-name-starts-with-a-digit/input.svelte @@ -0,0 +1,3 @@ +
+ +
From 0ab7077d40bd7fa947bfa0620480b3e7bc36c8cc Mon Sep 17 00:00:00 2001 From: Mikhail Korepanov Date: Mon, 25 Mar 2019 16:54:14 +0300 Subject: [PATCH 3/3] Move slot names test to runtime. Allow slot names that start with a digit --- src/compile/nodes/Slot.ts | 9 +- src/utils/names.ts | 6 +- test/js/samples/slot-names/expected.js | 121 ------------------ test/js/samples/slot-names/input.svelte | 5 - test/runtime/samples/slot-names/Nested.svelte | 6 + test/runtime/samples/slot-names/_config.js | 10 ++ test/runtime/samples/slot-names/main.svelte | 10 ++ .../slot-name-starts-with-a-digit/errors.json | 15 --- .../input.svelte | 3 - 9 files changed, 32 insertions(+), 153 deletions(-) delete mode 100644 test/js/samples/slot-names/expected.js delete mode 100644 test/js/samples/slot-names/input.svelte create mode 100644 test/runtime/samples/slot-names/Nested.svelte create mode 100644 test/runtime/samples/slot-names/_config.js create mode 100644 test/runtime/samples/slot-names/main.svelte delete mode 100644 test/validator/samples/slot-name-starts-with-a-digit/errors.json delete mode 100644 test/validator/samples/slot-name-starts-with-a-digit/input.svelte diff --git a/src/compile/nodes/Slot.ts b/src/compile/nodes/Slot.ts index fe9e1ff0e8..0a4f4af848 100644 --- a/src/compile/nodes/Slot.ts +++ b/src/compile/nodes/Slot.ts @@ -34,13 +34,6 @@ export default class Slot extends Element { message: `default is a reserved word — it cannot be used as a slot name` }); } - - if (/^[0-9]/.test(slot_name)) { - component.error(attr, { - code: `invalid-slot-name`, - message: ` name cannot start with a digit` - }); - } } // TODO should duplicate slots be disallowed? Feels like it's more likely to be a @@ -77,4 +70,4 @@ export default class Slot extends Element { return null; } -} +} \ No newline at end of file diff --git a/src/utils/names.ts b/src/utils/names.ts index 317379198f..1f0a9cc29e 100644 --- a/src/utils/names.ts +++ b/src/utils/names.ts @@ -111,5 +111,9 @@ export function quote_prop_if_necessary(name: string) { } export function sanitize(name: string) { - return name.replace(/[^a-zA-Z0-9_]+/g, '_').replace(/^_/, '').replace(/_$/, ''); + return name + .replace(/[^a-zA-Z0-9_]+/g, '_') + .replace(/^_/, '') + .replace(/_$/, '') + .replace(/^[0-9]/, '_$&'); } diff --git a/test/js/samples/slot-names/expected.js b/test/js/samples/slot-names/expected.js deleted file mode 100644 index a538888651..0000000000 --- a/test/js/samples/slot-names/expected.js +++ /dev/null @@ -1,121 +0,0 @@ -/* generated by Svelte vX.Y.Z */ -import { - SvelteComponent as SvelteComponent_1, - append, - create_slot, - detach, - element, - get_slot_changes, - get_slot_context, - init, - insert, - noop, - safe_not_equal, - space -} from "svelte/internal"; - -function create_fragment(ctx) { - var div, t0, t1; - - const header_1_slot_1 = ctx.$$slot_header_1; - const header_1_slot = create_slot(header_1_slot_1, ctx, null); - - const header_2__slot_1 = ctx.$$slot_header_2_; - const header_2__slot = create_slot(header_2__slot_1, ctx, null); - - const header_3_slot_1 = ctx.$$slot_header_3; - const header_3_slot = create_slot(header_3_slot_1, ctx, null); - - return { - c() { - div = element("div"); - - if (header_1_slot) header_1_slot.c(); - t0 = space(); - - if (header_2__slot) header_2__slot.c(); - t1 = space(); - - if (header_3_slot) header_3_slot.c(); - }, - - l(nodes) { - if (header_1_slot) header_1_slot.l(div_nodes); - if (header_2__slot) header_2__slot.l(div_nodes); - if (header_3_slot) header_3_slot.l(div_nodes); - }, - - m(target, anchor) { - insert(target, div, anchor); - - if (header_1_slot) { - header_1_slot.m(div, null); - } - - append(div, t0); - - if (header_2__slot) { - header_2__slot.m(div, null); - } - - append(div, t1); - - if (header_3_slot) { - header_3_slot.m(div, null); - } - }, - - p(changed, ctx) { - if (header_1_slot && header_1_slot.p && changed.$$scope) { - header_1_slot.p(get_slot_changes(header_1_slot_1, ctx, changed,), get_slot_context(header_1_slot_1, ctx, null)); - } - - if (header_2__slot && header_2__slot.p && changed.$$scope) { - header_2__slot.p(get_slot_changes(header_2__slot_1, ctx, changed,), get_slot_context(header_2__slot_1, ctx, null)); - } - - if (header_3_slot && header_3_slot.p && changed.$$scope) { - header_3_slot.p(get_slot_changes(header_3_slot_1, ctx, changed,), get_slot_context(header_3_slot_1, ctx, null)); - } - }, - - i: noop, - o: noop, - - d(detaching) { - if (detaching) { - detach(div); - } - - if (header_1_slot) header_1_slot.d(detaching); - - if (header_2__slot) header_2__slot.d(detaching); - - if (header_3_slot) header_3_slot.d(detaching); - } - }; -} - -function instance($$self, $$props, $$invalidate) { - let { $$slot_header_1, $$slot_header_2_, $$slot_header_3, $$scope } = $$props; - - $$self.$set = $$props => { - if ('$$scope' in $$props) $$invalidate('$$scope', $$scope = $$props.$$scope); - }; - - return { - $$slot_header_1, - $$slot_header_2_, - $$slot_header_3, - $$scope - }; -} - -class SvelteComponent extends SvelteComponent_1 { - constructor(options) { - super(); - init(this, options, instance, create_fragment, safe_not_equal, []); - } -} - -export default SvelteComponent; diff --git a/test/js/samples/slot-names/input.svelte b/test/js/samples/slot-names/input.svelte deleted file mode 100644 index 986fb798f4..0000000000 --- a/test/js/samples/slot-names/input.svelte +++ /dev/null @@ -1,5 +0,0 @@ -
- - - -
diff --git a/test/runtime/samples/slot-names/Nested.svelte b/test/runtime/samples/slot-names/Nested.svelte new file mode 100644 index 0000000000..70037db27e --- /dev/null +++ b/test/runtime/samples/slot-names/Nested.svelte @@ -0,0 +1,6 @@ +
+ + + + +
diff --git a/test/runtime/samples/slot-names/_config.js b/test/runtime/samples/slot-names/_config.js new file mode 100644 index 0000000000..c94d09edef --- /dev/null +++ b/test/runtime/samples/slot-names/_config.js @@ -0,0 +1,10 @@ +export default { + html: ` +
+

Header 1

+

Header 2

+

Header 3

+

Header 4

+
+ ` +}; diff --git a/test/runtime/samples/slot-names/main.svelte b/test/runtime/samples/slot-names/main.svelte new file mode 100644 index 0000000000..3538bc49e3 --- /dev/null +++ b/test/runtime/samples/slot-names/main.svelte @@ -0,0 +1,10 @@ + + + +

Header 1

+

Header 2

+

Header 3

+

Header 4

+
diff --git a/test/validator/samples/slot-name-starts-with-a-digit/errors.json b/test/validator/samples/slot-name-starts-with-a-digit/errors.json deleted file mode 100644 index a898e6e282..0000000000 --- a/test/validator/samples/slot-name-starts-with-a-digit/errors.json +++ /dev/null @@ -1,15 +0,0 @@ -[{ - "code": "invalid-slot-name", - "message": " name cannot start with a digit", - "start": { - "line": 2, - "column": 7, - "character": 13 - }, - "end": { - "line": 2, - "column": 21, - "character": 27 - }, - "pos": 13 -}] diff --git a/test/validator/samples/slot-name-starts-with-a-digit/input.svelte b/test/validator/samples/slot-name-starts-with-a-digit/input.svelte deleted file mode 100644 index ae9ede75ba..0000000000 --- a/test/validator/samples/slot-name-starts-with-a-digit/input.svelte +++ /dev/null @@ -1,3 +0,0 @@ -
- -