refactor: emit irprogram instead of yarndocument

This commit is contained in:
2026-04-14 23:54:04 +08:00
parent 0ecccb6fee
commit f5989a162b
11 changed files with 43 additions and 112 deletions
+10 -41
View File
@@ -14,14 +14,14 @@ describe('loadYarnProject', () => {
expect(result.project.projectName).toBe('Simple Test Project');
expect(result.project.sourceFiles).toEqual(['**/*.yarn']);
expect(result.project.baseLanguage).toBe('en');
expect(result.yarnFiles.length).toBeGreaterThan(0);
expect(Object.keys(result.program.nodes).length).toBeGreaterThan(0);
});
it('should parse all yarn files in simple project', async () => {
it('should compile all yarn files in simple project', async () => {
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
const result = await loadYarnProject(projectPath);
const titles = result.yarnFiles.flatMap(f => f.document.nodes.map(n => n.title));
const titles = Object.keys(result.program.nodes);
expect(titles).toContain('Start');
expect(titles).toContain('Greeting');
expect(titles).toContain('AnotherNode');
@@ -35,7 +35,7 @@ describe('loadYarnProject', () => {
expect(result.project.baseLanguage).toBe('en');
expect(result.project.localisation).toBeDefined();
expect(result.project.localisation!['zh-Hans']).toBeDefined();
expect(result.yarnFiles.length).toBeGreaterThan(0);
expect(Object.keys(result.program.nodes).length).toBeGreaterThan(0);
});
it('should load a complex project with multiple patterns', async () => {
@@ -48,51 +48,21 @@ describe('loadYarnProject', () => {
expect(result.project.sourceFiles).toContain('scripts/**/*.yarn');
});
it('should exclude files matching excludeFiles patterns', async () => {
const projectPath = resolve(__dirname, 'fixtures/complex/.yarnproject');
const result = await loadYarnProject(projectPath);
// Check that backup file is excluded
const backupFiles = result.yarnFiles.filter(f =>
f.relativePath.includes('backup'),
);
expect(backupFiles).toHaveLength(0);
});
it('should include files from multiple source patterns', async () => {
const projectPath = resolve(__dirname, 'fixtures/complex/.yarnproject');
const result = await loadYarnProject(projectPath);
const relativePaths = result.yarnFiles.map(f => f.relativePath);
// Should include dialogue files
expect(relativePaths.some(p => p.includes('dialogue'))).toBe(true);
// Should include script files
expect(relativePaths.some(p => p.includes('scripts'))).toBe(true);
});
it('should parse yarn documents correctly', async () => {
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
const result = await loadYarnProject(projectPath);
const startNode = result.yarnFiles
.flatMap(f => f.document.nodes)
.find(n => n.title === 'Start');
const startNode = result.program.nodes['Start'];
expect(startNode).toBeDefined();
expect(startNode!.body.length).toBeGreaterThan(0);
expect(startNode && 'instructions' in startNode ? startNode.instructions.length : 0).toBeGreaterThan(0);
});
it('should handle nodes with tags', async () => {
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
const result = await loadYarnProject(projectPath);
const greetingNode = result.yarnFiles
.flatMap(f => f.document.nodes)
.find(n => n.title === 'Greeting');
const greetingNode = result.program.nodes['Greeting'];
expect(greetingNode).toBeDefined();
expect(greetingNode!.nodeTags).toContain('greeting');
});
it('should throw LoadError for non-existent file', async () => {
@@ -104,7 +74,6 @@ describe('loadYarnProject', () => {
it('should throw error for invalid JSON', async () => {
const projectPath = resolve(__dirname, 'fixtures/invalid/.yarnproject');
// This will fail because the fixture doesn't exist
await expect(loadYarnProject(projectPath)).rejects.toThrow();
});
});
@@ -115,14 +84,14 @@ describe('loadYarnProjectSync', () => {
const result = loadYarnProjectSync(projectPath);
expect(result.project.projectFileVersion).toBe(4);
expect(result.yarnFiles.length).toBeGreaterThan(0);
expect(Object.keys(result.program.nodes).length).toBeGreaterThan(0);
});
it('should parse all yarn files synchronously', () => {
it('should compile all yarn files synchronously', () => {
const projectPath = resolve(__dirname, 'fixtures/simple/.yarnproject');
const result = loadYarnProjectSync(projectPath);
const titles = result.yarnFiles.flatMap(f => f.document.nodes.map(n => n.title));
const titles = Object.keys(result.program.nodes);
expect(titles).toContain('Start');
});
});
+1 -1
View File
@@ -60,7 +60,7 @@ describe('yarnSpinnerPlugin (esbuild)', () => {
expect(result).toBeDefined();
expect(result.contents).toContain('export default');
expect(result.contents).toContain('project');
expect(result.contents).toContain('yarnFiles');
expect(result.contents).toContain('program');
expect(result.loader).toBe('js');
});
+1 -1
View File
@@ -39,7 +39,7 @@ describe('yarnSpinnerRollup', () => {
expect(typeof result).toBe('string');
expect(result).toContain('export default');
expect(result).toContain('project');
expect(result).toContain('yarnFiles');
expect(result).toContain('program');
});
it('should return null for non-.yarnproject files', async () => {
+1 -1
View File
@@ -39,7 +39,7 @@ describe('yarnSpinnerVite', () => {
expect(typeof result).toBe('string');
expect(result).toContain('export default');
expect(result).toContain('project');
expect(result).toContain('yarnFiles');
expect(result).toContain('program');
});
it('should return null for non-.yarnproject files', async () => {
+1 -1
View File
@@ -26,7 +26,7 @@ describe('YarnSpinnerWebpackLoader', () => {
expect(typeof result).toBe('string');
expect(result).toContain('export default');
expect(result).toContain('project');
expect(result).toContain('yarnFiles');
expect(result).toContain('program');
// Ensure no errors were emitted
expect(mockContext.emitError).not.toHaveBeenCalled();
});