Skip to content

Commit caec96f

Browse files
committed
make abstract removeNode function
1 parent 3881f5f commit caec96f

File tree

4 files changed

+67
-47
lines changed

4 files changed

+67
-47
lines changed

src/generators/Generator.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import isReference from '../utils/isReference.js';
44
import flattenReference from '../utils/flattenReference.js';
55
import globalWhitelist from '../utils/globalWhitelist.js';
66
import reservedNames from '../utils/reservedNames.js';
7+
import { removeNode } from '../utils/removeNode.js';
78
import getIntro from './shared/utils/getIntro.js';
89
import getOutro from './shared/utils/getOutro.js';
910
import processCss from './shared/processCss.js';
@@ -268,28 +269,25 @@ export default class Generator {
268269

269270
if ( js ) {
270271
this.addSourcemapLocations( js.content );
272+
const body = js.content.body.slice(); // slice, because we're going to be mutating the original
271273

272274
// imports need to be hoisted out of the IIFE
273-
for ( let i = 0; i < js.content.body.length; i += 1 ) {
274-
const node = js.content.body[i];
275+
for ( let i = 0; i < body.length; i += 1 ) {
276+
const node = body[i];
275277
if ( node.type === 'ImportDeclaration' ) {
276-
let a = node.start;
277-
let b = node.end;
278-
while ( /[ \t]/.test( source[ a - 1 ] ) ) a -= 1;
279-
while ( source[b] === '\n' ) b += 1;
280-
278+
removeNode( this.code, js.content, node );
281279
imports.push( node );
282-
this.code.remove( a, b );
280+
283281
node.specifiers.forEach( specifier => {
284282
this.importedNames.add( specifier.local.name );
285283
});
286284
}
287285
}
288286

289-
defaultExport = js.content.body.find( node => node.type === 'ExportDefaultDeclaration' );
287+
defaultExport = body.find( node => node.type === 'ExportDefaultDeclaration' );
290288

291289
if ( defaultExport ) {
292-
const finalNode = js.content.body[ js.content.body.length - 1 ];
290+
const finalNode = body[ body.length - 1 ];
293291
if ( defaultExport === finalNode ) {
294292
// export is last property, we can just return it
295293
this.code.overwrite( defaultExport.start, defaultExport.declaration.start, `return ` );

src/generators/dom/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import deindent from '../../utils/deindent.js';
22
import getBuilders from './utils/getBuilders.js';
33
import CodeBuilder from '../../utils/CodeBuilder.js';
44
import namespaces from '../../utils/namespaces.js';
5-
import removeObjectKey from '../../utils/removeObjectKey.js';
5+
import { removeObjectKey } from '../../utils/removeNode.js';
66
import visitors from './visitors/index.js';
77
import Generator from '../Generator.js';
88
import * as shared from '../../shared/index.js';
@@ -175,7 +175,7 @@ export default function dom ( parsed, source, options ) {
175175
const ns = templateProperties.namespace.value.value;
176176
namespace = namespaces[ ns ] || ns;
177177

178-
removeObjectKey( generator, defaultExport.declaration, 'namespace' );
178+
removeObjectKey( generator.code, defaultExport.declaration, 'namespace' );
179179
}
180180

181181
if ( templateProperties.components ) {
@@ -192,11 +192,11 @@ export default function dom ( parsed, source, options ) {
192192
if ( hasNonImportedComponent ) {
193193
// remove the specific components that were imported, as we'll refer to them directly
194194
Array.from( generator.importedComponents.keys() ).forEach( key => {
195-
removeObjectKey( generator, templateProperties.components.value, key );
195+
removeObjectKey( generator.code, templateProperties.components.value, key );
196196
});
197197
} else {
198198
// remove the entire components portion of the export
199-
removeObjectKey( generator, defaultExport.declaration, 'components' );
199+
removeObjectKey( generator.code, defaultExport.declaration, 'components' );
200200
}
201201
}
202202

src/utils/removeNode.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const keys = {
2+
ObjectExpression: 'properties',
3+
Program: 'body'
4+
};
5+
6+
const offsets = {
7+
ObjectExpression: [ 1, -1 ],
8+
Program: [ 0, 0 ]
9+
};
10+
11+
export function removeNode ( code, parent, node ) {
12+
const key = keys[ parent.type ];
13+
const offset = offsets[ parent.type ];
14+
if ( !key || !offset ) throw new Error( `not implemented: ${parent.type}` );
15+
16+
const list = parent[ key ];
17+
const i = list.indexOf( node );
18+
if ( i === -1 ) throw new Error( 'node not in list' );
19+
20+
let a;
21+
let b;
22+
23+
if ( list.length === 1 ) {
24+
// remove everything, leave {}
25+
a = parent.start + offset[0];
26+
b = parent.end + offset[1];
27+
} else if ( i === 0 ) {
28+
// remove everything before second node, including comments
29+
a = parent.start + offset[0];
30+
while ( /\s/.test( code.original[a] ) ) a += 1;
31+
32+
b = list[i].end;
33+
while ( /[\s,]/.test( code.original[b] ) ) b += 1;
34+
} else {
35+
// remove the end of the previous node to the end of this one
36+
a = list[ i - 1 ].end;
37+
b = node.end;
38+
}
39+
40+
code.remove( a, b );
41+
list.splice( i, 1 );
42+
return;
43+
}
44+
45+
export function removeObjectKey ( code, node, key ) {
46+
if ( node.type !== 'ObjectExpression' ) return;
47+
48+
let i = node.properties.length;
49+
while ( i-- ) {
50+
const property = node.properties[i];
51+
if ( property.key.type === 'Identifier' && property.key.name === key ) {
52+
removeNode( code, node, property );
53+
}
54+
}
55+
}

src/utils/removeObjectKey.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)