Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion Gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,11 @@ gulp.task(serverFile, /*help*/ false, [servicesFile, typingsInstallerJs, cancell
.pipe(gulp.dest("src/server"));
});

const typesMapJson = path.join(builtLocalDirectory, "typesMap.json");
const tsserverLibraryFile = path.join(builtLocalDirectory, "tsserverlibrary.js");
const tsserverLibraryDefinitionFile = path.join(builtLocalDirectory, "tsserverlibrary.d.ts");

gulp.task(tsserverLibraryFile, /*help*/ false, [servicesFile], (done) => {
gulp.task(tsserverLibraryFile, /*help*/ false, [servicesFile, typesMapJson], (done) => {
const serverLibraryProject = tsc.createProject("src/server/tsconfig.library.json", getCompilerSettings({}, /*useBuiltCompiler*/ true));
const {js, dts}: { js: NodeJS.ReadableStream, dts: NodeJS.ReadableStream } = serverLibraryProject.src()
.pipe(sourcemaps.init())
Expand All @@ -485,6 +486,15 @@ gulp.task(tsserverLibraryFile, /*help*/ false, [servicesFile], (done) => {
]);
});

gulp.task(typesMapJson, /*help*/ false, [], () => {
return gulp.src("src/server/typesMap.json")
.pipe(insert.transform((contents, file) => {
JSON.parse(contents);
return contents;
}))
.pipe(gulp.dest(builtLocalDirectory));
});

gulp.task("lssl", "Builds language service server library", [tsserverLibraryFile]);
gulp.task("local", "Builds the full compiler and services", [builtLocalCompiler, servicesFile, serverFile, builtGeneratedDiagnosticMessagesJSON, tsserverLibraryFile]);
gulp.task("tsc", "Builds only the compiler", [builtLocalCompiler]);
Expand Down
16 changes: 14 additions & 2 deletions Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var watchGuardSources = filesFromConfig(path.join(serverDirectory, "watchGuard/t
var serverSources = filesFromConfig(path.join(serverDirectory, "tsconfig.json"))
var languageServiceLibrarySources = filesFromConfig(path.join(serverDirectory, "tsconfig.library.json"));

var typesMapOutputPath = path.join(builtLocalDirectory, 'typesMap.json');

var harnessCoreSources = [
"harness.ts",
"virtualFileSystem.ts",
Expand Down Expand Up @@ -423,6 +425,7 @@ var buildProtocolTs = path.join(scriptsDirectory, "buildProtocol.ts");
var buildProtocolJs = path.join(scriptsDirectory, "buildProtocol.js");
var buildProtocolDts = path.join(builtLocalDirectory, "protocol.d.ts");
var typescriptServicesDts = path.join(builtLocalDirectory, "typescriptServices.d.ts");
var typesMapJson = path.join(builtLocalDirectory, "typesMap.json");

file(buildProtocolTs);

Expand Down Expand Up @@ -587,6 +590,16 @@ var serverFile = path.join(builtLocalDirectory, "tsserver.js");
compileFile(serverFile, serverSources, [builtLocalDirectory, copyright, cancellationTokenFile, typingsInstallerFile, watchGuardFile].concat(serverSources).concat(servicesSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { types: ["node"], preserveConstEnums: true, lib: "es6" });
var tsserverLibraryFile = path.join(builtLocalDirectory, "tsserverlibrary.js");
var tsserverLibraryDefinitionFile = path.join(builtLocalDirectory, "tsserverlibrary.d.ts");
file(typesMapOutputPath, function() {
var content = fs.readFileSync(path.join(serverDirectory, 'typesMap.json'));
// Validate that it's valid JSON
try {
JSON.parse(content);
} catch (e) {
console.log("Parse error in typesMap.json: " + e);
}
fs.writeFileSync(typesMapOutputPath, content);
});
compileFile(
tsserverLibraryFile,
languageServiceLibrarySources,
Expand All @@ -609,7 +622,7 @@ compileFile(

// Local target to build the language service server library
desc("Builds language service server library");
task("lssl", [tsserverLibraryFile, tsserverLibraryDefinitionFile]);
task("lssl", [tsserverLibraryFile, tsserverLibraryDefinitionFile, typesMapOutputPath]);

desc("Emit the start of the build fold");
task("build-fold-start", [], function () {
Expand Down Expand Up @@ -638,7 +651,6 @@ task("release", function () {
// Set the default task to "local"
task("default", ["local"]);


// Cleans the built directory
desc("Cleans the compiler output, declare files, and tests");
task("clean", function () {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,8 @@
"fs": false,
"os": false,
"path": false
},
"dependencies": {
"browser-resolve": "^1.11.2"
}
}
34 changes: 24 additions & 10 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@ namespace ts.projectSystem {
})
};

const customSafeList = {
path: <Path>"/typeMapList.json",
content: JSON.stringify({
"quack": {
"match": "/duckquack-(\\d+)\\.min\\.js",
"types": ["duck-types"]
export const customTypesMap = {
path: <Path>"/typesMap.json",
content: `{
"typesMap": {
"jquery": {
"match": "jquery(-(\\\\.?\\\\d+)+)?(\\\\.intellisense)?(\\\\.min)?\\\\.js$",
"types": ["jquery"]
},
"quack": {
"match": "/duckquack-(\\\\d+)\\\\.min\\\\.js",
"types": ["duck-types"]
}
},
})
"simpleMap": {
"Bacon": "baconjs",
"bliss": "blissfuljs",
"commander": "commander",
"cordova": "cordova",
"react": "react",
"lodash": "lodash"
}
}`
};

export interface PostExecAction {
Expand Down Expand Up @@ -59,7 +73,7 @@ namespace ts.projectSystem {
installTypingHost: server.ServerHost,
readonly typesRegistry = createMap<void>(),
log?: TI.Log) {
super(installTypingHost, globalTypingsCacheLocation, safeList.path, throttleLimit, log);
super(installTypingHost, globalTypingsCacheLocation, safeList.path, customTypesMap.path, throttleLimit, log);
}

protected postExecActions: PostExecAction[] = [];
Expand Down Expand Up @@ -229,6 +243,7 @@ namespace ts.projectSystem {
useSingleInferredProject,
useInferredProjectPerProjectRoot: false,
typingsInstaller,
typesMapLocation: customTypesMap.path,
eventHandler,
...opts
});
Expand Down Expand Up @@ -1491,9 +1506,8 @@ namespace ts.projectSystem {
path: "/lib/duckquack-3.min.js",
content: "whoa do @@ not parse me ok thanks!!!"
};
const host = createServerHost([customSafeList, file1, office]);
const host = createServerHost([file1, office, customTypesMap]);
const projectService = createProjectService(host);
projectService.loadSafeList(customSafeList.path);
try {
projectService.openExternalProject({ projectFileName: "project", options: {}, rootFiles: toExternalFiles([file1.path, office.path]) });
const proj = projectService.externalProjects[0];
Expand Down
8 changes: 4 additions & 4 deletions src/harness/unittests/typingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ namespace ts.projectSystem {
content: "declare const lodash: { x: number }"
};

const host = createServerHost([file1, file2, file3]);
const host = createServerHost([file1, file2, file3, customTypesMap]);
const installer = new (class extends Installer {
constructor() {
super(host, { typesRegistry: createTypesRegistry("lodash", "react") });
Expand Down Expand Up @@ -445,7 +445,7 @@ namespace ts.projectSystem {
content: "declare const moment: { x: number }"
};

const host = createServerHost([file1, file2, file3, packageJson]);
const host = createServerHost([file1, file2, file3, packageJson, customTypesMap]);
const installer = new (class extends Installer {
constructor() {
super(host, { typesRegistry: createTypesRegistry("jquery", "commander", "moment", "express") });
Expand Down Expand Up @@ -521,7 +521,7 @@ namespace ts.projectSystem {
};

const typingFiles = [commander, express, jquery, moment, lodash];
const host = createServerHost([lodashJs, commanderJs, file3, packageJson]);
const host = createServerHost([lodashJs, commanderJs, file3, packageJson, customTypesMap]);
const installer = new (class extends Installer {
constructor() {
super(host, { throttleLimit: 3, typesRegistry: createTypesRegistry("commander", "express", "jquery", "moment", "lodash") });
Expand Down Expand Up @@ -600,7 +600,7 @@ namespace ts.projectSystem {
typings: typingsName("gulp")
};

const host = createServerHost([lodashJs, commanderJs, file3]);
const host = createServerHost([lodashJs, commanderJs, file3, customTypesMap]);
const installer = new (class extends Installer {
constructor() {
super(host, { throttleLimit: 1, typesRegistry: createTypesRegistry("commander", "jquery", "lodash", "cordova", "gulp", "grunt") });
Expand Down
66 changes: 51 additions & 15 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ namespace ts.server {
"smart": IndentStyle.Smart
});

export interface TypesMapFile {
typesMap: SafeList;
simpleMap: string[];
}

/**
* How to understand this block:
* * The 'match' property is a regexp that matches a filename.
Expand Down Expand Up @@ -330,6 +335,7 @@ namespace ts.server {
globalPlugins?: ReadonlyArray<string>;
pluginProbeLocations?: ReadonlyArray<string>;
allowLocalPluginLoads?: boolean;
typesMapLocation?: string;
}

export class ProjectService {
Expand Down Expand Up @@ -391,6 +397,7 @@ namespace ts.server {
public readonly globalPlugins: ReadonlyArray<string>;
public readonly pluginProbeLocations: ReadonlyArray<string>;
public readonly allowLocalPluginLoads: boolean;
public readonly typesMapLocation: string | undefined;

/** Tracks projects that we have already sent telemetry for. */
private readonly seenProjects = createMap<true>();
Expand All @@ -407,13 +414,18 @@ namespace ts.server {
this.globalPlugins = opts.globalPlugins || emptyArray;
this.pluginProbeLocations = opts.pluginProbeLocations || emptyArray;
this.allowLocalPluginLoads = !!opts.allowLocalPluginLoads;
this.typesMapLocation = (opts.typesMapLocation === undefined) ? combinePaths(this.host.getExecutingFilePath(), "../typesMap.json") : opts.typesMapLocation;

Debug.assert(!!this.host.createHash, "'ServerHost.createHash' is required for ProjectService");

this.toCanonicalFileName = createGetCanonicalFileName(this.host.useCaseSensitiveFileNames);
this.directoryWatchers = new DirectoryWatchers(this);
this.throttledOperations = new ThrottledOperations(this.host);

if (opts.typesMapLocation) {
this.loadTypesMap();
}

this.typingsInstaller.attach(this);

this.typingsCache = new TypingsCache(this.typingsInstaller);
Expand Down Expand Up @@ -451,6 +463,27 @@ namespace ts.server {
this.eventHandler(event);
}

private loadTypesMap() {
try {
const fileContent = this.host.readFile(this.typesMapLocation);
if (fileContent === undefined) {
this.logger.info(`Provided types map file "${this.typesMapLocation}" doesn't exist`);
return;
}
const raw: TypesMapFile = JSON.parse(fileContent);
// Parse the regexps
for (const k of Object.keys(raw.typesMap)) {
raw.typesMap[k].match = new RegExp(raw.typesMap[k].match as {} as string, "i");
}
// raw is now fixed and ready
this.safelist = raw.typesMap;
}
catch (e) {
this.logger.info(`Error loading types map: ${e}`);
this.safelist = defaultTypeSafeList;
}
}

updateTypingsForProject(response: SetTypings | InvalidateCachedTypings): void {
const project = this.findProject(response.projectName);
if (!project) {
Expand Down Expand Up @@ -1712,23 +1745,14 @@ namespace ts.server {
this.safelist = defaultTypeSafeList;
}

loadSafeList(fileName: string): void {
const raw: SafeList = JSON.parse(this.host.readFile(fileName, "utf-8"));
// Parse the regexps
for (const k of Object.keys(raw)) {
raw[k].match = new RegExp(raw[k].match as {} as string, "i");
}
// raw is now fixed and ready
this.safelist = raw;
}

applySafeList(proj: protocol.ExternalProject): void {
applySafeList(proj: protocol.ExternalProject): NormalizedPath[] {
const { rootFiles, typeAcquisition } = proj;
const types = (typeAcquisition && typeAcquisition.include) || [];

const excludeRules: string[] = [];

const normalizedNames = rootFiles.map(f => normalizeSlashes(f.fileName));
const normalizedNames = rootFiles.map(f => normalizeSlashes(f.fileName)) as NormalizedPath[];
const excludedFiles: NormalizedPath[] = [];

for (const name of Object.keys(this.safelist)) {
const rule = this.safelist[name];
Expand Down Expand Up @@ -1787,7 +1811,17 @@ namespace ts.server {
}

const excludeRegexes = excludeRules.map(e => new RegExp(e, "i"));
proj.rootFiles = proj.rootFiles.filter((_file, index) => !excludeRegexes.some(re => re.test(normalizedNames[index])));
const filesToKeep: ts.server.protocol.ExternalFile[] = [];
for (let i = 0; i < proj.rootFiles.length; i++) {
if (excludeRegexes.some(re => re.test(normalizedNames[i]))) {
excludedFiles.push(normalizedNames[i]);
}
else {
filesToKeep.push(proj.rootFiles[i]);
}
}
proj.rootFiles = filesToKeep;
return excludedFiles;
}

openExternalProject(proj: protocol.ExternalProject, suppressRefreshOfInferredProjects = false): void {
Expand All @@ -1798,7 +1832,7 @@ namespace ts.server {
proj.typeAcquisition = typeAcquisition;
}

this.applySafeList(proj);
const excludedFiles = this.applySafeList(proj);

let tsConfigFiles: NormalizedPath[];
const rootFiles: protocol.ExternalFile[] = [];
Expand All @@ -1822,6 +1856,7 @@ namespace ts.server {
const externalProject = this.findExternalProjectByProjectName(proj.projectFileName);
let exisingConfigFiles: string[];
if (externalProject) {
externalProject.excludedFiles = excludedFiles;
if (!tsConfigFiles) {
const compilerOptions = convertCompilerOptions(proj.options);
if (this.exceededTotalSizeLimitForNonTsFiles(proj.projectFileName, compilerOptions, proj.rootFiles, externalFilePropertyReader)) {
Expand Down Expand Up @@ -1891,7 +1926,8 @@ namespace ts.server {
else {
// no config files - remove the item from the collection
this.externalProjectToConfiguredProjectMap.delete(proj.projectFileName);
this.createAndAddExternalProject(proj.projectFileName, rootFiles, proj.options, proj.typeAcquisition);
const newProj = this.createAndAddExternalProject(proj.projectFileName, rootFiles, proj.options, proj.typeAcquisition);
newProj.excludedFiles = excludedFiles;
}
if (!suppressRefreshOfInferredProjects) {
this.refreshInferredProjects();
Expand Down
10 changes: 10 additions & 0 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ namespace ts.server {
return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles);
}

getExcludedFiles(): ReadonlyArray<NormalizedPath> {
return emptyArray;
}

getFileNames(excludeFilesFromExternalLibraries?: boolean, excludeConfigFiles?: boolean) {
if (!this.program) {
return [];
Expand Down Expand Up @@ -1166,6 +1170,7 @@ namespace ts.server {
* These are created only if a host explicitly calls `openExternalProject`.
*/
export class ExternalProject extends Project {
excludedFiles: ReadonlyArray<NormalizedPath> = [];
private typeAcquisition: TypeAcquisition;
constructor(public externalProjectName: string,
projectService: ProjectService,
Expand All @@ -1175,6 +1180,11 @@ namespace ts.server {
public compileOnSaveEnabled: boolean,
private readonly projectFilePath?: string) {
super(externalProjectName, ProjectKind.External, projectService, documentRegistry, /*hasExplicitListOfFiles*/ true, languageServiceEnabled, compilerOptions, compileOnSaveEnabled);

}

getExcludedFiles() {
return this.excludedFiles;
}

getProjectRootPath() {
Expand Down
Loading