Use implicit boolean casts; it doesn't hurt performance

This commit is contained in:
Andy Hanson 2016-06-20 11:30:48 -07:00
parent 166bc49f0c
commit 95cfaafdee
2 changed files with 38 additions and 38 deletions

View file

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

View file

@ -14,17 +14,17 @@ namespace ts.NavigationBar {
indent: number; // # of parents indent: number; // # of parents
} }
export function getNavigationBarItems(sourceFile_: SourceFile): NavigationBarItem[] { export function getNavigationBarItems(sourceFile: SourceFile): NavigationBarItem[] {
sourceFile = sourceFile_; curSourceFile = sourceFile;
const result = map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem); const result = map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem);
sourceFile = void 0; curSourceFile = undefined;
return result; return result;
} }
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`. // Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
let sourceFile: SourceFile; let curSourceFile: SourceFile;
function nodeText(node: Node): string { function nodeText(node: Node): string {
return node.getText(sourceFile); return node.getText(curSourceFile);
} }
function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind { function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind {
@ -32,7 +32,7 @@ namespace ts.NavigationBar {
} }
function pushChild(parent: NavigationBarNode, child: NavigationBarNode): void { function pushChild(parent: NavigationBarNode, child: NavigationBarNode): void {
if (parent.children !== void 0) { if (parent.children) {
parent.children.push(child); parent.children.push(child);
} }
else { else {
@ -50,13 +50,13 @@ namespace ts.NavigationBar {
function rootNavigationBarNode(sourceFile: SourceFile): NavigationBarNode { function rootNavigationBarNode(sourceFile: SourceFile): NavigationBarNode {
Debug.assert(!parentsStack.length); Debug.assert(!parentsStack.length);
const root: NavigationBarNode = { node: sourceFile, additionalNodes: void 0, parent: void 0, children: void 0, indent: 0 }; const root: NavigationBarNode = { node: sourceFile, additionalNodes: undefined, parent: undefined, children: undefined, indent: 0 };
parent = root; parent = root;
for (const statement of sourceFile.statements) { for (const statement of sourceFile.statements) {
addChildrenRecursively(statement); addChildrenRecursively(statement);
} }
endNode(); endNode();
Debug.assert(parent === void 0 && !parentsStack.length); Debug.assert(!parent && !parentsStack.length);
return root; return root;
} }
@ -67,9 +67,9 @@ namespace ts.NavigationBar {
function emptyNavigationBarNode(node: Node): NavigationBarNode { function emptyNavigationBarNode(node: Node): NavigationBarNode {
return { return {
node, node,
additionalNodes: void 0, additionalNodes: undefined,
parent, parent,
children: void 0, children: undefined,
indent: parent.indent + 1 indent: parent.indent + 1
}; };
} }
@ -89,7 +89,7 @@ namespace ts.NavigationBar {
/** Call after calling `startNode` and adding children to it. */ /** Call after calling `startNode` and adding children to it. */
function endNode(): void { function endNode(): void {
if (parent.children !== void 0) { if (parent.children) {
mergeChildren(parent.children); mergeChildren(parent.children);
sortChildren(parent.children); sortChildren(parent.children);
} }
@ -104,7 +104,7 @@ namespace ts.NavigationBar {
/** Look for navigation bar items in node's subtree, adding them to the current `parent`. */ /** Look for navigation bar items in node's subtree, adding them to the current `parent`. */
function addChildrenRecursively(node: Node): void { function addChildrenRecursively(node: Node): void {
if (node === void 0 || isToken(node)) { if (!node || isToken(node)) {
return; return;
} }
@ -142,7 +142,7 @@ namespace ts.NavigationBar {
let importClause = <ImportClause>node; let importClause = <ImportClause>node;
// Handle default import case e.g.: // Handle default import case e.g.:
// import d from "mod"; // import d from "mod";
if (importClause.name !== void 0) { if (importClause.name) {
addLeafNode(importClause); addLeafNode(importClause);
} }
@ -150,7 +150,7 @@ namespace ts.NavigationBar {
// import * as NS from "mod"; // import * as NS from "mod";
// import {a, b as B} from "mod"; // import {a, b as B} from "mod";
const {namedBindings} = importClause; const {namedBindings} = importClause;
if (namedBindings !== void 0) { if (namedBindings) {
if (namedBindings.kind === SyntaxKind.NamespaceImport) { if (namedBindings.kind === SyntaxKind.NamespaceImport) {
addLeafNode(<NamespaceImport>namedBindings); addLeafNode(<NamespaceImport>namedBindings);
} }
@ -169,7 +169,7 @@ namespace ts.NavigationBar {
if (isBindingPattern(name)) { if (isBindingPattern(name)) {
addChildrenRecursively(name); addChildrenRecursively(name);
} }
else if (decl.initializer !== void 0 && isFunctionOrClassExpression(decl.initializer)) { else if (decl.initializer && isFunctionOrClassExpression(decl.initializer)) {
// For `const x = function() {}`, just use the function node, not the const. // For `const x = function() {}`, just use the function node, not the const.
addChildrenRecursively(decl.initializer); addChildrenRecursively(decl.initializer);
} }
@ -218,7 +218,7 @@ namespace ts.NavigationBar {
break; break;
default: default:
if (node.jsDocComments !== void 0) { if (node.jsDocComments) {
for (const jsDocComment of node.jsDocComments) { for (const jsDocComment of node.jsDocComments) {
for (const tag of jsDocComment.tags) { for (const tag of jsDocComment.tags) {
if (tag.kind === SyntaxKind.JSDocTypedefTag) { if (tag.kind === SyntaxKind.JSDocTypedefTag) {
@ -238,13 +238,13 @@ namespace ts.NavigationBar {
filterMutate(children, child => { filterMutate(children, child => {
const decl = <Declaration>child.node; const decl = <Declaration>child.node;
const name = decl.name && nodeText(decl.name); const name = decl.name && nodeText(decl.name);
if (name === void 0) { if (!name) {
// Anonymous items are never merged. // Anonymous items are never merged.
return true; return true;
} }
const itemsWithSameName = getProperty(nameToItems, name); const itemsWithSameName = getProperty(nameToItems, name);
if (itemsWithSameName === void 0) { if (!itemsWithSameName) {
nameToItems[name] = child; nameToItems[name] = child;
return true; return true;
} }
@ -297,12 +297,12 @@ namespace ts.NavigationBar {
function merge(target: NavigationBarNode, source: NavigationBarNode): void { function merge(target: NavigationBarNode, source: NavigationBarNode): void {
target.additionalNodes = target.additionalNodes || []; target.additionalNodes = target.additionalNodes || [];
target.additionalNodes.push(source.node); target.additionalNodes.push(source.node);
if (source.additionalNodes !== void 0) { if (source.additionalNodes) {
target.additionalNodes.push(...source.additionalNodes); target.additionalNodes.push(...source.additionalNodes);
} }
target.children = concatenate(target.children, source.children); target.children = concatenate(target.children, source.children);
if (target.children !== void 0) { if (target.children) {
mergeChildren(target.children); mergeChildren(target.children);
sortChildren(target.children); sortChildren(target.children);
} }
@ -316,17 +316,17 @@ namespace ts.NavigationBar {
function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number { function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number {
const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node); const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node);
if (name1 !== void 0 && name2 !== void 0) { if (name1 && name2) {
const cmp = localeCompareFix(name1, name2); const cmp = localeCompareFix(name1, name2);
return cmp !== 0 ? cmp : navigationBarNodeKind(child1) - navigationBarNodeKind(child2); return cmp !== 0 ? cmp : navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
} }
else { else {
return name1 !== void 0 ? 1 : name2 !== void 0 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2); return name1 ? 1 : name2 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
} }
} }
// More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times. // More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times.
const collator: { compare(a: string, b: string): number } = typeof Intl === "undefined" ? void 0 : new Intl.Collator(); const collator: { compare(a: string, b: string): number } = typeof Intl === "undefined" ? undefined : new Intl.Collator();
// Intl is missing in Safari, and node 0.10 treats "a" as greater than "B". // Intl is missing in Safari, and node 0.10 treats "a" as greater than "B".
const localeCompareIsCorrect = collator && collator.compare("a", "B") < 0; const localeCompareIsCorrect = collator && collator.compare("a", "B") < 0;
const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) { const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) {
@ -358,7 +358,7 @@ namespace ts.NavigationBar {
} }
const decl = <Declaration>node; const decl = <Declaration>node;
if (decl.name !== void 0) { if (decl.name) {
return getPropertyNameForPropertyNameNode(decl.name); return getPropertyNameForPropertyNameNode(decl.name);
} }
switch (node.kind) { switch (node.kind) {
@ -369,7 +369,7 @@ namespace ts.NavigationBar {
case SyntaxKind.JSDocTypedefTag: case SyntaxKind.JSDocTypedefTag:
return getJSDocTypedefTagName(<JSDocTypedefTag>node); return getJSDocTypedefTagName(<JSDocTypedefTag>node);
default: default:
return void 0; return undefined;
} }
} }
@ -379,7 +379,7 @@ namespace ts.NavigationBar {
} }
const name = (<Declaration>node).name; const name = (<Declaration>node).name;
if (name !== void 0) { if (name) {
const text = nodeText(name); const text = nodeText(name);
if (text.length > 0) { if (text.length > 0) {
return text; return text;
@ -418,7 +418,7 @@ namespace ts.NavigationBar {
} }
function getJSDocTypedefTagName(node: JSDocTypedefTag): string { function getJSDocTypedefTagName(node: JSDocTypedefTag): string {
if (node.name !== void 0) { if (node.name) {
return node.name.text; return node.name.text;
} }
else { else {
@ -441,7 +441,7 @@ namespace ts.NavigationBar {
function recur(item: NavigationBarNode) { function recur(item: NavigationBarNode) {
if (isTopLevel(item)) { if (isTopLevel(item)) {
topLevel.push(item); topLevel.push(item);
if (item.children !== void 0) { if (item.children) {
for (const child of item.children) { for (const child of item.children) {
recur(child); recur(child);
} }
@ -478,7 +478,7 @@ namespace ts.NavigationBar {
return false; return false;
} }
function isTopLevelFunctionDeclaration(item: NavigationBarNode): boolean { function isTopLevelFunctionDeclaration(item: NavigationBarNode): boolean {
if ((<FunctionDeclaration>item.node).body === void 0) { if (!(<FunctionDeclaration>item.node).body) {
return false; return false;
} }
@ -531,7 +531,7 @@ namespace ts.NavigationBar {
function getSpans(n: NavigationBarNode): TextSpan[] { function getSpans(n: NavigationBarNode): TextSpan[] {
const spans = [getNodeSpan(n.node)]; const spans = [getNodeSpan(n.node)];
if (n.additionalNodes !== void 0) { if (n.additionalNodes) {
for (const node of n.additionalNodes) { for (const node of n.additionalNodes) {
spans.push(getNodeSpan(node)); spans.push(getNodeSpan(node));
} }
@ -562,7 +562,7 @@ namespace ts.NavigationBar {
while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) { while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) {
variableDeclarationNode = variableDeclarationNode.parent; variableDeclarationNode = variableDeclarationNode.parent;
} }
Debug.assert(variableDeclarationNode !== void 0); Debug.assert(!!variableDeclarationNode);
} }
else { else {
Debug.assert(!isBindingPattern((<VariableDeclaration>node).name)); Debug.assert(!isBindingPattern((<VariableDeclaration>node).name));
@ -620,17 +620,17 @@ namespace ts.NavigationBar {
} }
function isComputedProperty(member: EnumMember): boolean { function isComputedProperty(member: EnumMember): boolean {
return member.name === void 0 || member.name.kind === SyntaxKind.ComputedPropertyName; return !member.name || member.name.kind === SyntaxKind.ComputedPropertyName;
} }
function getNodeSpan(node: Node): TextSpan { function getNodeSpan(node: Node): TextSpan {
return node.kind === SyntaxKind.SourceFile return node.kind === SyntaxKind.SourceFile
? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) ? createTextSpanFromBounds(node.getFullStart(), node.getEnd())
: createTextSpanFromBounds(node.getStart(sourceFile), node.getEnd()); : createTextSpanFromBounds(node.getStart(curSourceFile), node.getEnd());
} }
function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string { function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string {
if (node.name !== void 0 && getFullWidth(node.name) > 0) { if (node.name && getFullWidth(node.name) > 0) {
return declarationNameToString(node.name); return declarationNameToString(node.name);
} }
// See if it is a var initializer. If so, use the var name. // See if it is a var initializer. If so, use the var name.