Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Handle potential exceptions from TS
Browse files Browse the repository at this point in the history
  • Loading branch information
IllusionMH committed May 17, 2019
1 parent 1c44cc7 commit cb4d3c7
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/preferArrayLiteralRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,18 @@ function walk(ctx: Lint.WalkContext<Options>, checker: ts.TypeChecker | undefine
} else {
// When typechecker is not available - allow any call with single expression
if (checker) {
const argument = callArguments[0];
const argumentType = checker.getTypeAtLocation(argument);
if (!tsutils.isTypeAssignableToNumber(checker, argumentType) || argument.kind === ts.SyntaxKind.SpreadElement) {
const failureString = Rule.getSizeParamFailureString(type);
ctx.addFailureAt(node.getStart(), node.getWidth(), failureString);
try {
// TS might throw exceptions in non-standard conditions (like .vue files)
// Use try...catch blocks to fallback to the same behavior as when checker is not available
// See https://github.com/microsoft/tslint-microsoft-contrib/issues/859
const argument = callArguments[0];
const argumentType = checker.getTypeAtLocation(argument);
if (!tsutils.isTypeAssignableToNumber(checker, argumentType) || argument.kind === ts.SyntaxKind.SpreadElement) {
const failureString = Rule.getSizeParamFailureString(type);
ctx.addFailureAt(node.getStart(), node.getWidth(), failureString);
}
} catch {
// No error to use same behavior as when typeChecker is not available
}
}
}
Expand Down

0 comments on commit cb4d3c7

Please sign in to comment.