Skip to content

Commit 8a29926

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

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
@@ -920,6 +920,7 @@ var servicesLintTargets = [
920920
"patternMatcher.ts",
921921
"services.ts",
922922
"shims.ts",
923+
"jsTyping.ts"
923924
].map(function (s) {
924925
return path.join(servicesDirectory, s);
925926
});

src/compiler/commandLineParser.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ namespace ts {
511511
return {
512512
options,
513513
fileNames: getFileNames(),
514+
typingOptions: getTypingOptions(),
514515
errors
515516
};
516517

@@ -575,6 +576,32 @@ namespace ts {
575576
}
576577
return fileNames;
577578
}
579+
580+
function getTypingOptions(): TypingOptions {
581+
const options: TypingOptions = getBaseFileName(configFileName) === "jsconfig.json"
582+
? { enableAutoDiscovery: true, include: [], exclude: [] }
583+
: { enableAutoDiscovery: false, include: [], exclude: [] };
584+
const jsonTypingOptions = json["typingOptions"];
585+
if (jsonTypingOptions) {
586+
for (const id in jsonTypingOptions) {
587+
if (id === "enableAutoDiscovery") {
588+
if (typeof jsonTypingOptions[id] === "boolean") {
589+
options.enableAutoDiscovery = jsonTypingOptions[id];
590+
}
591+
}
592+
else if (id === "include") {
593+
options.include = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
594+
}
595+
else if (id === "exclude") {
596+
options.exclude = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
597+
}
598+
else {
599+
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id));
600+
}
601+
}
602+
}
603+
return options;
604+
}
578605
}
579606

580607
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
@@ -769,6 +769,32 @@ namespace ts {
769769
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension;
770770
}
771771

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

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,5 +2654,9 @@
26542654
"'super' must be called before accessing 'this' in the constructor of a derived class.": {
26552655
"category": "Error",
26562656
"code": 17009
2657+
},
2658+
"Unknown typing option '{0}'.": {
2659+
"category": "Error",
2660+
"code": 17010
26572661
}
26582662
}

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
@@ -2449,6 +2449,13 @@ namespace ts {
24492449
[option: string]: string | number | boolean;
24502450
}
24512451

2452+
export interface TypingOptions {
2453+
enableAutoDiscovery?: boolean;
2454+
include?: string[];
2455+
exclude?: string[];
2456+
[option: string]: any;
2457+
}
2458+
24522459
export const enum ModuleKind {
24532460
None = 0,
24542461
CommonJS = 1,
@@ -2507,6 +2514,7 @@ namespace ts {
25072514

25082515
export interface ParsedCommandLine {
25092516
options: CompilerOptions;
2517+
typingOptions?: TypingOptions;
25102518
fileNames: string[];
25112519
errors: Diagnostic[];
25122520
}

0 commit comments

Comments
 (0)