-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Salsa: JS support for discovering and acquiring d.ts files #7179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
284d9f5
0aaedc5
5b06edb
317f5e2
71bfefc
20511f8
18883f9
70ca4bd
39a51d3
5981d8e
f76ef47
0346a98
b3ceea3
6aad783
4bbdf2a
e8772bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -537,6 +537,7 @@ namespace ts { | |
return { | ||
options, | ||
fileNames: getFileNames(), | ||
typingOptions: getTypingOptions(), | ||
errors | ||
}; | ||
|
||
|
@@ -601,6 +602,35 @@ namespace ts { | |
} | ||
return fileNames; | ||
} | ||
|
||
function getTypingOptions(): TypingOptions { | ||
const options: TypingOptions = getBaseFileName(configFileName) === "jsconfig.json" | ||
? { enableAutoDiscovery: true, include: [], exclude: [] } | ||
: { enableAutoDiscovery: false, include: [], exclude: [] }; | ||
const jsonTypingOptions = json["typingOptions"]; | ||
if (jsonTypingOptions) { | ||
for (const id in jsonTypingOptions) { | ||
if (id === "enableAutoDiscovery") { | ||
if (typeof jsonTypingOptions[id] === "boolean") { | ||
options.enableAutoDiscovery = jsonTypingOptions[id]; | ||
} | ||
else { | ||
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here Instead it should complain about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. |
||
} | ||
} | ||
else if (id === "include") { | ||
options.include = convertJsonOptionToStringArray(id, jsonTypingOptions[id], errors); | ||
} | ||
else if (id === "exclude") { | ||
options.exclude = convertJsonOptionToStringArray(id, jsonTypingOptions[id], errors); | ||
} | ||
else { | ||
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id)); | ||
} | ||
} | ||
} | ||
return options; | ||
} | ||
} | ||
|
||
export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } { | ||
|
@@ -641,28 +671,7 @@ namespace ts { | |
break; | ||
case "object": | ||
// "object" options with 'isFilePath' = true expected to be string arrays | ||
let paths: string[] = []; | ||
let invalidOptionType = false; | ||
if (!isArray(value)) { | ||
invalidOptionType = true; | ||
} | ||
else { | ||
for (const element of <any[]>value) { | ||
if (typeof element === "string") { | ||
paths.push(normalizePath(combinePaths(basePath, element))); | ||
} | ||
else { | ||
invalidOptionType = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (invalidOptionType) { | ||
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, opt.name)); | ||
} | ||
else { | ||
value = paths; | ||
} | ||
value = convertJsonOptionToStringArray(opt.name, value, errors, (element) => normalizePath(combinePaths(basePath, element))); | ||
break; | ||
} | ||
if (value === "") { | ||
|
@@ -682,4 +691,28 @@ namespace ts { | |
|
||
return { options, errors }; | ||
} | ||
|
||
function convertJsonOptionToStringArray(optionName: string, optionJson: any, errors: Diagnostic[], func?: (element: string) => string): string[] { | ||
const items: string[] = []; | ||
let invalidOptionType = false; | ||
if (!isArray(optionJson)) { | ||
invalidOptionType = true; | ||
} | ||
else { | ||
for (const element of <any[]>optionJson) { | ||
if (typeof element === "string") { | ||
const item = func ? func(element) : element; | ||
items.push(item); | ||
} | ||
else { | ||
invalidOptionType = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (invalidOptionType) { | ||
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, optionName)); | ||
} | ||
return items; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,6 +278,14 @@ namespace ts { | |
return hasOwnProperty.call(map, key); | ||
} | ||
|
||
export function getKeys<T>(map: Map<T>): string[] { | ||
const keys: string[] = []; | ||
for (const key in map) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why no hasOwnProperty check here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should have. Thanks for pointing out. |
||
keys.push(key); | ||
} | ||
return keys; | ||
} | ||
|
||
export function getProperty<T>(map: Map<T>, key: string): T { | ||
return hasOwnProperty.call(map, key) ? map[key] : undefined; | ||
} | ||
|
@@ -778,6 +786,32 @@ namespace ts { | |
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension; | ||
} | ||
|
||
export function ensureScriptKind(fileName: string, scriptKind?: ScriptKind): ScriptKind { | ||
// Using scriptKind as a condition handles both: | ||
// - 'scriptKind' is unspecified and thus it is `undefined` | ||
// - 'scriptKind' is set and it is `Unknown` (0) | ||
// 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; | ||
} | ||
|
||
export function getScriptKindFromFileName(fileName: string): ScriptKind { | ||
const ext = fileName.substr(fileName.lastIndexOf(".")); | ||
switch (ext.toLowerCase()) { | ||
case ".js": | ||
return ScriptKind.JS; | ||
case ".jsx": | ||
return ScriptKind.JSX; | ||
case ".ts": | ||
return ScriptKind.TS; | ||
case ".tsx": | ||
return ScriptKind.TSX; | ||
default: | ||
return ScriptKind.Unknown; | ||
} | ||
} | ||
|
||
/** | ||
* List of supported extensions in order of file resolution precedence. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else push an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good - we're doing it if the other options are invalid so makes sense here as well.