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

Commit

Permalink
Enable 'no-unnecessary-type-assertion' rule (#2670)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and adidahiya committed May 30, 2017
1 parent 487e425 commit ca20dfb
Show file tree
Hide file tree
Showing 17 changed files with 20 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Linter {

// add rule severity to failures
const ruleSeverityMap = new Map(enabledRules.map((rule) => {
// tslint:disable-next-line no-unnecessary-type-assertion
return [rule.getOptions().ruleName, rule.getOptions().ruleSeverity] as [string, RuleSeverity];
}));

Expand Down
2 changes: 1 addition & 1 deletion src/rules/callableTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function noSupertype(node: ts.InterfaceDeclaration): boolean {
if (node.heritageClauses.length !== 1) {
return false;
}
const expr = node.heritageClauses[0].types![0].expression;
const expr = node.heritageClauses[0].types[0].expression;
return isIdentifier(expr) && expr.text === "Function";
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/indentRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function walk(ctx: Lint.WalkContext<Options>): void {
? new RegExp(`^( {${size}})+( {1,${size - 1}})?`, "g")
: /\t/g;
const replacement = fullLeadingWhitespace.replace(replaceRegExp, (match) =>
(tabs ? "\t" : " ".repeat(size)).repeat(Math.ceil(match.length / size!)));
(tabs ? "\t" : " ".repeat(size)).repeat(Math.ceil(match.length / size)));
return new Lint.Replacement(lineStart, fullLeadingWhitespace.length, replacement);
}
}
2 changes: 1 addition & 1 deletion src/rules/memberOrderingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function categoryFromOption(orderOption: MemberCategoryJson[] | string): MemberC
return orderOption;
}

const preset = PRESETS.get(orderOption as string);
const preset = PRESETS.get(orderOption);
if (preset === undefined) {
throw new Error(`Bad order: ${JSON.stringify(orderOption)}`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/rules/noEmptyInterfaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ function walk(ctx: Lint.WalkContext<void>) {
if (isInterfaceDeclaration(node) &&
node.members.length === 0 &&
(node.heritageClauses === undefined ||
node.heritageClauses[0].types === undefined ||
// allow interfaces that extend 2 or more interfaces
node.heritageClauses[0].types!.length < 2)) {
node.heritageClauses[0].types.length < 2)) {
return ctx.addFailureAtNode(node.name,
node.heritageClauses !== undefined ? Rule.FAILURE_STRING_FOR_EXTENDS : Rule.FAILURE_STRING);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noStringThrowRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function walk(ctx: Lint.WalkContext<void>): void {
const { sourceFile } = ctx;
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (isThrowStatement(node)) {
const { expression } = node as ts.ThrowStatement;
const { expression } = node;
if (isString(expression)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING, [
Lint.Replacement.appendText(expression.getStart(sourceFile), "new Error("),
Expand Down
4 changes: 2 additions & 2 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
2 changes: 1 addition & 1 deletion src/rules/onlyArrowFunctionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function functionIsExempt(node: ts.FunctionLikeDeclaration): boolean {
function hasThisParameter(node: ts.FunctionLikeDeclaration): boolean {
const first = node.parameters[0];
return first !== undefined && first.name.kind === ts.SyntaxKind.Identifier &&
(first.name as ts.Identifier).originalKeywordKind === ts.SyntaxKind.ThisKeyword;
first.name.originalKeywordKind === ts.SyntaxKind.ThisKeyword;
}

function usesThisInBody(node: ts.Node): boolean {
Expand Down
7 changes: 3 additions & 4 deletions src/rules/preferConstRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export class Rule extends Lint.Rules.AbstractRule {

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const options: Options = {
destructuringAll: this.ruleArguments.length !== 0 &&
(this.ruleArguments[0] as any).destructuring === OPTION_DESTRUCTURING_ALL,
destructuringAll: this.ruleArguments.length !== 0 && this.ruleArguments[0].destructuring === OPTION_DESTRUCTURING_ALL,
};
const preferConstWalker = new PreferConstWalker(sourceFile, this.ruleName, options);
return this.applyWithWalker(preferConstWalker);
Expand Down Expand Up @@ -232,7 +231,7 @@ class PreferConstWalker extends Lint.AbstractWalker<Options> {
this.scope.reassigned.add((property.name as ts.Identifier).text);
} else {
// handle `...(variable)`
this.handleExpression(property.expression!);
this.handleExpression(property.expression);
}
break;
default:
Expand Down Expand Up @@ -324,7 +323,7 @@ class PreferConstWalker extends Lint.AbstractWalker<Options> {
!info.declarationInfo.reassignedSiblings &&
info.declarationInfo.isBlockScoped &&
!appliedFixes.has(info.declarationInfo.declarationList)) {
fix = new Lint.Replacement(info.declarationInfo.declarationList!.getStart(this.sourceFile), 3, "const");
fix = new Lint.Replacement(info.declarationInfo.declarationList.getStart(this.sourceFile), 3, "const");
// add only one fixer per VariableDeclarationList
appliedFixes.add(info.declarationInfo.declarationList);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/semicolonRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ abstract class SemicolonWalker extends Lint.AbstractWalker<Options> {
if (this.sourceFile.text[node.end - 1] !== ";") {
return;
}
const lastToken = utils.getPreviousToken(node.getLastToken(this.sourceFile)!, this.sourceFile)!;
const lastToken = utils.getPreviousToken(node.getLastToken(this.sourceFile), this.sourceFile)!;
// yield does not continue on the next line if there is no yielded expression
if (lastToken.kind === ts.SyntaxKind.YieldKeyword && lastToken.parent!.kind === ts.SyntaxKind.YieldExpression ||
// arrow functions with block as body don't continue on the next line
Expand Down
2 changes: 1 addition & 1 deletion src/rules/strictBooleanExpressionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ function showLocation(n: Location): string {
case ts.SyntaxKind.DoStatement:
return "'do-while' condition";
case ts.SyntaxKind.BinaryExpression:
return `operand for the '${binaryBooleanExpressionKind(n as ts.BinaryExpression)}' operator`;
return `operand for the '${binaryBooleanExpressionKind(n)}' operator`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/trailingCommaRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type OptionName = "arrays" | "exports" | "functions" | "imports" | "objects" | "
type CustomOptionValue = Record<OptionName, OptionValue>;
type Options = Record<"multiline" | "singleline", CustomOptionValue>;

const defaultOptions: CustomOptionValue = fillOptions("ignore" as "ignore");
const defaultOptions: CustomOptionValue = fillOptions("ignore" as "ignore"); // tslint:disable-line no-unnecessary-type-assertion

function fillOptions<T>(value: T): Record<OptionName, T> {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ async function tryReadFile(filename: string, logger: Logger): Promise<string | u
function showDiagnostic({ file, start, category, messageText }: ts.Diagnostic, program: ts.Program, outputAbsolutePaths: boolean): string {
let message = ts.DiagnosticCategory[category];
if (file) {
const {line, character} = file.getLineAndCharacterOfPosition(start!);
const {line, character} = file.getLineAndCharacterOfPosition(start);
const currentDirectory = program.getCurrentDirectory();
const filePath = outputAbsolutePaths
? path.resolve(currentDirectory, file.fileName)
Expand Down
2 changes: 1 addition & 1 deletion src/test/lintError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export function errorComparator(err1: LintError, err2: LintError) {
}

export function lintSyntaxError(message: string) {
return new Error(`Lint File Syntax Error: ${message}`) as Error;
return new Error(`Lint File Syntax Error: ${message}`);
}
2 changes: 1 addition & 1 deletion src/tslint-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ commander.on("--help", () => {
const parsed = commander.parseOptions(process.argv.slice(2));
commander.args = parsed.args;
if (parsed.unknown.length !== 0) {
(commander.parseArgs as any)([], parsed.unknown);
(commander.parseArgs as (args: string[], unknown: string[]) => void)([], parsed.unknown);
}
const argv = commander.opts() as any as Argv;

Expand Down
4 changes: 2 additions & 2 deletions test/configurationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ describe("Configuration", () => {
const config = loadConfigurationFromPath("./test/config/tslint-extends-package-two-levels.json");

assert.lengthOf(config.rulesDirectory, 2);
assert.isTrue(fs.existsSync(config.rulesDirectory![0]));
assert.isTrue(fs.existsSync(config.rulesDirectory![1]));
assert.isTrue(fs.existsSync(config.rulesDirectory[0]));
assert.isTrue(fs.existsSync(config.rulesDirectory[1]));

const expectedConfig = getEmptyConfig();
expectedConfig.rules.set("always-fail", { ruleSeverity: "off" });
Expand Down
1 change: 0 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"no-null-keyword": false,
"no-require-imports": false,
"no-unbound-method": false,
"no-unnecessary-type-assertion": false,
"no-unnecessary-qualifier": false,
"no-use-before-declare": false,
"no-void-expression": false,
Expand Down

0 comments on commit ca20dfb

Please sign in to comment.