Skip to content

Commit

Permalink
Fix allowJs import @types issue
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyreilly committed Oct 17, 2017
1 parent a504b11 commit f832bb6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/compilerSetup.ts
Expand Up @@ -44,9 +44,9 @@ export function getCompilerOptions(
configParseResult: typescript.ParsedCommandLine
) {
const compilerOptions = Object.assign({}, configParseResult.options, {
skipDefaultLibCheck: true,
skipLibCheck: true,
suppressOutputPathCheck: true, // This is why: https://github.com/Microsoft/TypeScript/issues/7363
});
} as typescript.CompilerOptions);

// if `module` is not specified and not using ES6+ target, default to CJS module output
if ((compilerOptions.module === undefined) &&
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Expand Up @@ -211,7 +211,7 @@ export interface ResolveSync {

export interface ModuleResolutionHost {
fileExists(fileName: string): boolean;
readFile(fileName: string): string;
readFile(fileName: string, encoding?: string | undefined): string | undefined;
}

export interface TSInstance {
Expand Down
44 changes: 30 additions & 14 deletions src/servicesHost.ts
Expand Up @@ -34,19 +34,27 @@ export function makeServicesHost(
// make a (sync) resolver that follows webpack's rules
const resolveSync = makeResolver(loader.options);

const readFileWithFallback = (path: string, encoding?: string | undefined): string | undefined =>
compiler.sys.readFile(path, encoding) || readFile(path, encoding);

const fileExists = (path: string) => compiler.sys.fileExists(path) || readFile(path) !== undefined;

const moduleResolutionHost: ModuleResolutionHost = {
fileExists: (fileName: string) => readFile(fileName) !== undefined,
readFile: (fileName: string) => readFile(fileName) || '',
fileExists,
readFile: readFileWithFallback
};

return {
const servicesHost: typescript.LanguageServiceHost = {
getProjectVersion: () => `${instance.version}`,

getScriptFileNames: () => Object.keys(files).filter(filePath => filePath.match(scriptRegex)),

getScriptVersion: (fileName: string) => {
fileName = path.normalize(fileName);
const file = files[fileName];
return file === undefined ? '' : file.version.toString();
},

getScriptSnapshot: (fileName: string) => {
// This is called any time TypeScript needs a file's text
// We either load from memory or from disk
Expand All @@ -73,23 +81,33 @@ export function makeServicesHost(
*/
directoryExists: compiler.sys ? (<any> compiler.sys).directoryExists : undefined,

useCaseSensitiveFileNames: () => compiler.sys.useCaseSensitiveFileNames,

// The following three methods are necessary for @types resolution from TS 2.4.1 onwards see: https://github.com/Microsoft/TypeScript/issues/16772
fileExists: compiler.sys ? (<any> compiler.sys).fileExists : undefined,
readFile: compiler.sys ? (<any> compiler.sys).readFile : undefined,
readDirectory: compiler.sys ? (<any> compiler.sys).readDirectory : undefined,

getCurrentDirectory: () => process.cwd(),
getCurrentDirectory: () => loader.context,

getCompilationSettings: () => compilerOptions,
getDefaultLibFileName: (options: typescript.CompilerOptions) => compiler.getDefaultLibFilePath(options),
getNewLine: () => newLine,
log: log.log,

resolveTypeReferenceDirectives: (typeDirectiveNames: string[], containingFile: string) =>
typeDirectiveNames.map(directive =>
compiler.resolveTypeReferenceDirective(directive, containingFile, compilerOptions, moduleResolutionHost).resolvedTypeReferenceDirective),

resolveModuleNames: (moduleNames: string[], containingFile: string) =>
resolveModuleNames(
resolveSync, moduleResolutionHost, appendTsSuffixTo, appendTsxSuffixTo, scriptRegex, instance,
moduleNames, containingFile),

getCustomTransformers: () => instance.transformers
};

return servicesHost;
}

function resolveModuleNames(
Expand All @@ -104,9 +122,8 @@ function resolveModuleNames(
) {
const resolvedModules = moduleNames.map(moduleName =>
resolveModuleName(resolveSync, moduleResolutionHost, appendTsSuffixTo, appendTsxSuffixTo, scriptRegex, instance,
moduleName, containingFile)
);

moduleName, containingFile));

populateDependencyGraphs(resolvedModules, instance, containingFile);

return resolvedModules;
Expand Down Expand Up @@ -159,12 +176,11 @@ function resolveModuleName(
resolvedFileName,
isExternalLibraryImport: tsResolution.resolvedModule.isExternalLibraryImport
};
if (resolutionResult!) {
if (resolutionResult!.resolvedFileName === tsResolutionResult.resolvedFileName ||
isJsImplementationOfTypings(resolutionResult!, tsResolutionResult)) {
resolutionResult!.isExternalLibraryImport = tsResolutionResult.isExternalLibraryImport;
}
} else {

if (resolutionResult! === undefined ||
resolutionResult!.resolvedFileName === tsResolutionResult.resolvedFileName ||
isJsImplementationOfTypings(resolutionResult!, tsResolutionResult)
) {
resolutionResult = tsResolutionResult;
}
}
Expand All @@ -177,7 +193,7 @@ function populateDependencyGraphs(
containingFile: string
) {
resolvedModules = resolvedModules
.filter(m => m !== null && m !== undefined);
.filter(mod => mod !== null && mod !== undefined);

instance.dependencyGraph[path.normalize(containingFile)] = resolvedModules;

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Expand Up @@ -84,10 +84,10 @@ export function formatErrors(
: [];
}

export function readFile(fileName: string) {
export function readFile(fileName: string, encoding: string | undefined = 'utf8') {
fileName = path.normalize(fileName);
try {
return fs.readFileSync(fileName, 'utf8');
return fs.readFileSync(fileName, encoding);
} catch (e) {
return undefined;
}
Expand Down
28 changes: 0 additions & 28 deletions test/comparison-tests/nolib/expectedOutput-2.5/output.txt
Expand Up @@ -2,34 +2,6 @@
bundle.js 2.49 kB 0 [emitted] main
[0] ./.test/nolib/app.ts 16 bytes {0} [built] [1 error]

ERROR in node_modules\@types\semver\index.d.ts
[tsl] ERROR in node_modules\@types\semver\index.d.ts(100,28)
 TS2304: Cannot find name 'Array'.

ERROR in node_modules\@types\semver\index.d.ts
[tsl] ERROR in node_modules\@types\semver\index.d.ts(100,70)
 TS2304: Cannot find name 'Array'.

ERROR in node_modules\@types\semver\index.d.ts
[tsl] ERROR in node_modules\@types\semver\index.d.ts(104,29)
 TS2304: Cannot find name 'Array'.

ERROR in node_modules\@types\semver\index.d.ts
[tsl] ERROR in node_modules\@types\semver\index.d.ts(104,71)
 TS2304: Cannot find name 'Array'.

ERROR in node_modules\@types\semver\index.d.ts
[tsl] ERROR in node_modules\@types\semver\index.d.ts(123,41)
 TS2304: Cannot find name 'Array'.

ERROR in node_modules\@types\semver\index.d.ts
[tsl] ERROR in node_modules\@types\semver\index.d.ts(127,41)
 TS2304: Cannot find name 'Array'.

ERROR in node_modules\@types\chalk\index.d.ts
[tsl] ERROR in node_modules\@types\chalk\index.d.ts(18,10)
 TS2370: A rest parameter must be of an array type.

ERROR in ./.test/nolib/app.ts
[tsl] ERROR in app.ts(1,1)
 TS2304: Cannot find name 'parseInt'.
Expand Down

0 comments on commit f832bb6

Please sign in to comment.