0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

Accept backtick string literals in tag/props properties

This commit is contained in:
Josh Duff 2018-03-28 16:12:38 -05:00
parent e81fb88f41
commit 864fd313bb
10 changed files with 55 additions and 15 deletions

View File

@ -600,7 +600,7 @@ export default class Generator {
}
if (templateProperties.namespace) {
const ns = nodeToString(templateProperties.namespace);
const ns = nodeToString(templateProperties.namespace.value);
this.namespace = namespaces[ns] || ns;
}
@ -619,7 +619,7 @@ export default class Generator {
}
if (templateProperties.props) {
this.props = templateProperties.props.value.elements.map((element: Node) => element.value);
this.props = templateProperties.props.value.elements.map((element: Node) => nodeToString(element));
}
if (templateProperties.setup) {
@ -631,7 +631,7 @@ export default class Generator {
}
if (templateProperties.tag) {
this.tag = templateProperties.tag.value.value;
this.tag = nodeToString(templateProperties.tag.value);
}
if (templateProperties.transitions) {

View File

@ -1,11 +1,11 @@
import { Node } from '../interfaces';
export default function nodeToString(prop: Node) {
if (prop.value.type === 'Literal' && typeof prop.value.value === 'string') {
return prop.value.value;
} else if (prop.value.type === 'TemplateLiteral'
&& prop.value.quasis.length === 1
&& prop.value.expressions.length === 0) {
return prop.value.quasis[0].value.raw;
export default function nodeToString(node: Node) {
if (node.type === 'Literal' && typeof node.value === 'string') {
return node.value;
} else if (node.type === 'TemplateLiteral'
&& node.quasis.length === 1
&& node.expressions.length === 0) {
return node.quasis[0].value.raw;
}
}

View File

@ -78,7 +78,7 @@ export default function validateJs(validator: Validator, js: Node) {
});
if (props.has('namespace')) {
const ns = nodeToString(props.get('namespace'));
const ns = nodeToString(props.get('namespace').value);
validator.namespace = namespaces[ns] || ns;
}

View File

@ -7,7 +7,7 @@ import { Node } from '../../../interfaces';
const valid = new Set(namespaces.validNamespaces);
export default function namespace(validator: Validator, prop: Node) {
const ns = nodeToString(prop);
const ns = nodeToString(prop.value);
if (typeof ns !== 'string') {
validator.error(

View File

@ -1,5 +1,6 @@
import { Validator } from '../../';
import { Node } from '../../../interfaces';
import nodeToString from '../../../utils/nodeToString';
export default function props(validator: Validator, prop: Node) {
if (prop.value.type !== 'ArrayExpression') {
@ -10,7 +11,7 @@ export default function props(validator: Validator, prop: Node) {
}
prop.value.elements.forEach((element: Node) => {
if (element.type !== 'Literal' || typeof element.value !== 'string') {
if (typeof nodeToString(element) !== 'string') {
validator.error(
`'props' must be an array of string literals`,
element

View File

@ -1,15 +1,16 @@
import { Validator } from '../../';
import { Node } from '../../../interfaces';
import nodeToString from '../../../utils/nodeToString';
export default function tag(validator: Validator, prop: Node) {
if (prop.value.type !== 'Literal' || typeof prop.value.value !== 'string') {
const tag = nodeToString(prop.value);
if (typeof tag !== 'string') {
validator.error(
`'tag' must be a string literal`,
prop.value
);
}
const tag = prop.value.value;
if (!/^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/.test(tag)) {
validator.error(
`tag name must be two or more words joined by the '-' character`,

View File

@ -0,0 +1,12 @@
[{
"message": "'props' must be an array expression, if specified",
"loc": {
"line": 5,
"column": 9
},
"end": {
"line": 5,
"column": 11
},
"pos": 49
}]

View File

@ -0,0 +1,7 @@
<div></div>
<script>
export default {
props: {}
};
</script>

View File

@ -0,0 +1,12 @@
[{
"message": "'props' must be an array of string literals",
"loc": {
"line": 5,
"column": 10
},
"end": {
"line": 5,
"column": 12
},
"pos": 50
}]

View File

@ -0,0 +1,7 @@
<div></div>
<script>
export default {
props: [{}]
};
</script>