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

validate transition directives - closes #564

This commit is contained in:
Rich-Harris 2017-05-04 14:06:50 -04:00
parent 90d2e7f883
commit 642b414c9c
19 changed files with 165 additions and 3 deletions

View File

@ -3,6 +3,10 @@ import validateEventHandler from './validateEventHandler.js';
export default function validateElement ( validator, node ) {
const isComponent = node.name === ':Self' || validator.components.has( node.name );
let hasIntro;
let hasOutro;
let hasTransition;
node.attributes.forEach( attribute => {
if ( !isComponent && attribute.type === 'Binding' ) {
const { name } = attribute;
@ -46,9 +50,31 @@ export default function validateElement ( validator, node ) {
}
}
if ( attribute.type === 'EventHandler' ) {
else if ( attribute.type === 'EventHandler' ) {
validateEventHandler( validator, attribute );
}
else if ( attribute.type === 'Transition' ) {
const bidi = attribute.intro && attribute.outro;
if ( hasTransition ) {
if ( bidi ) validator.error( `An element can only have one 'transition' directive`, attribute.start );
validator.error( `An element cannot have both a 'transition' directive and an '${attribute.intro ? 'in' : 'out'}' directive`, attribute.start );
}
if ( ( hasIntro && attribute.intro ) || ( hasOutro && attribute.outro ) ) {
if ( bidi ) validator.error( `An element cannot have both an '${hasIntro ? 'in' : 'out'}' directive and a 'transition' directive`, attribute.start );
validator.error( `An element can only have one '${hasIntro ? 'in' : 'out'}' directive`, attribute.start );
}
if ( attribute.intro ) hasIntro = true;
if ( attribute.outro ) hasOutro = true;
if ( bidi ) hasTransition = true;
if ( !validator.transitions.has( attribute.name ) ) {
validator.error( `Missing transition '${attribute.name}'`, attribute.start );
}
}
});
}

View File

@ -43,7 +43,8 @@ export default function validate ( parsed, source, { onerror, onwarn, name, file
properties: {},
components: new Map(),
methods: new Map(),
helpers: new Map()
helpers: new Map(),
transitions: new Map()
};
try {

View File

@ -63,7 +63,7 @@ export default function validateJs ( validator, js ) {
}
});
[ 'components', 'methods', 'helpers' ].forEach( key => {
[ 'components', 'methods', 'helpers', 'transitions' ].forEach( key => {
if ( validator.properties[ key ] ) {
validator.properties[ key ].value.properties.forEach( prop => {
validator[ key ].set( prop.key.name, prop.value );

View File

@ -0,0 +1,8 @@
[{
"message": "An element cannot have both an 'in' directive and a 'transition' directive",
"loc": {
"line": 1,
"column": 12
},
"pos": 12
}]

View File

@ -0,0 +1,10 @@
<div in:foo transition:bar>...</div>
<script>
export default {
transitions: {
foo,
bar
}
};
</script>

View File

@ -0,0 +1,8 @@
[{
"message": "An element can only have one 'in' directive",
"loc": {
"line": 1,
"column": 12
},
"pos": 12
}]

View File

@ -0,0 +1,10 @@
<div in:foo in:bar>...</div>
<script>
export default {
transitions: {
foo,
bar
}
};
</script>

View File

@ -0,0 +1,8 @@
[{
"message": "An element cannot have both an 'out' directive and a 'transition' directive",
"loc": {
"line": 1,
"column": 13
},
"pos": 13
}]

View File

@ -0,0 +1,10 @@
<div out:foo transition:bar>...</div>
<script>
export default {
transitions: {
foo,
bar
}
};
</script>

View File

@ -0,0 +1,8 @@
[{
"message": "An element can only have one 'out' directive",
"loc": {
"line": 1,
"column": 13
},
"pos": 13
}]

View File

@ -0,0 +1,10 @@
<div out:foo out:bar>...</div>
<script>
export default {
transitions: {
foo,
bar
}
};
</script>

View File

@ -0,0 +1,8 @@
[{
"message": "An element cannot have both a 'transition' directive and an 'in' directive",
"loc": {
"line": 1,
"column": 20
},
"pos": 20
}]

View File

@ -0,0 +1,10 @@
<div transition:foo in:bar>...</div>
<script>
export default {
transitions: {
foo,
bar
}
};
</script>

View File

@ -0,0 +1,8 @@
[{
"message": "An element cannot have both a 'transition' directive and an 'out' directive",
"loc": {
"line": 1,
"column": 20
},
"pos": 20
}]

View File

@ -0,0 +1,10 @@
<div transition:foo out:bar>...</div>
<script>
export default {
transitions: {
foo,
bar
}
};
</script>

View File

@ -0,0 +1,8 @@
[{
"message": "An element can only have one 'transition' directive",
"loc": {
"line": 1,
"column": 20
},
"pos": 20
}]

View File

@ -0,0 +1,10 @@
<div transition:foo transition:bar>...</div>
<script>
export default {
transitions: {
foo,
bar
}
};
</script>

View File

@ -0,0 +1,8 @@
[{
"message": "Missing transition 'foo'",
"loc": {
"line": 1,
"column": 5
},
"pos": 5
}]

View File

@ -0,0 +1 @@
<div in:foo>...</div>