Repository navigation
Expand file tree
/
Copy pathauthored-source-codemod-39396.test.ts
More file actions
133 lines (115 loc) · 5.26 KB
/
Copy pathauthored-source-codemod-39396.test.ts
File metadata and controls
133 lines (115 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* `os migrate meta --write` over the stock generator's module shape:
* `export const X = …; export default X;`, with the barrel re-exporting `X`.
*
* The `export default X;` line is how the module hands its one value out, not
* a second use of it, so the edit is written. A real second use still refuses
* `[shared]`: another module that imports `X` (by name or as the default) and
* reads it, or the module itself reading `X` again.
*
* In-process over the planner, against temp projects with no dependencies: no
* process is spawned and no kernel is booted.
*/
import { afterAll, describe, expect, it } from 'vitest';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import type { MigrationApplication } from '@objectstack/spec/migrations';
import { planAuthoredSourceWrite } from './authored-source-codemod.js';
const root = mkdtempSync(join(tmpdir(), 'os-codemod-39396-'));
afterAll(() => rmSync(root, { recursive: true, force: true }));
let seq = 0;
function writeProject(files: Record<string, string>): string {
const dir = join(root, `case-${++seq}`);
for (const [rel, text] of Object.entries(files)) {
const path = join(dir, rel);
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, text);
}
return join(dir, 'objectstack.config.ts');
}
/** The object module `os generate object contact` writes, cut to two keys. */
const GENERATED = `export const contact = {
name: 'contact',
label: 'Contact',
};
export default contact;
`;
const CONFIG = `import * as objects from './src/objects';
export default { objects: Object.values(objects) };
`;
const applied: MigrationApplication = {
toMajor: 19, conversionId: 'probe-label', surface: 'probe', from: 'a', to: 'b', path: 'objects[0].label',
};
async function plan(files: Record<string, string>, extra: Record<string, unknown> = {}) {
const configPath = writeProject(files);
const loaded = { objects: [{ name: 'contact', label: 'Contact' }], ...extra };
return planAuthoredSourceWrite({
configPath,
config: loaded,
namedExports: [],
normalized: loaded,
migrated: { ...loaded, objects: [{ name: 'contact', label: 'People' }] },
applied: [applied],
});
}
const kinds = (p: Awaited<ReturnType<typeof plan>>) => p.manual.map((m) => m.refusal.kind);
describe('--write over the generator shape (#39396)', () => {
it('writes the edit: `export default X;` beside `export const X` is not a second use', async () => {
const p = await plan({
'objectstack.config.ts': CONFIG,
'src/objects/index.ts': "export { contact } from './contact.object';\n",
'src/objects/contact.object.ts': GENERATED,
});
expect(kinds(p)).toEqual([]);
expect(p.written.map((w) => w.application.conversionId)).toEqual(['probe-label']);
expect(p.rewrites).toHaveLength(1);
expect(p.rewrites[0]!.after).toBe(GENERATED.replace("label: 'Contact'", "label: 'People'"));
});
it('writes it through a barrel that re-exports the default, too', async () => {
const p = await plan({
'objectstack.config.ts': CONFIG,
'src/objects/index.ts': "export { default as contact } from './contact.object';\n",
'src/objects/contact.object.ts': GENERATED,
});
expect(kinds(p)).toEqual([]);
expect(p.written.map((w) => w.application.conversionId)).toEqual(['probe-label']);
});
it('still refuses [shared] when another module imports the default and reads it', async () => {
const p = await plan({
'objectstack.config.ts': `import * as objects from './src/objects';
import { mirror } from './src/mirror';
export default { objects: Object.values(objects), description: mirror };
`,
'src/objects/index.ts': "export { contact } from './contact.object';\n",
'src/objects/contact.object.ts': GENERATED,
'src/mirror.ts': "import person from './objects/contact.object';\n\nexport const mirror = person.label;\n",
}, { description: 'Contact' });
expect(kinds(p)).toEqual(['shared']);
expect(p.manual[0]!.refusal.reason).toContain('`contact`');
expect(p.rewrites).toEqual([]);
});
it('still refuses [shared] when another module imports the name and reads it', async () => {
const p = await plan({
'objectstack.config.ts': `import * as objects from './src/objects';
import { mirror } from './src/mirror';
export default { objects: Object.values(objects), description: mirror };
`,
'src/objects/index.ts': "export { contact } from './contact.object';\n",
'src/objects/contact.object.ts': GENERATED,
'src/mirror.ts': "import { contact } from './objects/contact.object';\n\nexport const mirror = contact.label;\n",
}, { description: 'Contact' });
expect(kinds(p)).toEqual(['shared']);
expect(p.rewrites).toEqual([]);
});
it('still refuses [shared] when the module itself reads the binding again', async () => {
const p = await plan({
'objectstack.config.ts': CONFIG,
'src/objects/index.ts': "export { contact } from './contact.object';\n",
'src/objects/contact.object.ts': `${GENERATED}\nexport const contactLabel = contact.label;\n`,
});
expect(kinds(p)).toEqual(['shared']);
expect(p.rewrites).toEqual([]);
});
});