generate more idiomatic code when removing object properties

pull/445/head
Rich-Harris 8 years ago
parent bb5d9cc08b
commit 91bf849cc7

@ -1,9 +1,33 @@
export default function removeObjectKey ( generator, node, key ) { export default function removeObjectKey ( generator, node, key ) {
if ( node.type !== 'ObjectExpression' ) return; if ( node.type !== 'ObjectExpression' ) return;
const properties = node.properties;
const index = properties.findIndex( property => property.key.type === 'Identifier' && property.key.name === key ); let i = node.properties.length;
if ( index === -1 ) return; while ( i-- ) {
const a = properties[ index ].start; const property = node.properties[i];
const b = index < properties.length - 1 ? properties[ index + 1 ].start : node.end - 1; if ( property.key.type === 'Identifier' && property.key.name === key ) {
generator.code.remove( a, b ); let a;
let b;
if ( node.properties.length === 1 ) {
// remove everything, leave {}
a = node.start + 1;
b = node.end - 1;
} else if ( i === 0 ) {
// remove everything before second property, including comments
a = node.start + 1;
while ( /\s/.test( generator.code.original[a] ) ) a += 1;
b = node.properties[i].end;
while ( /[\s,]/.test( generator.code.original[b] ) ) b += 1;
} else {
// remove the end of the previous property to the end of this one
a = node.properties[ i - 1 ].end;
b = property.end;
}
generator.code.remove( a, b );
node.properties.splice( i, 1 );
return;
}
}
} }

@ -1,11 +1,11 @@
<Component value="10px"/> <Component value="10px"/>
<script> <script>
import Component from './Component.html'; import Component from './Component.html';
export default { export default {
components: { components: {
Component Component
} }
}; };
</script> </script>

Loading…
Cancel
Save