Skip to content

Commit

Permalink
Merge pull request #21392 from Microsoft/vscodeDynamicFiles
Browse files Browse the repository at this point in the history
[release-2.7] Allow dynamic files without external project and also use file names starting with ^ as dynamic file
  • Loading branch information
sheetalkamat authored Jan 25, 2018
2 parents cac6052 + dba61c5 commit 47e29fa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
39 changes: 39 additions & 0 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,45 @@ namespace ts.projectSystem {
const options = project.getCompilerOptions();
assert.equal(options.outDir, "C:/a/b", "");
});

it("dynamic file without external project", () => {
const file: FileOrFolder = {
path: "^walkThroughSnippet:/Users/UserName/projects/someProject/out/someFile#1.js",
content: "var x = 10;"
};
const host = createServerHost([libFile], { useCaseSensitiveFileNames: true });
const projectService = createProjectService(host);
projectService.setCompilerOptionsForInferredProjects({
module: ModuleKind.CommonJS,
allowJs: true,
allowSyntheticDefaultImports: true,
allowNonTsExtensions: true
});
projectService.openClientFile(file.path, "var x = 10;");

projectService.checkNumberOfProjects({ inferredProjects: 1 });
const project = projectService.inferredProjects[0];
checkProjectRootFiles(project, [file.path]);
checkProjectActualFiles(project, [file.path, libFile.path]);

assert.strictEqual(projectService.getDefaultProjectForFile(server.toNormalizedPath(file.path), /*ensureProject*/ true), project);
const indexOfX = file.content.indexOf("x");
assert.deepEqual(project.getLanguageService(/*ensureSynchronized*/ true).getQuickInfoAtPosition(file.path, indexOfX), {
kind: ScriptElementKind.variableElement,
kindModifiers: "",
textSpan: { start: indexOfX, length: 1 },
displayParts: [
{ text: "var", kind: "keyword" },
{ text: " ", kind: "space" },
{ text: "x", kind: "localName" },
{ text: ":", kind: "punctuation" },
{ text: " ", kind: "space" },
{ text: "number", kind: "keyword" }
],
documentation: [],
tags: []
});
});
});

describe("tsserverProjectSystem Proper errors", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ namespace ts.server {

const project = this.getOrCreateInferredProjectForProjectRootPathIfEnabled(info, projectRootPath) ||
this.getOrCreateSingleInferredProjectIfEnabled() ||
this.createInferredProject(getDirectoryPath(info.path));
this.createInferredProject(info.isDynamic ? this.currentDirectory : getDirectoryPath(info.path));

project.addRoot(info);
project.updateGraph();
Expand Down Expand Up @@ -1655,7 +1655,7 @@ namespace ts.server {
}

private getOrCreateInferredProjectForProjectRootPathIfEnabled(info: ScriptInfo, projectRootPath: NormalizedPath | undefined): InferredProject | undefined {
if (!this.useInferredProjectPerProjectRoot) {
if (info.isDynamic || !this.useInferredProjectPerProjectRoot) {
return undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/scriptInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ namespace ts.server {

/*@internal*/
export function isDynamicFileName(fileName: NormalizedPath) {
return getBaseFileName(fileName)[0] === "^";
return fileName[0] === "^" || getBaseFileName(fileName)[0] === "^";
}

export class ScriptInfo {
Expand Down

0 comments on commit 47e29fa

Please sign in to comment.