Skip to content
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

Remove unnecessary assert #37059

Merged
merged 1 commit into from
Feb 26, 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
9 changes: 2 additions & 7 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,7 @@ namespace ts.server {
this.openFilesWithNonRootedDiskPath.set(this.toCanonicalFileName(fileName), info);
}
}
if (openedByClient && !info.isScriptOpen()) {
if (openedByClient) {
// Opening closed script info
// either it was created just now, or was part of projects but was closed
this.stopWatchingScriptInfo(info);
Expand All @@ -2423,9 +2423,6 @@ namespace ts.server {
info.registerFileUpdate();
}
}
else {
Debug.assert(fileContent === undefined);
}
return info;
}

Expand Down Expand Up @@ -3170,11 +3167,9 @@ namespace ts.server {
const iterResult = openFiles.next();
if (iterResult.done) break;
const file = iterResult.value;
const scriptInfo = this.getScriptInfo(file.fileName);
Debug.assert(!scriptInfo || !scriptInfo.isScriptOpen(), "Script should not exist and not be open already");
// Create script infos so we have the new content for all the open files before we do any updates to projects
const info = this.getOrCreateOpenScriptInfo(
scriptInfo ? scriptInfo.fileName : toNormalizedPath(file.fileName),
toNormalizedPath(file.fileName),
file.content,
tryConvertScriptKindName(file.scriptKind!),
file.hasMixedContent,
Expand Down
51 changes: 42 additions & 9 deletions src/testRunner/unittests/tsserver/applyChangesToOpenFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ ${file.content}`;
assert.equal(Number(project.getProjectVersion()), expected);
}

function verify(applyChangesToOpen: (session: TestSession) => void) {
interface Verify {
applyChangesToOpen: (session: TestSession) => void;
openFile1Again: (session: TestSession) => void;
}
function verify({ applyChangesToOpen, openFile1Again }: Verify) {
const host = createServerHost([app, file3, commonFile1, commonFile2, libFile, configFile]);
const session = createSession(host);
session.executeCommandSeq<protocol.OpenRequest>({
Expand Down Expand Up @@ -65,11 +69,22 @@ ${file.content}`;
verifyText(service, commonFile2.path, fileContentWithComment(commonFile2));
verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;");
verifyText(service, file3.path, file3.content);

// Open file1 again
openFile1Again(session);
assert.isTrue(service.getScriptInfo(commonFile1.path)!.isScriptOpen());

// Verify that file1 contents are changed
verifyProjectVersion(project, 4);
verifyText(service, commonFile1.path, commonFile1.content);
verifyText(service, commonFile2.path, fileContentWithComment(commonFile2));
verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;");
verifyText(service, file3.path, file3.content);
}

it("with applyChangedToOpenFiles request", () => {
verify(session =>
session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
verify({
applyChangesToOpen: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
command: protocol.CommandTypes.ApplyChangedToOpenFiles,
arguments: {
openFiles: [
Expand Down Expand Up @@ -101,13 +116,22 @@ ${file.content}`;
file3.path
]
}
})
);
}),
openFile1Again: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
command: protocol.CommandTypes.ApplyChangedToOpenFiles,
arguments: {
openFiles: [{
fileName: commonFile1.path,
content: commonFile1.content
}]
}
}),
});
});

it("with updateOpen request", () => {
verify(session =>
session.executeCommandSeq<protocol.UpdateOpenRequest>({
verify({
applyChangesToOpen: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
openFiles: [
Expand Down Expand Up @@ -141,8 +165,17 @@ ${file.content}`;
file3.path
]
}
})
);
}),
openFile1Again: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
openFiles: [{
file: commonFile1.path,
fileContent: commonFile1.content
}]
}
}),
});
});
});
}
24 changes: 24 additions & 0 deletions src/testRunner/unittests/tsserver/openFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,29 @@ namespace ts.projectSystem {
checkProjectActualFiles(project, files.map(f => f.path));
}
});

it("can open same file again", () => {
const projectFolder = "/user/someuser/projects/myproject";
const aFile: File = {
path: `${projectFolder}/src/a.ts`,
content: "export const x = 0;"
};
const configFile: File = {
path: `${projectFolder}/tsconfig.json`,
content: "{}"
};
const files = [aFile, configFile, libFile];
const host = createServerHost(files);
const service = createProjectService(host);
verifyProject(aFile.content);
verifyProject(`${aFile.content}export const y = 10;`);

function verifyProject(aFileContent: string) {
service.openClientFile(aFile.path, aFileContent, ScriptKind.TS, projectFolder);
const project = service.configuredProjects.get(configFile.path)!;
checkProjectActualFiles(project, files.map(f => f.path));
assert.equal(project.getCurrentProgram()?.getSourceFile(aFile.path)!.text, aFileContent);
}
});
});
}