tsconfig.json mixed content support

This commit is contained in:
Jason Ramsay 2016-11-10 11:42:42 -08:00
parent c87bce1119
commit 7dd30dbfd4
14 changed files with 159 additions and 30 deletions

View File

@ -826,7 +826,7 @@ namespace ts {
* @param basePath A root directory to resolve relative path entries in the config
* file to. e.g. outDir
*/
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}, configFileName?: string, resolutionStack: Path[] = []): ParsedCommandLine {
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}, configFileName?: string, resolutionStack: Path[] = [], fileExtensionMap: FileExtensionMap = {}): ParsedCommandLine {
const errors: Diagnostic[] = [];
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames);
const resolvedPath = toPath(configFileName || "", basePath, getCanonicalFileName);
@ -963,7 +963,7 @@ namespace ts {
includeSpecs = ["**/*"];
}
const result = matchFileNames(fileNames, includeSpecs, excludeSpecs, basePath, options, host, errors);
const result = matchFileNames(fileNames, includeSpecs, excludeSpecs, basePath, options, host, errors, fileExtensionMap);
if (result.fileNames.length === 0 && !hasProperty(json, "files") && resolutionStack.length === 0) {
errors.push(
@ -1165,7 +1165,7 @@ namespace ts {
* @param host The host used to resolve files and directories.
* @param errors An array for diagnostic reporting.
*/
function matchFileNames(fileNames: string[], include: string[], exclude: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[]): ExpandResult {
function matchFileNames(fileNames: string[], include: string[], exclude: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[], fileExtensionMap: FileExtensionMap): ExpandResult {
basePath = normalizePath(basePath);
// The exclude spec list is converted into a regular expression, which allows us to quickly
@ -1199,7 +1199,7 @@ namespace ts {
// Rather than requery this for each file and filespec, we query the supported extensions
// once and store it on the expansion context.
const supportedExtensions = getSupportedExtensions(options);
const supportedExtensions = getSupportedExtensions(options, fileExtensionMap);
// Literal files are always included verbatim. An "include" or "exclude" specification cannot
// remove a literal file.

View File

@ -1912,8 +1912,16 @@ namespace ts {
export const supportedJavascriptExtensions = [".js", ".jsx"];
const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions);
export function getSupportedExtensions(options?: CompilerOptions): string[] {
return options && options.allowJs ? allSupportedExtensions : supportedTypeScriptExtensions;
export function getSupportedExtensions(options?: CompilerOptions, fileExtensionMap?: FileExtensionMap): string[] {
let typeScriptHostExtensions: string[] = [];
let allHostExtensions: string[] = [];
if (fileExtensionMap) {
allHostExtensions = concatenate(concatenate(fileExtensionMap.javaScript, fileExtensionMap.typeScript), fileExtensionMap.mixedContent);
typeScriptHostExtensions = fileExtensionMap.typeScript;
}
const allTypeScriptExtensions = concatenate(supportedTypeScriptExtensions, typeScriptHostExtensions);
const allExtensions = concatenate(allSupportedExtensions, allHostExtensions);
return options && options.allowJs ? allExtensions : allTypeScriptExtensions;
}
export function hasJavaScriptFileExtension(fileName: string) {
@ -1924,10 +1932,10 @@ namespace ts {
return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension));
}
export function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions) {
export function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions, fileExtensionMap?: FileExtensionMap) {
if (!fileName) { return false; }
for (const extension of getSupportedExtensions(compilerOptions)) {
for (const extension of getSupportedExtensions(compilerOptions, fileExtensionMap)) {
if (fileExtensionIs(fileName, extension)) {
return true;
}

View File

@ -289,7 +289,7 @@ namespace ts {
return resolutions;
}
export function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program {
export function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program, fileExtensionMap?: FileExtensionMap): Program {
let program: Program;
let files: SourceFile[] = [];
let commonSourceDirectory: string;
@ -324,7 +324,7 @@ namespace ts {
let skipDefaultLib = options.noLib;
const programDiagnostics = createDiagnosticCollection();
const currentDirectory = host.getCurrentDirectory();
const supportedExtensions = getSupportedExtensions(options);
const supportedExtensions = getSupportedExtensions(options, fileExtensionMap);
// Map storing if there is emit blocking diagnostics for given input
const hasEmitBlockingDiagnostics = createFileMap<boolean>(getCanonicalFileName);

View File

@ -2997,6 +2997,12 @@ namespace ts {
ThisProperty
}
export interface FileExtensionMap {
javaScript?: string[];
typeScript?: string[];
mixedContent?: string[];
}
export interface DiagnosticMessage {
key: string;
category: DiagnosticCategory;

View File

@ -1441,6 +1441,66 @@ namespace ts.projectSystem {
checkProjectActualFiles(projectService.inferredProjects[1], [file2.path]);
});
it("tsconfig script block support", () => {
const file1 = {
path: "/a/b/f1.ts",
content: ` `
};
const file2 = {
path: "/a/b/f2.html",
content: `var hello = "hello";`
};
const config = {
path: "/a/b/tsconfig.json",
content: JSON.stringify({ compilerOptions: { allowJs: true } })
};
const host = createServerHost([file1, file2, config]);
const session = createSession(host);
openFilesForSession([file1], session);
const projectService = session.getProjectService();
// HTML file will not be included in any projects yet
checkNumberOfProjects(projectService, { configuredProjects: 1 });
checkProjectActualFiles(projectService.configuredProjects[0], [file1.path]);
// Specify .html extension as mixed content
const configureHostRequest = makeSessionRequest<protocol.ConfigureRequestArguments>(CommandNames.Configure, { fileExtensionMap: { mixedContent: [".html"] } });
session.executeCommand(configureHostRequest).response;
// HTML file still not included in the project as it is closed
checkNumberOfProjects(projectService, { configuredProjects: 1 });
checkProjectActualFiles(projectService.configuredProjects[0], [file1.path]);
// Open HTML file
projectService.applyChangesInOpenFiles(
/*openFiles*/[{ fileName: file2.path, hasMixedContent: true, scriptKind: ScriptKind.JS, content: `var hello = "hello";` }],
/*changedFiles*/undefined,
/*closedFiles*/undefined);
// Now HTML file is included in the project
checkNumberOfProjects(projectService, { configuredProjects: 1 });
checkProjectActualFiles(projectService.configuredProjects[0], [file1.path, file2.path]);
// Check identifiers defined in HTML content are available in .ts file
const project = projectService.configuredProjects[0];
let completions = project.getLanguageService().getCompletionsAtPosition(file1.path, 1);
assert(completions && completions.entries[0].name === "hello", `expected entry hello to be in completion list`);
// Close HTML file
projectService.applyChangesInOpenFiles(
/*openFiles*/undefined,
/*changedFiles*/undefined,
/*closedFiles*/[file2.path]);
// HTML file is still included in project
checkNumberOfProjects(projectService, { configuredProjects: 1 });
checkProjectActualFiles(projectService.configuredProjects[0], [file1.path, file2.path]);
// Check identifiers defined in HTML content are not available in .ts file
completions = project.getLanguageService().getCompletionsAtPosition(file1.path, 5);
assert(completions && completions.entries[0].name !== "hello", `unexpected hello entry in completion list`);
});
it("project structure update is deferred if files are not added\removed", () => {
const file1 = {
path: "/a/b/f1.ts",

View File

@ -90,6 +90,7 @@ namespace ts.server {
export interface HostConfiguration {
formatCodeOptions: FormatCodeSettings;
hostInfo: string;
fileExtensionMap?: FileExtensionMap;
}
interface ConfigFileConversionResult {
@ -114,13 +115,13 @@ namespace ts.server {
interface FilePropertyReader<T> {
getFileName(f: T): string;
getScriptKind(f: T): ScriptKind;
hasMixedContent(f: T): boolean;
hasMixedContent(f: T, mixedContentExtensions: string[]): boolean;
}
const fileNamePropertyReader: FilePropertyReader<string> = {
getFileName: x => x,
getScriptKind: _ => undefined,
hasMixedContent: _ => false
hasMixedContent: (fileName, mixedContentExtensions) => forEach(mixedContentExtensions, extension => fileExtensionIs(fileName, extension))
};
const externalFilePropertyReader: FilePropertyReader<protocol.ExternalFile> = {
@ -235,12 +236,12 @@ namespace ts.server {
private readonly directoryWatchers: DirectoryWatchers;
private readonly throttledOperations: ThrottledOperations;
private readonly hostConfiguration: HostConfiguration;
private changedFiles: ScriptInfo[];
private toCanonicalFileName: (f: string) => string;
public readonly hostConfiguration: HostConfiguration;
public lastDeletedFile: ScriptInfo;
constructor(public readonly host: ServerHost,
@ -264,7 +265,8 @@ namespace ts.server {
this.hostConfiguration = {
formatCodeOptions: getDefaultFormatCodeSettings(this.host),
hostInfo: "Unknown host"
hostInfo: "Unknown host",
fileExtensionMap: {}
};
this.documentRegistry = createDocumentRegistry(host.useCaseSensitiveFileNames, host.getCurrentDirectory());
@ -455,7 +457,7 @@ namespace ts.server {
// If a change was made inside "folder/file", node will trigger the callback twice:
// one with the fileName being "folder/file", and the other one with "folder".
// We don't respond to the second one.
if (fileName && !ts.isSupportedSourceFileName(fileName, project.getCompilerOptions())) {
if (fileName && !ts.isSupportedSourceFileName(fileName, project.getCompilerOptions(), this.hostConfiguration.fileExtensionMap)) {
return;
}
@ -610,6 +612,9 @@ namespace ts.server {
let projectsToRemove: Project[];
for (const p of info.containingProjects) {
if (p.projectKind === ProjectKind.Configured) {
if (info.hasMixedContent) {
info.hasChanges = true;
}
// last open file in configured project - close it
if ((<ConfiguredProject>p).deleteOpenRef() === 0) {
(projectsToRemove || (projectsToRemove = [])).push(p);
@ -772,7 +777,9 @@ namespace ts.server {
this.host,
getDirectoryPath(configFilename),
/*existingOptions*/ {},
configFilename);
configFilename,
/*resolutionStack*/ [],
this.hostConfiguration.fileExtensionMap);
if (parsedCommandLine.errors.length) {
errors = concatenate(errors, parsedCommandLine.errors);
@ -876,7 +883,7 @@ namespace ts.server {
for (const f of files) {
const rootFilename = propertyReader.getFileName(f);
const scriptKind = propertyReader.getScriptKind(f);
const hasMixedContent = propertyReader.hasMixedContent(f);
const hasMixedContent = propertyReader.hasMixedContent(f, this.hostConfiguration.fileExtensionMap.mixedContent);
if (this.host.fileExists(rootFilename)) {
const info = this.getOrCreateScriptInfoForNormalizedPath(toNormalizedPath(rootFilename), /*openedByClient*/ clientFileName == rootFilename, /*fileContent*/ undefined, scriptKind, hasMixedContent);
project.addRoot(info);
@ -922,7 +929,7 @@ namespace ts.server {
rootFilesChanged = true;
if (!scriptInfo) {
const scriptKind = propertyReader.getScriptKind(f);
const hasMixedContent = propertyReader.hasMixedContent(f);
const hasMixedContent = propertyReader.hasMixedContent(f, this.hostConfiguration.fileExtensionMap.mixedContent);
scriptInfo = this.getOrCreateScriptInfoForNormalizedPath(normalizedPath, /*openedByClient*/ false, /*fileContent*/ undefined, scriptKind, hasMixedContent);
}
}
@ -1072,6 +1079,9 @@ namespace ts.server {
}
if (openedByClient) {
info.isOpen = true;
if (hasMixedContent) {
info.hasChanges = true;
}
}
}
return info;
@ -1103,6 +1113,10 @@ namespace ts.server {
mergeMaps(this.hostConfiguration.formatCodeOptions, convertFormatOptions(args.formatOptions));
this.logger.info("Format host information updated");
}
if (args.fileExtensionMap) {
this.hostConfiguration.fileExtensionMap = args.fileExtensionMap;
this.logger.info("Host file extension mappings updated");
}
}
}
@ -1168,12 +1182,12 @@ namespace ts.server {
}
openClientFileWithNormalizedPath(fileName: NormalizedPath, fileContent?: string, scriptKind?: ScriptKind, hasMixedContent?: boolean): OpenConfiguredProjectResult {
const info = this.getOrCreateScriptInfoForNormalizedPath(fileName, /*openedByClient*/ true, fileContent, scriptKind, hasMixedContent);
const { configFileName = undefined, configFileErrors = undefined }: OpenConfiguredProjectResult = this.findContainingExternalProject(fileName)
? {}
: this.openOrUpdateConfiguredProjectForFile(fileName);
// at this point if file is the part of some configured/external project then this project should be created
const info = this.getOrCreateScriptInfoForNormalizedPath(fileName, /*openedByClient*/ true, fileContent, scriptKind, hasMixedContent);
this.assignScriptInfoToInferredProjectIfNecessary(info, /*addToListOfOpenFiles*/ true);
this.printProjects();
return { configFileName, configFileErrors };

View File

@ -5,6 +5,7 @@
namespace ts.server {
export class LSHost implements ts.LanguageServiceHost, ModuleResolutionHost, ServerLanguageServiceHost {
private compilationSettings: ts.CompilerOptions;
private fileExtensionMap: FileExtensionMap;
private readonly resolvedModuleNames= createFileMap<Map<ResolvedModuleWithFailedLookupLocations>>();
private readonly resolvedTypeReferenceDirectives = createFileMap<Map<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>>();
private readonly getCanonicalFileName: (fileName: string) => string;
@ -143,6 +144,10 @@ namespace ts.server {
return this.compilationSettings;
}
getFileExtensionMap() {
return this.fileExtensionMap;
}
useCaseSensitiveFileNames() {
return this.host.useCaseSensitiveFileNames;
}
@ -231,5 +236,9 @@ namespace ts.server {
}
this.compilationSettings = opt;
}
setFileExtensionMap(fileExtensionMap: FileExtensionMap) {
this.fileExtensionMap = fileExtensionMap || {};
}
}
}

View File

@ -1,4 +1,4 @@
/// <reference path="..\services\services.ts" />
/// <reference path="..\services\services.ts" />
/// <reference path="utilities.ts"/>
/// <reference path="scriptInfo.ts"/>
/// <reference path="lsHost.ts"/>
@ -202,6 +202,7 @@ namespace ts.server {
enableLanguageService() {
const lsHost = new LSHost(this.projectService.host, this, this.projectService.cancellationToken);
lsHost.setCompilationSettings(this.compilerOptions);
lsHost.setFileExtensionMap(this.projectService.hostConfiguration.fileExtensionMap);
this.languageService = ts.createLanguageService(lsHost, this.documentRegistry);
this.lsHost = lsHost;
@ -462,6 +463,10 @@ namespace ts.server {
return !hasChanges;
}
private hasChangedFiles() {
return this.rootFiles && forEach(this.rootFiles, info => info.hasChanges);
}
private setTypings(typings: SortedReadonlyArray<string>): boolean {
if (arrayIsEqualTo(this.typingFiles, typings)) {
return false;
@ -475,7 +480,7 @@ namespace ts.server {
const oldProgram = this.program;
this.program = this.languageService.getProgram();
let hasChanges = false;
let hasChanges = this.hasChangedFiles();
// bump up the version if
// - oldProgram is not set - this is a first time updateGraph is called
// - newProgram is different from the old program and structure of the old program was not reused.
@ -578,6 +583,7 @@ namespace ts.server {
const added: string[] = [];
const removed: string[] = [];
const updated = this.rootFiles.filter(info => info.hasChanges).map(info => info.fileName);
for (const id in currentFiles) {
if (!hasProperty(lastReportedFileNames, id)) {
added.push(id);
@ -588,9 +594,12 @@ namespace ts.server {
removed.push(id);
}
}
for (const root of this.rootFiles) {
root.hasChanges = false;
}
this.lastReportedFileNames = currentFiles;
this.lastReportedVersion = this.projectStructureVersion;
return { info, changes: { added, removed }, projectErrors: this.projectErrors };
return { info, changes: { added, removed, updated }, projectErrors: this.projectErrors };
}
else {
// unknown version - return everything

View File

@ -914,6 +914,10 @@ namespace ts.server.protocol {
* List of removed files
*/
removed: string[];
/**
* List of updated files
*/
updated: string[];
}
/**
@ -986,6 +990,11 @@ namespace ts.server.protocol {
* The format options to use during formatting and other code editing features.
*/
formatOptions?: FormatCodeSettings;
/**
* The host's supported file extension mappings
*/
fileExtensionMap?: FileExtensionMap;
}
/**

View File

@ -13,7 +13,6 @@ namespace ts.server {
private fileWatcher: FileWatcher;
private svc: ScriptVersionCache;
// TODO: allow to update hasMixedContent from the outside
constructor(
private readonly host: ServerHost,
readonly fileName: NormalizedPath,
@ -29,6 +28,8 @@ namespace ts.server {
: getScriptKindFromFileName(fileName);
}
public hasChanges = false;
getFormatCodeSettings() {
return this.formatCodeSettings;
}

View File

@ -1,4 +1,4 @@
/// <reference path="types.d.ts" />
/// <reference path="types.d.ts" />
/// <reference path="shared.ts" />
namespace ts.server {
@ -211,6 +211,7 @@ namespace ts.server {
export interface ServerLanguageServiceHost {
setCompilationSettings(options: CompilerOptions): void;
setFileExtensionMap(fileExtensionMap: FileExtensionMap): void;
notifyFileRemoved(info: ScriptInfo): void;
startRecordingFilesWithChangedResolutions(): void;
finishRecordingFilesWithChangedResolutions(): Path[];
@ -218,6 +219,7 @@ namespace ts.server {
export const nullLanguageServiceHost: ServerLanguageServiceHost = {
setCompilationSettings: () => undefined,
setFileExtensionMap: () => undefined,
notifyFileRemoved: () => undefined,
startRecordingFilesWithChangedResolutions: () => undefined,
finishRecordingFilesWithChangedResolutions: () => undefined

View File

@ -271,13 +271,14 @@ namespace ts.Completions {
const span = getDirectoryFragmentTextSpan((<StringLiteral>node).text, node.getStart() + 1);
let entries: CompletionEntry[];
if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) {
const fileExtensionMap = host.getFileExtensionMap ? host.getFileExtensionMap() : {};
if (compilerOptions.rootDirs) {
entries = getCompletionEntriesForDirectoryFragmentWithRootDirs(
compilerOptions.rootDirs, literalValue, scriptDirectory, getSupportedExtensions(compilerOptions), /*includeExtensions*/false, span, scriptPath);
compilerOptions.rootDirs, literalValue, scriptDirectory, getSupportedExtensions(compilerOptions, fileExtensionMap), /*includeExtensions*/false, span, scriptPath);
}
else {
entries = getCompletionEntriesForDirectoryFragment(
literalValue, scriptDirectory, getSupportedExtensions(compilerOptions), /*includeExtensions*/false, span, scriptPath);
literalValue, scriptDirectory, getSupportedExtensions(compilerOptions, fileExtensionMap), /*includeExtensions*/false, span, scriptPath);
}
}
else {
@ -411,7 +412,8 @@ namespace ts.Completions {
let result: CompletionEntry[];
if (baseUrl) {
const fileExtensions = getSupportedExtensions(compilerOptions);
const fileExtensionMap = host.getFileExtensionMap ? host.getFileExtensionMap() : {};
const fileExtensions = getSupportedExtensions(compilerOptions, fileExtensionMap);
const projectDir = compilerOptions.project || host.getCurrentDirectory();
const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl);
result = getCompletionEntriesForDirectoryFragment(fragment, normalizePath(absolute), fileExtensions, /*includeExtensions*/false, span);
@ -588,7 +590,8 @@ namespace ts.Completions {
if (kind === "path") {
// Give completions for a relative path
const span: TextSpan = getDirectoryFragmentTextSpan(toComplete, range.pos + prefix.length);
completionInfo.entries = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, getSupportedExtensions(compilerOptions), /*includeExtensions*/true, span, sourceFile.path);
const fileExtensionMap = host.getFileExtensionMap ? host.getFileExtensionMap() : {};
completionInfo.entries = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, getSupportedExtensions(compilerOptions, fileExtensionMap), /*includeExtensions*/true, span, sourceFile.path);
}
else {
// Give completions based on the typings available

View File

@ -740,6 +740,7 @@ namespace ts {
class HostCache {
private fileNameToEntry: FileMap<HostFileInformation>;
private _compilationSettings: CompilerOptions;
private _fileExtensionMap: FileExtensionMap;
private currentDirectory: string;
constructor(private host: LanguageServiceHost, private getCanonicalFileName: (fileName: string) => string) {
@ -755,12 +756,18 @@ namespace ts {
// store the compilation settings
this._compilationSettings = host.getCompilationSettings() || getDefaultCompilerOptions();
this._fileExtensionMap = host.getFileExtensionMap ? host.getFileExtensionMap() : {};
}
public compilationSettings() {
return this._compilationSettings;
}
public fileExtensionMap() {
return this._fileExtensionMap;
}
private createEntry(fileName: string, path: Path) {
let entry: HostFileInformation;
const scriptSnapshot = this.host.getScriptSnapshot(fileName);
@ -1083,7 +1090,7 @@ namespace ts {
}
const documentRegistryBucketKey = documentRegistry.getKeyForCompilationSettings(newSettings);
const newProgram = createProgram(hostCache.getRootFileNames(), newSettings, compilerHost, program);
const newProgram = createProgram(hostCache.getRootFileNames(), newSettings, compilerHost, program, hostCache.fileExtensionMap());
// Release any files we have acquired in the old program but are
// not part of the new program.

View File

@ -125,6 +125,7 @@ namespace ts {
//
export interface LanguageServiceHost {
getCompilationSettings(): CompilerOptions;
getFileExtensionMap?(): FileExtensionMap;
getNewLine?(): string;
getProjectVersion?(): string;
getScriptFileNames(): string[];