Skip to content

Commit 1d593fd

Browse files
authored
Merge pull request microsoft#23191 from armanio123/AddVueSupport
Added deferred ScriptKind and renamed JsFileExtensionInfo to FileExte…
2 parents 5b7bec8 + b8ddc0d commit 1d593fd

File tree

10 files changed

+116
-34
lines changed

10 files changed

+116
-34
lines changed

src/compiler/commandLineParser.ts

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

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

@@ -1472,7 +1472,7 @@ namespace ts {
14721472
existingOptions: CompilerOptions = {},
14731473
configFileName?: string,
14741474
resolutionStack: Path[] = [],
1475-
extraFileExtensions: ReadonlyArray<JsFileExtensionInfo> = [],
1475+
extraFileExtensions: ReadonlyArray<FileExtensionInfo> = [],
14761476
): ParsedCommandLine {
14771477
Debug.assert((json === undefined && sourceFile !== undefined) || (json !== undefined && sourceFile === undefined));
14781478
const errors: Diagnostic[] = [];
@@ -2005,7 +2005,7 @@ namespace ts {
20052005
options: CompilerOptions,
20062006
host: ParseConfigHost,
20072007
errors: Push<Diagnostic>,
2008-
extraFileExtensions: ReadonlyArray<JsFileExtensionInfo>,
2008+
extraFileExtensions: ReadonlyArray<FileExtensionInfo>,
20092009
jsonSourceFile: JsonSourceFile
20102010
): ExpandResult {
20112011
basePath = normalizePath(basePath);
@@ -2043,7 +2043,7 @@ namespace ts {
20432043
* @param extraFileExtensions optionaly file extra file extension information from host
20442044
*/
20452045
/* @internal */
2046-
export function getFileNamesFromConfigSpecs(spec: ConfigFileSpecs, basePath: string, options: CompilerOptions, host: ParseConfigHost, extraFileExtensions: ReadonlyArray<JsFileExtensionInfo> = []): ExpandResult {
2046+
export function getFileNamesFromConfigSpecs(spec: ConfigFileSpecs, basePath: string, options: CompilerOptions, host: ParseConfigHost, extraFileExtensions: ReadonlyArray<FileExtensionInfo> = []): ExpandResult {
20472047
basePath = normalizePath(basePath);
20482048

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

src/compiler/core.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -2681,16 +2681,23 @@ namespace ts {
26812681
export const supportedJavascriptExtensions: ReadonlyArray<Extension> = [Extension.Js, Extension.Jsx];
26822682
const allSupportedExtensions: ReadonlyArray<Extension> = [...supportedTypeScriptExtensions, ...supportedJavascriptExtensions];
26832683

2684-
export function getSupportedExtensions(options?: CompilerOptions, extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ReadonlyArray<string> {
2685-
const needAllExtensions = options && options.allowJs;
2686-
if (!extraFileExtensions || extraFileExtensions.length === 0 || !needAllExtensions) {
2687-
return needAllExtensions ? allSupportedExtensions : supportedTypeScriptExtensions;
2684+
export function getSupportedExtensions(options?: CompilerOptions, extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ReadonlyArray<string> {
2685+
const needJsExtensions = options && options.allowJs;
2686+
2687+
if (!extraFileExtensions || extraFileExtensions.length === 0) {
2688+
return needJsExtensions ? allSupportedExtensions : supportedTypeScriptExtensions;
26882689
}
2689-
return deduplicate(
2690-
[...allSupportedExtensions, ...extraFileExtensions.map(e => e.extension)],
2691-
equateStringsCaseSensitive,
2692-
compareStringsCaseSensitive
2693-
);
2690+
2691+
const extensions = [
2692+
...needJsExtensions ? allSupportedExtensions : supportedTypeScriptExtensions,
2693+
...mapDefined(extraFileExtensions, x => x.scriptKind === ScriptKind.Deferred || needJsExtensions && isJavaScriptLike(x.scriptKind) ? x.extension : undefined)
2694+
];
2695+
2696+
return deduplicate(extensions, equateStringsCaseSensitive, compareStringsCaseSensitive);
2697+
}
2698+
2699+
function isJavaScriptLike(scriptKind: ScriptKind): boolean {
2700+
return scriptKind === ScriptKind.JS || scriptKind === ScriptKind.JSX;
26942701
}
26952702

26962703
export function hasJavaScriptFileExtension(fileName: string) {
@@ -2701,7 +2708,7 @@ namespace ts {
27012708
return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension));
27022709
}
27032710

2704-
export function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions, extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>) {
2711+
export function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions, extraFileExtensions?: ReadonlyArray<FileExtensionInfo>) {
27052712
if (!fileName) { return false; }
27062713

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

src/compiler/program.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1296,9 +1296,9 @@ namespace ts {
12961296
Debug.assert(!!sourceFile.bindDiagnostics);
12971297

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

src/compiler/types.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -4098,7 +4098,10 @@ namespace ts {
40984098
Prototype,
40994099
}
41004100

4101-
export interface JsFileExtensionInfo {
4101+
/** @deprecated Use FileExtensionInfo instead. */
4102+
export type JsFileExtensionInfo = FileExtensionInfo;
4103+
4104+
export interface FileExtensionInfo {
41024105
extension: string;
41034106
isMixedContent: boolean;
41044107
scriptKind?: ScriptKind;
@@ -4306,7 +4309,12 @@ namespace ts {
43064309
TS = 3,
43074310
TSX = 4,
43084311
External = 5,
4309-
JSON = 6
4312+
JSON = 6,
4313+
/**
4314+
* Used on extensions that doesn't define the ScriptKind but the content defines it.
4315+
* Deferred extensions are going to be included in all project contexts.
4316+
*/
4317+
Deferred = 7
43104318
}
43114319

43124320
export const enum ScriptTarget {

src/harness/unittests/tsserverProjectSystem.ts

+42
Original file line numberDiff line numberDiff line change
@@ -3217,6 +3217,48 @@ namespace ts.projectSystem {
32173217
});
32183218

32193219
});
3220+
3221+
it("includes deferred files in the project context", () => {
3222+
const file1 = {
3223+
path: "/a.deferred",
3224+
content: "const a = 1;"
3225+
};
3226+
// Deferred extensions should not affect JS files.
3227+
const file2 = {
3228+
path: "/b.js",
3229+
content: "const b = 1;"
3230+
};
3231+
const tsconfig = {
3232+
path: "/tsconfig.json",
3233+
content: ""
3234+
};
3235+
3236+
const host = createServerHost([file1, file2, tsconfig]);
3237+
const session = createSession(host);
3238+
const projectService = session.getProjectService();
3239+
3240+
// Configure the deferred extension.
3241+
const extraFileExtensions = [{ extension: ".deferred", scriptKind: ScriptKind.Deferred, isMixedContent: true }];
3242+
const configureHostRequest = makeSessionRequest<protocol.ConfigureRequestArguments>(CommandNames.Configure, { extraFileExtensions });
3243+
session.executeCommand(configureHostRequest);
3244+
3245+
// Open external project
3246+
const projectName = "/proj1";
3247+
projectService.openExternalProject({
3248+
projectFileName: projectName,
3249+
rootFiles: toExternalFiles([file1.path, file2.path, tsconfig.path]),
3250+
options: {}
3251+
});
3252+
3253+
// Assert
3254+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
3255+
3256+
const configuredProject = configuredProjectAt(projectService, 0);
3257+
checkProjectActualFiles(configuredProject, [file1.path, tsconfig.path]);
3258+
3259+
// Allow allowNonTsExtensions will be set to true for deferred extensions.
3260+
assert.isTrue(configuredProject.getCompilerOptions().allowNonTsExtensions);
3261+
});
32203262
});
32213263

32223264
describe("tsserverProjectSystem Proper errors", () => {

src/server/editorServices.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ namespace ts.server {
194194
formatCodeOptions: FormatCodeSettings;
195195
preferences: UserPreferences;
196196
hostInfo: string;
197-
extraFileExtensions?: JsFileExtensionInfo[];
197+
extraFileExtensions?: FileExtensionInfo[];
198198
}
199199

200200
export interface OpenConfiguredProjectResult {
@@ -204,8 +204,8 @@ namespace ts.server {
204204

205205
interface FilePropertyReader<T> {
206206
getFileName(f: T): string;
207-
getScriptKind(f: T, extraFileExtensions?: JsFileExtensionInfo[]): ScriptKind;
208-
hasMixedContent(f: T, extraFileExtensions: JsFileExtensionInfo[]): boolean;
207+
getScriptKind(f: T, extraFileExtensions?: FileExtensionInfo[]): ScriptKind;
208+
hasMixedContent(f: T, extraFileExtensions: FileExtensionInfo[]): boolean;
209209
}
210210

211211
const fileNamePropertyReader: FilePropertyReader<string> = {
@@ -2426,5 +2426,15 @@ namespace ts.server {
24262426
this.createExternalProject(proj.projectFileName, rootFiles, proj.options, proj.typeAcquisition, excludedFiles);
24272427
}
24282428
}
2429+
2430+
hasDeferredExtension() {
2431+
for (const extension of this.hostConfiguration.extraFileExtensions) {
2432+
if (extension.scriptKind === ScriptKind.Deferred) {
2433+
return true;
2434+
}
2435+
}
2436+
2437+
return false;
2438+
}
24292439
}
24302440
}

src/server/project.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ namespace ts.server {
207207
this.compilerOptions.allowNonTsExtensions = true;
208208
this.compilerOptions.allowJs = true;
209209
}
210-
else if (hasExplicitListOfFiles || this.compilerOptions.allowJs) {
210+
else if (hasExplicitListOfFiles || this.compilerOptions.allowJs || this.projectService.hasDeferredExtension()) {
211211
// If files are listed explicitly or allowJs is specified, allow all extensions
212212
this.compilerOptions.allowNonTsExtensions = true;
213213
}

src/server/protocol.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ namespace ts.server.protocol {
12931293
/**
12941294
* The host's additional supported .js file extensions
12951295
*/
1296-
extraFileExtensions?: JsFileExtensionInfo[];
1296+
extraFileExtensions?: FileExtensionInfo[];
12971297
}
12981298

12991299
/**

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,9 @@ declare namespace ts {
22742274
AlwaysStrict = 64,
22752275
PriorityImpliesCombination = 28
22762276
}
2277-
interface JsFileExtensionInfo {
2277+
/** @deprecated Use FileExtensionInfo instead. */
2278+
type JsFileExtensionInfo = FileExtensionInfo;
2279+
interface FileExtensionInfo {
22782280
extension: string;
22792281
isMixedContent: boolean;
22802282
scriptKind?: ScriptKind;
@@ -2437,7 +2439,12 @@ declare namespace ts {
24372439
TS = 3,
24382440
TSX = 4,
24392441
External = 5,
2440-
JSON = 6
2442+
JSON = 6,
2443+
/**
2444+
* Used on extensions that doesn't define the ScriptKind but the content defines it.
2445+
* Deferred extensions are going to be included in all project contexts.
2446+
*/
2447+
Deferred = 7
24412448
}
24422449
enum ScriptTarget {
24432450
ES3 = 0,
@@ -4225,15 +4232,15 @@ declare namespace ts {
42254232
* @param basePath A root directory to resolve relative path entries in the config
42264233
* file to. e.g. outDir
42274234
*/
4228-
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
4235+
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
42294236
/**
42304237
* Parse the contents of a config file (tsconfig.json).
42314238
* @param jsonNode The contents of the config file to parse
42324239
* @param host Instance of ParseConfigHost used to enumerate files in folder.
42334240
* @param basePath A root directory to resolve relative path entries in the config
42344241
* file to. e.g. outDir
42354242
*/
4236-
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
4243+
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
42374244
function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): {
42384245
options: CompilerOptions;
42394246
errors: Diagnostic[];
@@ -6318,7 +6325,7 @@ declare namespace ts.server.protocol {
63186325
/**
63196326
* The host's additional supported .js file extensions
63206327
*/
6321-
extraFileExtensions?: JsFileExtensionInfo[];
6328+
extraFileExtensions?: FileExtensionInfo[];
63226329
}
63236330
/**
63246331
* Configure request; value of command field is "configure". Specifies
@@ -7947,7 +7954,7 @@ declare namespace ts.server {
79477954
formatCodeOptions: FormatCodeSettings;
79487955
preferences: UserPreferences;
79497956
hostInfo: string;
7950-
extraFileExtensions?: JsFileExtensionInfo[];
7957+
extraFileExtensions?: FileExtensionInfo[];
79517958
}
79527959
interface OpenConfiguredProjectResult {
79537960
configFileName?: NormalizedPath;
@@ -8195,6 +8202,7 @@ declare namespace ts.server {
81958202
resetSafeList(): void;
81968203
applySafeList(proj: protocol.ExternalProject): NormalizedPath[];
81978204
openExternalProject(proj: protocol.ExternalProject): void;
8205+
hasDeferredExtension(): boolean;
81988206
}
81998207
}
82008208
declare namespace ts.server {

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,9 @@ declare namespace ts {
22742274
AlwaysStrict = 64,
22752275
PriorityImpliesCombination = 28
22762276
}
2277-
interface JsFileExtensionInfo {
2277+
/** @deprecated Use FileExtensionInfo instead. */
2278+
type JsFileExtensionInfo = FileExtensionInfo;
2279+
interface FileExtensionInfo {
22782280
extension: string;
22792281
isMixedContent: boolean;
22802282
scriptKind?: ScriptKind;
@@ -2437,7 +2439,12 @@ declare namespace ts {
24372439
TS = 3,
24382440
TSX = 4,
24392441
External = 5,
2440-
JSON = 6
2442+
JSON = 6,
2443+
/**
2444+
* Used on extensions that doesn't define the ScriptKind but the content defines it.
2445+
* Deferred extensions are going to be included in all project contexts.
2446+
*/
2447+
Deferred = 7
24412448
}
24422449
enum ScriptTarget {
24432450
ES3 = 0,
@@ -4225,15 +4232,15 @@ declare namespace ts {
42254232
* @param basePath A root directory to resolve relative path entries in the config
42264233
* file to. e.g. outDir
42274234
*/
4228-
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
4235+
function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
42294236
/**
42304237
* Parse the contents of a config file (tsconfig.json).
42314238
* @param jsonNode The contents of the config file to parse
42324239
* @param host Instance of ParseConfigHost used to enumerate files in folder.
42334240
* @param basePath A root directory to resolve relative path entries in the config
42344241
* file to. e.g. outDir
42354242
*/
4236-
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine;
4243+
function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<FileExtensionInfo>): ParsedCommandLine;
42374244
function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): {
42384245
options: CompilerOptions;
42394246
errors: Diagnostic[];

0 commit comments

Comments
 (0)