Skip to content

Commit 95cfaaf

Browse files
author
Andy Hanson
committed
Use implicit boolean casts; it doesn't hurt performance
1 parent 166bc49 commit 95cfaaf

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

src/compiler/parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ namespace ts {
1717
}
1818

1919
function visitNode<T>(cbNode: (node: Node) => T, node: Node): T {
20-
if (node !== void 0) {
20+
if (node) {
2121
return cbNode(node);
2222
}
2323
}
2424

2525
function visitNodeArray<T>(cbNodes: (nodes: Node[]) => T, nodes: Node[]) {
26-
if (nodes !== void 0) {
26+
if (nodes) {
2727
return cbNodes(nodes);
2828
}
2929
}
3030

3131
function visitEachNode<T>(cbNode: (node: Node) => T, nodes: Node[]) {
32-
if (nodes !== void 0) {
32+
if (nodes) {
3333
for (const node of nodes) {
3434
const result = cbNode(node);
3535
if (result) {
@@ -44,7 +44,7 @@ namespace ts {
4444
// embedded arrays are flattened and the 'cbNode' callback is invoked for each element. If a callback returns
4545
// a truthy value, iteration stops and that value is returned. Otherwise, undefined is returned.
4646
export function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T {
47-
if (node === void 0) {
47+
if (!node) {
4848
return;
4949
}
5050
// The visitXXX functions could be written as local functions that close over the cbNode and cbNodeArray

src/services/navigationBar.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ namespace ts.NavigationBar {
1414
indent: number; // # of parents
1515
}
1616

17-
export function getNavigationBarItems(sourceFile_: SourceFile): NavigationBarItem[] {
18-
sourceFile = sourceFile_;
17+
export function getNavigationBarItems(sourceFile: SourceFile): NavigationBarItem[] {
18+
curSourceFile = sourceFile;
1919
const result = map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem);
20-
sourceFile = void 0;
20+
curSourceFile = undefined;
2121
return result;
2222
}
2323

2424
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
25-
let sourceFile: SourceFile;
25+
let curSourceFile: SourceFile;
2626
function nodeText(node: Node): string {
27-
return node.getText(sourceFile);
27+
return node.getText(curSourceFile);
2828
}
2929

3030
function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind {
3131
return n.node.kind;
3232
}
3333

3434
function pushChild(parent: NavigationBarNode, child: NavigationBarNode): void {
35-
if (parent.children !== void 0) {
35+
if (parent.children) {
3636
parent.children.push(child);
3737
}
3838
else {
@@ -50,13 +50,13 @@ namespace ts.NavigationBar {
5050

5151
function rootNavigationBarNode(sourceFile: SourceFile): NavigationBarNode {
5252
Debug.assert(!parentsStack.length);
53-
const root: NavigationBarNode = { node: sourceFile, additionalNodes: void 0, parent: void 0, children: void 0, indent: 0 };
53+
const root: NavigationBarNode = { node: sourceFile, additionalNodes: undefined, parent: undefined, children: undefined, indent: 0 };
5454
parent = root;
5555
for (const statement of sourceFile.statements) {
5656
addChildrenRecursively(statement);
5757
}
5858
endNode();
59-
Debug.assert(parent === void 0 && !parentsStack.length);
59+
Debug.assert(!parent && !parentsStack.length);
6060
return root;
6161
}
6262

@@ -67,9 +67,9 @@ namespace ts.NavigationBar {
6767
function emptyNavigationBarNode(node: Node): NavigationBarNode {
6868
return {
6969
node,
70-
additionalNodes: void 0,
70+
additionalNodes: undefined,
7171
parent,
72-
children: void 0,
72+
children: undefined,
7373
indent: parent.indent + 1
7474
};
7575
}
@@ -89,7 +89,7 @@ namespace ts.NavigationBar {
8989

9090
/** Call after calling `startNode` and adding children to it. */
9191
function endNode(): void {
92-
if (parent.children !== void 0) {
92+
if (parent.children) {
9393
mergeChildren(parent.children);
9494
sortChildren(parent.children);
9595
}
@@ -104,7 +104,7 @@ namespace ts.NavigationBar {
104104

105105
/** Look for navigation bar items in node's subtree, adding them to the current `parent`. */
106106
function addChildrenRecursively(node: Node): void {
107-
if (node === void 0 || isToken(node)) {
107+
if (!node || isToken(node)) {
108108
return;
109109
}
110110

@@ -142,15 +142,15 @@ namespace ts.NavigationBar {
142142
let importClause = <ImportClause>node;
143143
// Handle default import case e.g.:
144144
// import d from "mod";
145-
if (importClause.name !== void 0) {
145+
if (importClause.name) {
146146
addLeafNode(importClause);
147147
}
148148

149149
// Handle named bindings in imports e.g.:
150150
// import * as NS from "mod";
151151
// import {a, b as B} from "mod";
152152
const {namedBindings} = importClause;
153-
if (namedBindings !== void 0) {
153+
if (namedBindings) {
154154
if (namedBindings.kind === SyntaxKind.NamespaceImport) {
155155
addLeafNode(<NamespaceImport>namedBindings);
156156
}
@@ -169,7 +169,7 @@ namespace ts.NavigationBar {
169169
if (isBindingPattern(name)) {
170170
addChildrenRecursively(name);
171171
}
172-
else if (decl.initializer !== void 0 && isFunctionOrClassExpression(decl.initializer)) {
172+
else if (decl.initializer && isFunctionOrClassExpression(decl.initializer)) {
173173
// For `const x = function() {}`, just use the function node, not the const.
174174
addChildrenRecursively(decl.initializer);
175175
}
@@ -218,7 +218,7 @@ namespace ts.NavigationBar {
218218
break;
219219

220220
default:
221-
if (node.jsDocComments !== void 0) {
221+
if (node.jsDocComments) {
222222
for (const jsDocComment of node.jsDocComments) {
223223
for (const tag of jsDocComment.tags) {
224224
if (tag.kind === SyntaxKind.JSDocTypedefTag) {
@@ -238,13 +238,13 @@ namespace ts.NavigationBar {
238238
filterMutate(children, child => {
239239
const decl = <Declaration>child.node;
240240
const name = decl.name && nodeText(decl.name);
241-
if (name === void 0) {
241+
if (!name) {
242242
// Anonymous items are never merged.
243243
return true;
244244
}
245245

246246
const itemsWithSameName = getProperty(nameToItems, name);
247-
if (itemsWithSameName === void 0) {
247+
if (!itemsWithSameName) {
248248
nameToItems[name] = child;
249249
return true;
250250
}
@@ -297,12 +297,12 @@ namespace ts.NavigationBar {
297297
function merge(target: NavigationBarNode, source: NavigationBarNode): void {
298298
target.additionalNodes = target.additionalNodes || [];
299299
target.additionalNodes.push(source.node);
300-
if (source.additionalNodes !== void 0) {
300+
if (source.additionalNodes) {
301301
target.additionalNodes.push(...source.additionalNodes);
302302
}
303303

304304
target.children = concatenate(target.children, source.children);
305-
if (target.children !== void 0) {
305+
if (target.children) {
306306
mergeChildren(target.children);
307307
sortChildren(target.children);
308308
}
@@ -316,17 +316,17 @@ namespace ts.NavigationBar {
316316

317317
function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number {
318318
const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node);
319-
if (name1 !== void 0 && name2 !== void 0) {
319+
if (name1 && name2) {
320320
const cmp = localeCompareFix(name1, name2);
321321
return cmp !== 0 ? cmp : navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
322322
}
323323
else {
324-
return name1 !== void 0 ? 1 : name2 !== void 0 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
324+
return name1 ? 1 : name2 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
325325
}
326326
}
327327

328328
// More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times.
329-
const collator: { compare(a: string, b: string): number } = typeof Intl === "undefined" ? void 0 : new Intl.Collator();
329+
const collator: { compare(a: string, b: string): number } = typeof Intl === "undefined" ? undefined : new Intl.Collator();
330330
// Intl is missing in Safari, and node 0.10 treats "a" as greater than "B".
331331
const localeCompareIsCorrect = collator && collator.compare("a", "B") < 0;
332332
const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) {
@@ -358,7 +358,7 @@ namespace ts.NavigationBar {
358358
}
359359

360360
const decl = <Declaration>node;
361-
if (decl.name !== void 0) {
361+
if (decl.name) {
362362
return getPropertyNameForPropertyNameNode(decl.name);
363363
}
364364
switch (node.kind) {
@@ -369,7 +369,7 @@ namespace ts.NavigationBar {
369369
case SyntaxKind.JSDocTypedefTag:
370370
return getJSDocTypedefTagName(<JSDocTypedefTag>node);
371371
default:
372-
return void 0;
372+
return undefined;
373373
}
374374
}
375375

@@ -379,7 +379,7 @@ namespace ts.NavigationBar {
379379
}
380380

381381
const name = (<Declaration>node).name;
382-
if (name !== void 0) {
382+
if (name) {
383383
const text = nodeText(name);
384384
if (text.length > 0) {
385385
return text;
@@ -418,7 +418,7 @@ namespace ts.NavigationBar {
418418
}
419419

420420
function getJSDocTypedefTagName(node: JSDocTypedefTag): string {
421-
if (node.name !== void 0) {
421+
if (node.name) {
422422
return node.name.text;
423423
}
424424
else {
@@ -441,7 +441,7 @@ namespace ts.NavigationBar {
441441
function recur(item: NavigationBarNode) {
442442
if (isTopLevel(item)) {
443443
topLevel.push(item);
444-
if (item.children !== void 0) {
444+
if (item.children) {
445445
for (const child of item.children) {
446446
recur(child);
447447
}
@@ -478,7 +478,7 @@ namespace ts.NavigationBar {
478478
return false;
479479
}
480480
function isTopLevelFunctionDeclaration(item: NavigationBarNode): boolean {
481-
if ((<FunctionDeclaration>item.node).body === void 0) {
481+
if (!(<FunctionDeclaration>item.node).body) {
482482
return false;
483483
}
484484

@@ -531,7 +531,7 @@ namespace ts.NavigationBar {
531531

532532
function getSpans(n: NavigationBarNode): TextSpan[] {
533533
const spans = [getNodeSpan(n.node)];
534-
if (n.additionalNodes !== void 0) {
534+
if (n.additionalNodes) {
535535
for (const node of n.additionalNodes) {
536536
spans.push(getNodeSpan(node));
537537
}
@@ -562,7 +562,7 @@ namespace ts.NavigationBar {
562562
while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) {
563563
variableDeclarationNode = variableDeclarationNode.parent;
564564
}
565-
Debug.assert(variableDeclarationNode !== void 0);
565+
Debug.assert(!!variableDeclarationNode);
566566
}
567567
else {
568568
Debug.assert(!isBindingPattern((<VariableDeclaration>node).name));
@@ -620,17 +620,17 @@ namespace ts.NavigationBar {
620620
}
621621

622622
function isComputedProperty(member: EnumMember): boolean {
623-
return member.name === void 0 || member.name.kind === SyntaxKind.ComputedPropertyName;
623+
return !member.name || member.name.kind === SyntaxKind.ComputedPropertyName;
624624
}
625625

626626
function getNodeSpan(node: Node): TextSpan {
627627
return node.kind === SyntaxKind.SourceFile
628628
? createTextSpanFromBounds(node.getFullStart(), node.getEnd())
629-
: createTextSpanFromBounds(node.getStart(sourceFile), node.getEnd());
629+
: createTextSpanFromBounds(node.getStart(curSourceFile), node.getEnd());
630630
}
631631

632632
function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string {
633-
if (node.name !== void 0 && getFullWidth(node.name) > 0) {
633+
if (node.name && getFullWidth(node.name) > 0) {
634634
return declarationNameToString(node.name);
635635
}
636636
// See if it is a var initializer. If so, use the var name.

0 commit comments

Comments
 (0)