Skip to content

Adding tsconfig.json mixed content (script block) support #12153

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

Merged
merged 15 commits into from
Dec 10, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,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);
Expand Down Expand Up @@ -964,7 +964,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it not really a map anymore. maybe extraExtensions: ExtensionInfo[]?

Copy link
Member Author

@jramsay jramsay Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good - will switch to extraFileExtensions: FileExtensionInfo[]


if (result.fileNames.length === 0 && !hasProperty(json, "files") && resolutionStack.length === 0) {
errors.push(
Expand Down Expand Up @@ -1166,7 +1166,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
Expand Down Expand Up @@ -1200,7 +1200,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.
Expand Down
16 changes: 12 additions & 4 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1942,8 +1942,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[] = [];
Copy link
Contributor

@vladima vladima Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit to reduce allocations:

let allExtensions: string[];
let allTypeScriptExtensions: string[];
const needAllExtensions = options && options.allowJs;
if (!extraFileExtensions) {
    return needAllExtensions ? allSupportedExtensions : supportedTypeScriptExtensions;
}
const extensions = (needAllExtensions ? allSupportedExtensions : supportedTypeScriptExtensions).slice(0);
for (const ext of extraExtensions) {
    if (!needAllExtensions || ext.scriptKind === ScriptKind.TS) {
        extensions.push(ext.fileName);
    }
}
return extensions;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clever - testing this change and will update

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) {
Expand All @@ -1954,10 +1962,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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to push concrete knowledge about these extra extensions through all layers, this will be necessary if we'll need to be able to add files with these extensions into program via imports or tripleslash references. Since both these scenarios are not supported and files with custom extensions should always be included in the list of root files I think we can:

  • revert changes in program, language service host, lsHost and services
  • in editor services for configured projects if host configuration specifies extra extensions and result of cracking config files contains files with these extensions - set allowNonTsExtensions bit on CompilerOptions to force adding these files into program.

this way I think we can update only project system part and keep all other layers relatively untouched.

let program: Program;
let files: SourceFile[] = [];
let commonSourceDirectory: string;
Expand Down Expand Up @@ -320,7 +320,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);
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3075,6 +3075,12 @@ namespace ts {
ThisProperty
}

export interface FileExtensionMap {
javaScript?: string[];
typeScript?: string[];
mixedContent?: string[];
}

export interface DiagnosticMessage {
key: string;
category: DiagnosticCategory;
Expand Down
60 changes: 60 additions & 0 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 24 additions & 10 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ namespace ts.server {
export interface HostConfiguration {
formatCodeOptions: FormatCodeSettings;
hostInfo: string;
fileExtensionMap?: FileExtensionMap;
}

interface ConfigFileConversionResult {
Expand All @@ -132,13 +133,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> = {
Expand Down Expand Up @@ -253,12 +254,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,
Expand All @@ -282,7 +283,8 @@ namespace ts.server {

this.hostConfiguration = {
formatCodeOptions: getDefaultFormatCodeSettings(this.host),
hostInfo: "Unknown host"
hostInfo: "Unknown host",
fileExtensionMap: {}
};

this.documentRegistry = createDocumentRegistry(host.useCaseSensitiveFileNames, host.getCurrentDirectory());
Expand Down Expand Up @@ -486,7 +488,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;
}

Expand Down Expand Up @@ -641,6 +643,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);
Expand Down Expand Up @@ -803,7 +808,9 @@ namespace ts.server {
this.host,
getDirectoryPath(configFilename),
/*existingOptions*/ {},
configFilename);
configFilename,
/*resolutionStack*/ [],
this.hostConfiguration.fileExtensionMap);

if (parsedCommandLine.errors.length) {
errors = concatenate(errors, parsedCommandLine.errors);
Expand Down Expand Up @@ -907,7 +914,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);
Expand Down Expand Up @@ -953,7 +960,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);
}
}
Expand Down Expand Up @@ -1103,6 +1110,9 @@ namespace ts.server {
}
if (openedByClient) {
info.isOpen = true;
if (hasMixedContent) {
info.hasChanges = true;
}
}
}
return info;
Expand Down Expand Up @@ -1134,6 +1144,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");
}
}
}

Expand Down Expand Up @@ -1199,12 +1213,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 };
Expand Down
9 changes: 9 additions & 0 deletions src/server/lsHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ts.server {
export class LSHost implements ts.LanguageServiceHost, ModuleResolutionHost {
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;
Expand Down Expand Up @@ -143,6 +144,10 @@ namespace ts.server {
return this.compilationSettings;
}

getFileExtensionMap() {
return this.fileExtensionMap;
}

useCaseSensitiveFileNames() {
return this.host.useCaseSensitiveFileNames;
}
Expand Down Expand Up @@ -231,5 +236,9 @@ namespace ts.server {
}
this.compilationSettings = opt;
}

setFileExtensionMap(fileExtensionMap: FileExtensionMap) {
this.fileExtensionMap = fileExtensionMap || {};
}
}
}
Loading