Skip to content

Commit 54663cf

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

35 files changed

+59
-150
lines changed

src/harness/fourslash.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,45 +1961,23 @@ namespace FourSlash {
19611961

19621962
public verifyNavigationBar(json: any) {
19631963
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;
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",

0 commit comments

Comments
 (0)