Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Fix: `missing-index` diagnostic showing at the wrong location",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
2 changes: 1 addition & 1 deletion packages/compiler/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4823,7 +4823,7 @@ export function createChecker(program: Program): Checker {
indexType: getTypeName(target.indexer.key),
sourceType: getTypeName(source),
},
target,
target: diagnosticTarget,
}),
],
];
Expand Down
35 changes: 22 additions & 13 deletions packages/compiler/test/checker/relation.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { deepStrictEqual, ok, strictEqual } from "assert";
import { Model } from "../../core/index.js";
import { Diagnostic, Model, ModelPropertyNode } from "../../core/index.js";
import {
BasicTestRunner,
createTestHost,
createTestWrapper,
DiagnosticMatch,
expectDiagnosticEmpty,
expectDiagnostics,
extractCursor,
} from "../../testing/index.js";

interface RelatedTypeOptions {
Expand All @@ -21,32 +22,40 @@ describe("compiler: checker: type relations", () => {
runner = createTestWrapper(await createTestHost());
});

async function checkTypeAssignable({ source, target, commonCode }: RelatedTypeOptions) {
const { Test } = (await runner.compile(`
async function checkTypeAssignable({ source, target, commonCode }: RelatedTypeOptions): Promise<{
related: boolean;
diagnostics: readonly Diagnostic[];
expectedDiagnosticPos: number;
}> {
const { source: code, pos } = extractCursor(`
${commonCode ?? ""}

@test model Test {
source: ${source};
source: ${source};
target: ${target};
}`)) as { Test: Model };
}`);
const { Test } = (await runner.compile(code)) as { Test: Model };
const sourceProp = Test.properties.get("source")!.type;
const targetProp = Test.properties.get("target")!.type;
return runner.program.checker.isTypeAssignableTo(sourceProp, targetProp, targetProp);

const [related, diagnostics] = runner.program.checker.isTypeAssignableTo(
sourceProp,
targetProp,
(Test.properties.get("source")!.node! as ModelPropertyNode).value
);
return { related, diagnostics, expectedDiagnosticPos: pos };
}

async function expectTypeAssignable(options: RelatedTypeOptions) {
const [related, diagnostics] = await checkTypeAssignable(options);
const { related, diagnostics } = await checkTypeAssignable(options);
expectDiagnosticEmpty(diagnostics);
ok(related, `Type ${options.source} should be assignable to ${options.target}`);
}

async function expectTypeNotAssignable(
options: RelatedTypeOptions,
match: DiagnosticMatch | DiagnosticMatch[]
) {
const [related, diagnostics] = await checkTypeAssignable(options);
async function expectTypeNotAssignable(options: RelatedTypeOptions, match: DiagnosticMatch) {
const { related, diagnostics, expectedDiagnosticPos } = await checkTypeAssignable(options);
ok(!related, `Type ${options.source} should NOT be assignable to ${options.target}`);
expectDiagnostics(diagnostics, match);
expectDiagnostics(diagnostics, { ...match, pos: expectedDiagnosticPos });
}

describe("model with indexer", () => {
Expand Down