Skip to content

Commit

Permalink
fix(@angular-devkit/core): handle number like strings in workspace wr…
Browse files Browse the repository at this point in the history
…iter

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
  • Loading branch information
alan-agius4 committed Jan 13, 2023
1 parent 89b9d94 commit d8ec0f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 37 deletions.
4 changes: 1 addition & 3 deletions packages/angular_devkit/core/src/workspace/json/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,10 @@ function updateJsonWorkspace(metadata: JsonWorkspaceMetadata): string {
jsonPath[2] = 'architect';
}

// modify
const newJsonPath = jsonPath.map((v) => (isFinite(+v) ? +v : v));
// TODO: `modify` re-parses the content every time.
// See: https://github.com/microsoft/node-jsonc-parser/blob/35d94cd71bd48f9784453b2439262c938e21d49b/src/impl/edit.ts#L18
// Ideally this should accept a string or an AST to avoid the potentially expensive repeat parsing operation.
const edits = modify(content, newJsonPath, normalizeValue(value, type), {
const edits = modify(content, jsonPath, normalizeValue(value, type), {
formattingOptions: {
insertSpaces: true,
tabSize: 2,
Expand Down
45 changes: 11 additions & 34 deletions packages/angular_devkit/core/src/workspace/json/writer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ describe('writeJsonWorkpaceFile', () => {

it('retains comments and formatting when modifying the workspace', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('adds a project to workspace without any projects', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('adds a project to workspace with existing projects', async () => {
const host = createTestCaseHost(representativeFile);
const workspace = await readJsonWorkspace('', host);

workspace.projects.add({
name: 'new',
root: 'src',
});

await writeJsonWorkspace(workspace, host, 'AddProject2');
});

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

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

it('adds a project with targets', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('adds a project with targets using reference to workspace', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it("modifies a project's properties", async () => {
const host = createTestCaseHost(representativeFile);

const workspace = await readJsonWorkspace('', host);

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

it("sets a project's properties", async () => {
const host = createTestCaseHost(representativeFile);

const workspace = await readJsonWorkspace('', host);

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

it('adds a target to an existing project', async () => {
const host = createTestCaseHost(representativeFile);

const workspace = await readJsonWorkspace('', host);

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

it('deletes a target from an existing project', async () => {
const host = createTestCaseHost(representativeFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports adding an empty array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports adding an array with values', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports adding an empty array then pushing as an extension', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports pushing to an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports unshifting to an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports shifting from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports splicing an existing array without new values', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports splicing an existing array with new values', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports popping from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('supports sorting from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces a value at zero index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces a value at inner index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces a value at last index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('deletes a value at zero index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('deletes a value at inner index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('deletes and then adds a value at inner index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('deletes a value at last index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('deletes and then adds a value at last index from an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces an existing array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces an existing array with an empty array', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces an existing object with a new object', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces an existing object with an empty object', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('replaces an existing object with a different value type', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('removes a property when property value is set to undefined', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('removes a property when using delete operator', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

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

it('removes multiple properties when using delete operator', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

delete workspace.extensions['x-baz'];
Expand Down

0 comments on commit d8ec0f8

Please sign in to comment.