Skip to content

Commit 14c3c95

Browse files
committed
refactor(@schematics/angular): remove tags usage from @angular-devkit/core
Usages of the `tags` template literal utility have been removed. The logic has been refactored to use standard JavaScript template literals and string manipulation methods to achieve the same formatting results. This change affects multiple schematics and their tests.
1 parent db775e1 commit 14c3c95

File tree

8 files changed

+38
-41
lines changed

8 files changed

+38
-41
lines changed

packages/schematics/angular/app-shell/index_spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { tags } from '@angular-devkit/core';
109
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
1110
import { Schema as ApplicationOptions } from '../application/schema';
1211
import { Schema as WorkspaceOptions } from '../workspace/schema';
@@ -126,8 +125,8 @@ describe('App Shell Schematic', () => {
126125
it(`should update the 'provideServerRendering' call to include 'withAppShell'`, async () => {
127126
const tree = await schematicRunner.runSchematic('app-shell', defaultOptions, appTree);
128127
const content = tree.readContent('/projects/bar/src/app/app.config.server.ts');
129-
expect(tags.oneLine`${content}`).toContain(
130-
tags.oneLine`provideServerRendering(withRoutes(serverRoutes), withAppShell(AppShell))`,
128+
expect(content.replace(/\s/g, '')).toContain(
129+
'provideServerRendering(withRoutes(serverRoutes),withAppShell(AppShell))',
131130
);
132131
});
133132

packages/schematics/angular/service-worker/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { join, normalize, tags } from '@angular-devkit/core';
9+
import { join, normalize } from '@angular-devkit/core';
1010
import {
1111
Rule,
1212
SchematicContext,
@@ -55,7 +55,7 @@ function updateAppModule(mainPath: string): Rule {
5555
addImport(host, modulePath, 'isDevMode', '@angular/core');
5656

5757
// register SW in application module
58-
const importText = tags.stripIndent`
58+
const importText = `
5959
ServiceWorkerModule.register('ngsw-worker.js', {
6060
enabled: !isDevMode(),
6161
// Register the ServiceWorker as soon as the application is stable

packages/schematics/angular/service-worker/index_spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { tags } from '@angular-devkit/core';
109
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
1110
import { Schema as ApplicationOptions } from '../application/schema';
1211
import { Builders } from '../utility/workspace-models';
@@ -127,12 +126,9 @@ describe('Service Worker Schematic', () => {
127126
it(`should add the 'provideServiceWorker' to providers`, async () => {
128127
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
129128
const content = tree.readContent('/projects/bar/src/app/app.config.ts');
130-
expect(tags.oneLine`${content}`).toContain(tags.oneLine`
131-
provideServiceWorker('ngsw-worker.js', {
132-
enabled: !isDevMode(),
133-
registrationStrategy: 'registerWhenStable:30000'
134-
})
135-
`);
129+
expect(content.replace(/\s/g, '')).toContain(
130+
`provideServiceWorker('ngsw-worker.js',{enabled:!isDevMode(),registrationStrategy:'registerWhenStable:30000'})`,
131+
);
136132
});
137133

138134
it(`should import 'isDevMode' from '@angular/core'`, async () => {

packages/schematics/angular/ssr/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { isJsonObject, join, normalize, strings } from '@angular-devkit/core';
9+
import { isJsonObject, join, normalize } from '@angular-devkit/core';
1010
import {
1111
Rule,
1212
SchematicContext,
@@ -18,6 +18,7 @@ import {
1818
mergeWith,
1919
move,
2020
schematic,
21+
strings,
2122
url,
2223
} from '@angular-devkit/schematics';
2324
import { posix } from 'node:path';

packages/schematics/angular/utility/ast-utils.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { tags } from '@angular-devkit/core';
109
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
1110
import { Change, InsertChange, NoopChange } from './change';
1211
import { getEOL } from './eol';
@@ -379,7 +378,11 @@ export function addSymbolToNgModuleMetadata(
379378
let toInsert: string;
380379
if (node.properties.length == 0) {
381380
position = node.getEnd() - 1;
382-
toInsert = `\n ${metadataField}: [\n${tags.indentBy(4)`${symbolName}`}\n ]\n`;
381+
toInsert = `
382+
${metadataField}: [
383+
${' '.repeat(4)}${symbolName}
384+
]
385+
`;
383386
} else {
384387
const childNode = node.properties[node.properties.length - 1];
385388
position = childNode.getEnd();
@@ -389,7 +392,7 @@ export function addSymbolToNgModuleMetadata(
389392
if (matches) {
390393
toInsert =
391394
`,${matches[0]}${metadataField}: [${matches[1]}` +
392-
`${tags.indentBy(matches[2].length + 2)`${symbolName}`}${matches[0]}]`;
395+
`${' '.repeat(matches[2].length + 2)}${symbolName}${matches[0]}]`;
393396
} else {
394397
toInsert = `, ${metadataField}: [${symbolName}]`;
395398
}
@@ -418,8 +421,8 @@ export function addSymbolToNgModuleMetadata(
418421
const elements = assignmentInit.elements;
419422

420423
if (elements.length) {
421-
const symbolsArray = elements.map((node) => tags.oneLine`${node.getText()}`);
422-
if (symbolsArray.includes(tags.oneLine`${symbolName}`)) {
424+
const symbolsArray = elements.map((node) => node.getText());
425+
if (symbolsArray.includes(symbolName)) {
423426
return [];
424427
}
425428

@@ -433,13 +436,13 @@ export function addSymbolToNgModuleMetadata(
433436
if (ts.isArrayLiteralExpression(expression)) {
434437
// We found the field but it's empty. Insert it just before the `]`.
435438
position--;
436-
toInsert = `\n${tags.indentBy(4)`${symbolName}`}\n `;
439+
toInsert = `\n${' '.repeat(4)}${symbolName}\n `;
437440
} else {
438441
// Get the indentation of the last element, if any.
439442
const text = expression.getFullText(source);
440443
const matches = text.match(/^(\r?\n)(\s*)/);
441444
if (matches) {
442-
toInsert = `,${matches[1]}${tags.indentBy(matches[2].length)`${symbolName}`}`;
445+
toInsert = `,${matches[1]}${' '.repeat(matches[2].length)}${symbolName}`;
443446
} else {
444447
toInsert = `, ${symbolName}`;
445448
}

packages/schematics/angular/utility/ast-utils_spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { tags } from '@angular-devkit/core';
109
import { HostTree } from '@angular-devkit/schematics';
1110
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
1211
import { Change, InsertChange } from '../utility/change';
@@ -73,7 +72,7 @@ describe('ast utils', () => {
7372
});
7473

7574
it('should add export to module if not indented', () => {
76-
moduleContent = tags.stripIndents`${moduleContent}`;
75+
moduleContent = moduleContent.replace(/^(\s+)/gm, '');
7776
const source = getTsSource(modulePath, moduleContent);
7877
const changes = addExportToModule(source, modulePath, 'FooComponent', './foo.component');
7978
const output = applyChanges(modulePath, moduleContent, changes);
@@ -82,7 +81,7 @@ describe('ast utils', () => {
8281
});
8382

8483
it('should add declarations to module if not indented', () => {
85-
moduleContent = tags.stripIndents`${moduleContent}`;
84+
moduleContent = moduleContent.replace(/^(\s+)/gm, '');
8685
const source = getTsSource(modulePath, moduleContent);
8786
const changes = addDeclarationToModule(source, modulePath, 'FooComponent', './foo.component');
8887
const output = applyChanges(modulePath, moduleContent, changes);
@@ -91,7 +90,7 @@ describe('ast utils', () => {
9190
});
9291

9392
it('should add declarations to module when PropertyAssignment is StringLiteral', () => {
94-
moduleContent = tags.stripIndents`
93+
moduleContent = `
9594
import { BrowserModule } from '@angular/platform-browser';
9695
import { NgModule } from '@angular/core';
9796
import { AppComponent } from './app.component';
@@ -110,7 +109,7 @@ describe('ast utils', () => {
110109
const changes = addDeclarationToModule(source, modulePath, 'FooComponent', './foo.component');
111110
const output = applyChanges(modulePath, moduleContent, changes);
112111
expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/);
113-
expect(output).toMatch(/"declarations": \[\nAppComponent,\nFooComponent\n\]/);
112+
expect(output).toMatch(/"declarations": \[\s*AppComponent,\s*FooComponent\s*\]/);
114113
});
115114

116115
it('should add metadata', () => {

packages/schematics/angular/utility/standalone/rules.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { tags } from '@angular-devkit/core';
109
import { Rule, SchematicsException, Tree, chain } from '@angular-devkit/schematics';
1110
import ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
1211
import { addSymbolToNgModuleMetadata, insertAfterLastOccurrence } from '../ast-utils';
@@ -188,7 +187,7 @@ function insertStandaloneRootProvider(tree: Tree, mainFilePath: string, expressi
188187
return;
189188
}
190189

191-
const newAppConfig = `, {\n${tags.indentBy(2)`providers: [${expression}]`}\n}`;
190+
const newAppConfig = `, {\n${' '.repeat(2)}providers: [${expression}]\n}`;
192191
let targetCall: ts.CallExpression;
193192

194193
if (bootstrapCall.arguments.length === 1) {
@@ -240,7 +239,7 @@ function addProvidersExpressionToAppConfig(
240239
),
241240
]);
242241
} else {
243-
const prop = tags.indentBy(2)`providers: [${expression}]`;
242+
const prop = `${' '.repeat(2)}providers: [${expression}]`;
244243
let toInsert: string;
245244
let insertPosition: number;
246245

packages/schematics/angular/web-worker/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { join, normalize, tags } from '@angular-devkit/core';
9+
import { join, normalize } from '@angular-devkit/core';
1010
import {
1111
Rule,
1212
SchematicContext,
@@ -51,18 +51,18 @@ function addSnippet(options: WebWorkerOptions): Rule {
5151

5252
const siblingModulePath = `${options.path}/${siblingModules[0]}`;
5353
const logMessage = 'console.log(`page got message: ${data}`);';
54-
const workerCreationSnippet = tags.stripIndent`
55-
if (typeof Worker !== 'undefined') {
56-
// Create a new
57-
const worker = new Worker(new URL('./${options.name}.worker', import.meta.url));
58-
worker.onmessage = ({ data }) => {
59-
${logMessage}
60-
};
61-
worker.postMessage('hello');
62-
} else {
63-
// Web Workers are not supported in this environment.
64-
// You should add a fallback so that your program still executes correctly.
65-
}
54+
const workerCreationSnippet = `
55+
if (typeof Worker !== 'undefined') {
56+
// Create a new
57+
const worker = new Worker(new URL('./${options.name}.worker', import.meta.url));
58+
worker.onmessage = ({ data }) => {
59+
${logMessage}
60+
};
61+
worker.postMessage('hello');
62+
} else {
63+
// Web Workers are not supported in this environment.
64+
// You should add a fallback so that your program still executes correctly.
65+
}
6666
`;
6767

6868
// Append the worker creation snippet.

0 commit comments

Comments
 (0)