Skip to content

Commit 3459304

Browse files
authored
improvement(shared-object-base): use shallowCloneObject helper in FluidSerializer to clean up no-unsafe-assignment lint disable (#23717)
## Description Follow up to #23662: #23662 (comment) Reason for `shallowCloneObject`: #23662 (comment) This PR also removes several unused `no-unsafe-assignment` lint disables in test file serializer.spec.ts.
1 parent c91b39f commit 3459304

File tree

2 files changed

+2
-13
lines changed

2 files changed

+2
-13
lines changed

packages/dds/shared-object-base/src/serializer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
IFluidHandleContext,
99
type IFluidHandleInternal,
1010
} from "@fluidframework/core-interfaces/internal";
11-
import { assert } from "@fluidframework/core-utils/internal";
11+
import { assert, shallowCloneObject } from "@fluidframework/core-utils/internal";
1212
import {
1313
generateHandleContextPath,
1414
isSerializedHandle,
@@ -185,8 +185,7 @@ export class FluidSerializer implements IFluidSerializer {
185185
// current property is replaced by the `replaced` value.
186186
if (replaced !== value) {
187187
// Lazily create a shallow clone of the `input` object if we haven't done so already.
188-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- TODO: not sure if there's a good solution
189-
clone = clone ?? (Array.isArray(input) ? [...input] : { ...input });
188+
clone ??= shallowCloneObject(input);
190189

191190
// Overwrite the current property `key` in the clone with the `replaced` value.
192191
clone[key] = replaced;

packages/dds/shared-object-base/src/test/serializer.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@ describe("FluidSerializer", () => {
6060
// Verify that `encode` is a no-op for these simple cases.
6161
for (const input of simple) {
6262
it(`${printHandle(input)} -> ${JSON.stringify(input)}`, () => {
63-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- test deals with several object shapes
6463
const actual = serializer.encode(input, handle);
6564
assert.strictEqual(
6665
actual,
6766
input,
6867
"encode() on input with no handles must return original input.",
6968
);
7069

71-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- test deals with several object shapes
7270
const decoded = serializer.decode(actual);
7371
assert.strictEqual(
7472
decoded,
@@ -120,15 +118,13 @@ describe("FluidSerializer", () => {
120118

121119
for (const input of tricky) {
122120
it(`${printHandle(input)} -> ${JSON.stringify(input)}`, () => {
123-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- test deals with several object shapes
124121
const actual = serializer.encode(input, handle);
125122
assert.strictEqual(
126123
actual,
127124
input,
128125
"encode() on input with no handles must return original input.",
129126
);
130127

131-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- test deals with several object shapes
132128
const decoded = serializer.decode(actual);
133129
assert.strictEqual(
134130
decoded,
@@ -177,7 +173,6 @@ describe("FluidSerializer", () => {
177173

178174
function check(decodedForm, encodedForm): void {
179175
it(`${printHandle(decodedForm)} -> ${JSON.stringify(encodedForm)}`, () => {
180-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- TODO: update when we can fix the return type of serializer.encode()
181176
const replaced = serializer.encode(decodedForm, handle);
182177
assert.notStrictEqual(
183178
replaced,
@@ -186,11 +181,9 @@ describe("FluidSerializer", () => {
186181
);
187182
assert.deepStrictEqual(replaced, encodedForm, "encode() must return expected output.");
188183

189-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- TODO: update when we can fix the return type of serializer.encode()
190184
const replacedTwice = serializer.encode(replaced, handle);
191185
assert.deepStrictEqual(replacedTwice, replaced, "encode should be idempotent");
192186

193-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- TODO: update when we can fix the return type of serializer.decode()
194187
const decodedRoundTrip = serializer.decode(replaced);
195188
assert.notStrictEqual(
196189
decodedRoundTrip,
@@ -203,7 +196,6 @@ describe("FluidSerializer", () => {
203196
"input must round-trip through encode()/decode().",
204197
);
205198

206-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- TODO: update when we can fix the return type of serializer.decode()
207199
const decodedTwice = serializer.decode(decodedRoundTrip);
208200
assert.deepStrictEqual(decodedTwice, decodedRoundTrip, "decode should be idempotent");
209201

@@ -251,15 +243,13 @@ describe("FluidSerializer", () => {
251243
input.h = handle; // eslint-disable-line @typescript-eslint/no-unsafe-member-access
252244
input.o1.h = handle; // eslint-disable-line @typescript-eslint/no-unsafe-member-access
253245

254-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- TODO: update when we can fix the return type of serializer.encode()
255246
const replaced = serializer.encode(input, handle);
256247
assert.notStrictEqual(
257248
replaced,
258249
input,
259250
"encode() must shallow-clone rather than mutate original object.",
260251
);
261252

262-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- TODO: update when we can fix the return type of serializer.decode()
263253
const decoded = serializer.decode(replaced);
264254
assert.notStrictEqual(
265255
decoded,

0 commit comments

Comments
 (0)