Skip to content

Commit f7e6a99

Browse files
committed
fixes
1 parent d23a24c commit f7e6a99

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

packages/codemod/src/migrations/v1-to-v2/transforms/specSchemaAccess.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function handleReference(
144144

145145
if (parent && Node.isShorthandPropertyAssignment(parent)) {
146146
const line = ref.getStartLineNumber();
147-
parent.replaceWithText(`${localName}: specTypeSchemas.${typeName}`);
147+
parent.replaceWithText(`'${localName}': specTypeSchemas.${typeName}`);
148148
ensureImport(sourceFile, 'specTypeSchemas');
149149
diagnostics.push(
150150
warning(
@@ -156,6 +156,14 @@ function handleReference(
156156
return true;
157157
}
158158

159+
if (parent && Node.isPropertyAssignment(parent) && parent.getNameNode() === ref) {
160+
return false;
161+
}
162+
163+
if (parent && Node.isBindingElement(parent) && parent.getPropertyNameNode() === ref) {
164+
return false;
165+
}
166+
159167
// Value position: replace identifier with specTypeSchemas.X
160168
const line = ref.getStartLineNumber();
161169
ref.replaceWithText(`specTypeSchemas.${typeName}`);

packages/codemod/test/v1-to-v2/transforms/specSchemaAccess.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,35 @@ describe('spec-schema-access transform', () => {
192192
expect(result.changesCount).toBe(0);
193193
});
194194

195-
it('expands shorthand property assignment', () => {
195+
it('expands shorthand property assignment and removes import', () => {
196196
const input = [`import { ToolSchema } from '@modelcontextprotocol/server';`, `const schemas = { ToolSchema };`, ''].join('\n');
197197
const { text, result } = applyTransform(input);
198-
expect(text).toContain('ToolSchema: specTypeSchemas.Tool');
198+
expect(text).toContain("'ToolSchema': specTypeSchemas.Tool");
199+
expect(text).not.toMatch(/import\s*\{[^}]*ToolSchema[^}]*\}/);
199200
expect(result.changesCount).toBeGreaterThan(0);
200201
});
202+
203+
it('skips PropertyAssignment name-node (non-shorthand)', () => {
204+
const input = [
205+
`import { ToolSchema } from '@modelcontextprotocol/server';`,
206+
`const schemas = { ToolSchema: myValidator };`,
207+
''
208+
].join('\n');
209+
const { text, result } = applyTransform(input);
210+
expect(text).toContain('ToolSchema: myValidator');
211+
expect(result.changesCount).toBe(0);
212+
});
213+
214+
it('skips BindingElement property-name', () => {
215+
const input = [
216+
`import { ToolSchema } from '@modelcontextprotocol/server';`,
217+
`const { ToolSchema: local } = obj;`,
218+
''
219+
].join('\n');
220+
const { text, result } = applyTransform(input);
221+
expect(text).toContain('ToolSchema: local');
222+
expect(result.changesCount).toBe(0);
223+
});
201224
});
202225

203226
describe('aliased imports', () => {

0 commit comments

Comments
 (0)