Skip to content

Commit b8e1441

Browse files
Fix mutation regression with numeric arguments in decorators (#9319)
fix #9318
1 parent 2685668 commit b8e1441

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
3+
changeKind: internal
4+
packages:
5+
- "@typespec/compiler"
6+
---
7+
8+
Fix mutation regression with numeric arguments in decorators

packages/compiler/src/experimental/mutators.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { compilerAssert } from "../core/diagnostics.js";
22
import { getLocationContext } from "../core/helpers/location-context.js";
3+
import { isNumeric } from "../core/numeric.js";
34
import { Program } from "../core/program.js";
45
import { isTemplateInstance, isType, isValue } from "../core/type-utils.js";
56
import {
@@ -738,10 +739,9 @@ function createMutatorEngine(
738739
}
739740
}
740741

741-
function mutateDecoratorArgumentValue<T extends DecoratorArgument["jsValue"]>(
742-
value: T,
743-
newMutators: Set<MutatorAll>,
744-
): T {
742+
function mutateDecoratorArgumentValue<
743+
T extends DecoratorArgument["jsValue"] | DecoratorArgument["value"],
744+
>(value: T, newMutators: Set<MutatorAll>): T {
745745
if (typeof value === "object" && value !== null) {
746746
if (isType(value as any)) {
747747
return isMutableTypeWithNamespace(value as any)
@@ -751,6 +751,9 @@ function createMutatorEngine(
751751
if (isValue(value as any)) {
752752
return mutateValue(value as any, newMutators) as T;
753753
}
754+
if (isNumeric(value)) {
755+
return value;
756+
}
754757
if (Array.isArray(value)) {
755758
return value.map((item) => mutateDecoratorArgumentValue(item as any, newMutators)) as T;
756759
}

packages/compiler/test/experimental/mutator.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import {
88
MutatorWithNamespace,
99
} from "../../src/experimental/mutators.js";
1010
import { Model, ModelProperty, Namespace, Operation } from "../../src/index.js";
11+
import { t } from "../../src/testing/index.js";
1112
import { createTestHost } from "../../src/testing/test-host.js";
1213
import { createTestWrapper, expectTypeEquals } from "../../src/testing/test-utils.js";
1314
import { BasicTestRunner, TestHost } from "../../src/testing/types.js";
15+
import { Tester } from "../tester.js";
1416

1517
let host: TestHost;
1618
let runner: BasicTestRunner;
@@ -481,6 +483,21 @@ describe("decorators", () => {
481483
expect(visited).toStrictEqual(["Foo", "a", "E"]);
482484
});
483485

486+
// Regression test for https://github.com/microsoft/typespec/issues/9318
487+
it("doesn't crash when mutating numeric value", async () => {
488+
const { prop, program } = await Tester.compile(t.code`
489+
model Test {
490+
@minValue(123)
491+
${t.modelProperty("prop")}: int32;
492+
}
493+
`);
494+
const mutator: Mutator = {
495+
name: "test",
496+
ModelProperty: { mutate: (_, clone) => {} },
497+
};
498+
expect(() => mutateSubgraph(program, [mutator], prop)).not.toThrow();
499+
});
500+
484501
// Regression test for https://github.com/microsoft/typespec/issues/6655
485502
it("doesn't crash when mutating null value", async () => {
486503
const host = await createTestHost();

0 commit comments

Comments
 (0)