Skip to content

Ensure moved files don't result in broken imports #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2020
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
111 changes: 111 additions & 0 deletions packages/tsserver-plugin/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,117 @@ describe('tsserver plugin', () => {
});
});

describe('renaming files', () => {
test('moving a transformed file updates its imports', async () => {
await project.open({
'dir/greeting.ts': stripIndent`
import Component, { hbs } from '@glimmerx/component';

export interface GreetingArgs {
message: string;
}

export default class Greeting extends Component<GreetingArgs> {
static template = hbs\`{{@message}}, World!\`;
}
`,
'dir/index.ts': stripIndent`
import Component, { hbs } from '@glimmerx/component';
import Greeting from './greeting';

export class Application extends Component {
static template = hbs\`
<Greeting @message="Hello" />
\`;
}
`,
});

let greetingMoveResult = await server.request(CommandTypes.GetEditsForFileRename, {
oldFilePath: project.filePath('dir/greeting.ts'),
newFilePath: project.filePath('other/greeting.ts'),
});

expect(greetingMoveResult).toMatchObject([
{
fileName: project.filePath('dir/index.ts'),
textChanges: [
{
start: { line: 2, offset: 23 },
end: { line: 2, offset: 33 },
newText: '../other/greeting',
},
],
},
]);

let indexMoveResult = await server.request(CommandTypes.GetEditsForFileRename, {
oldFilePath: project.filePath('dir/index.ts'),
newFilePath: project.filePath('other/index.ts'),
});

expect(indexMoveResult).toMatchObject([
{
fileName: project.filePath('dir/index.ts'),
textChanges: [
{
start: { line: 2, offset: 23 },
end: { line: 2, offset: 33 },
newText: '../dir/greeting',
},
],
},
]);
});

test('moving an untransformed file updates its imports', async () => {
await project.open({
'dir/foo.ts': stripIndent`
export const message = 'hello';
`,
'dir/index.ts': stripIndent`
export { message } from './foo';
`,
});

let greetingMoveResult = await server.request(CommandTypes.GetEditsForFileRename, {
oldFilePath: project.filePath('dir/foo.ts'),
newFilePath: project.filePath('other/foo.ts'),
});

expect(greetingMoveResult).toMatchObject([
{
fileName: project.filePath('dir/index.ts'),
textChanges: [
{
start: { line: 1, offset: 26 },
end: { line: 1, offset: 31 },
newText: '../other/foo',
},
],
},
]);

let indexMoveResult = await server.request(CommandTypes.GetEditsForFileRename, {
oldFilePath: project.filePath('dir/index.ts'),
newFilePath: project.filePath('other/index.ts'),
});

expect(indexMoveResult).toMatchObject([
{
fileName: project.filePath('dir/index.ts'),
textChanges: [
{
start: { line: 1, offset: 26 },
end: { line: 1, offset: 31 },
newText: '../dir/foo',
},
],
},
]);
});
});

describe('error recovery', () => {
test('introducing and fixing a template error with editor changes', async () => {
await project.open({
Expand Down
1 change: 1 addition & 0 deletions packages/tsserver-plugin/__tests__/test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class Project {

if (!this.openFiles.has(file)) {
if (!fs.existsSync(file)) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, fileContent ?? '');
}

Expand Down
5 changes: 3 additions & 2 deletions packages/tsserver-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const init: ts.server.PluginModuleFactory = ({ typescript: ts }) => {
return {
create(info) {
let logger = loggerFor(info);
let config = loadConfig(path.dirname(info.project.projectName));
let projectDir = path.dirname(info.project.projectName);
let config = loadConfig(projectDir);

logger.log('\nStarting @glint/tsserver-plugin at', new Date().toString());
logger.log('\nStarting @glint/tsserver-plugin in', projectDir, 'at', new Date().toString());

modules.addProject(config, info.project);

Expand Down
9 changes: 1 addition & 8 deletions packages/tsserver-plugin/src/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,7 @@ export default class GlintLanguageService implements Partial<ts.LanguageService>
formatOptions: ts.FormatCodeSettings,
preferences: ts.UserPreferences | undefined
): readonly ts.FileTextChanges[] {
let oldTransformedPath = isTransformablePath(oldFilePath)
? getTransformedPath(oldFilePath)
: oldFilePath;

let edits = [
...this.ls.getEditsForFileRename(oldTransformedPath, newFilePath, formatOptions, preferences),
...this.ls.getEditsForFileRename(oldFilePath, newFilePath, formatOptions, preferences),
];
let edits = this.ls.getEditsForFileRename(oldFilePath, newFilePath, formatOptions, preferences);

return edits.filter((edit) => !isTransformedPath(edit.fileName));
}
Expand Down