From 0a8744e841a021c4b982d58d5e3947be8b078610 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 16 Dec 2014 03:19:13 -0800 Subject: [PATCH] Add helper method to reduce so many double negatives in the code. --- src/compiler/checker.ts | 32 ++++++++++++++++---------------- src/compiler/emitter.ts | 4 ++-- src/compiler/parser.ts | 8 ++++---- src/compiler/utilities.ts | 12 ++++++++---- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index de75ae4c244..c4c9af07873 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -649,7 +649,7 @@ module ts { var members = node.members; for (var i = 0; i < members.length; i++) { var member = members[i]; - if (member.kind === SyntaxKind.Constructor && !isMissingNode((member).body)) { + if (member.kind === SyntaxKind.Constructor && nodeIsPresent((member).body)) { return member; } } @@ -2603,7 +2603,7 @@ module ts { returnType = getAnnotatedAccessorType(setter); } - if (!returnType && isMissingNode((declaration).body)) { + if (!returnType && nodeIsMissing((declaration).body)) { returnType = anyType; } } @@ -6354,7 +6354,7 @@ module ts { } // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check. - if (isMissingNode(func.body) || func.body.kind !== SyntaxKind.Block) { + if (nodeIsMissing(func.body) || func.body.kind !== SyntaxKind.Block) { return; } @@ -7012,7 +7012,7 @@ module ts { var func = getContainingFunction(node); if (node.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)) { func = getContainingFunction(node); - if (!(func.kind === SyntaxKind.Constructor && !isMissingNode(func.body))) { + if (!(func.kind === SyntaxKind.Constructor && nodeIsPresent(func.body))) { error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); } } @@ -7109,7 +7109,7 @@ module ts { } // exit early in the case of signature - super checks are not relevant to them - if (isMissingNode(node.body)) { + if (nodeIsMissing(node.body)) { return; } @@ -7183,7 +7183,7 @@ module ts { function checkAccessorDeclaration(node: AccessorDeclaration) { if (fullTypeCheck) { if (node.kind === SyntaxKind.GetAccessor) { - if (!isInAmbientContext(node) && !isMissingNode(node.body) && !(bodyContainsAReturnStatement(node.body) || bodyContainsSingleThrowStatement(node.body))) { + if (!isInAmbientContext(node) && nodeIsPresent(node.body) && !(bodyContainsAReturnStatement(node.body) || bodyContainsSingleThrowStatement(node.body))) { error(node.name, Diagnostics.A_get_accessor_must_return_a_value_or_consist_of_a_single_throw_statement); } } @@ -7272,7 +7272,7 @@ module ts { // TypeScript 1.0 spec (April 2014): 3.7.2.2 // Specialized signatures are not permitted in conjunction with a function body - if (!isMissingNode((signatureDeclarationNode).body)) { + if (nodeIsPresent((signatureDeclarationNode).body)) { error(signatureDeclarationNode, Diagnostics.A_signature_with_an_implementation_cannot_use_a_string_literal_type); return; } @@ -7405,7 +7405,7 @@ module ts { error(errorNode, diagnostic); return; } - else if (!isMissingNode((subsequentNode).body)) { + else if (nodeIsPresent((subsequentNode).body)) { error(errorNode, Diagnostics.Function_implementation_name_must_be_0, declarationNameToString(node.name)); return; } @@ -7447,7 +7447,7 @@ module ts { someHaveQuestionToken = someHaveQuestionToken || hasQuestionToken(node); allHaveQuestionToken = allHaveQuestionToken && hasQuestionToken(node); - if (!isMissingNode(node.body) && bodyDeclaration) { + if (nodeIsPresent(node.body) && bodyDeclaration) { if (isConstructor) { multipleConstructorImplementation = true; } @@ -7459,7 +7459,7 @@ module ts { reportImplementationExpectedError(previousDeclaration); } - if (!isMissingNode(node.body)) { + if (nodeIsPresent(node.body)) { if (!bodyDeclaration) { bodyDeclaration = node; } @@ -7642,7 +7642,7 @@ module ts { // Report an implicit any error if there is no body, no explicit return type, and node is not a private method // in an ambient context - if (compilerOptions.noImplicitAny && isMissingNode(node.body) && !node.type && !isPrivateWithinAmbient(node)) { + if (compilerOptions.noImplicitAny && nodeIsMissing(node.body) && !node.type && !isPrivateWithinAmbient(node)) { reportImplicitAnyError(node, anyType); } } @@ -7656,7 +7656,7 @@ module ts { function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) { // no rest parameters \ declaration context \ overload - no codegen impact - if (!hasRestParameters(node) || isInAmbientContext(node) || isMissingNode((node).body)) { + if (!hasRestParameters(node) || isInAmbientContext(node) || nodeIsMissing((node).body)) { return; } @@ -7688,7 +7688,7 @@ module ts { } var root = getRootDeclaration(node); - if (root.kind === SyntaxKind.Parameter && isMissingNode((root.parent).body)) { + if (root.kind === SyntaxKind.Parameter && nodeIsMissing((root.parent).body)) { // just an overload - no codegen impact return false; } @@ -7852,7 +7852,7 @@ module ts { forEach((node.name).elements, checkSourceElement); } // For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body - if (node.initializer && getRootDeclaration(node).kind === SyntaxKind.Parameter && isMissingNode(getContainingFunction(node).body)) { + if (node.initializer && getRootDeclaration(node).kind === SyntaxKind.Parameter && nodeIsMissing(getContainingFunction(node).body)) { error(node, Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation); return; } @@ -8606,7 +8606,7 @@ module ts { var declarations = symbol.declarations; for (var i = 0; i < declarations.length; i++) { var declaration = declarations[i]; - if ((declaration.kind === SyntaxKind.ClassDeclaration || (declaration.kind === SyntaxKind.FunctionDeclaration && !isMissingNode((declaration).body))) && !isInAmbientContext(declaration)) { + if ((declaration.kind === SyntaxKind.ClassDeclaration || (declaration.kind === SyntaxKind.FunctionDeclaration && nodeIsPresent((declaration).body))) && !isInAmbientContext(declaration)) { return declaration; } } @@ -9499,7 +9499,7 @@ module ts { } function isImplementationOfOverload(node: FunctionLikeDeclaration) { - if (!isMissingNode(node.body)) { + if (nodeIsPresent(node.body)) { var symbol = getSymbolOfNode(node); var signaturesOfSymbol = getSignaturesOfSymbol(symbol); // If this function body corresponds to function with multiple signature, it is implementation of overload diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 5e3290120b6..6db57460378 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -268,7 +268,7 @@ module ts { function getFirstConstructorWithBody(node: ClassDeclaration): ConstructorDeclaration { return forEach(node.members, member => { - if (member.kind === SyntaxKind.Constructor && !isMissingNode((member).body)) { + if (member.kind === SyntaxKind.Constructor && nodeIsPresent((member).body)) { return member; } }); @@ -3108,7 +3108,7 @@ module ts { } function emitFunctionDeclaration(node: FunctionLikeDeclaration) { - if (isMissingNode(node.body)) { + if (nodeIsMissing(node.body)) { return emitPinnedOrTripleSlashComments(node); } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 4bd8826e4c6..42a03873ff9 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -974,7 +974,7 @@ module ts { function getLastChildWorker(node: Node): Node { var last:Node = undefined; forEachChild(node, child => { - if (!isMissingNode(child)) { + if (nodeIsPresent(child)) { last = child; } }); @@ -982,7 +982,7 @@ module ts { } function visit(child: Node) { - if (isMissingNode(child)) { + if (nodeIsMissing(child)) { // Missing nodes are effectively invisible to us. We never even consider them // When trying to find the nearest node before us. return; @@ -1675,7 +1675,7 @@ module ts { var node = syntaxCursor.currentNode(scanner.getStartPos()); // Can't reuse a missing node. - if (isMissingNode(node)) { + if (nodeIsMissing(node)) { return undefined; } @@ -5493,7 +5493,7 @@ module ts { if (inAmbientContext) { return checkForDisallowedComputedProperty(node.name, Diagnostics.Computed_property_names_are_not_allowed_in_an_ambient_context); } - else if (isMissingNode(node.body)) { + else if (nodeIsMissing(node.body)) { return checkForDisallowedComputedProperty(node.name, Diagnostics.Computed_property_names_are_not_allowed_in_method_overloads); } } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6137d6e1955..2db5d9e3b7d 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -110,7 +110,7 @@ module ts { return node.pos; } - export function isMissingNode(node: Node) { + export function nodeIsMissing(node: Node) { if (!node) { return true; } @@ -118,10 +118,14 @@ module ts { return node.pos === node.end && node.kind !== SyntaxKind.EndOfFileToken; } + export function nodeIsPresent(node: Node) { + return !nodeIsMissing(node); + } + export function getTokenPosOfNode(node: Node, sourceFile?: SourceFile): number { // With nodes that have no width (i.e. 'Missing' nodes), we actually *don't* // want to skip trivia because this will launch us forward to the next token. - if (isMissingNode(node)) { + if (nodeIsMissing(node)) { return node.pos; } @@ -129,7 +133,7 @@ module ts { } export function getSourceTextOfNodeFromSourceFile(sourceFile: SourceFile, node: Node): string { - if (isMissingNode(node)) { + if (nodeIsMissing(node)) { return ""; } @@ -138,7 +142,7 @@ module ts { } export function getTextOfNodeFromSourceText(sourceText: string, node: Node): string { - if (isMissingNode(node)) { + if (nodeIsMissing(node)) { return ""; }