Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Fix failures on typescript@next
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson committed May 19, 2017
1 parent 71de201 commit f9bc4dd
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"rimraf": "^2.5.4",
"tslint": "latest",
"tslint-test-config-non-relative": "file:test/external/tslint-test-config-non-relative",
"typescript": "^2.3.0"
"typescript": "next"
},
"license": "Apache-2.0",
"engines": {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/completedDocsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ class CompletedDocsWalker extends Lint.ProgramAwareRuleWalker {
}

private checkNode(node: ts.Declaration, nodeType: DocType): void {
if (node.name === undefined) {
const { name } = node as ts.NamedDeclaration;
if (name === undefined) {
return;
}

Expand All @@ -398,7 +399,7 @@ class CompletedDocsWalker extends Lint.ProgramAwareRuleWalker {
return;
}

const symbol = this.getTypeChecker().getSymbolAtLocation(node.name);
const symbol = this.getTypeChecker().getSymbolAtLocation(name);
if (symbol === undefined) {
return;
}
Expand Down
9 changes: 7 additions & 2 deletions src/rules/matchDefaultExportNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ function walk(ctx: Lint.WalkContext<void>, tc: ts.TypeChecker) {
continue;
}
const defaultImport = statement.importClause.name;
const {declarations} = tc.getAliasedSymbol(tc.getSymbolAtLocation(defaultImport));
const symbol = tc.getSymbolAtLocation(defaultImport);
if (symbol === undefined || !Lint.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) {
continue;
}

const {declarations} = tc.getAliasedSymbol(symbol);
if (declarations !== undefined && declarations.length !== 0) {
const [{name}] = declarations;
const { name } = declarations[0] as ts.NamedDeclaration;
if (name !== undefined && name.kind === ts.SyntaxKind.Identifier && name.text !== defaultImport.text) {
ctx.addFailureAtNode(defaultImport, Rule.FAILURE_STRING(defaultImport.text, name.text));
}
Expand Down
18 changes: 12 additions & 6 deletions src/rules/noInferredEmptyObjectTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@ class NoInferredEmptyObjectTypeRule extends Lint.AbstractWalker<void> {
}

private checkCallExpression(node: ts.CallExpression): void {
if (node.typeArguments === undefined) {
const callSig = this.checker.getResolvedSignature(node);
const retType = this.checker.getReturnTypeOfSignature(callSig) as ts.TypeReference;
if (this.isEmptyObjectInterface(retType)) {
this.addFailureAtNode(node, Rule.EMPTY_INTERFACE_FUNCTION);
}
if (node.typeArguments !== undefined) {
return;
}

const callSig = this.checker.getResolvedSignature(node);
if (callSig === undefined) {
return;
}

const retType = this.checker.getReturnTypeOfSignature(callSig) as ts.TypeReference;
if (this.isEmptyObjectInterface(retType)) {
this.addFailureAtNode(node, Rule.EMPTY_INTERFACE_FUNCTION);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/rules/noUnusedExpressionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ function walk(ctx: Lint.WalkContext<Options>) {
check(node.left);
return cb(node.right);
}
return ts.forEachChild(node, cb);
return ts.forEachChild(node, cb) === true;
}

function forEachChild(node: ts.Node): boolean {
return ts.forEachChild(node, cb);
return ts.forEachChild(node, cb) === true;
}

function check(node: ts.Node, failNode?: ts.Node): void {
Expand Down
17 changes: 9 additions & 8 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function walk(ctx: Lint.WalkContext<void>, program: ts.Program, { checkParameter
const failure = ts.flattenDiagnosticMessageText(diag.messageText, "\n");

if (kind === UnusedKind.VARIABLE_OR_PARAMETER) {
const importName = findImport(diag.start, sourceFile);
const importName = findImport(diag.start!, sourceFile);
if (importName !== undefined) {
if (isImportUsed(importName, sourceFile, checker)) {
continue;
Expand All @@ -138,7 +138,7 @@ function walk(ctx: Lint.WalkContext<void>, program: ts.Program, { checkParameter
}
}

ctx.addFailureAt(diag.start, diag.length, failure);
ctx.addFailureAt(diag.start!, diag.length!, failure);
}

if (importSpecifierFailures.size !== 0) {
Expand Down Expand Up @@ -251,12 +251,12 @@ function addImportSpecifierFailures(ctx: Lint.WalkContext<void>, failures: Map<t
* Workround for https://github.com/Microsoft/TypeScript/issues/9944
*/
function isImportUsed(importSpecifier: ts.Identifier, sourceFile: ts.SourceFile, checker: ts.TypeChecker): boolean {
let symbol = checker.getSymbolAtLocation(importSpecifier);
if (symbol === undefined) {
const importedSymbol = checker.getSymbolAtLocation(importSpecifier);
if (importedSymbol === undefined) {
return false;
}

symbol = checker.getAliasedSymbol(symbol);
const symbol = checker.getAliasedSymbol(importedSymbol);
if (!Lint.isSymbolFlagSet(symbol, ts.SymbolFlags.Type)) {
return false;
}
Expand All @@ -272,15 +272,16 @@ function isImportUsed(importSpecifier: ts.Identifier, sourceFile: ts.SourceFile,
return true;
}

return ts.forEachChild(child, cb);
});
return ts.forEachChild(child, cb) === true;
}) === true;
}

function getImplicitType(node: ts.Node, checker: ts.TypeChecker): ts.Type | undefined {
if ((utils.isPropertyDeclaration(node) || utils.isVariableDeclaration(node)) && node.type === undefined) {
return checker.getTypeAtLocation(node);
} else if (utils.isSignatureDeclaration(node) && node.type === undefined) {
return checker.getSignatureFromDeclaration(node).getReturnType();
const sig = checker.getSignatureFromDeclaration(node);
return sig === undefined ? undefined : sig.getReturnType();
} else {
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/onlyArrowFunctionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ function hasThisParameter(node: ts.FunctionLikeDeclaration): boolean {
}

function usesThisInBody(node: ts.Node): boolean {
return node.kind === ts.SyntaxKind.ThisKeyword || !utils.hasOwnThisReference(node) && ts.forEachChild(node, usesThisInBody);
return node.kind === ts.SyntaxKind.ThisKeyword || !utils.hasOwnThisReference(node) && ts.forEachChild(node, usesThisInBody) === true;
}
9 changes: 6 additions & 3 deletions src/rules/restrictPlusOperandsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { isBinaryExpression, isEnumLiteralType, isUnionType } from "tsutils";
import { isBinaryExpression, isUnionType } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand Down Expand Up @@ -63,8 +63,11 @@ function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "invalid
} else if (isUnionType(type) && !Lint.isTypeFlagSet(type, ts.TypeFlags.Enum)) {
const types = type.types.map(getBaseTypeOfLiteralType);
return allSame(types) ? types[0] : "invalid";
} else if (isEnumLiteralType(type)) {
return getBaseTypeOfLiteralType(type.baseType);
} else if (Lint.isTypeFlagSet(type, ts.TypeFlags.Literal)) {
return typeof (type as ts.LiteralType).value as "string" | "number";
} else if (Lint.isTypeFlagSet(type, (ts.TypeFlags as { EnumLiteral: ts.TypeFlags }).EnumLiteral)) {
// Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
getBaseTypeOfLiteralType((type as any as { baseType: ts.LiteralType }).baseType);
}
return "invalid";
}
Expand Down
12 changes: 10 additions & 2 deletions src/rules/strictBooleanExpressionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ function getKind(type: ts.Type): TypeKind {
: is(ts.TypeFlags.Undefined | ts.TypeFlags.Void) ? TypeKind.Undefined // tslint:disable-line:no-bitwise
: is(ts.TypeFlags.EnumLike) ? TypeKind.Enum
: is(ts.TypeFlags.NumberLiteral) ?
((type as ts.LiteralType).text === "0" ? TypeKind.FalseNumberLiteral : TypeKind.AlwaysTruthy)
(numberLiteralIsZero(type as ts.LiteralType) ? TypeKind.FalseNumberLiteral : TypeKind.AlwaysTruthy)
: is(ts.TypeFlags.StringLiteral) ?
((type as ts.LiteralType).text === "" ? TypeKind.FalseStringLiteral : TypeKind.AlwaysTruthy)
(stringLiteralIsEmpty(type as ts.LiteralType) ? TypeKind.FalseStringLiteral : TypeKind.AlwaysTruthy)
: is(ts.TypeFlags.BooleanLiteral) ?
((type as ts.IntrinsicType).intrinsicName === "true" ? TypeKind.AlwaysTruthy : TypeKind.FalseBooleanLiteral)
: TypeKind.AlwaysTruthy;
Expand All @@ -321,6 +321,14 @@ function getKind(type: ts.Type): TypeKind {
}
}

function numberLiteralIsZero(type: ts.LiteralType): boolean {
// Uses 'value' in TypeScript>=2.4.
return type.value !== undefined ? type.value === 0 : (type as any).text === "0";
}
function stringLiteralIsEmpty(type: ts.LiteralType): boolean {
return ((type as ts.LiteralType).value !== undefined ? type.value : (type as any).text) === "";
}

/** Matches `&&` and `||` operators. */
function isBooleanBinaryExpression(node: ts.Expression): boolean {
return node.kind === ts.SyntaxKind.BinaryExpression && binaryBooleanExpressionKind(node as ts.BinaryExpression) !== undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/rules/unifiedSignaturesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function signatureUsesTypeParameter(sig: ts.SignatureDeclaration, isTypeParamete
return true;
}
}
return !!ts.forEachChild(type, typeContainsTypeParameter);
return ts.forEachChild(type, typeContainsTypeParameter) === true;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class Runner {
// emit any error messages
let message = ts.DiagnosticCategory[diag.category];
if (diag.file) {
const { line, character } = diag.file.getLineAndCharacterOfPosition(diag.start);
const { line, character } = diag.file.getLineAndCharacterOfPosition(diag.start!);
let file: string;
const currentDirectory = program!.getCurrentDirectory();
file = this.options.outputAbsolutePaths
Expand Down

0 comments on commit f9bc4dd

Please sign in to comment.