From 8c815711a255049512c3712ff89c9ab321a20b23 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 5 Jan 2024 09:15:10 +0000 Subject: [PATCH] fix(@schematics/angular): retain existing EOL when adding imports This commit updates the AST utility to retain the existing EOF when adding imports (cherry picked from commit dfde2750b4c16f826d13fa88121bc8ed5cc39957) --- packages/schematics/angular/utility/ast-utils.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/schematics/angular/utility/ast-utils.ts b/packages/schematics/angular/utility/ast-utils.ts index 7f4721ca9bba..e5ceefff083d 100644 --- a/packages/schematics/angular/utility/ast-utils.ts +++ b/packages/schematics/angular/utility/ast-utils.ts @@ -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 @@ -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,