Skip to content

Commit cca2cb2

Browse files
committed
fix(@angular-devkit/core): handle number like strings in workspace writer
The workspace writer previously transformed number like strings to numbers which causes failures when a project is named using a number like name. Closes angular#24541
1 parent 697df4f commit cca2cb2

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed

packages/angular_devkit/core/src/workspace/json/writer.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,10 @@ function updateJsonWorkspace(metadata: JsonWorkspaceMetadata): string {
156156
jsonPath[2] = 'architect';
157157
}
158158

159-
// modify
160-
const newJsonPath = jsonPath.map((v) => (isFinite(+v) ? +v : v));
161159
// TODO: `modify` re-parses the content every time.
162160
// See: https://github.com/microsoft/node-jsonc-parser/blob/35d94cd71bd48f9784453b2439262c938e21d49b/src/impl/edit.ts#L18
163161
// Ideally this should accept a string or an AST to avoid the potentially expensive repeat parsing operation.
164-
const edits = modify(content, newJsonPath, normalizeValue(value, type), {
162+
const edits = modify(content, jsonPath, normalizeValue(value, type), {
165163
formattingOptions: {
166164
insertSpaces: true,
167165
tabSize: 2,

packages/angular_devkit/core/src/workspace/json/writer_spec.ts

+11-34
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ describe('writeJsonWorkpaceFile', () => {
186186

187187
it('retains comments and formatting when modifying the workspace', async () => {
188188
const host = createTestCaseHost(basicFile);
189-
190189
const workspace = await readJsonWorkspace('', host);
191190

192191
workspace.extensions['x-baz'] = 10;
@@ -196,7 +195,6 @@ describe('writeJsonWorkpaceFile', () => {
196195

197196
it('adds a project to workspace without any projects', async () => {
198197
const host = createTestCaseHost(basicFile);
199-
200198
const workspace = await readJsonWorkspace('', host);
201199

202200
workspace.projects.add({
@@ -209,7 +207,18 @@ describe('writeJsonWorkpaceFile', () => {
209207

210208
it('adds a project to workspace with existing projects', async () => {
211209
const host = createTestCaseHost(representativeFile);
210+
const workspace = await readJsonWorkspace('', host);
211+
212+
workspace.projects.add({
213+
name: 'new',
214+
root: 'src',
215+
});
216+
217+
await writeJsonWorkspace(workspace, host, 'AddProject2');
218+
});
212219

220+
it('adds a project to workspace with existing projects when name is number like', async () => {
221+
const host = createTestCaseHost(representativeFile);
213222
const workspace = await readJsonWorkspace('', host);
214223

215224
workspace.projects.add({
@@ -222,7 +231,6 @@ describe('writeJsonWorkpaceFile', () => {
222231

223232
it('adds a project with targets', async () => {
224233
const host = createTestCaseHost(basicFile);
225-
226234
const workspace = await readJsonWorkspace('', host);
227235

228236
workspace.projects.add({
@@ -246,7 +254,6 @@ describe('writeJsonWorkpaceFile', () => {
246254

247255
it('adds a project with targets using reference to workspace', async () => {
248256
const host = createTestCaseHost(basicFile);
249-
250257
const workspace = await readJsonWorkspace('', host);
251258

252259
workspace.projects.add({
@@ -278,7 +285,6 @@ describe('writeJsonWorkpaceFile', () => {
278285

279286
it("modifies a project's properties", async () => {
280287
const host = createTestCaseHost(representativeFile);
281-
282288
const workspace = await readJsonWorkspace('', host);
283289

284290
const project = workspace.projects.get('my-app');
@@ -295,7 +301,6 @@ describe('writeJsonWorkpaceFile', () => {
295301

296302
it("sets a project's properties", async () => {
297303
const host = createTestCaseHost(representativeFile);
298-
299304
const workspace = await readJsonWorkspace('', host);
300305

301306
const project = workspace.projects.get('my-app');
@@ -312,7 +317,6 @@ describe('writeJsonWorkpaceFile', () => {
312317

313318
it('adds a target to an existing project', async () => {
314319
const host = createTestCaseHost(representativeFile);
315-
316320
const workspace = await readJsonWorkspace('', host);
317321

318322
const project = workspace.projects.get('my-app');
@@ -332,7 +336,6 @@ describe('writeJsonWorkpaceFile', () => {
332336

333337
it('deletes a target from an existing project', async () => {
334338
const host = createTestCaseHost(representativeFile);
335-
336339
const workspace = await readJsonWorkspace('', host);
337340

338341
const project = workspace.projects.get('my-app');
@@ -349,7 +352,6 @@ describe('writeJsonWorkpaceFile', () => {
349352

350353
it('supports adding an empty array', async () => {
351354
const host = createTestCaseHost(basicFile);
352-
353355
const workspace = await readJsonWorkspace('', host);
354356

355357
workspace.extensions['x-array'] = [];
@@ -359,7 +361,6 @@ describe('writeJsonWorkpaceFile', () => {
359361

360362
it('supports adding an array with values', async () => {
361363
const host = createTestCaseHost(basicFile);
362-
363364
const workspace = await readJsonWorkspace('', host);
364365

365366
workspace.extensions['x-array'] = [5, 'a', false, null, true, 9.9];
@@ -369,7 +370,6 @@ describe('writeJsonWorkpaceFile', () => {
369370

370371
it('supports adding an empty array then pushing as an extension', async () => {
371372
const host = createTestCaseHost(basicFile);
372-
373373
const workspace = await readJsonWorkspace('', host);
374374

375375
workspace.extensions['x-array'] = [];
@@ -380,7 +380,6 @@ describe('writeJsonWorkpaceFile', () => {
380380

381381
it('supports pushing to an existing array', async () => {
382382
const host = createTestCaseHost(basicFile);
383-
384383
const workspace = await readJsonWorkspace('', host);
385384

386385
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -391,7 +390,6 @@ describe('writeJsonWorkpaceFile', () => {
391390

392391
it('supports unshifting to an existing array', async () => {
393392
const host = createTestCaseHost(basicFile);
394-
395393
const workspace = await readJsonWorkspace('', host);
396394

397395
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -402,7 +400,6 @@ describe('writeJsonWorkpaceFile', () => {
402400

403401
it('supports shifting from an existing array', async () => {
404402
const host = createTestCaseHost(basicFile);
405-
406403
const workspace = await readJsonWorkspace('', host);
407404

408405
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -413,7 +410,6 @@ describe('writeJsonWorkpaceFile', () => {
413410

414411
it('supports splicing an existing array without new values', async () => {
415412
const host = createTestCaseHost(basicFile);
416-
417413
const workspace = await readJsonWorkspace('', host);
418414

419415
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -424,7 +420,6 @@ describe('writeJsonWorkpaceFile', () => {
424420

425421
it('supports splicing an existing array with new values', async () => {
426422
const host = createTestCaseHost(basicFile);
427-
428423
const workspace = await readJsonWorkspace('', host);
429424

430425
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -435,7 +430,6 @@ describe('writeJsonWorkpaceFile', () => {
435430

436431
it('supports popping from an existing array', async () => {
437432
const host = createTestCaseHost(basicFile);
438-
439433
const workspace = await readJsonWorkspace('', host);
440434

441435
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -446,7 +440,6 @@ describe('writeJsonWorkpaceFile', () => {
446440

447441
it('supports sorting from an existing array', async () => {
448442
const host = createTestCaseHost(basicFile);
449-
450443
const workspace = await readJsonWorkspace('', host);
451444

452445
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -457,7 +450,6 @@ describe('writeJsonWorkpaceFile', () => {
457450

458451
it('replaces a value at zero index from an existing array', async () => {
459452
const host = createTestCaseHost(basicFile);
460-
461453
const workspace = await readJsonWorkspace('', host);
462454

463455
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -468,7 +460,6 @@ describe('writeJsonWorkpaceFile', () => {
468460

469461
it('replaces a value at inner index from an existing array', async () => {
470462
const host = createTestCaseHost(basicFile);
471-
472463
const workspace = await readJsonWorkspace('', host);
473464

474465
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -479,7 +470,6 @@ describe('writeJsonWorkpaceFile', () => {
479470

480471
it('replaces a value at last index from an existing array', async () => {
481472
const host = createTestCaseHost(basicFile);
482-
483473
const workspace = await readJsonWorkspace('', host);
484474

485475
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -490,7 +480,6 @@ describe('writeJsonWorkpaceFile', () => {
490480

491481
it('deletes a value at zero index from an existing array', async () => {
492482
const host = createTestCaseHost(basicFile);
493-
494483
const workspace = await readJsonWorkspace('', host);
495484

496485
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -501,7 +490,6 @@ describe('writeJsonWorkpaceFile', () => {
501490

502491
it('deletes a value at inner index from an existing array', async () => {
503492
const host = createTestCaseHost(basicFile);
504-
505493
const workspace = await readJsonWorkspace('', host);
506494

507495
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -512,7 +500,6 @@ describe('writeJsonWorkpaceFile', () => {
512500

513501
it('deletes and then adds a value at inner index from an existing array', async () => {
514502
const host = createTestCaseHost(basicFile);
515-
516503
const workspace = await readJsonWorkspace('', host);
517504

518505
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -524,7 +511,6 @@ describe('writeJsonWorkpaceFile', () => {
524511

525512
it('deletes a value at last index from an existing array', async () => {
526513
const host = createTestCaseHost(basicFile);
527-
528514
const workspace = await readJsonWorkspace('', host);
529515

530516
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -535,7 +521,6 @@ describe('writeJsonWorkpaceFile', () => {
535521

536522
it('deletes and then adds a value at last index from an existing array', async () => {
537523
const host = createTestCaseHost(basicFile);
538-
539524
const workspace = await readJsonWorkspace('', host);
540525

541526
const array = (workspace.extensions['x-foo'] as JsonObject)['is'] as JsonArray;
@@ -547,7 +532,6 @@ describe('writeJsonWorkpaceFile', () => {
547532

548533
it('replaces an existing array', async () => {
549534
const host = createTestCaseHost(basicFile);
550-
551535
const workspace = await readJsonWorkspace('', host);
552536

553537
(workspace.extensions['x-foo'] as JsonObject)['is'] = ['value'];
@@ -557,7 +541,6 @@ describe('writeJsonWorkpaceFile', () => {
557541

558542
it('replaces an existing array with an empty array', async () => {
559543
const host = createTestCaseHost(basicFile);
560-
561544
const workspace = await readJsonWorkspace('', host);
562545

563546
(workspace.extensions['x-foo'] as JsonObject)['is'] = [];
@@ -567,7 +550,6 @@ describe('writeJsonWorkpaceFile', () => {
567550

568551
it('replaces an existing object with a new object', async () => {
569552
const host = createTestCaseHost(basicFile);
570-
571553
const workspace = await readJsonWorkspace('', host);
572554

573555
workspace.extensions['x-foo'] = { replacement: true };
@@ -577,7 +559,6 @@ describe('writeJsonWorkpaceFile', () => {
577559

578560
it('replaces an existing object with an empty object', async () => {
579561
const host = createTestCaseHost(basicFile);
580-
581562
const workspace = await readJsonWorkspace('', host);
582563

583564
workspace.extensions['x-foo'] = {};
@@ -587,7 +568,6 @@ describe('writeJsonWorkpaceFile', () => {
587568

588569
it('replaces an existing object with a different value type', async () => {
589570
const host = createTestCaseHost(basicFile);
590-
591571
const workspace = await readJsonWorkspace('', host);
592572

593573
workspace.extensions['x-foo'] = null;
@@ -597,7 +577,6 @@ describe('writeJsonWorkpaceFile', () => {
597577

598578
it('removes a property when property value is set to undefined', async () => {
599579
const host = createTestCaseHost(basicFile);
600-
601580
const workspace = await readJsonWorkspace('', host);
602581

603582
workspace.extensions['x-baz'] = undefined;
@@ -607,7 +586,6 @@ describe('writeJsonWorkpaceFile', () => {
607586

608587
it('removes a property when using delete operator', async () => {
609588
const host = createTestCaseHost(basicFile);
610-
611589
const workspace = await readJsonWorkspace('', host);
612590

613591
delete workspace.extensions['x-baz'];
@@ -617,7 +595,6 @@ describe('writeJsonWorkpaceFile', () => {
617595

618596
it('removes multiple properties when using delete operator', async () => {
619597
const host = createTestCaseHost(basicFile);
620-
621598
const workspace = await readJsonWorkspace('', host);
622599

623600
delete workspace.extensions['x-baz'];

0 commit comments

Comments
 (0)