Skip to content

Commit

Permalink
fix(@schematics/angular): retain existing EOL when adding imports
Browse files Browse the repository at this point in the history
This commit updates the AST utility to retain the existing EOF when adding imports

(cherry picked from commit dfde275)
  • Loading branch information
alan-agius4 committed Jan 5, 2024
1 parent 7f395be commit 8c81571
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/schematics/angular/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { tags } from '@angular-devkit/core';
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
import { Change, InsertChange, NoopChange } from './change';
import { getEOL } from './eol';

/**
* Add Import `import { symbolName } from fileName` if the import doesn't exit
Expand Down Expand Up @@ -73,12 +74,13 @@ export function insertImport(
}
const open = isDefault ? '' : '{ ';
const close = isDefault ? '' : ' }';
const eol = getEOL(rootNode.getText());
// if there are no imports or 'use strict' statement, insert import at beginning of file
const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
const separator = insertAtBeginning ? '' : ';\n';
const separator = insertAtBeginning ? '' : `;${eol}`;
const toInsert =
`${separator}import ${open}${importExpression}${close}` +
` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;
` from '${fileName}'${insertAtBeginning ? `;${eol}` : ''}`;

return insertAfterLastOccurrence(
allImports,
Expand Down

0 comments on commit 8c81571

Please sign in to comment.