From 7bbdeb4cd5e462f19c73868d71de74aebfabd459 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 23 Jun 2017 10:18:30 -0700 Subject: [PATCH 1/2] Clean up uses of ensureScriptKind --- src/compiler/core.ts | 2 +- src/services/jsTyping.ts | 5 ++--- src/services/utilities.ts | 9 +-------- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 7c9811c41c85d..51c36f04ffb42 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -2118,7 +2118,7 @@ namespace ts { return absolute.substring(0, absolute.lastIndexOf(directorySeparator, wildcardOffset)); } - export function ensureScriptKind(fileName: string, scriptKind?: ScriptKind): ScriptKind { + export function ensureScriptKind(fileName: string, scriptKind: ScriptKind | undefined): ScriptKind { // Using scriptKind as a condition handles both: // - 'scriptKind' is unspecified and thus it is `undefined` // - 'scriptKind' is set and it is `Unknown` (0) diff --git a/src/services/jsTyping.ts b/src/services/jsTyping.ts index 41bb01c1ae72f..008fe357ec5aa 100644 --- a/src/services/jsTyping.ts +++ b/src/services/jsTyping.ts @@ -71,8 +71,7 @@ namespace ts.JsTyping { // Only infer typings for .js and .jsx files fileNames = mapDefined(fileNames, fileName => { const path = normalizePath(fileName); - const kind = ensureScriptKind(path, getScriptKindFromFileName(path)); - if (kind === ScriptKind.JS || kind === ScriptKind.JSX) { + if (hasJavaScriptFileExtension(path)) { return path; } }); @@ -191,7 +190,7 @@ namespace ts.JsTyping { } } - const hasJsxFile = forEach(fileNames, f => ensureScriptKind(f, getScriptKindFromFileName(f)) === ScriptKind.JSX); + const hasJsxFile = some(fileNames, f => fileExtensionIs(f, Extension.Jsx)); if (hasJsxFile) { addInferredTyping("react"); } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index d48caf6f36f92..c49923a17aada 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1303,14 +1303,7 @@ namespace ts { export function getScriptKind(fileName: string, host?: LanguageServiceHost): ScriptKind { // First check to see if the script kind was specified by the host. Chances are the host // may override the default script kind for the file extension. - let scriptKind: ScriptKind; - if (host && host.getScriptKind) { - scriptKind = host.getScriptKind(fileName); - } - if (!scriptKind) { - scriptKind = getScriptKindFromFileName(fileName); - } - return ensureScriptKind(fileName, scriptKind); + return ensureScriptKind(fileName, host && host.getScriptKind && host.getScriptKind(fileName)); } export function getFirstNonSpaceCharacterPosition(text: string, position: number) { From 3b58c02b60b0c968bb1e0dc59ed054e670910ec4 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 28 Jun 2017 13:18:55 -0700 Subject: [PATCH 2/2] Remove unneeded parentheses --- src/compiler/core.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 51c36f04ffb42..19f89ba26b2ae 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -2125,7 +2125,7 @@ namespace ts { // If the 'scriptKind' is 'undefined' or 'Unknown' then we attempt // to get the ScriptKind from the file name. If it cannot be resolved // from the file name then the default 'TS' script kind is returned. - return (scriptKind || getScriptKindFromFileName(fileName)) || ScriptKind.TS; + return scriptKind || getScriptKindFromFileName(fileName) || ScriptKind.TS; } export function getScriptKindFromFileName(fileName: string): ScriptKind {