Skip to content

Commit 37e18d9

Browse files
author
Andy Hanson
committed
Add createMapFromTemplate helper
1 parent f0e1fd9 commit 37e18d9

File tree

14 files changed

+42
-37
lines changed

14 files changed

+42
-37
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ namespace ts {
315315
NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | TypeofNEHostObject | EQNull | EQUndefinedOrNull | NEUndefined | Falsy,
316316
}
317317

318-
const typeofEQFacts = createMap({
318+
const typeofEQFacts = createMapFromTemplate({
319319
"string": TypeFacts.TypeofEQString,
320320
"number": TypeFacts.TypeofEQNumber,
321321
"boolean": TypeFacts.TypeofEQBoolean,
@@ -325,7 +325,7 @@ namespace ts {
325325
"function": TypeFacts.TypeofEQFunction
326326
});
327327

328-
const typeofNEFacts = createMap({
328+
const typeofNEFacts = createMapFromTemplate({
329329
"string": TypeFacts.TypeofNEString,
330330
"number": TypeFacts.TypeofNENumber,
331331
"boolean": TypeFacts.TypeofNEBoolean,
@@ -335,7 +335,7 @@ namespace ts {
335335
"function": TypeFacts.TypeofNEFunction
336336
});
337337

338-
const typeofTypesByName = createMap<Type>({
338+
const typeofTypesByName = createMapFromTemplate<Type>({
339339
"string": stringType,
340340
"number": numberType,
341341
"boolean": booleanType,

src/compiler/commandLineParser.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace ts {
6565
},
6666
{
6767
name: "jsx",
68-
type: createMap({
68+
type: createMapFromTemplate({
6969
"preserve": JsxEmit.Preserve,
7070
"react": JsxEmit.React
7171
}),
@@ -100,7 +100,7 @@ namespace ts {
100100
{
101101
name: "module",
102102
shortName: "m",
103-
type: createMap({
103+
type: createMapFromTemplate({
104104
"none": ModuleKind.None,
105105
"commonjs": ModuleKind.CommonJS,
106106
"amd": ModuleKind.AMD,
@@ -114,7 +114,7 @@ namespace ts {
114114
},
115115
{
116116
name: "newLine",
117-
type: createMap({
117+
type: createMapFromTemplate({
118118
"crlf": NewLineKind.CarriageReturnLineFeed,
119119
"lf": NewLineKind.LineFeed
120120
}),
@@ -263,7 +263,7 @@ namespace ts {
263263
{
264264
name: "target",
265265
shortName: "t",
266-
type: createMap({
266+
type: createMapFromTemplate({
267267
"es3": ScriptTarget.ES3,
268268
"es5": ScriptTarget.ES5,
269269
"es6": ScriptTarget.ES2015,
@@ -300,7 +300,7 @@ namespace ts {
300300
},
301301
{
302302
name: "moduleResolution",
303-
type: createMap({
303+
type: createMapFromTemplate({
304304
"node": ModuleResolutionKind.NodeJs,
305305
"classic": ModuleResolutionKind.Classic,
306306
}),
@@ -409,7 +409,7 @@ namespace ts {
409409
type: "list",
410410
element: {
411411
name: "lib",
412-
type: createMap({
412+
type: createMapFromTemplate({
413413
// JavaScript only
414414
"es5": "lib.es5.d.ts",
415415
"es6": "lib.es2015.d.ts",

src/compiler/core.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ namespace ts {
4040
}
4141

4242
/** Create a new map. If a template object is provided, the map will copy entries from it. */
43-
export function createMap<T>(template?: MapLike<T>): Map<T> {
43+
export function createMap<T>(): Map<T> {
44+
return new MapCtr<T>();
45+
}
46+
47+
//!!!
48+
export function createMapFromTemplate<T>(template?: MapLike<T>): Map<T> {
4449
const map: Map<T> = new MapCtr<T>();
4550

4651
// Copies keys/values from template. Note that for..in will not throw if

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace ts {
5656
tryScan<T>(callback: () => T): T;
5757
}
5858

59-
const textToToken = createMap({
59+
const textToToken = createMapFromTemplate({
6060
"abstract": SyntaxKind.AbstractKeyword,
6161
"any": SyntaxKind.AnyKeyword,
6262
"as": SyntaxKind.AsKeyword,

src/compiler/transformers/jsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ namespace ts {
286286
}
287287
}
288288

289-
const entities = createMap<number>({
289+
const entities = createMapFromTemplate<number>({
290290
"quot": 0x0022,
291291
"amp": 0x0026,
292292
"apos": 0x0027,

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2351,7 +2351,7 @@ namespace ts {
23512351
// the map below must be updated. Note that this regexp *does not* include the 'delete' character.
23522352
// There is no reason for this other than that JSON.stringify does not handle it either.
23532353
const escapedCharsRegExp = /[\\\"\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g;
2354-
const escapedCharsMap = createMap({
2354+
const escapedCharsMap = createMapFromTemplate({
23552355
"\0": "\\0",
23562356
"\t": "\\t",
23572357
"\v": "\\v",

src/harness/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace FourSlash {
9090

9191
export import IndentStyle = ts.IndentStyle;
9292

93-
const entityMap = ts.createMap({
93+
const entityMap = ts.createMapFromTemplate({
9494
"&": "&amp;",
9595
"\"": "&quot;",
9696
"'": "&#39;",

src/harness/unittests/cachingInServerLSHost.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace ts {
8686
content: `foo()`
8787
};
8888

89-
const serverHost = createDefaultServerHost(createMap({ [root.name]: root, [imported.name]: imported }));
89+
const serverHost = createDefaultServerHost(createMapFromTemplate({ [root.name]: root, [imported.name]: imported }));
9090
const { project, rootScriptInfo } = createProject(root.name, serverHost);
9191

9292
// ensure that imported file was found
@@ -170,7 +170,7 @@ namespace ts {
170170
content: `export var y = 1`
171171
};
172172

173-
const fileMap = createMap({ [root.name]: root });
173+
const fileMap = createMapFromTemplate({ [root.name]: root });
174174
const serverHost = createDefaultServerHost(fileMap);
175175
const originalFileExists = serverHost.fileExists;
176176

src/harness/unittests/configurationExtension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// <reference path="..\virtualFileSystem.ts" />
33

44
namespace ts {
5-
const testContents = createMap({
5+
const testContents = createMapFromTemplate({
66
"/dev/tsconfig.json": `{
77
"extends": "./configs/base",
88
"files": [

src/harness/unittests/moduleResolution.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ namespace ts {
331331
}
332332

333333
it("should find all modules", () => {
334-
const files = createMap({
334+
const files = createMapFromTemplate({
335335
"/a/b/c/first/shared.ts": `
336336
class A {}
337337
export = A`,
@@ -350,15 +350,15 @@ export = C;
350350
});
351351

352352
it("should find modules in node_modules", () => {
353-
const files = createMap({
353+
const files = createMapFromTemplate({
354354
"/parent/node_modules/mod/index.d.ts": "export var x",
355355
"/parent/app/myapp.ts": `import {x} from "mod"`
356356
});
357357
test(files, "/parent/app", ["myapp.ts"], 2, []);
358358
});
359359

360360
it("should find file referenced via absolute and relative names", () => {
361-
const files = createMap({
361+
const files = createMapFromTemplate({
362362
"/a/b/c.ts": `/// <reference path="b.ts"/>`,
363363
"/a/b/b.ts": "var x"
364364
});
@@ -409,39 +409,39 @@ export = C;
409409
}
410410

411411
it("should succeed when the same file is referenced using absolute and relative names", () => {
412-
const files = createMap({
412+
const files = createMapFromTemplate({
413413
"/a/b/c.ts": `/// <reference path="d.ts"/>`,
414414
"/a/b/d.ts": "var x"
415415
});
416416
test(files, { module: ts.ModuleKind.AMD }, "/a/b", /*useCaseSensitiveFileNames*/ false, ["c.ts", "/a/b/d.ts"], []);
417417
});
418418

419419
it("should fail when two files used in program differ only in casing (tripleslash references)", () => {
420-
const files = createMap({
420+
const files = createMapFromTemplate({
421421
"/a/b/c.ts": `/// <reference path="D.ts"/>`,
422422
"/a/b/d.ts": "var x"
423423
});
424424
test(files, { module: ts.ModuleKind.AMD, forceConsistentCasingInFileNames: true }, "/a/b", /*useCaseSensitiveFileNames*/ false, ["c.ts", "d.ts"], [1149]);
425425
});
426426

427427
it("should fail when two files used in program differ only in casing (imports)", () => {
428-
const files = createMap({
428+
const files = createMapFromTemplate({
429429
"/a/b/c.ts": `import {x} from "D"`,
430430
"/a/b/d.ts": "export var x"
431431
});
432432
test(files, { module: ts.ModuleKind.AMD, forceConsistentCasingInFileNames: true }, "/a/b", /*useCaseSensitiveFileNames*/ false, ["c.ts", "d.ts"], [1149]);
433433
});
434434

435435
it("should fail when two files used in program differ only in casing (imports, relative module names)", () => {
436-
const files = createMap({
436+
const files = createMapFromTemplate({
437437
"moduleA.ts": `import {x} from "./ModuleB"`,
438438
"moduleB.ts": "export var x"
439439
});
440440
test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "", /*useCaseSensitiveFileNames*/ false, ["moduleA.ts", "moduleB.ts"], [1149]);
441441
});
442442

443443
it("should fail when two files exist on disk that differs only in casing", () => {
444-
const files = createMap({
444+
const files = createMapFromTemplate({
445445
"/a/b/c.ts": `import {x} from "D"`,
446446
"/a/b/D.ts": "export var x",
447447
"/a/b/d.ts": "export var y"
@@ -450,7 +450,7 @@ export = C;
450450
});
451451

452452
it("should fail when module name in 'require' calls has inconsistent casing", () => {
453-
const files = createMap({
453+
const files = createMapFromTemplate({
454454
"moduleA.ts": `import a = require("./ModuleC")`,
455455
"moduleB.ts": `import a = require("./moduleC")`,
456456
"moduleC.ts": "export var x"
@@ -459,7 +459,7 @@ export = C;
459459
});
460460

461461
it("should fail when module names in 'require' calls has inconsistent casing and current directory has uppercase chars", () => {
462-
const files = createMap({
462+
const files = createMapFromTemplate({
463463
"/a/B/c/moduleA.ts": `import a = require("./ModuleC")`,
464464
"/a/B/c/moduleB.ts": `import a = require("./moduleC")`,
465465
"/a/B/c/moduleC.ts": "export var x",
@@ -471,7 +471,7 @@ import b = require("./moduleB");
471471
test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "/a/B/c", /*useCaseSensitiveFileNames*/ false, ["moduleD.ts"], [1149]);
472472
});
473473
it("should not fail when module names in 'require' calls has consistent casing and current directory has uppercase chars", () => {
474-
const files = createMap({
474+
const files = createMapFromTemplate({
475475
"/a/B/c/moduleA.ts": `import a = require("./moduleC")`,
476476
"/a/B/c/moduleB.ts": `import a = require("./moduleC")`,
477477
"/a/B/c/moduleC.ts": "export var x",

0 commit comments

Comments
 (0)