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:
parent
90d2e7f883
commit
642b414c9c
@ -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 );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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 );
|
||||
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "An element cannot have both an 'in' directive and a 'transition' directive",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
},
|
||||
"pos": 12
|
||||
}]
|
@ -0,0 +1,10 @@
|
||||
<div in:foo transition:bar>...</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
transitions: {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
};
|
||||
</script>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "An element can only have one 'in' directive",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
},
|
||||
"pos": 12
|
||||
}]
|
10
test/validator/samples/transition-duplicate-in/input.html
Normal file
10
test/validator/samples/transition-duplicate-in/input.html
Normal file
@ -0,0 +1,10 @@
|
||||
<div in:foo in:bar>...</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
transitions: {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
};
|
||||
</script>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "An element cannot have both an 'out' directive and a 'transition' directive",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
},
|
||||
"pos": 13
|
||||
}]
|
@ -0,0 +1,10 @@
|
||||
<div out:foo transition:bar>...</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
transitions: {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
};
|
||||
</script>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "An element can only have one 'out' directive",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
},
|
||||
"pos": 13
|
||||
}]
|
10
test/validator/samples/transition-duplicate-out/input.html
Normal file
10
test/validator/samples/transition-duplicate-out/input.html
Normal file
@ -0,0 +1,10 @@
|
||||
<div out:foo out:bar>...</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
transitions: {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
};
|
||||
</script>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "An element cannot have both a 'transition' directive and an 'in' directive",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
},
|
||||
"pos": 20
|
||||
}]
|
@ -0,0 +1,10 @@
|
||||
<div transition:foo in:bar>...</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
transitions: {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
};
|
||||
</script>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "An element cannot have both a 'transition' directive and an 'out' directive",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
},
|
||||
"pos": 20
|
||||
}]
|
@ -0,0 +1,10 @@
|
||||
<div transition:foo out:bar>...</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
transitions: {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
};
|
||||
</script>
|
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "An element can only have one 'transition' directive",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
},
|
||||
"pos": 20
|
||||
}]
|
@ -0,0 +1,10 @@
|
||||
<div transition:foo transition:bar>...</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
transitions: {
|
||||
foo,
|
||||
bar
|
||||
}
|
||||
};
|
||||
</script>
|
8
test/validator/samples/transition-missing/errors.json
Normal file
8
test/validator/samples/transition-missing/errors.json
Normal file
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "Missing transition 'foo'",
|
||||
"loc": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"pos": 5
|
||||
}]
|
1
test/validator/samples/transition-missing/input.html
Normal file
1
test/validator/samples/transition-missing/input.html
Normal file
@ -0,0 +1 @@
|
||||
<div in:foo>...</div>
|
Loading…
Reference in New Issue
Block a user