Skip to content

Commit b2664e7

Browse files
author
Andy Hanson
committed
Use a JSON.stringify replacer function instead of changing the value beforehand
1 parent 63b5790 commit b2664e7

35 files changed

+60
-151
lines changed

src/harness/fourslash.ts

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,46 +1960,24 @@ namespace FourSlash {
19601960
}
19611961

19621962
public verifyNavigationBar(json: any) {
1963-
let items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
1964-
items = this.simplifyNavigationBar(items);
1965-
if (JSON.stringify(items) !== JSON.stringify(json)) {
1966-
this.raiseError(`verifyNavigationBar failed - expected: ${JSON.stringify(json, undefined, 2)}, got: ${JSON.stringify(items, undefined, 2)}`);
1967-
}
1968-
}
1969-
1970-
// Remove any properties that tend to all have the same value so that test data is easier to read.
1971-
private simplifyNavigationBar(items: ts.NavigationBarItem[]): any {
1972-
return items.map(item => {
1973-
item = ts.clone(item);
1974-
if (item.kindModifiers === "") {
1975-
delete item.kindModifiers;
1976-
}
1977-
// We won't check this.
1978-
delete item.spans;
1979-
item.childItems = item.childItems.map(child => {
1980-
child = ts.clone(child);
1981-
delete child.spans;
1982-
ts.Debug.assert(child.childItems.length === 0);
1983-
delete child.childItems;
1984-
ts.Debug.assert(child.indent === 0);
1985-
delete child.indent;
1986-
ts.Debug.assert(child.bolded === false);
1987-
delete child.bolded;
1988-
ts.Debug.assert(child.grayed === false);
1989-
delete child.grayed;
1990-
if (child.kindModifiers === "") {
1991-
delete child.kindModifiers;
1992-
}
1993-
return child;
1994-
});
1995-
if (item.bolded === false) {
1996-
delete item.bolded;
1997-
}
1998-
if (item.grayed === false) {
1999-
delete item.grayed;
1963+
const items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
1964+
if (JSON.stringify(items, replacer) !== JSON.stringify(json)) {
1965+
this.raiseError(`verifyNavigationBar failed - expected: ${JSON.stringify(json, undefined, 2)}, got: ${JSON.stringify(items, replacer, 2)}`);
1966+
}
1967+
1968+
// Make the data easier to read.
1969+
function replacer(key: string, value: any) {
1970+
switch (key) {
1971+
case "spans":
1972+
// We won't ever check this.
1973+
return undefined;
1974+
case "childItems":
1975+
return value.length === 0 ? undefined : value;
1976+
default:
1977+
// Omit falsy values, those are presumed to be the default.
1978+
return value || undefined;
20001979
}
2001-
return item;
2002-
});
1980+
}
20031981
}
20041982

20051983
public printNavigationItems(searchValue: string) {

tests/cases/fourslash/deleteClassWithEnumPresent.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ verify.navigationBar([
1414
"text": "Foo",
1515
"kind": "enum"
1616
}
17-
],
18-
"indent": 0
17+
]
1918
},
2019
{
2120
"text": "Foo",

tests/cases/fourslash/getNavigationBarItems.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ verify.navigationBar([
1414
"text": "C",
1515
"kind": "class"
1616
}
17-
],
18-
"indent": 0
17+
]
1918
},
2019
{
2120
"text": "C",

tests/cases/fourslash/navbar_const.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ verify.navigationBar([
1111
"text": "c",
1212
"kind": "const"
1313
}
14-
],
15-
"indent": 0
14+
]
1615
}
1716
]);

tests/cases/fourslash/navbar_contains-no-duplicates.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ verify.navigationBar([
4545
"kind": "module",
4646
"kindModifiers": "declare"
4747
}
48-
],
49-
"indent": 0
48+
]
5049
},
5150
{
5251
"text": "ABC",

tests/cases/fourslash/navbar_exportDefault.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ goTo.file("a.ts");
1616
verify.navigationBar([
1717
{
1818
"text": "\"a\"",
19-
"kind": "module",
20-
"childItems": [],
21-
"indent": 0
19+
"kind": "module"
2220
},
2321
{
2422
"text": "default",
2523
"kind": "class",
2624
"kindModifiers": "export",
27-
"childItems": [],
2825
"indent": 1
2926
}
3027
]);
@@ -40,14 +37,12 @@ verify.navigationBar([
4037
"kind": "class",
4138
"kindModifiers": "export"
4239
}
43-
],
44-
"indent": 0
40+
]
4541
},
4642
{
4743
"text": "C",
4844
"kind": "class",
4945
"kindModifiers": "export",
50-
"childItems": [],
5146
"indent": 1
5247
}
5348
]);
@@ -57,14 +52,11 @@ verify.navigationBar([
5752
{
5853
"text": "\"c\"",
5954
"kind": "module",
60-
"childItems": [],
61-
"indent": 0
6255
},
6356
{
6457
"text": "default",
6558
"kind": "function",
6659
"kindModifiers": "export",
67-
"childItems": [],
6860
"indent": 1
6961
}
7062
]);
@@ -80,14 +72,12 @@ verify.navigationBar([
8072
"kind": "function",
8173
"kindModifiers": "export"
8274
}
83-
],
84-
"indent": 0
75+
]
8576
},
8677
{
8778
"text": "Func",
8879
"kind": "function",
8980
"kindModifiers": "export",
90-
"childItems": [],
9181
"indent": 1
9282
}
9383
]);

tests/cases/fourslash/navbar_let.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ verify.navigationBar([
1111
"text": "c",
1212
"kind": "let"
1313
}
14-
],
15-
"indent": 0
14+
]
1615
}
1716
]);

tests/cases/fourslash/navigationBarItemsBindingPatterns.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ verify.navigationBar([
5555
"text": "g",
5656
"kind": "var"
5757
}
58-
],
59-
"indent": 0
58+
]
6059
}
6160
]);

tests/cases/fourslash/navigationBarItemsBindingPatternsInConstructor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ verify.navigationBar([
2424
"text": "B",
2525
"kind": "class"
2626
}
27-
],
28-
"indent": 0
27+
]
2928
},
3029
{
3130
"text": "A",

tests/cases/fourslash/navigationBarItemsEmptyConstructors.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ verify.navigationBar([
1414
"text": "Test",
1515
"kind": "class"
1616
}
17-
],
18-
"indent": 0
17+
]
1918
},
2019
{
2120
"text": "Test",

tests/cases/fourslash/navigationBarItemsExports.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ verify.navigationBar([
2727
"kind": "alias",
2828
"kindModifiers": "export"
2929
}
30-
],
31-
"indent": 0
30+
]
3231
}
3332
]);

tests/cases/fourslash/navigationBarItemsFunctions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ verify.navigationBar([
2727
"text": "foo",
2828
"kind": "function"
2929
}
30-
],
31-
"indent": 0
30+
]
3231
},
3332
{
3433
"text": "baz",
3534
"kind": "function",
36-
"childItems": [],
3735
"indent": 1
3836
},
3937
{

tests/cases/fourslash/navigationBarItemsFunctionsBroken.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ verify.navigationBar([
1313
"text": "f",
1414
"kind": "function"
1515
}
16-
],
17-
"indent": 0
16+
]
1817
},
1918
{
2019
"text": "f",
2120
"kind": "function",
22-
"childItems": [],
2321
"indent": 1
2422
}
2523
]);

tests/cases/fourslash/navigationBarItemsFunctionsBroken2.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ verify.navigationBar([
1414
"text": "f",
1515
"kind": "function"
1616
}
17-
],
18-
"indent": 0
17+
]
1918
},
2019
{
2120
"text": "f",
2221
"kind": "function",
23-
"childItems": [],
2422
"indent": 1
2523
}
2624
]);

tests/cases/fourslash/navigationBarItemsImports.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ verify.navigationBar([
5151
"text": "ns",
5252
"kind": "alias"
5353
}
54-
],
55-
"indent": 0
54+
]
5655
}
5756
]);

tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ verify.navigationBar([
2727
"text": "Class",
2828
"kind": "class"
2929
}
30-
],
31-
"indent": 0
30+
]
3231
},
3332
{
3433
"text": "Class",
@@ -82,13 +81,11 @@ verify.navigationBar([
8281
{
8382
"text": "LocalFunctionInConstructor",
8483
"kind": "function",
85-
"childItems": [],
8684
"indent": 2
8785
},
8886
{
8987
"text": "LocalInterfaceInConstrcutor",
9088
"kind": "interface",
91-
"childItems": [],
9289
"indent": 2
9390
},
9491
{
@@ -135,7 +132,6 @@ verify.navigationBar([
135132
{
136133
"text": "LocalInterfaceInMethod",
137134
"kind": "interface",
138-
"childItems": [],
139135
"indent": 2
140136
}
141137
]);

tests/cases/fourslash/navigationBarItemsItems.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ verify.navigationBar([
6060
"text": "Shapes",
6161
"kind": "module"
6262
}
63-
],
64-
"indent": 0
63+
]
6564
},
6665
{
6766
"text": "IPoint",

tests/cases/fourslash/navigationBarItemsItems2.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,17 @@ verify.navigationBar([
1616
"text": "A",
1717
"kind": "module"
1818
}
19-
],
20-
"indent": 0
19+
]
2120
},
2221
{
2322
"text": "default",
2423
"kind": "class",
2524
"kindModifiers": "export",
26-
"childItems": [],
2725
"indent": 1
2826
},
2927
{
3028
"text": "A",
3129
"kind": "module",
32-
"childItems": [],
3330
"indent": 1
3431
}
3532
]);

tests/cases/fourslash/navigationBarItemsItemsContainsNoAnonymousFunctions.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ goTo.marker("file1");
3333
verify.navigationBar([
3434
{
3535
"text": "<global>",
36-
"kind": "module",
37-
"childItems": [],
38-
"indent": 0
36+
"kind": "module"
3937
}
4038
]);
4139

@@ -49,8 +47,7 @@ verify.navigationBar([
4947
"text": "x",
5048
"kind": "var"
5149
}
52-
],
53-
"indent": 0
50+
]
5451
}
5552
]);
5653

@@ -68,19 +65,16 @@ verify.navigationBar([
6865
"text": "foo",
6966
"kind": "function"
7067
}
71-
],
72-
"indent": 0
68+
]
7369
},
7470
{
7571
"text": "bar",
7672
"kind": "function",
77-
"childItems": [],
7873
"indent": 1
7974
},
8075
{
8176
"text": "foo",
8277
"kind": "function",
83-
"childItems": [],
8478
"indent": 1
8579
}
8680
]);

tests/cases/fourslash/navigationBarItemsItemsExternalModules.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ verify.navigationBar([
1414
"kind": "class",
1515
"kindModifiers": "export"
1616
}
17-
],
18-
"indent": 0
17+
]
1918
},
2019
{
2120
"text": "Bar",

tests/cases/fourslash/navigationBarItemsItemsExternalModules2.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ verify.navigationBar([
2121
"kind": "var",
2222
"kindModifiers": "export"
2323
}
24-
],
25-
"indent": 0
24+
]
2625
},
2726
{
2827
"text": "Bar",

0 commit comments

Comments
 (0)