Skip to content

Commit 284d9f5

Browse files
committed
Salsa: JS support for discovering and acquiring d.ts files
(Mostly isolating VS host changes from PR#6448)
1 parent 6b05ad7 commit 284d9f5

11 files changed

Lines changed: 416 additions & 29 deletions

File tree

Jakefile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ var servicesLintTargets = [
927927
"patternMatcher.ts",
928928
"services.ts",
929929
"shims.ts",
930+
"jsTyping.ts"
930931
].map(function (s) {
931932
return path.join(servicesDirectory, s);
932933
});

src/compiler/commandLineParser.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ namespace ts {
537537
return {
538538
options,
539539
fileNames: getFileNames(),
540+
typingOptions: getTypingOptions(),
540541
errors
541542
};
542543

@@ -601,6 +602,32 @@ namespace ts {
601602
}
602603
return fileNames;
603604
}
605+
606+
function getTypingOptions(): TypingOptions {
607+
const options: TypingOptions = getBaseFileName(configFileName) === "jsconfig.json"
608+
? { enableAutoDiscovery: true, include: [], exclude: [] }
609+
: { enableAutoDiscovery: false, include: [], exclude: [] };
610+
const jsonTypingOptions = json["typingOptions"];
611+
if (jsonTypingOptions) {
612+
for (const id in jsonTypingOptions) {
613+
if (id === "enableAutoDiscovery") {
614+
if (typeof jsonTypingOptions[id] === "boolean") {
615+
options.enableAutoDiscovery = jsonTypingOptions[id];
616+
}
617+
}
618+
else if (id === "include") {
619+
options.include = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
620+
}
621+
else if (id === "exclude") {
622+
options.exclude = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
623+
}
624+
else {
625+
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id));
626+
}
627+
}
628+
}
629+
return options;
630+
}
604631
}
605632

606633
export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } {

src/compiler/core.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,32 @@ namespace ts {
778778
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension;
779779
}
780780

781+
export function ensureScriptKind(fileName: string, scriptKind?: ScriptKind): ScriptKind {
782+
// Using scriptKind as a condition handles both:
783+
// - 'scriptKind' is unspecified and thus it is `undefined`
784+
// - 'scriptKind' is set and it is `Unknown` (0)
785+
// If the 'scriptKind' is 'undefined' or 'Unknown' then we attempt
786+
// to get the ScriptKind from the file name. If it cannot be resolved
787+
// from the file name then the default 'TS' script kind is returned.
788+
return (scriptKind || getScriptKindFromFileName(fileName)) || ScriptKind.TS;
789+
}
790+
791+
export function getScriptKindFromFileName(fileName: string): ScriptKind {
792+
const ext = fileName.substr(fileName.lastIndexOf("."));
793+
switch (ext.toLowerCase()) {
794+
case ".js":
795+
return ScriptKind.JS;
796+
case ".jsx":
797+
return ScriptKind.JSX;
798+
case ".ts":
799+
return ScriptKind.TS;
800+
case ".tsx":
801+
return ScriptKind.TSX;
802+
default:
803+
return ScriptKind.Unknown;
804+
}
805+
}
806+
781807
/**
782808
* List of supported extensions in order of file resolution precedence.
783809
*/

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,5 +2800,9 @@
28002800
"'super' must be called before accessing 'this' in the constructor of a derived class.": {
28012801
"category": "Error",
28022802
"code": 17009
2803+
},
2804+
"Unknown typing option '{0}'.": {
2805+
"category": "Error",
2806+
"code": 17010
28032807
}
28042808
}

src/compiler/parser.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -407,23 +407,6 @@ namespace ts {
407407
return result;
408408
}
409409

410-
/* @internal */
411-
export function getScriptKindFromFileName(fileName: string): ScriptKind {
412-
const ext = fileName.substr(fileName.lastIndexOf("."));
413-
switch (ext.toLowerCase()) {
414-
case ".js":
415-
return ScriptKind.JS;
416-
case ".jsx":
417-
return ScriptKind.JSX;
418-
case ".ts":
419-
return ScriptKind.TS;
420-
case ".tsx":
421-
return ScriptKind.TSX;
422-
default:
423-
return ScriptKind.TS;
424-
}
425-
}
426-
427410
// Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter
428411
// indicates what changed between the 'text' that this SourceFile has and the 'newText'.
429412
// The SourceFile will be created with the compiler attempting to reuse as many nodes from
@@ -551,12 +534,7 @@ namespace ts {
551534
let parseErrorBeforeNextFinishedNode = false;
552535

553536
export function parseSourceFile(fileName: string, _sourceText: string, languageVersion: ScriptTarget, _syntaxCursor: IncrementalParser.SyntaxCursor, setParentNodes?: boolean, scriptKind?: ScriptKind): SourceFile {
554-
// Using scriptKind as a condition handles both:
555-
// - 'scriptKind' is unspecified and thus it is `undefined`
556-
// - 'scriptKind' is set and it is `Unknown` (0)
557-
// If the 'scriptKind' is 'undefined' or 'Unknown' then attempt
558-
// to get the ScriptKind from the file name.
559-
scriptKind = scriptKind ? scriptKind : getScriptKindFromFileName(fileName);
537+
scriptKind = ensureScriptKind(fileName, scriptKind);
560538

561539
initializeState(fileName, _sourceText, languageVersion, _syntaxCursor, scriptKind);
562540

src/compiler/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,6 +2432,13 @@ namespace ts {
24322432
[option: string]: string | number | boolean | TsConfigOnlyOptions;
24332433
}
24342434

2435+
export interface TypingOptions {
2436+
enableAutoDiscovery?: boolean;
2437+
include?: string[];
2438+
exclude?: string[];
2439+
[option: string]: any;
2440+
}
2441+
24352442
export enum ModuleKind {
24362443
None = 0,
24372444
CommonJS = 1,
@@ -2490,6 +2497,7 @@ namespace ts {
24902497

24912498
export interface ParsedCommandLine {
24922499
options: CompilerOptions;
2500+
typingOptions?: TypingOptions;
24932501
fileNames: string[];
24942502
errors: Diagnostic[];
24952503
}

0 commit comments

Comments
 (0)