From 7566760911f932c2cf639e254c15d5a571b063ad Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Mon, 16 Oct 2017 13:38:28 -0700 Subject: [PATCH] Set the scriptKind from the host configuration if present --- src/compiler/core.ts | 10 ++++++++++ src/compiler/program.ts | 6 ++++-- src/compiler/types.ts | 1 + src/server/editorServices.ts | 21 ++++++++++++++++++--- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 07c9deff1db..af43b3e59bc 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -2593,6 +2593,16 @@ namespace ts { return find(supportedTypescriptExtensionsForExtractExtension, e => fileExtensionIs(path, e)) || find(supportedJavascriptExtensions, e => fileExtensionIs(path, e)); } + // Retrieves any string from the final "." onwards from a base file name. + // Unlike extensionFromPath, which throws an exception on unrecognized extensions. + export function getAnyExtensionFromPath(path: string): string | undefined { + const baseFileName = getBaseFileName(path); + const extensionIndex = baseFileName.lastIndexOf("."); + if (extensionIndex >= 0) { + return baseFileName.substring(extensionIndex); + } + } + export function isCheckJsEnabledForFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) { return sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : compilerOptions.checkJs; } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 7086394ce4e..097724776f6 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1146,8 +1146,10 @@ namespace ts { const typeChecker = getDiagnosticsProducingTypeChecker(); Debug.assert(!!sourceFile.bindDiagnostics); - // For JavaScript files, we don't want to report semantic errors unless explicitly requested. - const includeBindAndCheckDiagnostics = !isSourceFileJavaScript(sourceFile) || isCheckJsEnabledForFile(sourceFile, options); + + // By default, only type-check .ts, .tsx, and 'External' files (external files are added by plugins) + const includeBindAndCheckDiagnostics = sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX || + sourceFile.scriptKind === ScriptKind.External || isCheckJsEnabledForFile(sourceFile, options); const bindDiagnostics = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray; const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : emptyArray; const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e2665a44388..201d7ea7a50 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3499,6 +3499,7 @@ namespace ts { export interface JsFileExtensionInfo { extension: string; isMixedContent: boolean; + scriptKind?: ScriptKind; } export interface DiagnosticMessage { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 18435043696..59cdfcc6178 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -232,13 +232,28 @@ namespace ts.server { interface FilePropertyReader { getFileName(f: T): string; - getScriptKind(f: T): ScriptKind; + getScriptKind(f: T, extraFileExtensions?: JsFileExtensionInfo[]): ScriptKind; hasMixedContent(f: T, extraFileExtensions: JsFileExtensionInfo[]): boolean; } const fileNamePropertyReader: FilePropertyReader = { getFileName: x => x, - getScriptKind: _ => undefined, + getScriptKind: (fileName, extraFileExtensions) => { + let result: ScriptKind; + if (extraFileExtensions) { + const fileExtension = getAnyExtensionFromPath(fileName); + if (fileExtension) { + some(extraFileExtensions, info => { + if (info.extension === fileExtension) { + result = info.scriptKind; + return true; + } + return false; + }); + } + } + return result; + }, hasMixedContent: (fileName, extraFileExtensions) => some(extraFileExtensions, ext => ext.isMixedContent && fileExtensionIs(fileName, ext.extension)), }; @@ -1178,7 +1193,7 @@ namespace ts.server { let errors: Diagnostic[]; for (const f of files) { const rootFilename = propertyReader.getFileName(f); - const scriptKind = propertyReader.getScriptKind(f); + const scriptKind = propertyReader.getScriptKind(f, this.hostConfiguration.extraFileExtensions); const hasMixedContent = propertyReader.hasMixedContent(f, this.hostConfiguration.extraFileExtensions); if (this.host.fileExists(rootFilename)) { const info = this.getOrCreateScriptInfoForNormalizedPath(toNormalizedPath(rootFilename), /*openedByClient*/ clientFileName === rootFilename, /*fileContent*/ undefined, scriptKind, hasMixedContent);