Skip to content

Commit

Permalink
wire up the optional projectDirectory for @types support. Closes #231
Browse files Browse the repository at this point in the history
  • Loading branch information
basarat committed Oct 9, 2016
1 parent 7f9f306 commit ba75e80
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/app/monaco/model/classifierCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {TypedEvent} from "../../../common/events";
import * as lsh from "../../../languageServiceHost/languageServiceHost";

const languageServiceHost = new lsh.LanguageServiceHost();
const languageServiceHost = new lsh.LanguageServiceHost(undefined);
const languageService = ts.createLanguageService(languageServiceHost);

export function addFile(filePath: string, contents: string) {
Expand Down
23 changes: 14 additions & 9 deletions src/languageServiceHost/languageServiceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ export class LSHost implements ts.LanguageServiceHost {
filenameToScript: ts.FileMap<ScriptInfo>;
roots: ScriptInfo[] = [];

/** BAS: added compilation settings as an option */
constructor(public compilerOptions = defaultCompilerOptions) {
/**
* BAS: add configuration arguments
* - projectDirectory,
* - compilerOptions
*/
constructor(public projectDirectory: string | undefined, public compilerOptions = defaultCompilerOptions) {
this.filenameToScript = createFileMap<ScriptInfo>();
}

Expand Down Expand Up @@ -274,10 +278,9 @@ export class LSHost implements ts.LanguageServiceHost {
return this.getScriptInfo(filename).svc.latestVersion().toString();
}

// BAS We work with full paths
getCurrentDirectory(): string {
return "";
}
// BAS : wired to config. Needed for proper `@types` expansion.
// See the child class `LanguageServiceHost` implementation below (with more comments).
getCurrentDirectory = () => this.projectDirectory;

getScriptIsOpen(filename: string) {
return this.getScriptInfo(filename).isOpen;
Expand Down Expand Up @@ -433,15 +436,17 @@ export class LanguageServiceHost extends LSHost {
*/
getDirectories = ts.sys ? ts.sys.getDirectories : undefined;

/** For @types expansion */
/**
* For @types expansion, these two functions are needed.
*/
directoryExists = ts.sys ? ts.sys.directoryExists : undefined;
getCurrentDirectory() {
getCurrentDirectory = () => {
/**
* TODO: use the same path as the path of tsconfig.json (if any)
* `undefined` is handled correctly in the compiler source :
* https://github.com/Microsoft/TypeScript/blob/02493de5ccd9e8c4c901bb154ba584dee392bd14/src/compiler/moduleNameResolver.ts#L98
*/
return undefined;
return this.projectDirectory;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/server/lang/config/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Project {
languageService: ts.LanguageService;
languageServiceHost: lsh.LanguageServiceHost;
constructor() {
this.languageServiceHost = new lsh.LanguageServiceHost({
this.languageServiceHost = new lsh.LanguageServiceHost(undefined, {
allowNonTsExtensions: true,
allowJs: true,
noLib: true, // Will result in lots of errors but we don't want lib.d.ts completions
Expand Down
2 changes: 1 addition & 1 deletion src/server/workers/lang/core/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Project {
constructor(projectData: types.ProjectDataLoaded) {
this.configFile = projectData.configFile;

this.languageServiceHost = new LanguageServiceHost(projectData.configFile.project.compilerOptions);
this.languageServiceHost = new LanguageServiceHost(projectData.configFile.projectFilePath, projectData.configFile.project.compilerOptions);

// Add all the files
projectData.filePathWithContents.forEach(({filePath,contents}) => {
Expand Down
2 changes: 1 addition & 1 deletion src/server/workers/lint/lintWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace LinterImplementation {
/**
* Create the program
*/
const languageServiceHost = new LanguageServiceHost(projectData.configFile.project.compilerOptions);
const languageServiceHost = new LanguageServiceHost(undefined, projectData.configFile.project.compilerOptions);

// Add all the files
projectData.filePathWithContents.forEach(({filePath, contents}) => {
Expand Down

3 comments on commit ba75e80

@johnnyreilly
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @basarat ,

Does ts-loader need something like this? I haven't actually tried to use @types within a project yet and so have no idea. If I try is it likely to fail because it has to be catered for directly as you have here?

John

@basarat
Copy link
Member Author

@basarat basarat commented on ba75e80 Oct 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does ts-loader need something like this

Maybe. Basically the two key functions are getCurrentDirectory and directoryExists for TypeScript to do its compliation context expansion for types. Tsify actually needed this and the relevant discussion is here : TypeStrong/tsify#203 (comment)

@blakeembrey
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. See my comments in TypeStrong/ts-loader#247.

Please sign in to comment.