Skip to content

Commit 50eb512

Browse files
author
Armando Aguirre
committed
Added deferred ScriptKind and renamed JsFileExtensionInfo to FileExtensionInfo
1 parent 2167b24 commit 50eb512

9 files changed

Lines changed: 67 additions & 35 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ namespace ts {
14211421
* @param basePath A root directory to resolve relative path entries in the config
14221422
* file to. e.g. outDir
14231423
*/
1424-
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine {
1424+
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine {
14251425
return parseJsonConfigFileContentWorker(json, /*sourceFile*/ undefined, host, basePath, existingOptions, configFileName, resolutionStack, extraFileExtensions);
14261426
}
14271427

@@ -1432,7 +1432,7 @@ namespace ts {
14321432
* @param basePath A root directory to resolve relative path entries in the config
14331433
* file to. e.g. outDir
14341434
*/
1435-
export function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine {
1435+
export function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine {
14361436
return parseJsonConfigFileContentWorker(/*json*/ undefined, sourceFile, host, basePath, existingOptions, configFileName, resolutionStack, extraFileExtensions);
14371437
}
14381438

@@ -1471,7 +1471,7 @@ namespace ts {
14711471
existingOptions: CompilerOptions = {},
14721472
configFileName?: string,
14731473
resolutionStack: Path[] = [],
1474-
extraFileExtensions: ReadonlyArray<JsFileExtensionInfo> = [],
1474+
extraFileExtensions: ReadonlyArray<FileExtensionInfo> = [],
14751475
): ParsedCommandLine {
14761476
Debug.assert((json === undefined && sourceFile !== undefined) || (json !== undefined && sourceFile === undefined));
14771477
const errors: Diagnostic[] = [];
@@ -2004,7 +2004,7 @@ namespace ts {
20042004
options: CompilerOptions,
20052005
host: ParseConfigHost,
20062006
errors: Push<Diagnostic>,
2007-
extraFileExtensions: ReadonlyArray<JsFileExtensionInfo>,
2007+
extraFileExtensions: ReadonlyArray<FileExtensionInfo>,
20082008
jsonSourceFile: JsonSourceFile
20092009
): ExpandResult {
20102010
basePath = normalizePath(basePath);
@@ -2042,7 +2042,7 @@ namespace ts {
20422042
* @param extraFileExtensions optionaly file extra file extension information from host
20432043
*/
20442044
/* @internal */
2045-
export function getFileNamesFromConfigSpecs(spec: ConfigFileSpecs, basePath: string, options: CompilerOptions, host: ParseConfigHost, extraFileExtensions: ReadonlyArray<JsFileExtensionInfo> = []): ExpandResult {
2045+
export function getFileNamesFromConfigSpecs(spec: ConfigFileSpecs, basePath: string, options: CompilerOptions, host: ParseConfigHost, extraFileExtensions: ReadonlyArray<FileExtensionInfo> = []): ExpandResult {
20462046
basePath = normalizePath(basePath);
20472047

20482048
const keyMapper = host.useCaseSensitiveFileNames ? identity : toLowerCase;

src/compiler/core.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,7 +2322,7 @@ namespace ts {
23222322
}
23232323

23242324
export function fileExtensionIs(path: string, extension: string): boolean {
2325-
return path.length > extension.length && endsWith(path, extension);
2325+
return path.length >= extension.length && endsWith(path, extension);
23262326
}
23272327

23282328
export function fileExtensionIsOneOf(path: string, extensions: ReadonlyArray<string>): boolean {
@@ -2666,16 +2666,22 @@ namespace ts {
26662666
export const supportedJavascriptExtensions: ReadonlyArray<Extension> = [Extension.Js, Extension.Jsx];
26672667
const allSupportedExtensions: ReadonlyArray<Extension> = [...supportedTypeScriptExtensions, ...supportedJavascriptExtensions];
26682668

2669-
export function getSupportedExtensions(options?: CompilerOptions, extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ReadonlyArray<string> {
2670-
const needAllExtensions = options && options.allowJs;
2671-
if (!extraFileExtensions || extraFileExtensions.length === 0 || !needAllExtensions) {
2672-
return needAllExtensions ? allSupportedExtensions : supportedTypeScriptExtensions;
2669+
export function getSupportedExtensions(options?: CompilerOptions, extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ReadonlyArray<string> {
2670+
const needJsExtensions = options && options.allowJs;
2671+
let extensions: string[] = needJsExtensions ? [...allSupportedExtensions] : [...supportedTypeScriptExtensions];
2672+
2673+
if (extraFileExtensions) {
2674+
extensions = [
2675+
...extensions,
2676+
...extraFileExtensions.filter(x => x.scriptKind === ScriptKind.Deferred || needJsExtensions && isJavaScriptLike(x.scriptKind)).map(x => x.extension),
2677+
];
26732678
}
2674-
return deduplicate(
2675-
[...allSupportedExtensions, ...extraFileExtensions.map(e => e.extension)],
2676-
equateStringsCaseSensitive,
2677-
compareStringsCaseSensitive
2678-
);
2679+
2680+
return deduplicate(extensions, equateStringsCaseSensitive, compareStringsCaseSensitive);
2681+
}
2682+
2683+
function isJavaScriptLike(scriptKind: ScriptKind): boolean {
2684+
return scriptKind === ScriptKind.JS || scriptKind === ScriptKind.JSX;
26792685
}
26802686

26812687
export function hasJavaScriptFileExtension(fileName: string) {
@@ -2686,7 +2692,7 @@ namespace ts {
26862692
return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension));
26872693
}
26882694

2689-
export function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions, extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>) {
2695+
export function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions, extraFileExtensions?: ReadonlyArray<FileExtensionInfo>) {
26902696
if (!fileName) { return false; }
26912697

26922698
for (const extension of getSupportedExtensions(compilerOptions, extraFileExtensions)) {

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,9 +1299,9 @@ namespace ts {
12991299
Debug.assert(!!sourceFile.bindDiagnostics);
13001300

13011301
const isCheckJs = isCheckJsEnabledForFile(sourceFile, options);
1302-
// By default, only type-check .ts, .tsx, and 'External' files (external files are added by plugins)
1302+
// By default, only type-check .ts, .tsx, 'Deferred' and 'External' files (external files are added by plugins)
13031303
const includeBindAndCheckDiagnostics = sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX ||
1304-
sourceFile.scriptKind === ScriptKind.External || isCheckJs;
1304+
sourceFile.scriptKind === ScriptKind.External || isCheckJs || sourceFile.scriptKind === ScriptKind.Deferred;
13051305
const bindDiagnostics = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
13061306
const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : emptyArray;
13071307
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);

src/compiler/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,7 +4058,7 @@ namespace ts {
40584058
Prototype,
40594059
}
40604060

4061-
export interface JsFileExtensionInfo {
4061+
export interface FileExtensionInfo {
40624062
extension: string;
40634063
isMixedContent: boolean;
40644064
scriptKind?: ScriptKind;
@@ -4265,7 +4265,12 @@ namespace ts {
42654265
TS = 3,
42664266
TSX = 4,
42674267
External = 5,
4268-
JSON = 6
4268+
JSON = 6,
4269+
/**
4270+
* Used on extensions that doesn't define the ScriptKind but the content defines it.
4271+
* Deferred extensions are going to be included in all project contexts.
4272+
*/
4273+
Deferred = 7
42694274
}
42704275

42714276
export const enum ScriptTarget {

src/server/editorServices.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ namespace ts.server {
202202
formatCodeOptions: FormatCodeSettings;
203203
preferences: UserPreferences;
204204
hostInfo: string;
205-
extraFileExtensions?: JsFileExtensionInfo[];
205+
extraFileExtensions?: FileExtensionInfo[];
206206
}
207207

208208
export interface OpenConfiguredProjectResult {
@@ -212,8 +212,8 @@ namespace ts.server {
212212

213213
interface FilePropertyReader<T> {
214214
getFileName(f: T): string;
215-
getScriptKind(f: T, extraFileExtensions?: JsFileExtensionInfo[]): ScriptKind;
216-
hasMixedContent(f: T, extraFileExtensions: JsFileExtensionInfo[]): boolean;
215+
getScriptKind(f: T, extraFileExtensions?: FileExtensionInfo[]): ScriptKind;
216+
hasMixedContent(f: T, extraFileExtensions: FileExtensionInfo[]): boolean;
217217
}
218218

219219
const fileNamePropertyReader: FilePropertyReader<string> = {
@@ -2405,5 +2405,15 @@ namespace ts.server {
24052405
this.createExternalProject(proj.projectFileName, rootFiles, proj.options, proj.typeAcquisition, excludedFiles);
24062406
}
24072407
}
2408+
2409+
hasDeferredExtension() {
2410+
for (const extension of this.hostConfiguration.extraFileExtensions) {
2411+
if (extension.scriptKind === ScriptKind.Deferred) {
2412+
return true;
2413+
}
2414+
}
2415+
2416+
return false;
2417+
}
24082418
}
24092419
}

src/server/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ namespace ts.server {
235235
this.compilerOptions.allowNonTsExtensions = true;
236236
this.compilerOptions.allowJs = true;
237237
}
238-
else if (hasExplicitListOfFiles || this.compilerOptions.allowJs) {
238+
else if (hasExplicitListOfFiles || this.compilerOptions.allowJs || this.projectService.hasDeferredExtension()) {
239239
// If files are listed explicitly or allowJs is specified, allow all extensions
240240
this.compilerOptions.allowNonTsExtensions = true;
241241
}

src/server/protocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ namespace ts.server.protocol {
12741274
/**
12751275
* The host's additional supported .js file extensions
12761276
*/
1277-
extraFileExtensions?: JsFileExtensionInfo[];
1277+
extraFileExtensions?: FileExtensionInfo[];
12781278
}
12791279

12801280
/**

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,7 @@ declare namespace ts {
22612261
AlwaysStrict = 64,
22622262
PriorityImpliesCombination = 28
22632263
}
2264-
interface JsFileExtensionInfo {
2264+
interface FileExtensionInfo {
22652265
extension: string;
22662266
isMixedContent: boolean;
22672267
scriptKind?: ScriptKind;
@@ -2423,7 +2423,12 @@ declare namespace ts {
24232423
TS = 3,
24242424
TSX = 4,
24252425
External = 5,
2426-
JSON = 6
2426+
JSON = 6,
2427+
/**
2428+
* Used on extensions that doesn't define the ScriptKind but the content defines it.
2429+
* Deferred extensions are going to be included in all project contexts.
2430+
*/
2431+
Deferred = 7
24272432
}
24282433
enum ScriptTarget {
24292434
ES3 = 0,
@@ -3384,15 +3389,15 @@ declare namespace ts {
33843389
* @param basePath A root directory to resolve relative path entries in the config
33853390
* file to. e.g. outDir
33863391
*/
3387-
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
3392+
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
33883393
/**
33893394
* Parse the contents of a config file (tsconfig.json).
33903395
* @param jsonNode The contents of the config file to parse
33913396
* @param host Instance of ParseConfigHost used to enumerate files in folder.
33923397
* @param basePath A root directory to resolve relative path entries in the config
33933398
* file to. e.g. outDir
33943399
*/
3395-
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
3400+
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
33963401
function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): {
33973402
options: CompilerOptions;
33983403
errors: Diagnostic[];
@@ -6016,7 +6021,7 @@ declare namespace ts.server.protocol {
60166021
/**
60176022
* The host's additional supported .js file extensions
60186023
*/
6019-
extraFileExtensions?: JsFileExtensionInfo[];
6024+
extraFileExtensions?: FileExtensionInfo[];
60206025
}
60216026
/**
60226027
* Configure request; value of command field is "configure". Specifies
@@ -7850,7 +7855,7 @@ declare namespace ts.server {
78507855
formatCodeOptions: FormatCodeSettings;
78517856
preferences: UserPreferences;
78527857
hostInfo: string;
7853-
extraFileExtensions?: JsFileExtensionInfo[];
7858+
extraFileExtensions?: FileExtensionInfo[];
78547859
}
78557860
interface OpenConfiguredProjectResult {
78567861
configFileName?: NormalizedPath;
@@ -8099,6 +8104,7 @@ declare namespace ts.server {
80998104
resetSafeList(): void;
81008105
applySafeList(proj: protocol.ExternalProject): NormalizedPath[];
81018106
openExternalProject(proj: protocol.ExternalProject): void;
8107+
hasDeferredExtension(): boolean;
81028108
}
81038109
}
81048110

tests/baselines/reference/api/typescript.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,7 @@ declare namespace ts {
22612261
AlwaysStrict = 64,
22622262
PriorityImpliesCombination = 28
22632263
}
2264-
interface JsFileExtensionInfo {
2264+
interface FileExtensionInfo {
22652265
extension: string;
22662266
isMixedContent: boolean;
22672267
scriptKind?: ScriptKind;
@@ -2423,7 +2423,12 @@ declare namespace ts {
24232423
TS = 3,
24242424
TSX = 4,
24252425
External = 5,
2426-
JSON = 6
2426+
JSON = 6,
2427+
/**
2428+
* Used on extensions that doesn't define the ScriptKind but the content defines it.
2429+
* Deferred extensions are going to be included in all project contexts.
2430+
*/
2431+
Deferred = 7
24272432
}
24282433
enum ScriptTarget {
24292434
ES3 = 0,
@@ -4201,15 +4206,15 @@ declare namespace ts {
42014206
* @param basePath A root directory to resolve relative path entries in the config
42024207
* file to. e.g. outDir
42034208
*/
4204-
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
4209+
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
42054210
/**
42064211
* Parse the contents of a config file (tsconfig.json).
42074212
* @param jsonNode The contents of the config file to parse
42084213
* @param host Instance of ParseConfigHost used to enumerate files in folder.
42094214
* @param basePath A root directory to resolve relative path entries in the config
42104215
* file to. e.g. outDir
42114216
*/
4212-
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
4217+
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
42134218
function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): {
42144219
options: CompilerOptions;
42154220
errors: Diagnostic[];

0 commit comments

Comments
 (0)