Skip to content

Commit

Permalink
Switched all non-test code to use assert and fail methods from common…
Browse files Browse the repository at this point in the history
…/debug rather than the node assert module.
  • Loading branch information
msfterictraut committed Feb 15, 2020
1 parent 46e792c commit 094bd03
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 57 deletions.
5 changes: 2 additions & 3 deletions server/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
* This binder doesn't perform any static type checking.
*/

import * as assert from 'assert';

import { Commands } from '../commands/commands';
import { DiagnosticLevel } from '../common/configOptions';
import { assert, fail } from '../common/debug';
import { CreateTypeStubFileAction } from '../common/diagnostic';
import { getEmptyRange } from '../common/textRange';
import { DiagnosticRule } from '../common/diagnosticRules';
Expand Down Expand Up @@ -183,7 +182,7 @@ export class Binder extends ParseTreeWalker {
visitModule(node: ModuleNode): boolean {
// Tree walking should start with the children of
// the node, so we should never get here.
assert.fail('We should never get here');
fail('We should never get here');
return false;
}

Expand Down
3 changes: 1 addition & 2 deletions server/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
* cannot (or should not be) performed lazily.
*/

import * as assert from 'assert';

import { DiagnosticLevel } from '../common/configOptions';
import { assert } from '../common/debug';
import { Diagnostic, DiagnosticAddendum } from '../common/diagnostic';
import { DiagnosticRule } from '../common/diagnosticRules';
import { TextRange } from '../common/textRange';
Expand Down
3 changes: 1 addition & 2 deletions server/src/analyzer/codeFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
* TypeScript compiler.
*/

import * as assert from 'assert';

import { assert } from '../common/debug';
import { CallNode, ExpressionNode, ImportFromNode, MemberAccessNode, NameNode,
ParseNodeType } from '../parser/parseNodes';

Expand Down
7 changes: 3 additions & 4 deletions server/src/analyzer/parseTreeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* Utility routines for traversing a parse tree.
*/

import * as assert from 'assert';

import { fail } from '../common/debug';
import { Position } from '../common/textRange';
import { convertPositionToOffset } from '../common/positionUtils';
import { TextRange } from '../common/textRange';
Expand Down Expand Up @@ -420,7 +419,7 @@ export function getEnclosingModule(node: ParseNode): ModuleNode {
curNode = curNode.parent;
}

assert.fail('Module node not found');
fail('Module node not found');
return undefined!;
}

Expand Down Expand Up @@ -536,7 +535,7 @@ export function getEvaluationScopeNode(node: ParseNode): EvaluationScopeNode {
curNode = curNode.parent;
}

assert.fail('Did not find evaluation scope');
fail('Did not find evaluation scope');
return undefined!;
}

Expand Down
5 changes: 2 additions & 3 deletions server/src/analyzer/parseTreeWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* Class that traverses a parse tree.
*/

import * as assert from 'assert';

import { fail } from '../common/debug';
import { ArgumentNode, AssertNode, AssignmentExpressionNode, AssignmentNode,
AugmentedAssignmentNode, AwaitNode, BinaryOperationNode, BreakNode, CallNode,
ClassNode, ConstantNode, ContinueNode, DecoratorNode, DelNode,
Expand Down Expand Up @@ -428,7 +427,7 @@ export class ParseTreeWalker {
break;

default:
assert.fail('Unexpected node type');
fail('Unexpected node type');
break;
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/analyzer/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* and all of their recursive imports.
*/

import * as assert from 'assert';
import { CompletionItem, CompletionList, DocumentSymbol, SymbolInformation } from 'vscode-languageserver';

import { ConfigOptions } from '../common/configOptions';
import { ConsoleInterface, StandardConsole } from '../common/console';
import { assert } from '../common/debug';
import { Diagnostic } from '../common/diagnostic';
import { FileDiagnostics } from '../common/diagnosticSink';
import { FileEditAction, TextEditAction } from '../common/editAction';
Expand Down
5 changes: 2 additions & 3 deletions server/src/analyzer/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
* top-most built-in scope).
*/

import * as assert from 'assert';

import { fail } from '../common/debug';
import { Symbol, SymbolFlags, SymbolTable } from './symbol';

export const enum ScopeType {
Expand Down Expand Up @@ -79,7 +78,7 @@ export class Scope {
curScope = curScope.parent;
}

assert.fail('failed to find scope');
fail('failed to find scope');
return this;
}

Expand Down
9 changes: 5 additions & 4 deletions server/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
* A persistent service that is able to analyze a collection of
* Python files.
*/
import * as assert from 'assert';

import { CompletionItem, CompletionList, DocumentSymbol, SymbolInformation } from 'vscode-languageserver';

import { CommandLineOptions } from '../common/commandLineOptions';
import { ConfigOptions } from '../common/configOptions';
import { ConsoleInterface, StandardConsole } from '../common/console';
import { assert } from '../common/debug';
import { Diagnostic } from '../common/diagnostic';
import { FileDiagnostics } from '../common/diagnosticSink';
import { FileEditAction, TextEditAction } from '../common/editAction';
Expand All @@ -28,7 +29,7 @@ import { HoverResults } from '../languageService/hoverProvider';
import { SignatureHelpResults } from '../languageService/signatureHelpProvider';
import { ImportedModuleDescriptor, ImportResolver, ImportResolverFactory } from './importResolver';
import { MaxAnalysisTime, Program } from './program';
import * as PythonPathUtils from './pythonPathUtils';
import { findPythonSearchPaths, getPythonPathFromPythonInterpreter } from './pythonPathUtils';

export { MaxAnalysisTime } from './program';

Expand Down Expand Up @@ -367,7 +368,7 @@ export class AnalyzerService {
`in venv path ${ configOptions.venvPath }.`);
} else {
const importFailureInfo: string[] = [];
if (PythonPathUtils.findPythonSearchPaths(this._fs, configOptions, undefined, importFailureInfo) === undefined) {
if (findPythonSearchPaths(this._fs, configOptions, undefined, importFailureInfo) === undefined) {
this._console.log(
`site-packages directory cannot be located for venvPath ` +
`${ configOptions.venvPath } and venv ${ configOptions.defaultVenv }.`);
Expand All @@ -382,7 +383,7 @@ export class AnalyzerService {
}
} else {
const importFailureInfo: string[] = [];
const pythonPaths = PythonPathUtils.getPythonPathFromPythonInterpreter(this._fs, configOptions.pythonPath, importFailureInfo);
const pythonPaths = getPythonPathFromPythonInterpreter(this._fs, configOptions.pythonPath, importFailureInfo);
if (pythonPaths.length === 0) {
if (configOptions.verboseOutput) {
this._console.log(
Expand Down
8 changes: 4 additions & 4 deletions server/src/analyzer/sourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* Class that represents a single python source file.
*/

import * as assert from 'assert';
import { CompletionItem, CompletionList, DocumentSymbol, SymbolInformation } from 'vscode-languageserver';

import {
Expand All @@ -16,13 +15,15 @@ import {
} from '../common/configOptions';
import { ConsoleInterface, StandardConsole } from '../common/console';
import { Diagnostic, DiagnosticCategory } from '../common/diagnostic';
import { assert } from '../common/debug';
import { DiagnosticSink, TextRangeDiagnosticSink } from '../common/diagnosticSink';
import { TextEditAction } from '../common/editAction';
import { getFileName, normalizeSlashes } from '../common/pathUtils';
import * as StringUtils from '../common/stringUtils';
import { TextRange, getEmptyRange, Position, DocumentRange } from '../common/textRange';
import { TextRangeCollection } from '../common/textRangeCollection';
import { timingStats } from '../common/timing';
import { VirtualFileSystem } from '../common/vfs';
import { CompletionItemData, CompletionProvider, ModuleSymbolMap } from '../languageService/completionProvider';
import { DefinitionProvider } from '../languageService/definitionProvider';
import { DocumentSymbolProvider } from '../languageService/documentSymbolProvider';
Expand All @@ -46,7 +47,6 @@ import { Scope } from './scope';
import { SymbolTable } from './symbol';
import { TestWalker } from './testWalker';
import { TypeEvaluator } from './typeEvaluator';
import { VirtualFileSystem } from '../common/vfs';

const _maxImportCyclesPerFile = 4;

Expand Down Expand Up @@ -674,7 +674,7 @@ export class SourceFile {
assert(!this.isParseRequired());
assert(this.isBindingRequired());
assert(!this._isBindingInProgress);
assert(this._parseResults);
assert(this._parseResults !== undefined);

try {
// Perform name binding.
Expand Down Expand Up @@ -727,7 +727,7 @@ export class SourceFile {
assert(!this.isBindingRequired());
assert(!this._isBindingInProgress);
assert(this.isCheckingRequired());
assert(this._parseResults);
assert(this._parseResults !== undefined);

try {
timingStats.typeCheckerTime.timeOperation(() => {
Expand Down
9 changes: 4 additions & 5 deletions server/src/analyzer/testWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* Walks a parse tree to validate internal consistency and completeness.
*/

import * as assert from 'assert';

import { ParseTreeWalker } from '../analyzer/parseTreeWalker';
import { fail } from '../common/debug';
import { TextRange } from '../common/textRange';
import { ParseNode, ParseNodeArray, ParseNodeType } from '../parser/parseNodes';

Expand All @@ -28,7 +27,7 @@ export class TestWalker extends ParseTreeWalker {
children.forEach(child => {
if (child) {
if (child.parent !== node) {
assert.fail(`Child node ${ child.nodeType } does not ` +
fail(`Child node ${ child.nodeType } does not ` +
`contain a reference to its parent ${ node.nodeType }`);
}
}
Expand Down Expand Up @@ -63,13 +62,13 @@ export class TestWalker extends ParseTreeWalker {
if (!skipCheck) {
// Make sure the child is contained within the parent.
if (child.start < node.start || TextRange.getEnd(child) > TextRange.getEnd(node)) {
assert.fail(`Child node ${ child.nodeType } is not ` +
fail(`Child node ${ child.nodeType } is not ` +
`contained within its parent ${ node.nodeType }`);
}
if (prevNode) {
// Make sure the child is after the previous child.
if (child.start < TextRange.getEnd(prevNode)) {
assert.fail(`Child node is not after previous child node`);
fail(`Child node is not after previous child node`);
}
}

Expand Down
21 changes: 10 additions & 11 deletions server/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
* taken by the TypeScript compiler.
*/

import * as assert from 'assert';

import { Commands } from '../commands/commands';
import { DiagnosticLevel } from '../common/configOptions';
import { assert, fail } from '../common/debug';
import { AddMissingOptionalToParamAction, Diagnostic, DiagnosticAddendum } from '../common/diagnostic';
import { getEmptyRange } from '../common/textRange';
import { DiagnosticRule } from '../common/diagnosticRules';
Expand Down Expand Up @@ -721,7 +720,7 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {

if (!typeResult) {
// We shouldn't get here. If we do, report an error.
assert.fail(`Unhandled expression type '${ ParseTreeUtils.printExpression(node) }'`);
fail(`Unhandled expression type '${ ParseTreeUtils.printExpression(node) }'`);
typeResult = { type: UnknownType.create(), node };
}

Expand Down Expand Up @@ -1546,7 +1545,7 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {

const symbolWithScope = lookUpSymbolRecursive(nameNode, nameValue);
if (!symbolWithScope) {
assert.fail(`Missing symbol '${ nameValue }'`);
fail(`Missing symbol '${ nameValue }'`);
return;
}

Expand Down Expand Up @@ -7115,7 +7114,7 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {
}

function evaluateTypeOfParameter(node: ParameterNode): void {
assert(node.name);
assert(node.name !== undefined);

// We need to handle lambdas differently from functions because
// the former never have parameter type annotations but can
Expand Down Expand Up @@ -7238,17 +7237,17 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {
curNode = curNode.parent;
}

assert.fail('Unexpected assignment target');
fail('Unexpected assignment target');
return undefined;
}

function getTypeFromWildcardImport(flowNode: FlowWildcardImport, name: string): Type {
const importInfo = AnalyzerNodeInfo.getImportInfo(flowNode.node.module);
assert(importInfo && importInfo.isImportFound);
assert(importInfo !== undefined && importInfo.isImportFound);
assert(flowNode.node.isWildcardImport);

const symbolWithScope = lookUpSymbolRecursive(flowNode.node, name);
assert(symbolWithScope);
assert(symbolWithScope !== undefined);
const decls = symbolWithScope!.symbol.getDeclarations();
const wildcardDecl = decls.find(decl => decl.node === flowNode.node);

Expand Down Expand Up @@ -7512,7 +7511,7 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {
evaluateTypesForStatement(flowNode.node);
cachedType = readTypeCache(nodeForCacheLookup);
if (!cachedType) {
assert.fail('evaluateAssignmentFlowNode failed to evaluate target');
fail('evaluateAssignmentFlowNode failed to evaluate target');
cachedType = UnknownType.create();
}
}
Expand Down Expand Up @@ -7703,7 +7702,7 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {
}

// We shouldn't get here.
assert.fail('Unexpected flow node flags');
fail('Unexpected flow node flags');
return setCacheEntry(curFlowNode, undefined, false);
}
}
Expand Down Expand Up @@ -7818,7 +7817,7 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {
}

// We shouldn't get here.
assert.fail('Unexpected flow node flags');
fail('Unexpected flow node flags');
return false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions server/src/analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* Representation of types used during type analysis within Python.
*/

import * as assert from 'assert';

import { assert } from '../common/debug';
import { ParameterCategory } from '../parser/parseNodes';
import { FunctionDeclaration } from './declaration';
import { Symbol, SymbolTable } from './symbol';
Expand Down
3 changes: 1 addition & 2 deletions server/src/common/positionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
* line/column positions.
*/

import * as assert from 'assert';

import { Position, Range, TextRange } from './textRange';
import { TextRangeCollection } from './textRangeCollection';
import { assert } from './debug';

// Translates a file offset into a line/column pair.
export function convertOffsetToPosition(offset: number, lines: TextRangeCollection<TextRange>): Position {
Expand Down
Loading

0 comments on commit 094bd03

Please sign in to comment.