@@ -33,13 +33,30 @@ export function analyze(sourceFile: ts.SourceFile, analyzedFiles: Map<string, An
3333 } ) ;
3434}
3535
36+ function checkIfShouldChange ( decl : ts . ImportDeclaration , removeCommonModule : boolean ) {
37+ // should change if you can remove the common module
38+ // if it's not safe to remove the common module
39+ // and that's the only thing there, we should do nothing.
40+ const clause = decl . getChildAt ( 1 ) as ts . ImportClause ;
41+ return ! (
42+ ! removeCommonModule && clause . namedBindings && ts . isNamedImports ( clause . namedBindings ) &&
43+ clause . namedBindings . elements . length === 1 &&
44+ clause . namedBindings . elements [ 0 ] . getText ( ) === 'CommonModule' ) ;
45+ }
46+
3647function updateImportDeclaration ( decl : ts . ImportDeclaration , removeCommonModule : boolean ) : string {
3748 const clause = decl . getChildAt ( 1 ) as ts . ImportClause ;
3849 const updatedClause = updateImportClause ( clause , removeCommonModule ) ;
3950 if ( updatedClause === null ) {
4051 return '' ;
4152 }
42- const printer = ts . createPrinter ( ) ;
53+ // removeComments is set to true to prevent duplication of comments
54+ // when the import declaration is at the top of the file, but right after a comment
55+ // without this, the comment gets duplicated when the declaration is updated.
56+ // the typescript AST includes that preceding comment as part of the import declaration full text.
57+ const printer = ts . createPrinter ( {
58+ removeComments : true ,
59+ } ) ;
4360 const updated = ts . factory . updateImportDeclaration (
4461 decl , decl . modifiers , updatedClause , decl . moduleSpecifier , undefined ) ;
4562 return printer . printNode ( ts . EmitHint . Unspecified , updated , clause . getSourceFile ( ) ) ;
@@ -326,7 +343,7 @@ export function removeImports(
326343 template : string , node : ts . Node , removeCommonModule : boolean ) : string {
327344 if ( template . startsWith ( 'imports' ) && ts . isPropertyAssignment ( node ) ) {
328345 return updateClassImports ( node , removeCommonModule ) ;
329- } else if ( ts . isImportDeclaration ( node ) ) {
346+ } else if ( ts . isImportDeclaration ( node ) && checkIfShouldChange ( node , removeCommonModule ) ) {
330347 return updateImportDeclaration ( node , removeCommonModule ) ;
331348 }
332349 return template ;
@@ -423,29 +440,29 @@ export function formatTemplate(tmpl: string): string {
423440
424441 // regex for matching an html element opening
425442 // <div thing="stuff" [binding]="true"> || <div thing="stuff" [binding]="true"
426- const openElRegex = / ^ \s * < ( [ a - z ] + ) (? ! [ ^ > ] * \/ > ) [ ^ > ] * > ? / ;
443+ const openElRegex = / ^ \s * < ( [ a - z 0 - 9 ] + ) (? ! [ ^ > ] * \/ > ) [ ^ > ] * > ? / ;
427444
428445 // match closing block or else block
429446 // } | } @else
430447 const closeBlockRegex = / ^ \s * \} \s * $ | ^ \s * \} \s \@ e l s e / ;
431448
432449 // matches closing of an html element
433450 // </element>
434- const closeElRegex = / \s * < \/ ( [ a - z \- ] + ) * > / ;
451+ const closeElRegex = / \s * < \/ ( [ a - z 0 - 9 \- ] + ) * > / ;
435452
436453 // matches closing of a self closing html element when the element is on multiple lines
437454 // [binding]="value" />
438455 const closeMultiLineElRegex = / ^ \s * ( [ a - z 0 - 9 \- \[ \] ] + ) ? = ? " ? ( [ ^ ” < ] + ) ? " ? \s ? \/ > $ / ;
439456
440457 // matches an open and close of an html element on a single line with no breaks
441458 // <div>blah</div>
442- const singleLineElRegex = / ^ \s * < ( [ a - z ] + ) (? ! [ ^ > ] * \/ > ) [ ^ > ] * > .* < \/ ( [ a - z \- ] + ) * > / ;
459+ const singleLineElRegex = / ^ \s * < ( [ a - z 0 - 9 ] + ) (? ! [ ^ > ] * \/ > ) [ ^ > ] * > .* < \/ ( [ a - z 0 - 9 \- ] + ) * > / ;
443460 const lines = tmpl . split ( '\n' ) ;
444461 const formatted = [ ] ;
445462 let indent = '' ;
446- for ( let line of lines ) {
447- if ( line . trim ( ) === '' ) {
448- // skip blank lines
463+ for ( let [ index , line ] of lines . entries ( ) ) {
464+ if ( line . trim ( ) === '' && index !== 0 && index !== lines . length - 1 ) {
465+ // skip blank lines except if it's the first line or last line
449466 continue ;
450467 }
451468 if ( ( closeBlockRegex . test ( line ) ||
0 commit comments