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

feat: add isKnownTypesPackageName option #909

Merged
merged 1 commit into from
Jan 31, 2021
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
1 change: 1 addition & 0 deletions packages/bootstrap/lib/ts-morph-bootstrap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export interface ProjectOptions {
fileSystem?: FileSystemHost;
/** Creates a resolution host for specifying custom module and/or type reference directive resolution. */
resolutionHost?: ResolutionHostFactory;
isKnownTypesPackageName?: ts.LanguageServiceHost['isKnownTypesPackageName'];
}

/** Project that holds source files. */
Expand Down
2 changes: 2 additions & 0 deletions packages/bootstrap/src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ProjectOptions {
fileSystem?: FileSystemHost;
/** Creates a resolution host for specifying custom module and/or type reference directive resolution. */
resolutionHost?: ResolutionHostFactory;
isKnownTypesPackageName?: ts.LanguageServiceHost['isKnownTypesPackageName'];
}

/**
Expand Down Expand Up @@ -152,6 +153,7 @@ export class Project {
getNewLine: () => newLineKind,
resolutionHost: resolutionHost || {},
getProjectVersion: () => this._sourceFileCache.getProjectVersion().toString(),
isKnownTypesPackageName: options.isKnownTypesPackageName
});
this.languageServiceHost = languageServiceHost;
this.compilerHost = compilerHost;
Expand Down
1 change: 1 addition & 0 deletions packages/common/lib/ts-morph-common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export interface CreateHostsOptions {
/** Provides the current project version to be used to tell if source files have
* changed. Provide this for a performance improvement. */
getProjectVersion?: () => string;
isKnownTypesPackageName?: ts.LanguageServiceHost['isKnownTypesPackageName'];
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/common/src/compiler/createHosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ export interface CreateHostsOptions {
/** Provides the current project version to be used to tell if source files have
* changed. Provide this for a performance improvement. */
getProjectVersion?: () => string;
isKnownTypesPackageName?: ts.LanguageServiceHost['isKnownTypesPackageName'];
}

const returnTrue = () => true;

/**
* Creates a language service host and compiler host.
* @param options - Options for creating the hosts.
*/
export function createHosts(options: CreateHostsOptions) {
const { transactionalFileSystem, sourceFileContainer, compilerOptions, getNewLine, resolutionHost, getProjectVersion } = options;
const { transactionalFileSystem, sourceFileContainer, compilerOptions, getNewLine, resolutionHost, getProjectVersion, isKnownTypesPackageName } = options;
let version = 0;
const fileExistsSync = (path: StandardizedFilePath) =>
sourceFileContainer.containsSourceFileAtPath(path)
Expand Down Expand Up @@ -65,7 +68,8 @@ export function createHosts(options: CreateHostsOptions) {
);
}
},
useCaseSensitiveFileNames: () => true,
isKnownTypesPackageName: isKnownTypesPackageName || returnTrue,
Copy link
Owner

Choose a reason for hiding this comment

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

I seem to not be able to push commits to this PR, so I'm going to merge this as-is, but afterwards I'm going to remove this || returnTrue since I'd rather have undefined be the default as is done in the compiler api.

useCaseSensitiveFileNames: returnTrue,
readFile: (path, encoding) => {
const standardizedPath = transactionalFileSystem.getStandardizedAbsolutePath(path);
if (sourceFileContainer.containsSourceFileAtPath(standardizedPath))
Expand Down
18 changes: 14 additions & 4 deletions packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9542,15 +9542,25 @@ export declare class Type<TType extends ts.Type = ts.Type> {
/** Gets the string index type. */
getStringIndexType(): Type | undefined;
/**
* Gets the target type of a type reference if it exists.
* Returns the generic type when the type is a type reference, returns itself when it's
* already a generic type, or otherwise returns undefined.
*
* For example, given `Promise<string>` this will return `Promise<T>`.
* For example:
*
* - Given type reference `Promise<string>` returns `Promise<T>`.
* - Given generic type `Promise<T>` returns the same `Promise<T>`.
* - Given `string` returns `undefined`.
*/
getTargetType(): Type<ts.GenericType> | undefined;
/**
* Gets the target type of a type reference or throws if it doesn't exist.
* Returns the generic type when the type is a type reference, returns itself when it's
* already a generic type, or otherwise throws an error.
*
* For example:
*
* For example, given `Promise<string>` this will return `Promise<T>`.
* - Given type reference `Promise<string>` returns `Promise<T>`.
* - Given generic type `Promise<T>` returns the same `Promise<T>`.
* - Given `string` throws an error.
*/
getTargetTypeOrThrow(): Type<ts.GenericType>;
/** Gets type arguments. */
Expand Down