Skip to content

Commit 87df3b1

Browse files
Copilotkudoas
andauthored
feat: implement resolveModuleNames for type resolution in ERB virtual files (#29)
* Initial plan * Initial plan for fixing intersection type resolution in ERB virtual files Co-authored-by: kudoas <45157831+kudoas@users.noreply.github.com> * feat: implement resolveModuleNames for proper type resolution in ERB virtual files Co-authored-by: kudoas <45157831+kudoas@users.noreply.github.com> * fix: remove incorrect cleanup of non-existent test file Co-authored-by: kudoas <45157831+kudoas@users.noreply.github.com> * docs: update CHANGELOG with fix for intersection type resolution Co-authored-by: kudoas <45157831+kudoas@users.noreply.github.com> * chore: Refactor code structure for improved readability and maintainability --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kudoas <45157831+kudoas@users.noreply.github.com> Co-authored-by: Daichi <sakanactor624@gmail.com>
1 parent 622c8a0 commit 87df3b1

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

src/services/typescriptCompletionService.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,16 @@ export class TypeScriptCompletionService {
9191
getCompilationSettings: () => this.compilerOptions,
9292
getDefaultLibFileName: (options) => getDefaultLibFilePath(options),
9393
fileExists: (fileName) => fileName === this.fileName || sys.fileExists(fileName),
94-
readFile: (fileName) => (fileName === this.fileName ? this.content : sys.readFile(fileName))
94+
readFile: (fileName) => (fileName === this.fileName ? this.content : sys.readFile(fileName)),
95+
resolveModuleNames: (moduleNames, containingFile) => {
96+
return moduleNames.map((moduleName) => {
97+
const result = ts.resolveModuleName(moduleName, containingFile, this.compilerOptions, {
98+
fileExists: sys.fileExists,
99+
readFile: sys.readFile
100+
});
101+
return result.resolvedModule;
102+
});
103+
}
95104
};
96105
}
97106
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { ok, strictEqual } from 'node:assert';
2+
import * as path from 'node:path';
3+
import * as fs from 'node:fs';
4+
import * as os from 'node:os';
5+
6+
import { TypeScriptCompletionService } from '../../services/typescriptCompletionService';
7+
8+
suite('TypeScriptCompletionService', () => {
9+
test('provides completions for simple JavaScript', () => {
10+
const service = new TypeScriptCompletionService();
11+
const jsContent = 'const answer = 42; answer.';
12+
service.updateContent(jsContent);
13+
14+
const completions = service.getCompletions(jsContent.length);
15+
ok(completions, 'Expected completions to be defined');
16+
ok(completions.entries.length > 0, 'Expected at least one completion');
17+
18+
// Should have Number methods
19+
const hasToFixed = completions.entries.some((e) => e.name === 'toFixed');
20+
ok(hasToFixed, 'Expected to find Number.toFixed method');
21+
});
22+
23+
test('provides hover information for variables', () => {
24+
const service = new TypeScriptCompletionService();
25+
const jsContent = 'const answer = 42;';
26+
service.updateContent(jsContent);
27+
28+
const offset = jsContent.indexOf('answer');
29+
const info = service.getQuickInfo(offset);
30+
ok(info, 'Expected quick info to be defined');
31+
ok(info.displayParts, 'Expected display parts to be defined');
32+
33+
const display = info.displayParts.map((part) => part.text).join('');
34+
ok(display.includes('answer'), 'Expected hover to include variable name');
35+
});
36+
37+
test('resolves module imports with JSDoc types', () => {
38+
// Create a temporary directory with a type definition file
39+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'erb-test-'));
40+
const typeDefPath = path.join(tmpDir, 'types.js');
41+
const testFilePath = path.join(tmpDir, 'test.js');
42+
43+
try {
44+
// Create a type definition file
45+
fs.writeFileSync(
46+
typeDefPath,
47+
`
48+
/**
49+
* @typedef {Object} User
50+
* @property {string} name
51+
* @property {number} age
52+
*/
53+
54+
/**
55+
* @typedef {Object} Address
56+
* @property {string} street
57+
* @property {string} city
58+
*/
59+
`
60+
);
61+
62+
// Create a test file that imports and uses intersection types
63+
const jsContent = `
64+
/**
65+
* @typedef {import('./types').User} User
66+
* @typedef {import('./types').Address} Address
67+
* @typedef {User & Address} UserWithAddress
68+
*/
69+
70+
/** @type {UserWithAddress} */
71+
const person = { name: 'John', age: 30, street: '123 Main', city: 'NYC' };
72+
person.
73+
`;
74+
75+
const service = new TypeScriptCompletionService();
76+
// Update with the full path as the containing file
77+
service.updateContent(jsContent.trim(), testFilePath);
78+
79+
const completions = service.getCompletions(jsContent.trim().length);
80+
ok(completions, 'Expected completions to be defined');
81+
ok(completions.entries.length > 0, 'Expected at least one completion');
82+
83+
// Should have properties from both User and Address types
84+
const hasName = completions.entries.some((e) => e.name === 'name');
85+
const hasAge = completions.entries.some((e) => e.name === 'age');
86+
const hasStreet = completions.entries.some((e) => e.name === 'street');
87+
const hasCity = completions.entries.some((e) => e.name === 'city');
88+
89+
ok(hasName, 'Expected to find name property from User type');
90+
ok(hasAge, 'Expected to find age property from User type');
91+
ok(hasStreet, 'Expected to find street property from Address type');
92+
ok(hasCity, 'Expected to find city property from Address type');
93+
} finally {
94+
// Cleanup
95+
try {
96+
fs.unlinkSync(typeDefPath);
97+
fs.rmdirSync(tmpDir);
98+
} catch (e) {
99+
// Ignore cleanup errors
100+
}
101+
}
102+
});
103+
104+
test('updates version when content changes', () => {
105+
const service = new TypeScriptCompletionService();
106+
const version1 = service.getVirtualFileName();
107+
108+
service.updateContent('const x = 1;');
109+
const completions1 = service.getCompletions(15);
110+
ok(completions1, 'Expected first completions to be defined');
111+
112+
service.updateContent('const y = 2;');
113+
const completions2 = service.getCompletions(15);
114+
ok(completions2, 'Expected second completions to be defined');
115+
116+
// Version tracking is internal, but we can verify service still works
117+
ok(true, 'Service handled multiple content updates');
118+
});
119+
});

0 commit comments

Comments
 (0)