Skip to content

Commit f2931a1

Browse files
committed
1 parent df57cdc commit f2931a1

File tree

8 files changed

+146
-69
lines changed

8 files changed

+146
-69
lines changed

src/compiler/core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,17 @@ namespace ts {
24132413
return <T>(removeFileExtension(path) + newExtension);
24142414
}
24152415

2416+
/**
2417+
* Takes a string like "jquery-min.4.2.3" and returns "jquery"
2418+
*/
2419+
export function removeMinAndVersionNumbers(fileName: string) {
2420+
// Match a "." or "-" followed by a version number or 'min' at the end of the name
2421+
const trailingMinOrVersion = /[.-]((min)|(\d+(\.\d+)*))$/;
2422+
2423+
// The "min" or version may both be present, in either order, so try applying the above twice.
2424+
return fileName.replace(trailingMinOrVersion, "").replace(trailingMinOrVersion, "");
2425+
}
2426+
24162427
export interface ObjectAllocator {
24172428
getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node;
24182429
getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos?: number, end?: number) => Token<TKind>;

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ namespace ts.projectSystem {
14831483

14841484
it("ignores files excluded by a custom safe type list", () => {
14851485
const file1 = {
1486-
path: "/a/b/f1.ts",
1486+
path: "/a/b/f1.js",
14871487
content: "export let x = 5"
14881488
};
14891489
const office = {
@@ -1504,7 +1504,7 @@ namespace ts.projectSystem {
15041504

15051505
it("ignores files excluded by the default type list", () => {
15061506
const file1 = {
1507-
path: "/a/b/f1.ts",
1507+
path: "/a/b/f1.js",
15081508
content: "export let x = 5"
15091509
};
15101510
const minFile = {

src/harness/unittests/typingsInstaller.ts

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -304,35 +304,35 @@ namespace ts.projectSystem {
304304
// 1. react typings are installed for .jsx
305305
// 2. loose files names are matched against safe list for typings if
306306
// this is a JS project (only js, jsx, d.ts files are present)
307-
const file1 = {
307+
const lodashJs = {
308308
path: "/a/b/lodash.js",
309309
content: ""
310310
};
311-
const file2 = {
311+
const file2Jsx = {
312312
path: "/a/b/file2.jsx",
313313
content: ""
314314
};
315-
const file3 = {
315+
const file3dts = {
316316
path: "/a/b/file3.d.ts",
317317
content: ""
318318
};
319-
const react = {
319+
const reactDts = {
320320
path: "/a/data/node_modules/@types/react/index.d.ts",
321321
content: "declare const react: { x: number }"
322322
};
323-
const lodash = {
323+
const lodashDts = {
324324
path: "/a/data/node_modules/@types/lodash/index.d.ts",
325325
content: "declare const lodash: { x: number }"
326326
};
327327

328-
const host = createServerHost([file1, file2, file3, customTypesMap]);
328+
const host = createServerHost([lodashJs, file2Jsx, file3dts, customTypesMap]);
329329
const installer = new (class extends Installer {
330330
constructor() {
331331
super(host, { typesRegistry: createTypesRegistry("lodash", "react") });
332332
}
333333
installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction): void {
334334
const installedTypings = ["@types/lodash", "@types/react"];
335-
const typingFiles = [lodash, react];
335+
const typingFiles = [lodashDts, reactDts];
336336
executeCommand(this, host, installedTypings, typingFiles, cb);
337337
}
338338
})();
@@ -342,35 +342,74 @@ namespace ts.projectSystem {
342342
projectService.openExternalProject({
343343
projectFileName,
344344
options: { allowJS: true, moduleResolution: ModuleResolutionKind.NodeJs },
345-
rootFiles: [toExternalFile(file1.path), toExternalFile(file2.path), toExternalFile(file3.path)],
346-
typeAcquisition: {}
345+
rootFiles: [toExternalFile(lodashJs.path), toExternalFile(file2Jsx.path), toExternalFile(file3dts.path)],
346+
typeAcquisition: { }
347347
});
348348

349349
const p = projectService.externalProjects[0];
350350
projectService.checkNumberOfProjects({ externalProjects: 1 });
351-
checkProjectActualFiles(p, [file1.path, file2.path, file3.path]);
351+
checkProjectActualFiles(p, [file2Jsx.path, file3dts.path]);
352352

353353
installer.installAll(/*expectedCount*/ 1);
354354

355355
checkNumberOfProjects(projectService, { externalProjects: 1 });
356356
host.checkTimeoutQueueLengthAndRun(2);
357357
checkNumberOfProjects(projectService, { externalProjects: 1 });
358-
checkProjectActualFiles(p, [file1.path, file2.path, file3.path, lodash.path, react.path]);
358+
checkProjectActualFiles(p, [file2Jsx.path, file3dts.path, lodashDts.path, reactDts.path]);
359359
});
360360

361+
it("external project - type acquisition with enable: false", () => {
362+
// Tests:
363+
// Exclude
364+
const jqueryJs = {
365+
path: "/a/b/jquery.js",
366+
content: ""
367+
};
368+
369+
const host = createServerHost([jqueryJs]);
370+
const installer = new (class extends Installer {
371+
constructor() {
372+
super(host, { typesRegistry: createTypesRegistry("jquery") });
373+
}
374+
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray<string>) {
375+
super.enqueueInstallTypingsRequest(project, typeAcquisition, unresolvedImports);
376+
}
377+
installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction): void {
378+
const installedTypings: string[] = [];
379+
const typingFiles: FileOrFolder[] = [];
380+
executeCommand(this, host, installedTypings, typingFiles, cb);
381+
}
382+
})();
383+
384+
const projectFileName = "/a/app/test.csproj";
385+
const projectService = createProjectService(host, { typingsInstaller: installer });
386+
projectService.openExternalProject({
387+
projectFileName,
388+
options: { allowJS: true, moduleResolution: ModuleResolutionKind.NodeJs },
389+
rootFiles: [toExternalFile(jqueryJs.path)],
390+
typeAcquisition: { enable: false }
391+
});
392+
393+
const p = projectService.externalProjects[0];
394+
projectService.checkNumberOfProjects({ externalProjects: 1 });
395+
396+
checkProjectActualFiles(p, [jqueryJs.path]);
397+
398+
installer.checkPendingCommands(/*expectedCount*/ 0);
399+
});
361400
it("external project - no type acquisition, with js & ts files", () => {
362401
// Tests:
363402
// 1. No typings are included for JS projects when the project contains ts files
364-
const file1 = {
403+
const jqueryJs = {
365404
path: "/a/b/jquery.js",
366405
content: ""
367406
};
368-
const file2 = {
407+
const file2Ts = {
369408
path: "/a/b/file2.ts",
370409
content: ""
371410
};
372411

373-
const host = createServerHost([file1, file2]);
412+
const host = createServerHost([jqueryJs, file2Ts]);
374413
const installer = new (class extends Installer {
375414
constructor() {
376415
super(host, { typesRegistry: createTypesRegistry("jquery") });
@@ -390,34 +429,35 @@ namespace ts.projectSystem {
390429
projectService.openExternalProject({
391430
projectFileName,
392431
options: { allowJS: true, moduleResolution: ModuleResolutionKind.NodeJs },
393-
rootFiles: [toExternalFile(file1.path), toExternalFile(file2.path)],
432+
rootFiles: [toExternalFile(jqueryJs.path), toExternalFile(file2Ts.path)],
394433
typeAcquisition: {}
395434
});
396435

397436
const p = projectService.externalProjects[0];
398437
projectService.checkNumberOfProjects({ externalProjects: 1 });
399-
checkProjectActualFiles(p, [file2.path]);
438+
439+
checkProjectActualFiles(p, [jqueryJs.path, file2Ts.path]);
400440

401441
installer.checkPendingCommands(/*expectedCount*/ 0);
402442

403443
checkNumberOfProjects(projectService, { externalProjects: 1 });
404-
checkProjectActualFiles(p, [file2.path]);
444+
checkProjectActualFiles(p, [jqueryJs.path, file2Ts.path]);
405445
});
406446

407447
it("external project - with type acquisition, with only js, d.ts files", () => {
408448
// Tests:
409449
// 1. Safelist matching, type acquisition includes/excludes and package.json typings are all acquired
410450
// 2. Types for safelist matches are not included when they also appear in the type acquisition exclude list
411451
// 3. Multiple includes and excludes are respected in type acquisition
412-
const file1 = {
452+
const lodashJs = {
413453
path: "/a/b/lodash.js",
414454
content: ""
415455
};
416-
const file2 = {
456+
const commanderJs = {
417457
path: "/a/b/commander.js",
418458
content: ""
419459
};
420-
const file3 = {
460+
const file3dts = {
421461
path: "/a/b/file3.d.ts",
422462
content: ""
423463
};
@@ -448,7 +488,7 @@ namespace ts.projectSystem {
448488
content: "declare const moment: { x: number }"
449489
};
450490

451-
const host = createServerHost([file1, file2, file3, packageJson, customTypesMap]);
491+
const host = createServerHost([lodashJs, commanderJs, file3dts, packageJson, customTypesMap]);
452492
const installer = new (class extends Installer {
453493
constructor() {
454494
super(host, { typesRegistry: createTypesRegistry("jquery", "commander", "moment", "express") });
@@ -465,20 +505,24 @@ namespace ts.projectSystem {
465505
projectService.openExternalProject({
466506
projectFileName,
467507
options: { allowJS: true, moduleResolution: ModuleResolutionKind.NodeJs },
468-
rootFiles: [toExternalFile(file1.path), toExternalFile(file2.path), toExternalFile(file3.path)],
469-
typeAcquisition: { include: ["jquery", "moment"], exclude: ["lodash"] }
508+
rootFiles: [toExternalFile(lodashJs.path), toExternalFile(commanderJs.path), toExternalFile(file3dts.path)],
509+
typeAcquisition: { enable: true, include: ["jquery", "moment"], exclude: ["lodash"] }
470510
});
471511

472512
const p = projectService.externalProjects[0];
473513
projectService.checkNumberOfProjects({ externalProjects: 1 });
474-
checkProjectActualFiles(p, [file1.path, file2.path, file3.path]);
514+
checkProjectActualFiles(p, [file3dts.path]);
475515

476516
installer.installAll(/*expectedCount*/ 1);
477517

478518
checkNumberOfProjects(projectService, { externalProjects: 1 });
479519
host.checkTimeoutQueueLengthAndRun(2);
480520
checkNumberOfProjects(projectService, { externalProjects: 1 });
481-
checkProjectActualFiles(p, [file1.path, file2.path, file3.path, commander.path, express.path, jquery.path, moment.path]);
521+
// Commander: Existed as a JS file
522+
// JQuery: Specified in 'include'
523+
// Moment: Specified in 'include'
524+
// lodash: Excluded (not present)
525+
checkProjectActualFiles(p, [file3dts.path, commander.path, jquery.path, moment.path]);
482526
});
483527

484528
it("Throttle - delayed typings to install", () => {
@@ -548,7 +592,7 @@ namespace ts.projectSystem {
548592

549593
const p = projectService.externalProjects[0];
550594
projectService.checkNumberOfProjects({ externalProjects: 1 });
551-
checkProjectActualFiles(p, [lodashJs.path, commanderJs.path, file3.path]);
595+
checkProjectActualFiles(p, [file3.path]);
552596
installer.checkPendingCommands(/*expectedCount*/ 1);
553597
installer.executePendingCommands();
554598
// expected all typings file to exist
@@ -557,7 +601,7 @@ namespace ts.projectSystem {
557601
}
558602
host.checkTimeoutQueueLengthAndRun(2);
559603
checkNumberOfProjects(projectService, { externalProjects: 1 });
560-
checkProjectActualFiles(p, [lodashJs.path, commanderJs.path, file3.path, commander.path, express.path, jquery.path, moment.path, lodash.path]);
604+
checkProjectActualFiles(p, [file3.path, commander.path, jquery.path, moment.path, lodash.path]);
561605
});
562606

563607
it("Throttle - delayed run install requests", () => {
@@ -648,7 +692,7 @@ namespace ts.projectSystem {
648692
const p1 = projectService.externalProjects[0];
649693
const p2 = projectService.externalProjects[1];
650694
projectService.checkNumberOfProjects({ externalProjects: 2 });
651-
checkProjectActualFiles(p1, [lodashJs.path, commanderJs.path, file3.path]);
695+
checkProjectActualFiles(p1, [file3.path]);
652696
checkProjectActualFiles(p2, [file3.path]);
653697

654698
installer.executePendingCommands();
@@ -659,7 +703,7 @@ namespace ts.projectSystem {
659703

660704
installer.executePendingCommands();
661705
host.checkTimeoutQueueLengthAndRun(3); // for 2 projects and 1 refreshing inferred project
662-
checkProjectActualFiles(p1, [lodashJs.path, commanderJs.path, file3.path, commander.path, jquery.path, lodash.path, cordova.path]);
706+
checkProjectActualFiles(p1, [file3.path, commander.path, jquery.path, lodash.path, cordova.path]);
663707
checkProjectActualFiles(p2, [file3.path, grunt.path, gulp.path]);
664708
});
665709

@@ -973,7 +1017,7 @@ namespace ts.projectSystem {
9731017
}
9741018
};
9751019
session.executeCommand(changeRequest);
976-
host.checkTimeoutQueueLengthAndRun(2); // This enqueues the updategraph and refresh inferred projects
1020+
host.checkTimeoutQueueLengthAndRun(0); // This enqueues the updategraph and refresh inferred projects
9771021
const version2 = proj.getCachedUnresolvedImportsPerFile_TestOnly().getVersion();
9781022
assert.equal(version1, version2, "set of unresolved imports should not change");
9791023
});

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ namespace ts.TestFSWithWatch {
137137
}
138138

139139
export function checkFileNames(caption: string, actualFileNames: ReadonlyArray<string>, expectedFileNames: string[]) {
140-
assert.equal(actualFileNames.length, expectedFileNames.length, `${caption}: incorrect actual number of files, expected ${expectedFileNames}, got ${actualFileNames}`);
140+
assert.equal(actualFileNames.length, expectedFileNames.length, `${caption}: incorrect actual number of files, expected:\r\n${expectedFileNames.join("\r\n")}\r\ngot: ${actualFileNames.join("\r\n")}`);
141141
for (const f of expectedFileNames) {
142-
assert.isTrue(contains(actualFileNames, f), `${caption}: expected to find ${f} in ${actualFileNames}`);
142+
assert.equal(true, contains(actualFileNames, f), `${caption}: expected to find ${f} in ${actualFileNames}`);
143143
}
144144
}
145145

0 commit comments

Comments
 (0)